sub get_oldest_file { my $path = shift; # name of directory to search opendir( D, $path ) or return "Unable to read directory $path\n"; my $oldest_age = 0; my $oldest_name = ''; for ( readdir( D )) { next unless ( -f "$path/$_"); my $age = ( -M _ ); # note the "_" : uses stat data loaded by "-f" above if ( $age > $oldest_age ) { $oldest_name = $_; $oldest_age = $age; } } closedir D; return $oldest_name; } #### sub GetMessage { my $filename; my $timeout = $q->param('Timeout'); # default timeout period is 10 seconds if ($timeout eq ""){ $timeout = 10; } $branch = $q->param('Branch'); my $dir = $baseDir . "\\" . $branch . "\\"; # first check to see if the directory exists if(! -e $dir){ &error("Requested branch at " . $dir . " does not exist"); } chdir($dir) ; eval { local $SIG{ALRM} = sub { die "alarm\n" }; alarm $timeout; # This process may take a while. If it does not complete within $timeout seconds # it will be stopped. $filename=get_oldest_file("."); alarm 0; }; if ($@ eq "alarm\n") { # TIMED OUT - Was unable to calculate the oldest file in the given # timeout period. Return not found print $q->header('text/html','404 Not Found - operation timed out'); exit (0); } else { # Found the oldest file ok. Continue processing. my $size; $size = -s $filename || error("Unable to check file size " . $dir . $filename . " : " . $!); my $messageID = $filename; print $q->header(-Type=>'application/octet-stream', -Message_ID=>$messageID, -Content_Length=>$size); open(MSG,"< $filename") || error("Unable to open file " . $dir . $filename . " for read: " . $!); binmode MSG; while() { print $_; } close MSG; } }