dovecot-1.3: dbox: Don't crash if index files can't be opened.

dovecot at dovecot.org dovecot at dovecot.org
Tue Apr 14 03:08:05 EEST 2009


details:   http://hg.dovecot.org/dovecot-1.3/rev/8be5ca07189f
changeset: 9102:8be5ca07189f
user:      Timo Sirainen <tss at iki.fi>
date:      Mon Apr 13 20:07:59 2009 -0400
description:
dbox: Don't crash if index files can't be opened.

diffstat:

5 files changed, 45 insertions(+), 25 deletions(-)
src/lib-storage/index/dbox/dbox-storage.c |   17 +++++++++++++----
src/lib-storage/index/dbox/dbox-sync.c    |   14 ++++++++------
src/lib-storage/index/index-storage.c     |   29 +++++++++++++++++++----------
src/lib-storage/index/index-storage.h     |    8 ++++----
src/lib-storage/index/index-sync.c        |    2 +-

diffs (185 lines):

diff -r 2e20a1a9bcd4 -r 8be5ca07189f src/lib-storage/index/dbox/dbox-storage.c
--- 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 @@ dbox_open(struct dbox_storage *storage, 
 {
 	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 @@ dbox_open(struct dbox_storage *storage, 
 	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 @@ static int create_dbox(struct mail_stora
 			/* 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 @@ dbox_mailbox_open(struct mail_storage *_
 
 	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 */
diff -r 2e20a1a9bcd4 -r 8be5ca07189f src/lib-storage/index/dbox/dbox-sync.c
--- 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 @@ static int dbox_sync_add_seq(struct dbox
 
 	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 @@ dbox_storage_sync_init(struct mailbox *b
 	enum dbox_sync_flags dbox_sync_flags = 0;
 	int ret = 0;
 
-	if (!box->opened)
-		index_storage_mailbox_open(&mbox->ibox);
-
-	if (index_mailbox_want_full_sync(&mbox->ibox, flags) ||
-	    mbox->storage->sync_rebuild) {
+	if (!box->opened) {
+		if (index_storage_mailbox_open(&mbox->ibox) < 0)
+			ret = -1;
+	}
+
+	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);
diff -r 2e20a1a9bcd4 -r 8be5ca07189f src/lib-storage/index/index-storage.c
--- 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 @@ void index_storage_lock_notify_reset(str
 	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 @@ void index_storage_mailbox_open(struct i
 
 	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 @@ void index_storage_mailbox_open(struct i
 	index_thread_mailbox_index_opened(ibox);
 	if (hook_mailbox_index_opened != NULL)
 		hook_mailbox_index_opened(&ibox->box);
-}
-
-void index_storage_mailbox_init(struct index_mailbox *ibox, const char *name,
-				enum mailbox_open_flags flags,
-				bool move_to_memory)
+	return 0;
+}
+
+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 @@ void index_storage_mailbox_init(struct i
 		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 @@ int index_storage_mailbox_enable(struct 
 
 	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)
diff -r 2e20a1a9bcd4 -r 8be5ca07189f src/lib-storage/index/index-storage.h
--- 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_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);
diff -r 2e20a1a9bcd4 -r 8be5ca07189f src/lib-storage/index/index-sync.c
--- 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 @@ int index_mailbox_sync_deinit(struct mai
 	}
 	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) {


More information about the dovecot-cvs mailing list