changeset 8786:db9811ec1f47 HEAD

Added mail_user_get_temp_prefix() and mail_storage_get_temp_prefix().
author Timo Sirainen <tss@iki.fi>
date Thu, 26 Feb 2009 16:43:21 -0500
parents 3d11825abf66
children 5c3406520c3f
files src/lib-storage/index/maildir/maildir-storage.c src/lib-storage/index/mbox/mbox-storage.c src/lib-storage/mail-storage-private.h src/lib-storage/mail-storage.c src/lib-storage/mail-storage.h src/lib-storage/mail-user.c src/lib-storage/mail-user.h
diffstat 7 files changed, 65 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib-storage/index/maildir/maildir-storage.c	Thu Feb 26 16:42:00 2009 -0500
+++ b/src/lib-storage/index/maildir/maildir-storage.c	Thu Feb 26 16:43:21 2009 -0500
@@ -191,7 +191,7 @@
 	enum mail_storage_flags flags = _storage->flags;
 	struct mailbox_list_settings list_set;
 	struct mailbox_list *list;
-	const char *layout;
+	const char *layout, *dir;
 	struct stat st;
 
 	if (maildir_get_list_settings(&list_set, data, _storage, &layout,
@@ -258,12 +258,20 @@
 	storage->stat_dirs = getenv("MAILDIR_STAT_DIRS") != NULL;
 
 	storage->temp_prefix = mailbox_list_get_temp_prefix(list);
-	if (list_set.control_dir == NULL) {
+	if (list_set.control_dir == NULL &&
+	    (_storage->ns->flags & NAMESPACE_FLAG_INBOX) != 0) {
 		/* put the temp files into tmp/ directory preferrably */
-		storage->temp_prefix =
-			p_strconcat(_storage->pool,
-				    "tmp/", storage->temp_prefix, NULL);
+		storage->temp_prefix = p_strconcat(_storage->pool, "tmp/",
+						   storage->temp_prefix, NULL);
+		dir = mailbox_list_get_path(list, NULL,
+					    MAILBOX_LIST_PATH_TYPE_DIR);
+	} else {
+		/* control dir should also be writable */
+		dir = mailbox_list_get_path(list, NULL,
+					    MAILBOX_LIST_PATH_TYPE_CONTROL);
 	}
+	_storage->temp_path_prefix = p_strconcat(_storage->pool, dir, "/",
+						 storage->temp_prefix, NULL);
 	return 0;
 }
 
--- a/src/lib-storage/index/mbox/mbox-storage.c	Thu Feb 26 16:42:00 2009 -0500
+++ b/src/lib-storage/index/mbox/mbox-storage.c	Thu Feb 26 16:43:21 2009 -0500
@@ -386,6 +386,9 @@
 	path = storage->list_module_ctx.super.get_path(list, name, type);
 	if (type == MAILBOX_LIST_PATH_TYPE_CONTROL ||
 	    type == MAILBOX_LIST_PATH_TYPE_INDEX) {
+		if (name == NULL)
+			return t_strconcat(path, "/"MBOX_INDEX_DIR_NAME, NULL);
+
 		p = strrchr(path, '/');
 		if (p == NULL)
 			return "";
@@ -442,7 +445,7 @@
 {
 	struct mbox_storage *storage = (struct mbox_storage *)_storage;
 	struct mailbox_list_settings list_set;
-	const char *layout;
+	const char *layout, *dir;
 
 	if (mbox_get_list_settings(&list_set, data, _storage,
 				   &layout, error_r) < 0)
@@ -470,6 +473,16 @@
 	mailbox_list_init(_storage->list, _storage->ns, &list_set,
 			  mail_storage_get_list_flags(_storage->flags) |
 			  MAILBOX_LIST_FLAG_MAILBOX_FILES);
+
+	dir = mailbox_list_get_path(_storage->list, NULL,
+				    MAILBOX_LIST_PATH_TYPE_INDEX);
+	if (*dir == '\0') {
+		/* no index directory. just fallback to writing to root. */
+		dir = mailbox_list_get_path(_storage->list, NULL,
+					    MAILBOX_LIST_PATH_TYPE_DIR);
+	}
+	_storage->temp_path_prefix = p_strconcat(_storage->pool, dir, "/",
+		mailbox_list_get_temp_prefix(_storage->list), NULL);
 	return 0;
 }
 
--- a/src/lib-storage/mail-storage-private.h	Thu Feb 26 16:42:00 2009 -0500
+++ b/src/lib-storage/mail-storage-private.h	Thu Feb 26 16:43:21 2009 -0500
@@ -61,6 +61,7 @@
         const struct mail_storage *storage_class;
 	struct mail_namespace *ns;
 	struct mailbox_list *list;
+	const char *temp_path_prefix;
 
 	enum mail_storage_flags flags;
 	enum file_lock_method lock_method;
--- a/src/lib-storage/mail-storage.c	Thu Feb 26 16:42:00 2009 -0500
+++ b/src/lib-storage/mail-storage.c	Thu Feb 26 16:43:21 2009 -0500
@@ -237,7 +237,7 @@
 	value = getenv("MAIL_MAX_KEYWORD_LENGTH");
 	storage->keyword_max_len = value != NULL ?
 		atoi(value) : DEFAULT_MAX_KEYWORD_LENGTH;
-	
+
 	if (hook_mail_storage_created != NULL) {
 		T_BEGIN {
 			hook_mail_storage_created(storage);
@@ -404,6 +404,20 @@
 				     MAILBOX_LIST_PATH_TYPE_INDEX);
 }
 
+const char *mail_storage_get_temp_prefix(struct mail_storage *storage)
+{
+	const char *dir;
+
+	if (storage->temp_path_prefix == NULL) {
+		dir = mailbox_list_get_path(storage->list, NULL,
+					    MAILBOX_LIST_PATH_TYPE_DIR);
+		storage->temp_path_prefix = p_strconcat(storage->pool, dir, "/",
+			mailbox_list_get_temp_prefix(storage->list), NULL);
+	}
+
+	return storage->temp_path_prefix;
+}
+
 enum mailbox_list_flags
 mail_storage_get_list_flags(enum mail_storage_flags storage_flags)
 {
--- a/src/lib-storage/mail-storage.h	Thu Feb 26 16:42:00 2009 -0500
+++ b/src/lib-storage/mail-storage.h	Thu Feb 26 16:43:21 2009 -0500
@@ -307,6 +307,8 @@
    in-memory indexes or mailbox doesn't exist. */
 const char *mail_storage_get_mailbox_index_dir(struct mail_storage *storage,
 					       const char *name);
+/* Returns path + file prefix for creating a temporary file. */
+const char *mail_storage_get_temp_prefix(struct mail_storage *storage);
 
 /* Open a mailbox. If input stream is given, mailbox is opened read-only
    using it as a backend. If storage doesn't support stream backends and its
--- a/src/lib-storage/mail-user.c	Thu Feb 26 16:42:00 2009 -0500
+++ b/src/lib-storage/mail-user.c	Thu Feb 26 16:43:21 2009 -0500
@@ -2,8 +2,10 @@
 
 #include "lib.h"
 #include "array.h"
+#include "hostpid.h"
 #include "auth-master.h"
 #include "mail-namespace.h"
+#include "mail-storage.h"
 #include "mail-user.h"
 
 #include <stdlib.h>
@@ -157,6 +159,21 @@
 	return 0;
 }
 
+const char *mail_user_get_temp_prefix(struct mail_user *user)
+{
+	struct mail_namespace *ns;
+
+	if (user->_home != NULL) {
+		return t_strconcat(user->_home, "/.temp.", my_hostname, ".",
+				   my_pid, ".", NULL);
+	}
+
+	ns = mail_namespace_find_inbox(user->namespaces);
+	if (ns == NULL)
+		ns = user->namespaces;
+	return mail_storage_get_temp_prefix(ns->storage);
+}
+
 void mail_users_init(const char *auth_socket_path, bool debug)
 {
 	const char *base_dir;
--- a/src/lib-storage/mail-user.h	Thu Feb 26 16:42:00 2009 -0500
+++ b/src/lib-storage/mail-user.h	Thu Feb 26 16:43:21 2009 -0500
@@ -59,6 +59,9 @@
    successfully, 0 if there is no home directory (either user doesn't exist or
    has no home directory) or -1 if lookup failed. */
 int mail_user_get_home(struct mail_user *user, const char **home_r);
+/* Returns path + file prefix for creating a temporary file. Uses home
+   directory if possible, fallbacks to mail directory. */
+const char *mail_user_get_temp_prefix(struct mail_user *user);
 
 /* Add more namespaces to user's namespaces. The ->next pointers may be
    changed, so the namespaces pointer will be updated to user->namespaces. */