- 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" "mbox was modified while we were syncing, " "check your locking settings");
"Sync failed for mbox: %s",
"Unexpectedly lost From-line at offset %"PRIuUOFF_T,
/* Do this even if ext_modified is already set. Expunging code relies on last_stat being updated. */ /* just mark the stat as dirty. */ "Couldn't get header offset for seq=%u",
mail_ctx->
seq);
/* need to add 'O' flag to Status-header */ /* nothing for this or the future ones */ /* we can't expunge anything from read-only mboxes */ /* externally expunged message, remove from index */ /* this UID was already in index and it was expunged */ "Expunged message reappeared to mailbox " "(UID %u < %u, seq=%u, idx_msgs=%u)",
/* new UID in the middle of the mailbox - shouldn't happen */ "UID inserted in the middle of mailbox " "(%u > %u, seq=%u, idx_msgs=%u)",
/* externally expunged message, remove from index */ /* see if from_offset needs updating */ /* default to undirtying the message. it gets added back if /* flags and keywords are dirty. replace the current ones from the flags in index file. */ /* changes are written to the mbox file */ /* make sure this message gets written later */ /* only dirty flag state changed */ /* see if keywords changed */ /* see if we need to update md5 sum. */ /* Mail has "Status: O" header. No messages before this /* 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 offset unexpectedly outside mbox");
if (
buf[i] <
'0' ||
buf[i] >
'9') {
"X-IMAPbase uid-last unexpectedly lost");
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. we can't do this if we're in the middle of rewriting acquiring more /* first mail with no space to write it */ /* create dummy message to describe the expunged data */ /* if this is going to be the first mail, increase the from_offset to point to the beginning of the From-line, because the previous [CR]LF is already covered by expunged_space. */ /* mail's keywords are allocated from a pool that's cleared for each mail. we'll need to copy it to something more /* 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");
"Message was expunged unexpectedly");
"Error seeking back to original " /* set to -1, since it's always increased later */ /* this mbox has pseudo mail which contains the X-IMAP header */ "Message body offset lookup failed");
/* doesn't exist anymore, seek to end of file */ "Error seeking to end of mbox");
/* delete sync records up to next message. so if there's still something left in array, it means the next message needs modifying */ /* we'll need to rewrite Status: O headers */ /* we'll need to rewrite Status: O headers */ /* 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. */ i_warning(
"UIDVALIDITY changed (%u -> %u) in mbox file %s",
/* always start from first message so we can read X-IMAP or /* UID ordering problems, resync everything to make sure we get everything right */ "UIDs broken with partial sync");
/* 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. with pseudo message just get the first sync record so we can jump to it with partial seeking. */ /* if it was set, it was for the next message */ /* message wasn't found from index. we have to read everything from now on, no skipping */ /* oh no, we're out of UIDs. this shouldn't happen normally, so just try to get it fixed "Out of UIDs, renumbering them in mbox");
/* rest of the messages in index don't exist -> expunge them */ /* once we get around to writing the changes, we'll need to do a full sync to avoid the "UIDs broken in partial sync" "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 */ /* Not a file - allow anyway */ "file size unexpectedly shrank " /* copy trailer, then truncate the file */ /* everything deleted, the trailer_size still contains /* everything deleted and we didn't have a proper /* upgrading from v1.x */ /* We moved messages inside the mbox file without changing the file's size. If mtime doesn't change, another process not using the same index file as us can't know that the file was changed. So make sure the mtime changes. This should happen rarely enough that the sleeping doesn't become a Note that to do this perfectly safe we should do this wait whenever mails are moved or expunged, regardless of whether the file's size changed. That however could become a performance problem and the consequences of being wrong are quite minimal (an extra logged error message). */ /* 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. */ /* other sessions have already marked more messages as /* mark recent messages */ /* forcing a full sync. assume file has changed. */ /* 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 /* a) partial sync didn't work /* fixing a broken mbox state, be sure to write the changes (except if we're readonly). */ /* 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. */ /* Rewrite uid_last in X-IMAPbase header if we've seen it (ie. the file isn't empty) */ /* need to assign mailbox GUID */ /* 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 */ /* flush input streams' buffers */ /* 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). */ /* see if we need to drop recent flags */ /* index may need to do internal syncing though, so commit instead of rolling back. */ /* make sure we've read the latest keywords in index */ /* if we have only flag changes, we don't need to open the /* ok, we have something to do but no locks. we'll have to restart syncing to avoid deadlocking. */ /* try to set atime back to its original value. (it'll fail with EPERM for shared mailboxes where we aren't /* syncing failed, don't leave it locked */