changeset 21148:04894c6f4fb2

lib-storage: Add mail_namespace_alloc() This makes it easier to create new namespaces. Based on patch by Jeff Sipek
author Timo Sirainen <timo.sirainen@dovecot.fi>
date Mon, 14 Nov 2016 18:33:37 +0100
parents 7f7aa5fb7175
children 1051201a6936
files src/lib-storage/mail-namespace.c src/lib-storage/mail-namespace.h
diffstat 2 files changed, 53 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib-storage/mail-namespace.c	Thu Nov 10 13:19:05 2016 +0200
+++ b/src/lib-storage/mail-namespace.c	Mon Nov 14 18:33:37 2016 +0100
@@ -82,19 +82,23 @@
 	return FALSE;
 }
 
-int mail_namespaces_init_add(struct mail_user *user,
-			     struct mail_namespace_settings *ns_set,
-			     struct mail_namespace_settings *unexpanded_ns_set,
-			     struct mail_namespace **ns_p, const char **error_r)
+int mail_namespace_alloc(struct mail_user *user,
+			 struct mail_namespace_settings *ns_set,
+			 struct mail_namespace_settings *unexpanded_set,
+			 struct mail_namespace **ns_r,
+			 const char **error_r)
 {
-	const struct mail_storage_settings *mail_set =
-		mail_user_set_get_storage_set(user);
-        struct mail_namespace *ns;
-	const char *driver, *error;
+	struct mail_namespace *ns;
 
 	ns = i_new(struct mail_namespace, 1);
 	ns->refcount = 1;
 	ns->user = user;
+	ns->prefix = i_strdup(ns_set->prefix);
+	ns->set = ns_set;
+	ns->unexpanded_set = unexpanded_set;
+	ns->mail_set = mail_user_set_get_storage_set(user);
+	i_array_init(&ns->all_storages, 2);
+
 	if (strcmp(ns_set->type, "private") == 0) {
 		ns->owner = user;
 		ns->type = MAIL_NAMESPACE_TYPE_PRIVATE;
@@ -128,11 +132,22 @@
 		ns->flags |= NAMESPACE_FLAG_HIDDEN;
 	if (ns_set->subscriptions)
 		ns->flags |= NAMESPACE_FLAG_SUBSCRIPTIONS;
-	if (ns_set == &prefixless_ns_set) {
-		/* autocreated prefix="" namespace */
-		ns->flags |= NAMESPACE_FLAG_UNUSABLE |
-			NAMESPACE_FLAG_AUTOCREATED;
-	}
+
+	*ns_r = ns;
+
+	return 0;
+}
+
+int mail_namespaces_init_add(struct mail_user *user,
+			     struct mail_namespace_settings *ns_set,
+			     struct mail_namespace_settings *unexpanded_ns_set,
+			     struct mail_namespace **ns_p, const char **error_r)
+{
+	const struct mail_storage_settings *mail_set =
+		mail_user_set_get_storage_set(user);
+	struct mail_namespace *ns;
+	const char *driver, *error;
+	int ret;
 
 	if (*ns_set->location == '\0')
 		ns_set->location = mail_set->mail_location;
@@ -149,12 +164,17 @@
 			ns_set->subscriptions ? "yes" : "no", ns_set->location);
 	}
 
-	ns->set = ns_set;
-	ns->unexpanded_set = unexpanded_ns_set;
-	ns->mail_set = mail_set;
-	ns->prefix = i_strdup(ns_set->prefix);
+	if ((ret = mail_namespace_alloc(user, ns_set, unexpanded_ns_set,
+					&ns, error_r)) < 0)
+		return ret;
+
+	if (ns_set == &prefixless_ns_set) {
+		/* autocreated prefix="" namespace */
+		ns->flags |= NAMESPACE_FLAG_UNUSABLE |
+			NAMESPACE_FLAG_AUTOCREATED;
+	}
+
 	ns->special_use_mailboxes = namespace_has_special_use_mailboxes(ns_set);
-	i_array_init(&ns->all_storages, 2);
 
 	if (ns->type == MAIL_NAMESPACE_TYPE_SHARED &&
 	    (strchr(ns->prefix, '%') != NULL ||
--- a/src/lib-storage/mail-namespace.h	Thu Nov 10 13:19:05 2016 +0200
+++ b/src/lib-storage/mail-namespace.h	Mon Nov 14 18:33:37 2016 +0100
@@ -77,15 +77,29 @@
 	unsigned int destroyed:1;
 };
 
+
+/* Allocate a new namespace, and fill it based on the passed in settings.
+   This is the most low-level namespace creation function. The storage isn't
+   initialized for the namespace. */
+int mail_namespace_alloc(struct mail_user *user,
+			 struct mail_namespace_settings *ns_set,
+			 struct mail_namespace_settings *unexpanded_set,
+			 struct mail_namespace **ns_r,
+			 const char **error_r);
+
+/* Add and initialize namespaces to user based on namespace settings. */
 int mail_namespaces_init(struct mail_user *user, const char **error_r);
+/* Add and initialize INBOX namespace to user based on the given location. */
 int mail_namespaces_init_location(struct mail_user *user, const char *location,
 				  const char **error_r) ATTR_NULL(2);
+/* Add an empty namespace to user. */
 struct mail_namespace *mail_namespaces_init_empty(struct mail_user *user);
 /* Deinitialize all namespaces. mail_user_deinit() calls this automatically
    for user's namespaces. */
 void mail_namespaces_deinit(struct mail_namespace **namespaces);
 
-/* Manually initialize namespaces one by one. */
+/* Allocate a new namespace and initialize it. This is called automatically by
+   mail_namespaces_init(). */
 int mail_namespaces_init_add(struct mail_user *user,
 			     struct mail_namespace_settings *ns_set,
 			     struct mail_namespace_settings *unexpanded_ns_set,