mbox-sync.c revision f5b919e9b07dfd9d2401b998ef8759e5f0312719
451N/A/* Copyright (C) 2004 Timo Sirainen */ 451N/A Modifying mbox can be slow, so we try to do it all at once minimizing the 451N/A required disk I/O. We may need to: 451N/A - Update message flags in Status, X-Status and X-Keywords headers 451N/A - Write missing X-UID and X-IMAPbase headers 451N/A - Write missing or broken Content-Length header if there's space 451N/A - Expunge specified messages 451N/A - Start reading the mails from the beginning 451N/A - X-Keywords, X-UID and X-IMAPbase headers may contain padding at the end 451N/A of them, remember how much each message has and offset to beginning of the 451N/A - If header needs to be rewritten and there's enough space, do it 451N/A - If we didn't have enough space, remember how much was missing 451N/A - Continue reading and counting the padding in each message. If available 451N/A padding is enough to rewrite all the previous messages needing it, do it 451N/A - When we encounter expunged message, treat all of it as padding and 451N/A rewrite previous messages if needed (and there's enough space). 451N/A Afterwards keep moving messages backwards to fill the expunged space. 451N/A Moving is done by rewriting each message's headers, with possibly adding 742N/A missing Content-Length header and padding. Message bodies are moved 742N/A - If we encounter end of file, grow the file and rewrite needed messages 742N/A - Rewriting is done by moving message body forward, rewriting message's 742N/A header and doing the same for previous message, until all of them are 742N/A /* need to add 'O' flag to Status-header */ 742N/A /* nothing for this or the future ones */ for (i = 0; i <
size; i++) {
/* 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 */ /*FIXME:mail_cache_add(sync_ctx->cache_trans, str_data(mail_ctx->uidl), str_len(mail_ctx->uidl));*/ /* see if flags changed */ /* flags are dirty, ignore whatever was in the file. but remove recent flag if needed. */ /* 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;
/* expunging first message, fix space to contain next message's \n header too since it will be removed. */ /* 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",
/* we sync only what we need to. jump to first record that "UIDVALIDITY changed (%u -> %u) " /* UID ordering problems, resync everything to make sure we get everything right */ /* 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 */ /* if there's no sync records left, /* we can skip forward to next record which needs updating. if it failes because the offset is dirty, just ignore and continue from where we are now. */ /* rest of the messages in index don't exist -> expunge them */ /* copy trailer, then truncate the file */ /* everything deleted, the trailer_size still contains /* we didn't rewrite X-IMAPbase header because a) mbox is read-only, b) we're lazy-writing */ /* 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. */ /* view is invalidated */ /* rewrite X-IMAPbase header. do it after mail_index_sync_end() so previous transactions have been committed. */ /* 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 */