setantae has asked for the wisdom of the Perl Monks concerning the following question:
I just want to truncate a mailbox in place (standard UNIX format) after copying all the mail out of it for a play by mail game that I'm writing.
Here's the sub that I'm having trouble with (see the bit commented out with 4 sh's - I've left my other comments in to show how I'm thinking):
sub get_mail { unless (-s $mailspool) { # Nothing in the mailbox? Don't bot +her then $debug and print LOG "No mail in mailspool\n"; return; } open(MAILBOX, "<$mailspool") or &loganddie("$mailspool exists but I + can't open it : $!"); flock(MAILBOX, LOCK_EX|LOCK_NB) or do {$failed_locks++; return 0}; # Not getting the lock is not all that fatal - we can try later # If we get here, then we can finally do something with the mailspo +ol. # First though, we have to open the file we're moving it to. opendir (PROCESSDIR, $processdir) or &loganddie("Someone broke $pro +cessdir : $!"); my @allfiles = grep !/^\.\.?\z/, readdir PROCESSDIR; closedir PROCESSDIR; my $output_file = &POSIX::tmpnam(); $output_file =~ s/\/tmp\/(.*)/$1/; # remove /tmp/ from string open(PENDING, ">>$processdir/$output_file") or &loganddie("Can't wr +ite to $processdir/$output_file : $!"); $debug and print "opened file\n"; flock(PENDING, LOCK_EX|LOCK_NB) or return 0; $debug and print "got lock\n"; seek(PENDING,0,2) or return 0; # in case some other process append +ed $debug and print "did seek\n"; print PENDING while (<MAILBOX>); $debug and print "read mailbox\n"; close PENDING or &loganddie("man there is some crazy sh|t going on\ +n\tcouldn't close $processdir/$output_file : $!"); $debug and print "closed pending\n"; #### OK, somehow we now have to truncate the file without unlinking + it (don't have write on /var/spool/mail) or releasing the lock (what + if more mail comes in between us releasing the lock and truncating t +he file?) close MAILBOX; $debug and print "closed MAILBOX\n"; return; }
Can anyone help? Cheer
setantae@eidosnet.co.uk|setantae|www.setantae.uklinux.net
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Truncate a mailbox in place
by chipmunk (Parson) on Jan 13, 2001 at 03:27 UTC | |
by setantae (Scribe) on Jan 13, 2001 at 03:51 UTC | |
by tye (Sage) on Jan 13, 2001 at 03:57 UTC | |
by setantae (Scribe) on Jan 13, 2001 at 04:04 UTC |