changeset 5618:5ea33dbddbae HEAD

Moved index directory creation code to index_storage_alloc().
author Timo Sirainen <tss@iki.fi>
date Sun, 13 May 2007 21:28:41 +0300
parents a0310d7b6971
children 121af23cfc65
files src/lib-storage/index/cydir/cydir-storage.c src/lib-storage/index/dbox/dbox-storage.c src/lib-storage/index/index-storage.c src/lib-storage/index/index-storage.h src/lib-storage/index/maildir/maildir-storage.c src/lib-storage/index/mbox/mbox-storage.c
diffstat 6 files changed, 77 insertions(+), 159 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib-storage/index/cydir/cydir-storage.c	Sun May 13 21:07:05 2007 +0300
+++ b/src/lib-storage/index/cydir/cydir-storage.c	Sun May 13 21:28:41 2007 +0300
@@ -151,26 +151,6 @@
 	return 0;
 }
 
-static int create_index_dir(struct mail_storage *storage, const char *name)
-{
-	const char *root_dir, *index_dir;
-
-	root_dir = mailbox_list_get_path(storage->list, name,
-					 MAILBOX_LIST_PATH_TYPE_MAILBOX);
-	index_dir = mailbox_list_get_path(storage->list, name,
-					  MAILBOX_LIST_PATH_TYPE_INDEX);
-	if (strcmp(index_dir, root_dir) == 0)
-		return 0;
-
-	if (mkdir_parents(index_dir, CREATE_MODE) < 0 && errno != EEXIST) {
-		mail_storage_set_critical(storage, "mkdir(%s) failed: %m",
-					  index_dir);
-		return -1;
-	}
-
-	return 0;
-}
-
 static struct mailbox *
 cydir_open(struct cydir_storage *storage, const char *name,
 	   enum mailbox_open_flags flags)
@@ -178,20 +158,15 @@
 	struct mail_storage *_storage = &storage->storage;
 	struct cydir_mailbox *mbox;
 	struct mail_index *index;
-	const char *path, *index_dir;
+	const char *path;
 	pool_t pool;
 
 	path = mailbox_list_get_path(_storage->list, name,
 				     MAILBOX_LIST_PATH_TYPE_MAILBOX);
-	index_dir = mailbox_list_get_path(_storage->list, name,
-					  MAILBOX_LIST_PATH_TYPE_INDEX);
-
 	if (create_cydir(_storage, path) < 0)
 		return NULL;
-	if (create_index_dir(_storage, name) < 0)
-		return NULL;
 
-	index = index_storage_alloc(index_dir, path, CYDIR_INDEX_PREFIX);
+	index = index_storage_alloc(_storage, name, flags, CYDIR_INDEX_PREFIX);
 
 	pool = pool_alloconly_create("cydir mailbox", 1024+512);
 	mbox = p_new(pool, struct cydir_mailbox, 1);
--- a/src/lib-storage/index/dbox/dbox-storage.c	Sun May 13 21:07:05 2007 +0300
+++ b/src/lib-storage/index/dbox/dbox-storage.c	Sun May 13 21:28:41 2007 +0300
@@ -267,25 +267,6 @@
 	return 0;
 }
 
-static int create_index_dir(struct mail_storage *storage, const char *name)
-{
-	const char *root_dir, *index_dir;
-
-	root_dir = mailbox_list_get_path(storage->list, name,
-					 MAILBOX_LIST_PATH_TYPE_MAILBOX);
-	index_dir = mailbox_list_get_path(storage->list, name,
-					  MAILBOX_LIST_PATH_TYPE_INDEX);
-	if (strcmp(index_dir, root_dir) == 0)
-		return 0;
-
-	if (mkdir_parents(index_dir, CREATE_MODE) < 0 && errno != EEXIST) {
-		mail_storage_set_critical(storage, "mkdir(%s) failed: %m",
-					  index_dir);
-		return -1;
-	}
-
-	return 0;
-}
 
 static void dbox_lock_touch_timeout(struct dbox_mailbox *mbox)
 {
@@ -299,20 +280,15 @@
 	struct mail_storage *_storage = &storage->storage;
 	struct dbox_mailbox *mbox;
 	struct mail_index *index;
-	const char *path, *index_dir, *value;
+	const char *path, *value;
 	pool_t pool;
 
 	path = mailbox_list_get_path(_storage->list, name,
 				     MAILBOX_LIST_PATH_TYPE_MAILBOX);
-	index_dir = mailbox_list_get_path(_storage->list, name,
-					  MAILBOX_LIST_PATH_TYPE_INDEX);
-
 	if (create_dbox(_storage, path) < 0)
 		return NULL;
-	if (create_index_dir(_storage, name) < 0)
-		return NULL;
 
-	index = index_storage_alloc(index_dir, path, DBOX_INDEX_PREFIX);
+	index = index_storage_alloc(_storage, name, flags, DBOX_INDEX_PREFIX);
 
 	pool = pool_alloconly_create("dbox mailbox", 1024+512);
 	mbox = p_new(pool, struct dbox_mailbox, 1);
--- a/src/lib-storage/index/index-storage.c	Sun May 13 21:07:05 2007 +0300
+++ b/src/lib-storage/index/index-storage.c	Sun May 13 21:28:41 2007 +0300
@@ -4,6 +4,7 @@
 #include "array.h"
 #include "buffer.h"
 #include "ioloop.h"
+#include "mkdir-parents.h"
 #include "mail-index-private.h"
 #include "index-storage.h"
 #include "index-mail.h"
@@ -13,6 +14,8 @@
 #include <unistd.h>
 #include <sys/stat.h>
 
+#define CREATE_MODE 0770 /* umask() should limit it more */
+
 #define DEFAULT_CACHE_FIELDS "flags"
 #define DEFAULT_NEVER_CACHE_FIELDS "imap.envelope"
 
@@ -63,20 +66,72 @@
 	i_free(list);
 }
 
+static int create_index_dir(struct mail_storage *storage, const char *name)
+{
+	const char *root_dir, *index_dir;
+
+	root_dir = mailbox_list_get_path(storage->list, name,
+					 MAILBOX_LIST_PATH_TYPE_MAILBOX);
+	index_dir = mailbox_list_get_path(storage->list, name,
+					  MAILBOX_LIST_PATH_TYPE_INDEX);
+	if (strcmp(index_dir, root_dir) == 0 || *index_dir == '\0')
+		return 0;
+
+	if (mkdir_parents(index_dir, CREATE_MODE) < 0 && errno != EEXIST) {
+		mail_storage_set_critical(storage, "mkdir(%s) failed: %m",
+					  index_dir);
+		return -1;
+	}
+
+	return 0;
+}
+
+static const char *
+get_index_dir(struct mail_storage *storage, const char *name,
+	      enum mailbox_open_flags flags, struct stat *st_r)
+{
+	const char *index_dir;
+
+	index_dir = (flags & MAILBOX_OPEN_NO_INDEX_FILES) != 0 ? "" :
+		mailbox_list_get_path(storage->list, name,
+				      MAILBOX_LIST_PATH_TYPE_INDEX);
+	if (*index_dir == '\0') {
+		/* disabled */
+		return NULL;
+	}
+
+	if (stat(index_dir, st_r) < 0) {
+		if (errno == ENOENT) {
+			/* try to create it */
+			if (create_index_dir(storage, name) < 0)
+				return NULL;
+			if (stat(index_dir, st_r) == 0)
+				return index_dir;
+		}
+
+		mail_storage_set_critical(storage, "stat(%s) failed: %m",
+					  index_dir);
+		return NULL;
+	}
+	return index_dir;
+}
+
 struct mail_index *
-index_storage_alloc(const char *index_dir, const char *mailbox_path,
-		    const char *prefix)
+index_storage_alloc(struct mail_storage *storage, const char *name,
+		    enum mailbox_open_flags flags, const char *prefix)
 {
 	struct index_list **list, *rec;
 	struct mail_index *index;
 	struct stat st, st2;
+	const char *index_dir, *mailbox_path;
 	int destroy_count;
 
-	if (*index_dir == '\0' || stat(index_dir, &st) < 0) {
-		if (*index_dir == '\0')
-			index_dir = NULL;
+	mailbox_path = mailbox_list_get_path(storage->list, name,
+					     MAILBOX_LIST_PATH_TYPE_MAILBOX);
+	index_dir = get_index_dir(storage, name, flags, &st);
+
+	if (index_dir == NULL)
 		memset(&st, 0, sizeof(st));
-	}
 
 	/* compare index_dir inodes so we don't break even with symlinks.
 	   for in-memory indexes compare just mailbox paths */
--- a/src/lib-storage/index/index-storage.h	Sun May 13 21:07:05 2007 +0300
+++ b/src/lib-storage/index/index-storage.h	Sun May 13 21:28:41 2007 +0300
@@ -95,8 +95,8 @@
 void index_storage_lock_notify_reset(struct index_mailbox *ibox);
 
 struct mail_index *
-index_storage_alloc(const char *index_dir, const char *mailbox_path,
-		    const char *prefix);
+index_storage_alloc(struct mail_storage *storage, const char *name,
+		    enum mailbox_open_flags flags, const char *prefix);
 void index_storage_unref(struct mail_index *index);
 void index_storage_destroy_unrefed(void);
 
--- a/src/lib-storage/index/maildir/maildir-storage.c	Sun May 13 21:07:05 2007 +0300
+++ b/src/lib-storage/index/maildir/maildir-storage.c	Sun May 13 21:28:41 2007 +0300
@@ -40,8 +40,7 @@
 static MODULE_CONTEXT_DEFINE_INIT(maildir_mailbox_list_module,
 				  &mailbox_list_module_register);
 
-static int verify_inbox(struct mail_storage *storage,
-			enum mailbox_open_flags *flags);
+static int verify_inbox(struct mail_storage *storage);
 static int
 maildir_list_delete_mailbox(struct mailbox_list *list, const char *name);
 static int
@@ -230,7 +229,6 @@
 	enum mail_storage_flags flags = _storage->flags;
 	struct mailbox_list_settings list_set;
 	struct mailbox_list *list;
-	enum mailbox_open_flags open_flags;
 	const char *layout, *error;
 	struct stat st;
 
@@ -287,8 +285,7 @@
 				    "tmp/", storage->temp_prefix, NULL);
 	}
 
-	open_flags = 0;
-	(void)verify_inbox(_storage, &open_flags);
+	(void)verify_inbox(_storage);
 	return 0;
 }
 
@@ -378,34 +375,6 @@
 	return 0;
 }
 
-static int create_index_dir(struct mail_storage *storage, const char *name)
-{
-	const char *index_dir, *root_dir, *dir;
-
-	index_dir = mailbox_list_get_path(storage->list, name,
-					  MAILBOX_LIST_PATH_TYPE_INDEX);
-	if (*index_dir == '\0')
-		return 0;
-
-	root_dir = mailbox_list_get_path(storage->list, name,
-					 MAILBOX_LIST_PATH_TYPE_MAILBOX);
-	if (strcmp(index_dir, root_dir) == 0)
-		return 0;
-
-	dir = t_strdup_printf("%s/%c%s", index_dir,
-			      mailbox_list_get_hierarchy_sep(storage->list),
-			      name);
-	if (mkdir_parents(dir, CREATE_MODE) < 0 && errno != EEXIST) {
-		if (!ENOSPACE(errno)) {
-			mail_storage_set_critical(storage,
-						  "mkdir(%s) failed: %m", dir);
-		}
-		return -1;
-	}
-
-	return 0;
-}
-
 static int create_control_dir(struct mail_storage *storage, const char *name)
 {
 	const char *control_dir, *root_dir, *dir;
@@ -429,8 +398,7 @@
 	return 0;
 }
 
-static int verify_inbox(struct mail_storage *storage,
-			enum mailbox_open_flags *flags)
+static int verify_inbox(struct mail_storage *storage)
 {
 	const char *path;
 
@@ -438,9 +406,6 @@
 				     MAILBOX_LIST_PATH_TYPE_MAILBOX);
 	if (create_maildir(storage, path, TRUE) < 0)
 		return -1;
-
-	if (create_index_dir(storage, "INBOX") < 0)
-		*flags |= MAILBOX_OPEN_NO_INDEX_FILES;
 	if (create_control_dir(storage, "INBOX") < 0)
 		return -1;
 	return 0;
@@ -464,22 +429,17 @@
 {
 	struct maildir_mailbox *mbox;
 	struct mail_index *index;
-	const char *path, *index_dir, *control_dir;
+	const char *path, *control_dir;
 	struct stat st;
 	int shared;
 	pool_t pool;
 
 	path = mailbox_list_get_path(storage->storage.list, name,
 				     MAILBOX_LIST_PATH_TYPE_MAILBOX);
-	index_dir = mailbox_list_get_path(storage->storage.list, name,
-					  MAILBOX_LIST_PATH_TYPE_INDEX);
 	control_dir = mailbox_list_get_path(storage->storage.list, name,
 					    MAILBOX_LIST_PATH_TYPE_CONTROL);
 
-	if ((flags & MAILBOX_OPEN_NO_INDEX_FILES) != 0)
-		index_dir = "";
-
-	index = index_storage_alloc(index_dir, path,
+	index = index_storage_alloc(&storage->storage, name, flags,
 				    MAILDIR_INDEX_PREFIX);
 
 	/* for shared mailboxes get the create mode from the
@@ -548,7 +508,7 @@
 	}
 
 	if (strcmp(name, "INBOX") == 0) {
-		if (verify_inbox(_storage, &flags) < 0)
+		if (verify_inbox(_storage) < 0)
 			return NULL;
 		return maildir_open(storage, "INBOX", flags);
 	}
@@ -561,11 +521,6 @@
 		    create_control_dir(_storage, name) < 0)
 			return NULL;
 
-		if ((flags & MAILBOX_OPEN_NO_INDEX_FILES) == 0) {
-			if (create_index_dir(_storage, name) < 0)
-				flags |= MAILBOX_OPEN_NO_INDEX_FILES;
-		}
-
 		return maildir_open(storage, name, flags);
 	} else if (errno == ENOENT) {
 		mail_storage_set_error(_storage, MAIL_ERROR_NOTFOUND,
--- a/src/lib-storage/index/mbox/mbox-storage.c	Sun May 13 21:07:05 2007 +0300
+++ b/src/lib-storage/index/mbox/mbox-storage.c	Sun May 13 21:28:41 2007 +0300
@@ -444,27 +444,6 @@
 	return 0;
 }
 
-static int create_mbox_index_dirs(struct mail_storage *storage,
-				  const char *name)
-{
-	const char *index_dir;
-
-	index_dir = mailbox_list_get_path(storage->list, name,
-					  MAILBOX_LIST_PATH_TYPE_INDEX);
-	if (*index_dir == '\0')
-		return 0;
-
-	if (mkdir_parents(index_dir, CREATE_MODE) < 0) {
-		if (!ENOSPACE(errno)) {
-			mail_storage_set_critical(storage,
-				"mkdir_parents(%s) failed: %m", index_dir);
-		}
-		return -1;
-	}
-
-	return 0;
-}
-
 static int verify_inbox(struct mail_storage *storage)
 {
 	const char *inbox_path;
@@ -574,23 +553,12 @@
 	struct mail_storage *_storage = &storage->storage;
 	struct mbox_mailbox *mbox;
 	struct mail_index *index;
-	const char *path, *index_dir;
+	const char *path;
 
 	path = mailbox_list_get_path(_storage->list, name,
 				     MAILBOX_LIST_PATH_TYPE_MAILBOX);
-	index_dir = mailbox_list_get_path(_storage->list, name,
-					  MAILBOX_LIST_PATH_TYPE_INDEX);
 
-	if ((flags & MAILBOX_OPEN_NO_INDEX_FILES) != 0)
-		index_dir = "";
-
-	if (*index_dir != '\0') {
-		/* make sure the index directories exist */
-		if (create_mbox_index_dirs(_storage, name) < 0)
-			index_dir = "";
-	}
-
-	index = index_storage_alloc(index_dir, path, MBOX_INDEX_PREFIX);
+	index = index_storage_alloc(_storage, name, flags, MBOX_INDEX_PREFIX);
 	mbox = mbox_alloc_mailbox(storage, index, name, path, flags);
 
 	if (access(path, R_OK|W_OK) < 0) {
@@ -612,24 +580,13 @@
 	struct mail_storage *_storage = &storage->storage;
 	struct mail_index *index;
 	struct mbox_mailbox *mbox;
-	const char *path, *index_dir;
+	const char *path;
 
 	flags |= MAILBOX_OPEN_READONLY;
 
 	path = mailbox_list_get_path(_storage->list, name,
 				     MAILBOX_LIST_PATH_TYPE_MAILBOX);
-	if ((flags & MAILBOX_OPEN_NO_INDEX_FILES) != 0)
-		index_dir = "";
-	else {
-		index_dir = mailbox_list_get_path(_storage->list, name,
-						  MAILBOX_LIST_PATH_TYPE_INDEX);
-
-		/* make sure the required directories are also there */
-		if (create_mbox_index_dirs(_storage, name) < 0)
-			index_dir = "";
-	}
-
-	index = index_storage_alloc(index_dir, path, MBOX_INDEX_PREFIX);
+	index = index_storage_alloc(_storage, name, flags, MBOX_INDEX_PREFIX);
 	mbox = mbox_alloc_mailbox(storage, index, name, path, flags);
 	if (mbox == NULL)
 		return NULL;