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;
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) {
}
}
- return $highest + 1;
+ return $highest;
}
# We want to print the name of each list that has new mail only once,
$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
# 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;
}
}
# 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.