changeset 5564:47274663a71c HEAD

Call sync_notify() once at the end of syncing. Allow accessing mails even while syncing, as long as it's safe to do so.
author Timo Sirainen <tss@iki.fi>
date Tue, 17 Apr 2007 18:09:10 +0300
parents 063b184597fa
children 64bdb703f6bd
files src/lib-storage/index/cydir/cydir-sync.c src/lib-storage/index/dbox/dbox-mail.c src/lib-storage/index/dbox/dbox-storage.h src/lib-storage/index/dbox/dbox-sync.c src/lib-storage/index/maildir/maildir-sync.c src/lib-storage/index/mbox/mbox-mail.c src/lib-storage/index/mbox/mbox-storage.h src/lib-storage/index/mbox/mbox-sync.c src/lib-storage/mail-storage-private.h
diffstat 9 files changed, 56 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib-storage/index/cydir/cydir-sync.c	Tue Apr 17 17:07:08 2007 +0300
+++ b/src/lib-storage/index/cydir/cydir-sync.c	Tue Apr 17 18:09:10 2007 +0300
@@ -73,10 +73,11 @@
 
 static int cydir_sync_index(struct cydir_sync_context *ctx)
 {
+	struct mailbox *box = &ctx->mbox->ibox.box;
 	const struct mail_index_header *hdr;
 	struct mail_index_sync_rec sync_rec;
 	uint32_t seq1, seq2;
-	int ret;
+	int ret = 1;
 
 	hdr = mail_index_get_header(ctx->sync_view);
 	if (hdr->uid_validity == 0) {
@@ -84,7 +85,8 @@
 			return -1;
 	}
 
-	while ((ret = mail_index_sync_next(ctx->index_sync_ctx,
+	while (ret > 0 &&
+	       (ret = mail_index_sync_next(ctx->index_sync_ctx,
 					   &sync_rec)) > 0) {
 		if (mail_index_lookup_uid_range(ctx->sync_view,
 						sync_rec.uid1, sync_rec.uid2,
@@ -103,7 +105,7 @@
 			break;
 		case MAIL_INDEX_SYNC_TYPE_EXPUNGE:
 			if (cydir_sync_expunge(ctx, seq1, seq2) < 0)
-				return -1;
+				ret = -1;
 			break;
 		case MAIL_INDEX_SYNC_TYPE_FLAGS:
 		case MAIL_INDEX_SYNC_TYPE_KEYWORD_ADD:
@@ -113,7 +115,10 @@
 			break;
 		}
 	}
-	return 0;
+
+	if (box->v.sync_notify != NULL)
+		box->v.sync_notify(box, 0, 0);
+	return ret;
 }
 
 int cydir_sync_begin(struct cydir_mailbox *mbox,
--- a/src/lib-storage/index/dbox/dbox-mail.c	Tue Apr 17 17:07:08 2007 +0300
+++ b/src/lib-storage/index/dbox/dbox-mail.c	Tue Apr 17 18:09:10 2007 +0300
@@ -114,7 +114,7 @@
 	uoff_t prev_offset = 0;
 	int i, ret;
 
-	if (mail->mail.mail.expunged)
+	if (mail->mail.mail.expunged || mbox->syncing)
 		return 0;
 
 	for (i = 0; i < 3; i++) {
--- a/src/lib-storage/index/dbox/dbox-storage.h	Tue Apr 17 17:07:08 2007 +0300
+++ b/src/lib-storage/index/dbox/dbox-storage.h	Tue Apr 17 18:09:10 2007 +0300
@@ -69,6 +69,8 @@
 
 	uoff_t rotate_size, rotate_min_size;
 	unsigned int rotate_days;
+
+	unsigned int syncing:1;
 };
 
 struct dbox_transaction_context {
--- a/src/lib-storage/index/dbox/dbox-sync.c	Tue Apr 17 17:07:08 2007 +0300
+++ b/src/lib-storage/index/dbox/dbox-sync.c	Tue Apr 17 18:09:10 2007 +0300
@@ -436,6 +436,7 @@
 
 static int dbox_sync_index(struct dbox_sync_context *ctx)
 {
+	struct mailbox *box = &ctx->mbox->ibox.box;
 	struct mail_index_sync_rec sync_rec;
         struct hash_iterate_context *iter;
 	void *key, *value;
@@ -470,6 +471,9 @@
 	}
 	hash_iterate_deinit(iter);
 
+	if (box->v.sync_notify != NULL)
+		box->v.sync_notify(box, 0, 0);
+
 	hash_destroy(ctx->syncs);
 	pool_unref(ctx->pool);
 
@@ -577,7 +581,7 @@
 	return 0;
 }
 
-int dbox_sync(struct dbox_mailbox *mbox, bool force)
+static int dbox_sync_int(struct dbox_mailbox *mbox, bool force)
 {
 	struct dbox_sync_context ctx;
 	int ret;
@@ -609,6 +613,16 @@
 	}
 }
 
+int dbox_sync(struct dbox_mailbox *mbox, bool force)
+{
+	int ret;
+
+	mbox->syncing = TRUE;
+	ret = dbox_sync_int(mbox, force);
+	mbox->syncing = FALSE;
+	return ret;
+}
+
 struct mailbox_sync_context *
 dbox_storage_sync_init(struct mailbox *box, enum mailbox_sync_flags flags)
 {
--- a/src/lib-storage/index/maildir/maildir-sync.c	Tue Apr 17 17:07:08 2007 +0300
+++ b/src/lib-storage/index/maildir/maildir-sync.c	Tue Apr 17 18:09:10 2007 +0300
@@ -1309,6 +1309,9 @@
 		if (maildir_sync_index_records(sync_ctx) < 0)
 			ret = -1;
 		mbox->syncing_commit = FALSE;
+
+		if (mbox->ibox.box.v.sync_notify != NULL)
+			mbox->ibox.box.v.sync_notify(&mbox->ibox.box, 0, 0);
 	}
 
 	if (mbox->dirty_cur_time != 0)
--- a/src/lib-storage/index/mbox/mbox-mail.c	Tue Apr 17 17:07:08 2007 +0300
+++ b/src/lib-storage/index/mbox/mbox-mail.c	Tue Apr 17 18:09:10 2007 +0300
@@ -39,8 +39,8 @@
 	int ret, try;
 	bool deleted;
 
-	if (mail->mail.mail.expunged)
-		return 0;
+	if (mail->mail.mail.expunged || mbox->syncing)
+		return -1;
 
 	for (try = 0; try < 2; try++) {
 		if (mbox->mbox_lock_type == F_UNLCK) {
@@ -74,10 +74,8 @@
 			break;
 		}
 		if (ret < 0) {
-			if (deleted) {
+			if (deleted)
 				mail->mail.mail.expunged = TRUE;
-				return 0;
-			}
 			return -1;
 		}
 
@@ -90,7 +88,7 @@
 			"Losing sync for mail uid=%u in mbox file %s",
 			mail->mail.mail.uid, mbox->path);
 	}
-	return 1;
+	return 0;
 }
 
 static time_t mbox_mail_get_received_date(struct mail *_mail)
@@ -104,7 +102,7 @@
 	if (data->received_date != (time_t)-1)
 		return data->received_date;
 
-	if (mbox_mail_seek(mail) <= 0)
+	if (mbox_mail_seek(mail) < 0)
 		return (time_t)-1;
 	data->received_date =
 		istream_raw_mbox_get_received_time(mbox->mbox_stream);
@@ -147,7 +145,7 @@
 
 	switch (field) {
 	case MAIL_FETCH_FROM_ENVELOPE:
-		if (mbox_mail_seek(mail) <= 0)
+		if (mbox_mail_seek(mail) < 0)
 			return NULL;
 
 		return istream_raw_mbox_get_sender(mbox->mbox_stream);
@@ -179,7 +177,7 @@
 	struct istream *stream;
 	uoff_t hdr_offset, body_offset, body_size;
 
-	if (mbox_mail_seek(mail) <= 0)
+	if (mbox_mail_seek(mail) < 0)
 		return (uoff_t)-1;
 
 	/* our header size varies, so don't do any caching */
@@ -206,7 +204,7 @@
 	uoff_t offset;
 
 	if (data->stream == NULL) {
-		if (mbox_mail_seek(mail) <= 0)
+		if (mbox_mail_seek(mail) < 0)
 			return NULL;
 
 		raw_stream = mbox->mbox_stream;
--- a/src/lib-storage/index/mbox/mbox-storage.h	Tue Apr 17 17:07:08 2007 +0300
+++ b/src/lib-storage/index/mbox/mbox-storage.h	Tue Apr 17 18:09:10 2007 +0300
@@ -47,6 +47,7 @@
 	unsigned int mbox_very_dirty_syncs:1;
 	unsigned int mbox_save_md5:1;
 	unsigned int mbox_dotlocked:1;
+	unsigned int syncing:1;
 };
 
 struct mbox_transaction_context {
--- a/src/lib-storage/index/mbox/mbox-sync.c	Tue Apr 17 17:07:08 2007 +0300
+++ b/src/lib-storage/index/mbox/mbox-sync.c	Tue Apr 17 18:09:10 2007 +0300
@@ -1686,7 +1686,7 @@
 	array_free(&sync_ctx->syncs);
 }
 
-int mbox_sync(struct mbox_mailbox *mbox, enum mbox_sync_flags flags)
+static int mbox_sync_int(struct mbox_mailbox *mbox, enum mbox_sync_flags flags)
 {
 	struct mail_index_sync_ctx *index_sync_ctx;
 	struct mail_index_view *sync_view;
@@ -1904,6 +1904,19 @@
 	return ret;
 }
 
+int mbox_sync(struct mbox_mailbox *mbox, enum mbox_sync_flags flags)
+{
+	int ret;
+
+	mbox->syncing = TRUE;
+	ret = mbox_sync_int(mbox, flags);
+	mbox->syncing = FALSE;
+
+	if (mbox->ibox.box.v.sync_notify != NULL)
+		mbox->ibox.box.v.sync_notify(&mbox->ibox.box, 0, 0);
+	return ret;
+}
+
 struct mailbox_sync_context *
 mbox_storage_sync_init(struct mailbox *box, enum mailbox_sync_flags flags)
 {
--- a/src/lib-storage/mail-storage-private.h	Tue Apr 17 17:07:08 2007 +0300
+++ b/src/lib-storage/mail-storage-private.h	Tue Apr 17 18:09:10 2007 +0300
@@ -91,6 +91,9 @@
 			   enum mailbox_status_items status_items,
 			   struct mailbox_status *status_r);
 
+	/* Called once for each expunge. Called one or more times for
+	   flag/keyword changes. Once the sync is finished, called with
+	   uid=0 and sync_type=0. */
 	void (*sync_notify)(struct mailbox *box, uint32_t uid,
 			    enum mailbox_sync_type sync_type);