(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.
require 'sysexits.ph';
use Data::Dumper;
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;
use File::Basename;
use Getopt::Std;
use Log::Dispatch;
sub get_highest_msgnum {
my $mhfolder = shift;
sub get_highest_msgnum {
my $mhfolder = shift;
+ $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) {
# Look for integers.
if ($msgnum =~ /^[0-9]+$/) {
if ($msgnum > $highest) {
}
# We want to print the name of each list that has new mail only once,
}
# 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++;
$msgnum = get_highest_msgnum($mhfolder);
for ($try = 0; ; $try++) {
$msgnum++;
- $mhmsg = "$mhfolder/$msgnum";
+ $mhmsg = "$mh/$mhfolder/$msgnum";
# 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
# 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.
# 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 {
# This algorithm is different from the maildir one; let's make
# 10 tries instead of 3.
if ($try == 9) {
# 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.
}
# This algorithm is different; i don't think we need to sleep.