Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Truncate a mailbox in place

by setantae (Scribe)
on Jan 13, 2001 at 03:14 UTC ( [id://51498]=perlquestion: print w/replies, xml ) Need Help??

setantae has asked for the wisdom of the Perl Monks concerning the following question:

Ok, well I'm sure that this is really basic, but I am having a nightmare working it out.
I've looked at Mail::Folder::Mbox, but I don't like this limitation (it's the same problem that I'm having) :
<quote>* Mark it as readonly if the folder is not writable.</QUOTE>

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
    I think the truncate function meets all your needs, including preserving the lock.
      It looks like it should, but if i add the line
      (truncate (MAILBOX, 0)) or print "ACK!: truncate didn't work : $!\n";
      I get : ACK!: truncate didn't work : Permission denied
      Permissions on /var/spool/mail are
      drwxrwxr-x   2 root     mail         1024 Jan 12 22:43 /var/spool/mail/
      and permissions on the mailbox file in that directory are 0660, so it looks like truncate is trying to unlink the file and recreate it (or something - it does the same with a non-zero LENGTH).

      setantae@eidosnet.co.uk|setantae|www.setantae.uklinux.net

        You can't truncate a mailbox that you have open to only read from. Use "+<" isntead of "<" as your open mode if you want to use the MAILBOX handle to truncate the file.

                - tye (but my friends call me "Tye")

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://51498]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (2)
As of 2024-04-25 20:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found