changeset 9102:8be5ca07189f HEAD

dbox: Don't crash if index files can't be opened.
author Timo Sirainen <tss@iki.fi>
date Mon, 13 Apr 2009 20:07:59 -0400
parents 2e20a1a9bcd4
children 5c12eac2c3ca
files src/lib-storage/index/dbox/dbox-storage.c src/lib-storage/index/dbox/dbox-sync.c src/lib-storage/index/index-storage.c src/lib-storage/index/index-storage.h src/lib-storage/index/index-sync.c
diffstat 5 files changed, 42 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib-storage/index/dbox/dbox-storage.c	Mon Apr 13 20:06:40 2009 -0400
+++ b/src/lib-storage/index/dbox/dbox-storage.c	Mon Apr 13 20:07:59 2009 -0400
@@ -223,9 +223,11 @@
 {
 	struct mail_storage *_storage = &storage->storage;
 	struct dbox_mailbox *mbox;
+	struct mailbox *box;
 	struct mail_index *index;
 	const char *path;
 	pool_t pool;
+	int ret;
 
 	path = mailbox_list_get_path(_storage->list, name,
 				     MAILBOX_LIST_PATH_TYPE_MAILBOX);
@@ -257,9 +259,13 @@
 	mbox->guid_ext_id =
 		mail_index_ext_register(index, "guid", 0, DBOX_GUID_BIN_LEN, 1);
 
-	index_storage_mailbox_init(&mbox->ibox, name, flags, FALSE);
+	ret = index_storage_mailbox_init(&mbox->ibox, name, flags, FALSE);
 	mbox->maildir_uidlist = maildir_uidlist_init_readonly(&mbox->ibox);
-	return &mbox->ibox.box;
+
+	box = &mbox->ibox.box;
+	if (ret < 0)
+		mailbox_close(&box);
+	return box;
 }
 
 uint32_t dbox_get_uidvalidity_next(struct mail_storage *storage)
@@ -309,8 +315,11 @@
 			/* create indexes immediately with the dbox header */
 			box = dbox_open(storage, name,
 					MAILBOX_OPEN_KEEP_RECENT);
+			if (box == NULL)
+				return -1;
 			dbox_write_index_header(box);
 			mailbox_close(&box);
+			return 0;
 		}
 	} else if (errno != EEXIST) {
 		if (!mail_storage_set_error_from_errno(_storage)) {
@@ -360,9 +369,9 @@
 
 	path = mailbox_list_get_path(_storage->list, name,
 				     MAILBOX_LIST_PATH_TYPE_MAILBOX);
-	if (dbox_cleanup_if_exists(_storage, path))
+	if (dbox_cleanup_if_exists(_storage, path)) {
 		return dbox_open(storage, name, flags);
-	else if (errno == ENOENT) {
+	} else if (errno == ENOENT) {
 		if (strcmp(name, "INBOX") == 0 &&
 		    (_storage->ns->flags & NAMESPACE_FLAG_INBOX) != 0) {
 			/* INBOX always exists, create it */
--- a/src/lib-storage/index/dbox/dbox-sync.c	Mon Apr 13 20:06:40 2009 -0400
+++ b/src/lib-storage/index/dbox/dbox-sync.c	Mon Apr 13 20:07:59 2009 -0400
@@ -49,7 +49,7 @@
 
 	memset(&lookup_entry, 0, sizeof(lookup_entry));
 	if (dbox_mail_lookup(ctx->mbox, ctx->sync_view, seq, &map_uid) < 0)
-		return 0;
+		return ctx->mbox->storage->sync_rebuild ? 0 : -1;
 	if (map_uid == 0)
 		mail_index_lookup_uid(ctx->sync_view, seq, &lookup_entry.uid);
 	else {
@@ -361,11 +361,13 @@
 	enum dbox_sync_flags dbox_sync_flags = 0;
 	int ret = 0;
 
-	if (!box->opened)
-		index_storage_mailbox_open(&mbox->ibox);
+	if (!box->opened) {
+		if (index_storage_mailbox_open(&mbox->ibox) < 0)
+			ret = -1;
+	}
 
-	if (index_mailbox_want_full_sync(&mbox->ibox, flags) ||
-	    mbox->storage->sync_rebuild) {
+	if (ret == 0 && (index_mailbox_want_full_sync(&mbox->ibox, flags) ||
+			 mbox->storage->sync_rebuild)) {
 		if ((flags & MAILBOX_SYNC_FLAG_FORCE_RESYNC) != 0)
 			dbox_sync_flags |= DBOX_SYNC_FLAG_FORCE_REBUILD;
 		ret = dbox_sync(mbox, dbox_sync_flags);
--- a/src/lib-storage/index/index-storage.c	Mon Apr 13 20:06:40 2009 -0400
+++ b/src/lib-storage/index/index-storage.c	Mon Apr 13 20:07:59 2009 -0400
@@ -349,7 +349,7 @@
 	ibox->last_notify_type = MAILBOX_LOCK_NOTIFY_NONE;
 }
 
-void index_storage_mailbox_open(struct index_mailbox *ibox)
+int index_storage_mailbox_open(struct index_mailbox *ibox)
 {
 	struct mail_storage *storage = ibox->storage;
 	enum mail_index_open_flags index_flags;
@@ -367,6 +367,11 @@
 
 	ret = mail_index_open(ibox->index, index_flags, storage->lock_method);
 	if (ret <= 0 || ibox->move_to_memory) {
+		if (ibox->index_never_in_memory) {
+			mail_storage_set_index_error(ibox);
+			return -1;
+		}
+
 		if (mail_index_move_to_memory(ibox->index) < 0) {
 			/* try opening once more. it should be created
 			   directly into memory now. */
@@ -390,11 +395,12 @@
 	index_thread_mailbox_index_opened(ibox);
 	if (hook_mailbox_index_opened != NULL)
 		hook_mailbox_index_opened(&ibox->box);
+	return 0;
 }
 
-void index_storage_mailbox_init(struct index_mailbox *ibox, const char *name,
-				enum mailbox_open_flags flags,
-				bool move_to_memory)
+int index_storage_mailbox_init(struct index_mailbox *ibox, const char *name,
+			       enum mailbox_open_flags flags,
+			       bool move_to_memory)
 {
 	struct mail_storage *storage = ibox->storage;
 	struct mailbox *box = &ibox->box;
@@ -431,7 +437,8 @@
 		mail_index_ext_register(ibox->index, "header-md5", 0, 16, 1);
 
 	if ((flags & MAILBOX_OPEN_FAST) == 0)
-		index_storage_mailbox_open(ibox);
+		return index_storage_mailbox_open(ibox);
+	return 0;
 }
 
 int index_storage_mailbox_enable(struct mailbox *box,
@@ -441,11 +448,13 @@
 
 	if ((feature & MAILBOX_FEATURE_CONDSTORE) != 0) {
 		box->enabled_features |= MAILBOX_FEATURE_CONDSTORE;
-		if (!box->opened)
-			index_storage_mailbox_open(ibox);
+		if (!box->opened) {
+			if (index_storage_mailbox_open(ibox) < 0)
+				return -1;
+		}
 		mail_index_modseq_enable(ibox->index);
 	}
-	return TRUE;
+	return 0;
 }
 
 int index_storage_mailbox_close(struct mailbox *box)
--- a/src/lib-storage/index/index-storage.h	Mon Apr 13 20:06:40 2009 -0400
+++ b/src/lib-storage/index/index-storage.h	Mon Apr 13 20:07:59 2009 -0400
@@ -99,10 +99,10 @@
 void index_storage_destroy_unrefed(void);
 void index_storage_destroy(struct mail_storage *storage ATTR_UNUSED);
 
-void index_storage_mailbox_init(struct index_mailbox *ibox, const char *name,
-				enum mailbox_open_flags flags,
-				bool move_to_memory);
-void index_storage_mailbox_open(struct index_mailbox *ibox);
+int index_storage_mailbox_init(struct index_mailbox *ibox, const char *name,
+			       enum mailbox_open_flags flags,
+			       bool move_to_memory);
+int index_storage_mailbox_open(struct index_mailbox *ibox);
 int index_storage_mailbox_enable(struct mailbox *box,
 				 enum mailbox_feature feature);
 int index_storage_mailbox_close(struct mailbox *box);
--- a/src/lib-storage/index/index-sync.c	Mon Apr 13 20:06:40 2009 -0400
+++ b/src/lib-storage/index/index-sync.c	Mon Apr 13 20:07:59 2009 -0400
@@ -323,7 +323,7 @@
 	}
 	index_mailbox_expunge_unseen_recent(ctx);
 
-	if (ibox->keep_recent) {
+	if (ibox->keep_recent && ibox->box.opened) {
 		/* mailbox syncing didn't necessarily update our recent state */
 		hdr = mail_index_get_header(ibox->view);
 		if (hdr->first_recent_uid > ibox->recent_flags_prev_uid) {