diff src/lib-storage/index/dbox-common/dbox-storage.c @ 11484:7fc5db26f6eb HEAD

dbox, mdbox: Fixed race conditions when creating mailboxes.
author Timo Sirainen <tss@iki.fi>
date Fri, 04 Jun 2010 21:09:12 +0100
parents 26b9138d561f
children ad122febbf26
line wrap: on
line diff
--- a/src/lib-storage/index/dbox-common/dbox-storage.c	Fri Jun 04 19:57:49 2010 +0100
+++ b/src/lib-storage/index/dbox-common/dbox-storage.c	Fri Jun 04 21:09:12 2010 +0100
@@ -94,6 +94,10 @@
 			const struct mailbox_update *update, bool directory)
 {
 	struct dbox_storage *storage = (struct dbox_storage *)box->storage;
+	struct mail_index_sync_ctx *sync_ctx;
+	struct mail_index_view *view;
+	struct mail_index_transaction *trans;
+	int ret;
 
 	if (directory &&
 	    (box->list->props & MAILBOX_LIST_PROP_NO_NOSELECT) == 0)
@@ -101,7 +105,22 @@
 
 	if (index_storage_mailbox_open(box, FALSE) < 0)
 		return -1;
-	if (storage->v.mailbox_create_indexes(box, update) < 0)
+
+	/* use syncing as a lock */
+	ret = mail_index_sync_begin(box->index, &sync_ctx, &view, &trans, 0);
+	if (ret <= 0) {
+		i_assert(ret != 0);
+		mail_storage_set_internal_error(box->storage);
+		mail_index_reset_error(box->index);
 		return -1;
-	return 0;
+	}
+
+	if (mail_index_get_header(view)->uid_validity == 0) {
+		if (storage->v.mailbox_create_indexes(box, update, trans) < 0) {
+			mail_index_sync_rollback(&sync_ctx);
+			return -1;
+		}
+	}
+
+	return mail_index_sync_commit(&sync_ctx);
 }