mbox-sync.c revision cdaf255d6a3daeef0ac85edaa60bfa6d1f945bff
/* Copyright (C) 2004 Timo Sirainen */
/*
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
Here's how we do it:
- 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
padding
- 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
without modifications.
- 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
rewritten.
*/
#include "lib.h"
#include "ioloop.h"
#include "buffer.h"
#include "hostpid.h"
#include "istream.h"
#include "file-set-size.h"
#include "str.h"
#include "read-full.h"
#include "write-full.h"
#include "message-date.h"
#include "istream-raw-mbox.h"
#include "mbox-storage.h"
#include "mbox-from.h"
#include "mbox-file.h"
#include "mbox-lock.h"
#include "mbox-sync-private.h"
#include <stddef.h>
#include <stdlib.h>
#define MBOX_SYNC_SECS 1
/* The text below was taken exactly as c-client wrote it to my mailbox,
so it's probably copyrighted by University of Washington. */
#define PSEUDO_MESSAGE_BODY \
"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
" from mbox file %s", from_offset,
return -1;
}
return 0;
}
{
struct mail_index_sync_rec *syncs;
/* keep it */
dest++;
}
}
}
static int
struct mbox_sync_mail_context *mail_ctx)
{
/* get EOF */
return 0;
/* need to add 'O' flag to Status-header */
}
}
return 1;
}
{
const struct mail_index_sync_rec *syncs;
unsigned int i, count;
for (i = 0; i < count; i++) {
return TRUE;
}
return FALSE;
}
{
int ret;
*sync_expunge_r = FALSE;
return 0;
if (uid == 0) {
/* nothing for this or the future ones */
}
*sync_expunge_r = TRUE;
}
if (ret < 0) {
return -1;
}
if (ret == 0) {
break;
}
}
}
if (!*sync_expunge_r)
return 0;
}
struct mbox_sync_mail *mail,
int *keywords_changed_r)
{
const struct mail_index_sync_rec *syncs;
unsigned int i, count;
for (i = 0; i < count; i++) {
break;
/* no existing keywords */
break;
/* adding, create the array */
unsigned int,
}
if (mail_index_sync_keywords_apply(&syncs[i],
break;
default:
break;
}
}
}
static int
{
int ret = 0;
if (ret < 0) {
return -1;
}
break;
/* 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 "
} else {
ret = 1;
}
return ret;
}
unsigned char hdr_md5_sum[],
const struct mail_index_record **rec_r)
{
const void *data;
int ret;
if (ret < 0) {
return -1;
}
&data) < 0) {
return -1;
}
break;
/* externally expunged message, remove from index */
}
return 0;
}
static int
struct mbox_sync_mail *mail,
int nocheck)
{
const void *data;
if (!nocheck) {
/* see if from_offset needs updating */
&data) < 0) {
return -1;
}
return 0;
}
return 0;
}
static void
{
struct mail_keywords *keywords;
}
const struct mail_index_record *rec)
{
else if (!sync_ctx->delay_writes)
/* new message */
}
} else {
/* 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 */
struct mbox_sync_mail idx_mail;
int keywords_changed;
/* get old keywords */
t_push();
unsigned int, 32);
t_pop();
return -1;
}
/* flags are dirty. ignore whatever was in the mbox,
mbox_flags &= SYNC_FLAGS;
} else {
/* keep index's internal flags */
~(MAIL_FLAGS_MASK | SYNC_FLAGS);
}
(mbox_flags & ~SYNC_FLAGS)) {
} else {
MAIL_RECENT) != 0) {
/* drop recent flag (it can only be dropped) */
}
MAIL_INDEX_MAIL_FLAG_DIRTY) != 0) {
/* dirty flag state changed */
int dirty = (mbox_flags &
MAIL_INDEX_MAIL_FLAG_DIRTY) != 0;
}
}
t_pop();
}
}
/* update from_offsets, but not if we're going to rewrite this message.
rewriting would just move it anyway. */
if (sync_ctx->need_space_seq == 0) {
return -1;
}
return 0;
}
{
const unsigned char *data;
for (;;) {
if (size >= from_line_size)
from_line_size -= size;
if (from_line_size == 0)
break;
if (i_stream_read(input) < 0)
return -1;
}
return 0;
}
{
unsigned char buf[10];
const char *str;
unsigned int i;
int ret;
/* 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. */
if (ret < 0) {
return -1;
}
if (ret == 0) {
"X-IMAPbase uid-last unexpectedly points outside "
return -1;
}
break;
}
}
"X-IMAPbase uid-last unexpectedly lost in mbox file %s",
return -1;
}
/* and write it */
sync_ctx->base_uid_last_offset) < 0) {
return -1;
}
return 0;
}
static int
{
return -1;
}
return 0;
}
{
const struct mbox_sync_mail *mails;
unsigned int i, count;
for (i = 0; i < count; i++) {
continue;
}
}
{
/* expunging first message, fix space to contain next
message's \n header too since it will be removed. */
/* uid-last offset is invalid now */
}
}
{
int ret;
/* move the header backwards to fill expunged space */
if (sync_ctx->dest_first_mail) {
/* we're moving this mail to beginning of file.
skip the initial \n (it's already counted in
expunged_space) */
}
/* read the From-line before rewriting overwrites it */
if (mbox_read_from_line(mail_ctx) < 0)
return -1;
if (ret < 0)
return -1;
if (ret > 0) {
/* rewrite successful, write From-line to
new location */
if (mbox_write_from_line(mail_ctx) < 0)
return -1;
} else {
if (sync_ctx->dest_first_mail) {
/* didn't have enough space, move the offset
back so seeking into it doesn't fail */
}
}
} else if (mail_ctx->need_rewrite ||
if (sync_ctx->delay_writes) {
/* mark it dirty and do it later */
return 0;
}
return -1;
} else {
/* nothing to do */
return 0;
}
/* first mail with no space to write it */
sync_ctx->space_diff = 0;
if (sync_ctx->expunged_space > 0) {
/* create dummy message to describe the expunged data */
struct mbox_sync_mail mail;
sync_ctx->expunged_space = 0;
}
}
return 0;
}
static int
{
if (sync_ctx->space_diff < 0) {
if (sync_ctx->expunged_space > 0) {
sync_ctx->expunged_space = 0;
}
return 0;
}
/* 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 */
} else {
sync_ctx->expunged_space = 0;
}
} else {
/* this message gave enough space from headers. rewriting stops
at the end of this message's headers. */
sync_ctx->expunged_space = 0;
move_diff = 0;
}
return -1;
/* mail_ctx may contain wrong data after rewrite, so make sure we
don't try to access it */
sync_ctx->need_space_seq = 0;
sync_ctx->space_diff = 0;
return 0;
}
static int
{
if (seq == 0) {
"Mailbox isn't a valid mbox file");
return -1;
}
seq++;
} else {
if (ret < 0)
return -1;
if (ret == 0) {
old_offset) < 0) {
"Error seeking back to original "
"offset %s in mbox file %s",
return -1;
}
return 0;
}
}
if (seq <= 1)
uid = 0;
return -1;
}
/* set to -1, since it's always increased later */
/* this mbox has pseudo mail which contains the X-IMAP header */
}
return 1;
}
static int
{
return -1;
}
if (seq1 == 0) {
/* doesn't exist anymore, seek to end of file */
"i_stream_stat()");
return -1;
}
"Error seeking to end of mbox file %s",
return -1;
}
return 1;
}
}
int *skipped_mails)
{
int ret;
/* delete sync records up to next message. so if there's still
something left in array, it means the next message needs modifying */
return 1;
/* we can skip forward to next record which needs updating. */
*skipped_mails = TRUE;
}
} else {
/* if there's no sync records left, we can stop. except if
this is a dirty sync, check if there are new messages. */
return 0;
*skipped_mails = TRUE;
} else {
ret = 1;
}
}
if (ret == 0) {
/* seek failed because the offset is dirty. just ignore and
continue from where we are now. */
ret = 1;
}
return ret;
}
struct mbox_sync_mail_context *mail_ctx,
int partial)
{
const struct mail_index_record *rec;
/* always start from first message so we can read X-IMAP or
X-IMAPbase header */
if (ret <= 0)
return ret;
"UIDVALIDITY changed (%u -> %u) "
"in mbox file %s",
return -1;
}
/* UID ordering problems, resync everything to make
sure we get everything right */
return 0;
}
uid = 0;
if (uid != 0) {
if (ret < 0)
return -1;
}
if (ret == 0) {
/* UID found but it's broken */
uid = 0;
} else if (uid == 0 &&
(sync_ctx->delay_writes ||
Also check for existing MD5 sums when we're actually
able to write X-UIDs. */
&rec) < 0)
return -1;
}
/* from now on, don't skip anything */
}
/* get all sync records related to this message */
&expunged) < 0)
return -1;
} else {
}
need new UIDs. */
}
}
if (!expunged) {
if (mbox_sync_handle_header(mail_ctx) < 0)
return -1;
} else {
}
if (!expunged) {
return -1;
}
}
if (sync_ctx->need_space_seq != 0) {
if (mbox_sync_handle_missing_space(mail_ctx) < 0)
return -1;
return -1;
} else if (sync_ctx->expunged_space > 0) {
if (!expunged) {
/* move the body */
return -1;
return -1;
}
} else if (partial) {
&partial,
if (ret <= 0) {
if (ret < 0)
return -1;
break;
}
}
}
/* rest of the messages in index don't exist -> expunge them */
}
if (!skipped_mails)
return 1;
}
{
unsigned int uid_validity;
i_assert(uid_validity != 0);
"From: Mail System Internal Data <MAILER-DAEMON@%s>\n"
"Subject: DON'T DELETE THIS MESSAGE -- FOLDER INTERNAL DATA"
"\nMessage-ID: <%s@%s>\n"
"X-IMAP: %u %010u\n"
"Status: RO\n"
"\n"
"\n",
"pwrite_full()");
return -1;
}
/* out of disk space, truncate to empty */
}
return 0;
}
struct mbox_sync_mail_context *mail_ctx)
{
return 0;
}
return -1;
}
if (sync_ctx->need_space_seq != 0) {
sync_ctx->expunged_space = 0;
"file_set_size()");
return -1;
}
return -1;
sync_ctx->need_space_seq = 0;
}
if (sync_ctx->expunged_space > 0) {
/* copy trailer, then truncate the file */
"i_stream_stat()");
return -1;
}
/* everything deleted, the trailer_size still contains
the \n trailer though */
trailer_size = 0;
}
trailer_size) < 0)
return -1;
offset + trailer_size) < 0) {
return -1;
}
if (offset == 0) {
if (mbox_write_pseudo(sync_ctx) < 0)
return -1;
}
sync_ctx->expunged_space = 0;
}
return 0;
}
{
return -1;
}
/* 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. */
sync_ctx->base_uid_validity == 0) {
if (sync_ctx->base_uid_validity == 0) {
(unsigned int)ioloop_time;
}
}
}
}
}
return 0;
}
{
sync_ctx->base_uid_validity = 0;
sync_ctx->base_uid_last = 0;
sync_ctx->base_uid_last_offset = 0;
sync_ctx->prev_msg_uid = 0;
sync_ctx->need_space_seq = 0;
sync_ctx->expunged_space = 0;
sync_ctx->space_diff = 0;
}
enum mbox_sync_flags flags)
{
struct mbox_sync_mail_context mail_ctx;
"i_stream_stat()");
return -1;
}
/* file is fully synced */
} else if ((flags & MBOX_SYNC_UNDIRTY) != 0 ||
/* 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 */
} else {
/* see if we can delay syncing the whole file.
normally we only notice expunges and appends
in partial syncing. */
}
if (ret <= 0) {
if (ret < 0)
return -1;
/* partial syncing didn't work, do it again */
if (ret <= 0) {
return -1;
}
}
return -1;
/* 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. */
if (mbox_sync_update_index_header(sync_ctx) < 0)
return -1;
return 0;
}
{
const struct mail_index_header *hdr;
/* read-only stream */
return -1;
}
} else {
return -1;
}
}
/* fully synced */
return 0;
}
return 1;
}
{
struct mail_index_sync_ctx *index_sync_ctx;
struct mail_index_view *sync_view;
struct mbox_sync_context sync_ctx;
unsigned int lock_id = 0;
if (!mbox->mbox_do_dirty_syncs)
if ((flags & MBOX_SYNC_LOCK_READING) != 0) {
return -1;
}
if ((flags & MBOX_SYNC_HEADER) != 0)
changed = 1;
else {
if ((flags & MBOX_SYNC_LOCK_READING) != 0)
return -1;
}
}
if ((flags & MBOX_SYNC_LOCK_READING) != 0) {
/* we just want to lock it for reading. if mbox hasn't been
modified don't do any syncing. */
if (!changed)
return 0;
/* have to sync to make sure offsets have stayed the same */
lock_id = 0;
}
/* reopen input stream to make sure it has nothing buffered */
if (changed) {
/* 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). */
return -1;
}
if ((flags & MBOX_SYNC_LAST_COMMIT) != 0) {
} else {
}
(flags & MBOX_SYNC_REWRITE) != 0);
if (ret <= 0) {
if (ret < 0)
if (lock_id != 0)
return ret;
}
/* nothing to do */
if (lock_id != 0)
/* index may need to do internal syncing though, so commit
instead of rollbacking. */
if (mail_index_sync_commit(index_sync_ctx) < 0) {
return -1;
}
return 0;
}
if (lock_id == 0) {
/* ok, we have something to do but no locks. we'll have to
restart syncing to avoid deadlocking. */
changed = 1;
goto __again;
}
if (mbox_file_open_stream(mbox) < 0) {
return -1;
}
/* make sure we've read the latest keywords in index */
struct mbox_sync_mail, 64);
struct mail_index_sync_rec, 32);
((flags & MBOX_SYNC_REWRITE) == 0 &&
if (ret < 0)
ret = -1;
} else {
}
if (ret < 0)
else if (mail_index_sync_commit(index_sync_ctx) < 0) {
ret = -1;
}
sync_ctx.base_uid_last_offset != 0) {
/* Rewrite uid_last in X-IMAPbase header if we've seen it
(ie. the file isn't empty) */
}
!mbox->mbox_writeonly) {
ret = -1;
}
}
/* drop to read lock */
unsigned int read_lock_id = 0;
ret = -1;
else {
ret = -1;
}
}
/* 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 */
ret = -1;
}
return ret;
}
struct mailbox_sync_context *
{
enum mbox_sync_flags mbox_sync_flags = 0;
int ret = 0;
if ((flags & MAILBOX_SYNC_FLAG_FAST) == 0 ||
ioloop_time) {
if ((flags & MAILBOX_SYNC_FLAG_FULL_READ) != 0 &&
if ((flags & MAILBOX_SYNC_FLAG_FULL_WRITE) != 0)
}
}