changeset 5266:0dc6b54bb6aa HEAD

Cleaned up the code a bit. Fixed sync rewrite crashing with pseudo mails.
author Timo Sirainen <tss@iki.fi>
date Sat, 10 Mar 2007 18:05:48 +0200
parents d7bd3f80c6f2
children 02a4228a921b
files src/lib-storage/index/mbox/mbox-sync-parse.c src/lib-storage/index/mbox/mbox-sync-private.h src/lib-storage/index/mbox/mbox-sync-rewrite.c src/lib-storage/index/mbox/mbox-sync-update.c src/lib-storage/index/mbox/mbox-sync.c
diffstat 5 files changed, 32 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib-storage/index/mbox/mbox-sync-parse.c	Sat Mar 10 17:47:32 2007 +0200
+++ b/src/lib-storage/index/mbox/mbox-sync-parse.c	Sat Mar 10 18:05:48 2007 +0200
@@ -253,7 +253,7 @@
 
 	/* this is the c-client style "FOLDER INTERNAL DATA" message.
 	   skip it. */
-	ctx->pseudo = TRUE;
+	ctx->mail.pseudo = TRUE;
 	return TRUE;
 }
 
--- a/src/lib-storage/index/mbox/mbox-sync-private.h	Sat Mar 10 17:47:32 2007 +0200
+++ b/src/lib-storage/index/mbox/mbox-sync-private.h	Sat Mar 10 18:05:48 2007 +0200
@@ -34,7 +34,6 @@
    header, because 'O' flag means non-recent but internally we want to use
    recent flag. */
 #define MBOX_NONRECENT_KLUDGE MAIL_RECENT
-#define MBOX_EXPUNGED 0x40
 
 #define STATUS_FLAGS_MASK (MAIL_SEEN|MBOX_NONRECENT_KLUDGE)
 #define XSTATUS_FLAGS_MASK (MAIL_ANSWERED|MAIL_FLAGGED|MAIL_DRAFT|MAIL_DELETED)
@@ -42,13 +41,17 @@
 extern struct mbox_flag_type mbox_xstatus_flags[];
 
 struct mbox_sync_mail {
+	/* uid=0 can mean that this mail describes an expunged area or that
+	   this is a pseudo message */
 	uint32_t uid;
 	uint32_t idx_seq;
 
 	ARRAY_TYPE(keyword_indexes) keywords;
 	uint8_t flags;
 
-	unsigned int uid_broken:1;
+	uint8_t uid_broken:1;
+	uint8_t expunged:1;
+	uint8_t pseudo:1;
 
 	uoff_t from_offset;
 	uoff_t body_size;
@@ -87,7 +90,6 @@
 	unsigned int have_eoh:1;
 	unsigned int need_rewrite:1;
 	unsigned int seen_imapbase:1;
-	unsigned int pseudo:1;
 	unsigned int updated:1;
 	unsigned int recent:1;
 	unsigned int dirty:1;
--- a/src/lib-storage/index/mbox/mbox-sync-rewrite.c	Sat Mar 10 17:47:32 2007 +0200
+++ b/src/lib-storage/index/mbox/mbox-sync-rewrite.c	Sat Mar 10 18:05:48 2007 +0200
@@ -86,7 +86,7 @@
 
 	i_assert(size < SSIZE_T_MAX);
 
-	if (ctx->pseudo)
+	if (ctx->mail.pseudo)
 		start_pos = ctx->hdr_pos[MBOX_HDR_X_IMAPBASE];
 	else if (ctx->mail.space > 0) {
 		/* update the header using the existing offset.
@@ -331,14 +331,16 @@
 		istream_raw_mbox_get_header_offset(sync_ctx->input);
 	mail_ctx->mail.body_size = mails[idx].body_size;
 
-	/* only expunged mails have uid=0 */
-	i_assert(mails[idx].uid != 0);
-
 	/* This will force the UID to be the one that we originally assigned
 	   to it, regardless of whether it's broken or not in the file. */
 	orig_next_uid = sync_ctx->next_uid;
-	sync_ctx->next_uid = mails[idx].uid;
-	sync_ctx->prev_msg_uid = mails[idx].uid - 1;
+	if (mails[idx].uid != 0) {
+		sync_ctx->next_uid = mails[idx].uid;
+		sync_ctx->prev_msg_uid = mails[idx].uid - 1;
+	} else {
+		i_assert(mails[idx].pseudo);
+		sync_ctx->prev_msg_uid = 0;
+	}
 
 	first_mail_expunge_extra = 1 +
 		sync_ctx->first_mail_crlf_expunged ? 1 : 0;
@@ -353,6 +355,7 @@
 	}
 
 	mbox_sync_parse_next_mail(sync_ctx->input, mail_ctx);
+	i_assert(mail_ctx->mail.pseudo == mails[idx].pseudo);
 
 	/* set next_uid back before updating the headers. this is important
 	   if we're updating the first message to make X-IMAP[base] header
@@ -486,7 +489,7 @@
 	start_offset = mails[0].from_offset;
 	for (first_nonexpunged_idx = 0;; first_nonexpunged_idx++) {
 		i_assert(first_nonexpunged_idx != idx);
-		if ((mails[first_nonexpunged_idx].flags & MBOX_EXPUNGED) == 0)
+		if (!mails[first_nonexpunged_idx].expunged)
 			break;
                 expunged_space += mails[first_nonexpunged_idx].space;
 	}
@@ -507,8 +510,7 @@
 
 		next_end_offset = mails[idx].offset;
 
-		if (mails[idx].space <= 0 &&
-		    (mails[idx].flags & MBOX_EXPUNGED) == 0) {
+		if (mails[idx].space <= 0 && !mails[idx].expunged) {
 			/* give space to this mail. end_offset is left to
 			   contain this message's From-line (ie. below we
 			   move only headers + body). */
@@ -544,7 +546,7 @@
 			}
 
 			move_diff += mails[idx].space;
-			if ((mails[idx].flags & MBOX_EXPUNGED) == 0) {
+			if (!mails[idx].expunged) {
 				move_diff -= padding_per_mail;
 				mails[idx].space = padding_per_mail;
 
--- a/src/lib-storage/index/mbox/mbox-sync-update.c	Sat Mar 10 17:47:32 2007 +0200
+++ b/src/lib-storage/index/mbox/mbox-sync-update.c	Sat Mar 10 18:05:48 2007 +0200
@@ -211,7 +211,7 @@
 		str_append_c(ctx->header, '\n');
 	}
 
-	if (ctx->hdr_pos[MBOX_HDR_X_UID] == (size_t)-1 && !ctx->pseudo) {
+	if (ctx->hdr_pos[MBOX_HDR_X_UID] == (size_t)-1 && !ctx->mail.pseudo) {
 		str_append(ctx->header, "X-UID: ");
 		ctx->hdr_pos[MBOX_HDR_X_UID] = str_len(ctx->header);
 		str_printfa(ctx->header, "%u\n", ctx->mail.uid);
@@ -384,7 +384,7 @@
 	uint8_t old_flags;
 	bool keywords_changed;
 
-	i_assert(ctx->mail.uid != 0 || ctx->pseudo);
+	i_assert(ctx->mail.uid != 0 || ctx->mail.pseudo);
 
 	old_flags = ctx->mail.flags;
 
--- a/src/lib-storage/index/mbox/mbox-sync.c	Sat Mar 10 17:47:32 2007 +0200
+++ b/src/lib-storage/index/mbox/mbox-sync.c	Sat Mar 10 18:05:48 2007 +0200
@@ -124,7 +124,8 @@
 					       mail_ctx->content_length);
 	i_assert(mail_ctx->mail.body_size < OFF_T_MAX);
 
-	if ((mail_ctx->mail.flags & MAIL_RECENT) != 0 && !mail_ctx->pseudo) {
+	if ((mail_ctx->mail.flags & MAIL_RECENT) != 0 &&
+	    !mail_ctx->mail.pseudo) {
 		if (!sync_ctx->mbox->ibox.keep_recent) {
 			/* need to add 'O' flag to Status-header */
 			mail_ctx->need_rewrite = TRUE;
@@ -651,8 +652,7 @@
 
 	mails = array_get(&sync_ctx->mails, &count);
 	for (i = 0; i < count; i++) {
-		if (mails[i].idx_seq == 0 ||
-		    (mails[i].flags & MBOX_EXPUNGED) != 0)
+		if (mails[i].idx_seq == 0 || mails[i].expunged)
 			continue;
 
 		sync_ctx->moved_offsets = TRUE;
@@ -666,7 +666,7 @@
 {
 	struct mbox_sync_context *sync_ctx = mail_ctx->sync_ctx;
 
-	mail_ctx->mail.flags = MBOX_EXPUNGED;
+	mail_ctx->mail.expunged = TRUE;
 	mail_ctx->mail.offset = mail_ctx->mail.from_offset;
 	mail_ctx->mail.space =
 		mail_ctx->body_offset - mail_ctx->mail.from_offset +
@@ -762,7 +762,7 @@
 			struct mbox_sync_mail mail;
 
 			memset(&mail, 0, sizeof(mail));
-			mail.flags = MBOX_EXPUNGED;
+			mail.expunged = TRUE;
 			mail.offset = mail.from_offset =
 				(sync_ctx->dest_first_mail ? 1 : 0) +
 				mail_ctx->mail.from_offset -
@@ -1074,7 +1074,7 @@
 		if (mail_ctx->mail.uid_broken)
 			uids_broken = TRUE;
 
-		if (mail_ctx->pseudo)
+		if (mail_ctx->mail.pseudo)
 			uid = 0;
 
 		rec = NULL; ret = 1;
@@ -1088,7 +1088,7 @@
 			/* UID found but it's broken */
 			uid = 0;
 		} else if (uid == 0 &&
-			   !mail_ctx->pseudo &&
+			   !mail_ctx->mail.pseudo &&
 			   (sync_ctx->delay_writes ||
 			    sync_ctx->idx_seq <= messages_count)) {
 			/* If we can't use/store X-UID header, use MD5 sum.
@@ -1109,11 +1109,11 @@
 		   message just get the first sync record so we can jump to
 		   it with partial seeking. */
 		if (mbox_sync_read_index_syncs(sync_ctx,
-					       mail_ctx->pseudo ? 1 : uid,
+					       mail_ctx->mail.pseudo ? 1 : uid,
 					       &expunged) < 0)
 			return -1;
 
-		if (mail_ctx->pseudo) {
+		if (mail_ctx->mail.pseudo) {
 			/* if it was set, it was for the next message */
 			expunged = FALSE;
 		} else {
@@ -1124,7 +1124,7 @@
 			}
 		}
 
-		if (uid == 0 && !mail_ctx->pseudo) {
+		if (uid == 0 && !mail_ctx->mail.pseudo) {
 			/* missing/broken X-UID. all the rest of the mails
 			   need new UIDs. */
 			while (sync_ctx->idx_seq <= messages_count) {
@@ -1149,7 +1149,7 @@
 			sync_ctx->prev_msg_uid = mail_ctx->mail.uid;
 		}
 
-		if (!mail_ctx->pseudo)
+		if (!mail_ctx->mail.pseudo)
 			mail_ctx->mail.idx_seq = sync_ctx->idx_seq;
 
 		if (!expunged) {
@@ -1161,7 +1161,7 @@
 			mbox_sync_handle_expunge(mail_ctx);
 		}
 
-		if (!mail_ctx->pseudo) {
+		if (!mail_ctx->mail.pseudo) {
 			if (!expunged) {
 				if (mbox_sync_update_index(mail_ctx, rec) < 0)
 					return -1;