mbox-sync.c revision 3f26c5aced2e71efc783f26bb8a7ac53f7504622
Modifying mbox can be slow, so we try to do it all at once minimizing the required disk I/O. We may need to: - Update message flags in Status, X-Status and X-Keywords headers - Write missing X-UID and X-IMAPbase headers - Write missing or broken Content-Length header if there's space - Expunge specified messages - Start reading the mails from the beginning - X-Keywords, X-UID and X-IMAPbase headers may contain padding at the end of them, remember how much each message has and offset to beginning of the - If header needs to be rewritten and there's enough space, do it - If we didn't have enough space, remember how much was missing - Continue reading and counting the padding in each message. If available padding is enough to rewrite all the previous messages needing it, do it - When we encounter expunged message, treat all of it as padding and rewrite previous messages if needed (and there's enough space). Afterwards keep moving messages backwards to fill the expunged space. Moving is done by rewriting each message's headers, with possibly adding missing Content-Length header and padding. Message bodies are moved - If we encounter end of file, grow the file and rewrite needed messages - Rewriting is done by moving message body forward, rewriting message's header and doing the same for previous message, until all of them are /* The text below was taken exactly as c-client wrote it to my mailbox, so it's probably copyrighted by University of Washington. */ "This text is part of the internal format of your mail folder, and is not\n" \
"a real message. It is created automatically by the mail system software.\n" \
"If deleted, important folder data will be lost, and it will be re-created\n" \
"with the data reset to initial values.\n" "Unexpectedly lost From-line at offset %"PRIuUOFF_T /* need to add 'O' flag to Status-header */ for (i = 0; i <
count; i++) {
/* nothing for this or the future ones */ for (i = 0; i <
count; i++) {
/* no existing keywords */ /* adding, create the array */ /* externally expunged message, remove from index */ /* this UID was already in index and it was expunged */ "mbox sync: Expunged message reappeared in mailbox %s " /* new UID in the middle of the mailbox - shouldn't happen */ "mbox sync: UID inserted in the middle of mailbox %s " /* externally expunged message, remove from index */ /* see if from_offset needs updating */ /* see if we need to update flags in index file. the flags in sync records are automatically applied to rec->flags at the end of index syncing, so calculate those new flags first */ /* flags are dirty. ignore whatever was in the mbox, /* keep index's internal flags */ /* drop recent flag (it can only be dropped) */ /* dirty flag state changed */ /* update from_offsets, but not if we're going to rewrite this message. rewriting would just move it anyway. */ const unsigned char *
data;
/* first check that the 10 bytes are there and they're exactly as expected. just an extra safety check to make sure we never write to wrong location in the mbox file. */ "X-IMAPbase uid-last unexpectedly points outside " if (
buf[i] <
'0' ||
buf[i] >
'9') {
"X-IMAPbase uid-last unexpectedly lost in mbox file %s",
for (i = 0; i <
count; i++) {
/* expunging first message, fix space to contain next message's \n header too since it will be removed. */ /* uid-last offset is invalid now */ /* move the header backwards to fill expunged space */ /* we're moving this mail to beginning of file. skip the initial \n (it's already counted in /* read the From-line before rewriting overwrites it */ /* rewrite successful, write From-line to /* didn't have enough space, move the offset back so seeking into it doesn't fail */ /* mark it dirty and do it later */ /* first mail with no space to write it */ /* create dummy message to describe the expunged data */ /* we have enough space now */ /* this message was expunged. fill more or less of the space. space_diff now consists of a negative "bytes needed" sum, plus the expunged space of this message. so it contains how many bytes of _extra_ space we have. */ /* don't waste too much on padding */ /* this message gave enough space from headers. rewriting stops at the end of this message's headers. */ /* mail_ctx may contain wrong data after rewrite, so make sure we don't try to access it */ "Mailbox isn't a valid mbox file");
"Error seeking back to original " "offset %s in mbox file %s",
/* set to -1, since it's always increased later */ /* this mbox has pseudo mail which contains the X-IMAP header */ /* doesn't exist anymore, seek to end of file */ "Error seeking to end of mbox file %s",
/* delete sync records up to next message. so if there's still something left in array, it means the next message needs modifying */ /* we can skip forward to next record which needs updating. */ /* if there's no sync records left, we can stop. except if this is a dirty sync, check if there are new messages. */ /* seek failed because the offset is dirty. just ignore and continue from where we are now. */ /* always start from first message so we can read X-IMAP or "UIDVALIDITY changed (%u -> %u) " /* we need to recreate all messages in index */ /* next_uid must be reset before message syncing begins, or we get errors about UIDs larger than /* UID ordering problems, resync everything to make sure we get everything right */ /* from now on, don't skip anything */ /* UID found but it's broken */ /* If we can't use/store X-UID header, use MD5 sum. Also check for existing MD5 sums when we're actually /* get all sync records related to this message */ /* rest of the messages in index don't exist -> expunge them */ "From: Mail System Internal Data <MAILER-DAEMON@%s>\n" "Subject: DON'T DELETE THIS MESSAGE -- FOLDER INTERNAL DATA" "\nMessage-ID: <%s@%s>\n" /* out of disk space, truncate to empty */ /* copy trailer, then truncate the file */ /* everything deleted, the trailer_size still contains /* only reason not to have UID validity at this point is if the file is entirely empty. In that case just make up a new one if needed. */ /* file is fully synced */ /* we want to do full syncing. always do this if file size hasn't changed but timestamp has. it most likely means that someone had modified some header and we probably want to know about it */ /* see if we can delay syncing the whole file. normally we only notice expunges and appends /* partial syncing didn't work, do it again */ /* only syncs left should be just appends (and their updates) which weren't synced yet for some reason (crash). we'll just ignore them, as we've overwritten them above. */ /* we just want to lock it for reading. if mbox hasn't been modified don't do any syncing. */ /* have to sync to make sure offsets have stayed the same */ /* reopen input stream to make sure it has nothing buffered */ /* we're most likely modifying the mbox while syncing, just lock it for writing immediately. the mbox must be locked before index syncing is started to avoid deadlocks, so we don't have much choice either (well, easy ones anyway). */ /* index may need to do internal syncing though, so commit instead of rollbacking. */ /* ok, we have something to do but no locks. we'll have to restart syncing to avoid deadlocking. */ /* make sure we've read the latest keywords in index */ /* Rewrite uid_last in X-IMAPbase header if we've seen it (ie. the file isn't empty) */ /* FIXME: keep the lock MBOX_SYNC_SECS+1 to make sure we notice changes made by others .. and this has to be done even if lock_reading is set.. except if mbox_sync_dirty = TRUE */