#!/usr/bin/perl # # Copyright (c) 2004 Scalable Informatics LLC # Property of Scalable Informatics LLC # You may not distribute this program. # use strict; use constant true => (1==1); use constant false => (1==0); use constant interval => 60; # how often to wake up use constant MB => 1024*1024; use constant limit => 900*MB; use Getopt::Long; use IO::Dir; use File::Copy; my ($time,$stop,%dir,$elapsed,%files,%current,%archive,$rc,%previous,$file); my ($ndx,@index,$target,$source); my $max = limit; my $time = interval; my $debug = false; my $dryrun = false; my $done = false; my $elapsed = 0; my $first_pass = true; my $last_archive = 0; my $count = 0; Getopt::Long::Configure("pass_through"); $rc = GetOptions ( "debug" => \$debug, "stop" => \$stop, "max=i" => \$max, "dryrun" => \$dryrun, "time=i" => \$time ); $debug = true if ($dryrun); exit if ($time < interval); if ($debug) { printf ". file size = %i\n",$max; printf ". wake interval = %i\n",interval; } tie %dir, 'IO::Dir', "."; while (!$done) { $done = true; # now prove me wrong ... # find all the *.pstt* files undef %current; undef %archive; foreach (keys %dir) { if ($_ =~ /(\S+).pstt$/) { $current{$1} = $dir{$_}->size; printf "Found PSTT: \"%s\" size = %i\n",$1,$current{$1} if ($debug); $done = false; $previous{$1} = 0 if ($count == 0); printf ". first pass for \'%s\'\n",$1 if (($debug) && ($count == 0)); } if ($_ =~ /(\S+).pstt_(\d+)$/) { printf "Found PSTT archive for file \"%s\" and archive number = %i\n", $1,$2 if ($debug); $archive{$1}->{$2}=$dir{$_}->size; } } $done = true if ($elapsed >= $time); last if $done; ### ### this is where the real work will get done # loop over the PSTT files ... foreach $file (keys %current) { printf "o file =\'%s\': current = %i, previous = %i\n", $file,$current{$file},$previous{$file} if ($debug); if ( ($current{$file} > $max) && (($current{$file} - $previous{$file}) == 0) ) { printf "+ file =\'%s\':\n",$file if ($debug); # find last archive number, if any, if (exists($archive{$file})) { @index = sort { $a <=> $b } keys %{$archive{$file}}; $last_archive = pop @index; } # and increment it $last_archive++; printf "++ archive number = %i\n",$last_archive; # create target $target = sprintf '%s.pstt_%-i',$file,$last_archive; # create source $source = sprintf '%s.pstt',$file; # and move file if (!$dryrun) { printf "++ moving file = \'%s\' to \'%s\'\n", $source,$target if ($debug); move($source,$target) or die "FATAL ERROR: unable to move file=\'". $source."\' to \'".$target."\' \n"; } else { printf "-- [dryrun: not] moving file = \'%s\' to \'%s\'\n", $source,$target if ($debug); } } printf "++ setting previous = \'%i\'\n", $current{$file} if ($debug); $previous{$file} = $current{$file}; } ### ### $elapsed += interval; sleep(interval); $count++; } untie %dir;