From 560f85f023d93fe441ec74c8c4b6fb1725455f68 Mon Sep 17 00:00:00 2001 From: epg <> Date: Sat, 31 Aug 2002 18:44:47 +0000 Subject: [PATCH] use Errno and Fcntl qw(O_WRONLY O_EXCL O_CREAT) . (get_highest_msgnum): Restructure to step through readdir itself rather than go through getfiles, since we don't want full path names like that returns, but only the base filenames readdir returns. Also fix it to return the highest number in the folder, not that number plus one, making its name accurate. (store_message): Adapt to get_highest_msgnum returning only the highest message number. Use better $! test. Only create the empty message file with sysopen when not $run. Use the full filename in the error message when we couldn't find a new filename. --- minc | 40 +++++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/minc b/minc index ebe447f..9664413 100755 --- a/minc +++ b/minc @@ -29,6 +29,8 @@ use warnings; require 'sysexits.ph'; use Data::Dumper; +use Errno; +use Fcntl qw(O_WRONLY O_EXCL O_CREAT); use File::Basename; use Getopt::Std; use Log::Dispatch; @@ -295,12 +297,27 @@ sub getfiles { sub get_highest_msgnum { my $mhfolder = shift; - my $msgnum; + my $dir; + my @list; my $highest; + my $msgnum; - $highest = 0; + $dir = "$mh/$mhfolder"; + if (not opendir(DIR, $dir)) { + err(&EX_TEMPFAIL, "Failed opendir($dir)"); + } + + $! = 0; + @list = readdir(DIR); - foreach $msgnum (getfiles($mhfolder)) { + if ($! != 0) { + err(&EX_TEMPFAIL, "Failed readdir($dir)"); + } + + closedir(DIR); + + $highest = 0; + foreach $msgnum (@list) { # Look for integers. if ($msgnum =~ /^[0-9]+$/) { if ($msgnum > $highest) { @@ -309,7 +326,7 @@ sub get_highest_msgnum { } } - return $highest + 1; + return $highest; } # We want to print the name of each list that has new mail only once, @@ -333,10 +350,10 @@ sub store_message { $msgnum = get_highest_msgnum($mhfolder); for ($try = 0; ; $try++) { $msgnum++; - $mhmsg = "$mhfolder/$msgnum"; + $mhmsg = "$mh/$mhfolder/$msgnum"; if (not stat($mhmsg)) { - if ($! == ENOENT) { + if ($!{ENOENT}) { # Now we have a non-existent file, let's try to create # it. We must create a zero-byte file first because a # file my appear between our happy stat results and @@ -344,8 +361,13 @@ sub store_message { # So attempt to create a file with this name. If it # succeeds, in just a bit here we'll knowingly clobber # this file with the rename(2) call. - if (sysopen(MSG, $mhmsg, O_WRONLY | O_EXCL | O_CREAT, 0600)) { - close(MSG); + if ($run) { + if (sysopen(MSG, "$mhmsg", + O_WRONLY | O_EXCL | O_CREAT, 0600)) { + close(MSG); + last; + } + } else { last; } } @@ -354,7 +376,7 @@ sub store_message { # This algorithm is different from the maildir one; let's make # 10 tries instead of 3. if ($try == 9) { - errx(&EX_TEMPFAIL, "Attempted filename exists."); + errx(&EX_TEMPFAIL, "Attempted filename $mhmsg exists."); } # This algorithm is different; i don't think we need to sleep. -- 2.56.0