procmail
[Top] [All Lists]

Re: Can procmail be used to move mail between folders?

2001-07-31 11:04:21
Andrew Edelstein <andrew(_at_)pure-chaos(_dot_)com> writes:
On Tue, Jul 31, 2001 at 08:55:43AM +0100, Tim Allen wrote:
1. The imap server is wu-imap running on RH7.1. Is there any way to lock
sentfolder while performing this action to prevent wu-imap from adding to
the sentfolder while I'm passing it through formail? Or is it a matter of
running the cron job at 2.00 am on the assumption that everything's quiet
then? I've been looking at lockfile but can't see that's relevant to this.
...
Moving sentfolder to a new file name would solve both problems. Move the file,
touch the original file name to create it, then process the sent folder under
it's new name. The server will append new incoming messages to the brand new
file (which you'll process on the next run). You can simply delete the new
(old) file when you're done processing.

Renaming a file doesn't protect it against concurrent access, it just
makes the window smaller.  Processes that open()ed the original file
will continue to access the moved file, possibly appending messages.
The only way to detect when they (there could be more than one!) are
done is to lock the file!  Renaming the file may still be useful to
solve problem #2 (elided above), but it doesn't solve the locking problem.

...
UW has a discussion of locking in docs/locking.txt in the source tree for
UW-IMAP. Unfortunatly it is beyond my mental capacity to parse at this hour,
and appears to be a general discussion of the issue, and a lot of "this is how
we do it on this type of system, using this type of mailbox" kind of thing.

Yeah, that file is mostly a rant on how badly designed and implemented
the various kernel locking methods (mainly SysV's fcntl() additions) are.
At the bottom, however, it does explain how c-client does locking on
folders.  Berkeley format (c-client's "unix" driver) and MMDF ("mmdf"
driver) are locked using .lock file, kernel advisory locks, and a c-client
specific lockfile in /tmp used for internal magic.

So, assuming this is a unix format mailbox (not mtx or one of c-client's
other formats) the lockfile command can and should be used to protect
the mailbox across the rename:

        f=sentfolder
        if test -f "$f.old"
        then
                echo "Aargh!  $f.old already exists!" >&2
                exit 1
        elsif lockfile "$f.lock"
        then
                mv "$f" "$f.old"
                touch "$f"
                rm -f "$f.lock"
                formail -s procmail -m sentrc < "$f.old"
                rm "$f.old"
                exit 0
        else
                echo "Unable to create lock $f.lock" >&2
                exit 1
        fi


Philip Guenther
_______________________________________________
procmail mailing list
procmail(_at_)lists(_dot_)RWTH-Aachen(_dot_)DE
http://MailMan.RWTH-Aachen.DE/mailman/listinfo/procmail