dovecot-2.2: mbox: Fixed mailbox_list_index=yes to work with non...

dovecot at dovecot.org dovecot at dovecot.org
Sun Sep 22 02:07:25 EEST 2013


details:   http://hg.dovecot.org/dovecot-2.2/rev/2a209302d064
changeset: 16803:2a209302d064
user:      Timo Sirainen <tss at iki.fi>
date:      Sun Sep 22 02:07:16 2013 +0300
description:
mbox: Fixed mailbox_list_index=yes to work with non-Dovecot mbox changes.

diffstat:

 src/lib-storage/index/mbox/Makefile.am            |   1 +
 src/lib-storage/index/mbox/mbox-storage.c         |   5 +-
 src/lib-storage/index/mbox/mbox-storage.h         |   8 +-
 src/lib-storage/index/mbox/mbox-sync-list-index.c |  93 +++++++++++++++++++++++
 src/lib-storage/index/mbox/mbox-sync-private.h    |   7 +
 5 files changed, 111 insertions(+), 3 deletions(-)

diffs (170 lines):

diff -r a32eea97afc1 -r 2a209302d064 src/lib-storage/index/mbox/Makefile.am
--- a/src/lib-storage/index/mbox/Makefile.am	Sun Sep 22 01:40:11 2013 +0300
+++ b/src/lib-storage/index/mbox/Makefile.am	Sun Sep 22 02:07:16 2013 +0300
@@ -19,6 +19,7 @@
 	mbox-md5-all.c \
 	mbox-save.c \
 	mbox-settings.c \
+	mbox-sync-list-index.c \
 	mbox-sync-parse.c \
 	mbox-sync-rewrite.c \
 	mbox-sync-update.c \
diff -r a32eea97afc1 -r 2a209302d064 src/lib-storage/index/mbox/mbox-storage.c
--- a/src/lib-storage/index/mbox/mbox-storage.c	Sun Sep 22 01:40:11 2013 +0300
+++ b/src/lib-storage/index/mbox/mbox-storage.c	Sun Sep 22 02:07:16 2013 +0300
@@ -379,6 +379,7 @@
 	mbox->storage = (struct mbox_storage *)storage;
 	mbox->mbox_fd = -1;
 	mbox->mbox_lock_type = F_UNLCK;
+	mbox->mbox_list_index_ext_id = (uint32_t)-1;
 
 	if (strcmp(mbox->storage->set->mbox_md5, "apop3d") == 0)
 		mbox->md5_v = mbox_md5_apop3d;
@@ -836,8 +837,8 @@
 		index_storage_attribute_iter_init,
 		index_storage_attribute_iter_next,
 		index_storage_attribute_iter_deinit,
-		index_storage_list_index_has_changed,
-		index_storage_list_index_update_sync,
+		mbox_list_index_has_changed,
+		mbox_list_index_update_sync,
 		mbox_storage_sync_init,
 		index_mailbox_sync_next,
 		index_mailbox_sync_deinit,
diff -r a32eea97afc1 -r 2a209302d064 src/lib-storage/index/mbox/mbox-storage.h
--- a/src/lib-storage/index/mbox/mbox-storage.h	Sun Sep 22 01:40:11 2013 +0300
+++ b/src/lib-storage/index/mbox/mbox-storage.h	Sun Sep 22 02:07:16 2013 +0300
@@ -22,6 +22,12 @@
 	uint8_t unused[3];
 	guid_128_t mailbox_guid;
 };
+
+struct mbox_list_index_record {
+	uint32_t mtime;
+	uint32_t size;
+};
+
 struct mbox_storage {
 	struct mail_storage storage;
 
@@ -47,7 +53,7 @@
 	bool mbox_writeonly;
 	unsigned int external_transactions;
 
-	uint32_t mbox_ext_idx, md5hdr_ext_idx;
+	uint32_t mbox_ext_idx, md5hdr_ext_idx, mbox_list_index_ext_id;
 	struct mbox_index_header mbox_hdr;
 	const struct mailbox_update *sync_hdr_update;
 
diff -r a32eea97afc1 -r 2a209302d064 src/lib-storage/index/mbox/mbox-sync-list-index.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/lib-storage/index/mbox/mbox-sync-list-index.c	Sun Sep 22 02:07:16 2013 +0300
@@ -0,0 +1,93 @@
+/* Copyright (c) 2013 Dovecot authors, see the included COPYING file */
+
+#include "lib.h"
+#include "mbox-storage.h"
+#include "mbox-sync-private.h"
+
+static unsigned int
+mbox_list_get_ext_id(struct mbox_mailbox *mbox,
+		     struct mail_index_view *view)
+{
+	if (mbox->mbox_list_index_ext_id == (uint32_t)-1) {
+		mbox->mbox_list_index_ext_id =
+			mail_index_ext_register(mail_index_view_get_index(view),
+				"mbox", 0,
+				sizeof(struct mbox_list_index_record),
+				sizeof(uint32_t));
+	}
+	return mbox->mbox_list_index_ext_id;
+}
+
+int mbox_list_index_has_changed(struct mailbox *box,
+				struct mail_index_view *list_view,
+				uint32_t seq)
+{
+	struct mbox_mailbox *mbox = (struct mbox_mailbox *)box;
+	const struct mbox_list_index_record *rec;
+	const void *data;
+	const char *path;
+	struct stat st;
+	uint32_t ext_id;
+	bool expunged;
+	int ret;
+
+	ret = index_storage_list_index_has_changed(box, list_view, seq);
+	if (ret != 0)
+		return ret;
+
+	ext_id = mbox_list_get_ext_id(mbox, list_view);
+	mail_index_lookup_ext(list_view, seq, ext_id, &data, &expunged);
+	rec = data;
+
+	if (rec == NULL || expunged || rec->mtime == 0) {
+		/* doesn't exist or not synced */
+		return 1;
+	}
+
+	ret = mailbox_get_path_to(box, MAILBOX_LIST_PATH_TYPE_MAILBOX, &path);
+	if (ret < 0)
+		return ret;
+	i_assert(ret > 0);
+
+	if (stat(path, &st) < 0) {
+		mail_storage_set_critical(box->storage,
+					  "stat(%s) failed: %m", path);
+		return -1;
+	}
+	if ((time_t)rec->mtime != st.st_mtime ||
+	    rec->size != (uint32_t)(st.st_size & 0xffffffffU))
+		return 1;
+	return 0;
+}
+
+void mbox_list_index_update_sync(struct mailbox *box,
+				 struct mail_index_transaction *trans,
+				 uint32_t seq)
+{
+	struct mbox_mailbox *mbox = (struct mbox_mailbox *)box;
+	struct mail_index_view *list_view;
+	const struct mbox_index_header *mhdr = &mbox->mbox_hdr;
+	const struct mbox_list_index_record *old_rec;
+	struct mbox_list_index_record new_rec;
+	const void *data;
+	uint32_t ext_id;
+	bool expunged;
+
+	index_storage_list_index_update_sync(box, trans, seq);
+
+	/* get the current record */
+	list_view = mail_index_transaction_get_view(trans);
+	ext_id = mbox_list_get_ext_id(mbox, list_view);
+	mail_index_lookup_ext(list_view, seq, ext_id, &data, &expunged);
+	if (expunged)
+		return;
+	old_rec = data;
+
+	memset(&new_rec, 0, sizeof(new_rec));
+	new_rec.mtime = mhdr->sync_mtime;
+	new_rec.size = mhdr->sync_size & 0xffffffffU;
+
+	if (old_rec == NULL ||
+	    memcmp(old_rec, &new_rec, sizeof(*old_rec)) != 0)
+		mail_index_update_ext(trans, seq, ext_id, &new_rec, NULL);
+}
diff -r a32eea97afc1 -r 2a209302d064 src/lib-storage/index/mbox/mbox-sync-private.h
--- a/src/lib-storage/index/mbox/mbox-sync-private.h	Sun Sep 22 01:40:11 2013 +0300
+++ b/src/lib-storage/index/mbox/mbox-sync-private.h	Sun Sep 22 02:07:16 2013 +0300
@@ -183,4 +183,11 @@
 				 size_t size);
 int mbox_sync_get_guid(struct mbox_mailbox *mbox);
 
+int mbox_list_index_has_changed(struct mailbox *box,
+				struct mail_index_view *list_view,
+				uint32_t seq);
+void mbox_list_index_update_sync(struct mailbox *box,
+				 struct mail_index_transaction *trans,
+				 uint32_t seq);
+
 #endif


More information about the dovecot-cvs mailing list