#!/usr/bin/perl -wT use strict; # Copyright (C) 2002 Kirk Strauser (kirk@strauser.com) # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details, published at # http://www.gnu.org/copyleft/gpl.html # $Id: cleansessions,v 1.1 2002/09/11 17:55:48 kirk Exp $ use lib ( '.' ); use lib ( '../lib' ); use TWiki; my $dataDir = &TWiki::getDataDir(); my $sessionDir = "$dataDir/.session"; my $expireThreshold = 86400 * 7; my %recent; my %file; my %deleted; foreach my $key (qw {null old obsolete}) { $deleted{$key} = 0; } chdir $sessionDir; opendir INDIR, '.'; while (defined (my $fname = readdir INDIR)) { next if $fname =~ /^\.\.?$/; my ($size, $mtime) = (stat $fname)[7, 9]; $fname =~ /(.*)/; $fname = $1; if ($size == 0) { $deleted{'null'}++; unlink $fname; next; } if (time - $mtime > $expireThreshold) { $deleted{'old'}++; unlink $fname; next; } my $username; open INFILE, $fname; while (defined (my $inline = )) { chomp $inline; my ($key, $value) = (split '\|', $inline)[0, 1]; next unless $key eq 'user'; $username = $value; last; } close INFILE; # Track the name and age of the newest file for each user if (!defined $recent{$username} or $mtime > $recent{$username}{'mtime'}) { $recent{$username}{'mtime'} = $mtime; $recent{$username}{'file'} = $fname; } # Track all of the filenames associated with a user $file{$username}{$fname} = $mtime; } closedir INDIR; foreach my $username (keys %recent) { foreach my $fname (keys %{$file{$username}}) { if ($fname ne $recent{$username}{'file'}) { $deleted{'obsolete'}++; unlink $fname; } } } my $total = 0; foreach my $key (keys %deleted) { $total += $deleted{$key}; } print <<__EOHD__; Content-Type: text/html Session-cleaning Report

Keys Deleted

Zero-length: $deleted{'null'}
Expired: $deleted{'old'}
Obsolete: $deleted{'obsolete'}
Total: $total
__EOHD__