Greetings fellow monks

I am trying to find the oldest file in a directory, am running win 2000, apache 1.3. I have the following sub.
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; }
Which I gleamed from another post here at the monastery. When running this a lot of times in quick succession with files being created and removed in between (but not at the same time), about one time in 10 this will break, not returning any value (when there are definitely files in the directory). Perplexed I have changed the line my $oldest_age = 0; to my $oldest_age = -1; Thinking that possibly the $age could be = 0 when there is only one file....This seems to have helped a lot, reducing the number of "breaks" down to about 1 in 100, but there are still multiple files in the directory. (In my tests, 5 and 8).

It is possible that the calling code is broken, but I can't see anything obvious. Here is that sub in case that helps
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 withi +n $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 g +iven # 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(<MSG>) { print $_; } close MSG; } }
This now runs most of the time, but not all the time... It dies when trying to check the file size, as $filename doesn't contain anything.

Any ideas on what I am doing wrong, and what exactly -M file contains. To be honest I was surprised that changing it to -1 helped, but I changed it back to 0 just to check, and sure enough the problem occured a lot more frequently. Help?

Thanks, Gerard

In reply to Oldest file using -M by Gerard

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.