]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-storage: Split off mail_namespaces_init_default_location()
authorTimo Sirainen <timo.sirainen@open-xchange.com>
Wed, 1 Nov 2023 14:02:23 +0000 (16:02 +0200)
committerAki Tuomi <aki.tuomi@open-xchange.com>
Wed, 12 Feb 2025 10:34:10 +0000 (12:34 +0200)
Now mail_namespaces_init_location() is required to have non-NULL location
parameter.

src/lib-storage/index/shared/shared-storage.c
src/lib-storage/mail-namespace.c
src/lib-storage/mail-namespace.h

index d77984ca384c4f42a69f3e0f801a38c4293faed9..02b1b963950fd036414624520e4f4d89d9b47c39 100644 (file)
@@ -379,7 +379,9 @@ shared_mail_user_init(struct mail_storage *_storage,
 
        /* We need to create a prefix="" namespace for the owner */
        if (mail_namespaces_init_location(owner, str_c(location), &error) < 0) {
-               e_error(ns->user->event, "shared: %s: %s", new_ns->prefix, error);
+               e_error(ns->user->event,
+                       "Failed to create shared namespace %s: %s",
+                       new_ns->prefix, error);
                /* owner gets freed by namespace deinit */
                mail_namespace_destroy(new_ns);
                return -1;
index 08334b224e608c8550148933992d0cc9ba9724e5..536010d957f2965f6cc79932dd82f8723dd04b5e 100644 (file)
@@ -28,6 +28,10 @@ static struct mail_namespace_settings prefixless_ns_set = {
        .disabled = FALSE,
 };
 
+static int
+mail_namespaces_init_default_location(struct mail_user *user,
+                                     const char **error_r);
+
 void mail_namespace_add_storage(struct mail_namespace *ns,
                                struct mail_storage *storage)
 {
@@ -473,49 +477,27 @@ int mail_namespaces_init(struct mail_user *user, const char **error_r)
 
        if (namespaces == NULL) {
                /* no namespaces defined, create a default one */
-               return mail_namespaces_init_location(user, NULL, error_r);
+               return mail_namespaces_init_default_location(user, error_r);
        }
        return mail_namespaces_init_finish(namespaces, error_r);
 }
 
-int mail_namespaces_init_location(struct mail_user *user, const char *location,
-                                 const char **error_r)
+static int
+mail_namespaces_init_location_full(struct mail_user *user, const char *location,
+                                  bool default_location, const char **error_r)
 {
        struct mail_namespace_settings *inbox_set;
        struct mail_namespace *ns;
-       const struct mail_storage_settings *mail_set;
-       const char *error, *value, *location_source;
-       bool default_location = FALSE;
        int ret;
 
-       i_assert(location == NULL || *location != '\0');
-
        inbox_set = p_new(user->pool, struct mail_namespace_settings, 1);
        *inbox_set = mail_namespace_default_settings;
        inbox_set->inbox = TRUE;
        /* enums must be changed */
        inbox_set->type = "private";
        inbox_set->list = "yes";
+       inbox_set->location = p_strdup(user->pool, location);
 
-       mail_set = mail_user_set_get_storage_set(user);
-       if (location != NULL) {
-               inbox_set->location = p_strdup(user->pool, location);
-               location_source = "mail_location parameter";
-       } else if (*mail_set->mail_location != '\0') {
-               location_source = "mail_location setting";
-               inbox_set->location = mail_set->mail_location;
-               default_location = TRUE;
-       } else if ((value = getenv("MAIL")) != NULL) {
-               location_source = "environment MAIL";
-               inbox_set->location = value;
-       } else if ((value = getenv("MAILDIR")) != NULL) {
-               inbox_set->location =
-                       p_strdup_printf(user->pool, "maildir:%s", value);
-               location_source = "environment MAILDIR";
-       } else {
-               inbox_set->location = "";
-               location_source = "autodetection";
-       }
        if (default_location) {
                /* treat this the same as if a namespace was created with
                   default settings. dsync relies on finding a namespace
@@ -529,21 +511,57 @@ int mail_namespaces_init_location(struct mail_user *user, const char *location,
        if ((ret = mail_namespace_alloc(user, inbox_set, &ns, error_r)) < 0)
                return ret;
 
-       if (mail_storage_create(ns, NULL, 0, &error) < 0) {
-               if (*inbox_set->location != '\0') {
-                       *error_r = t_strdup_printf(
-                               "Initializing mail storage from %s "
-                               "failed: %s", location_source, error);
-               } else {
-                       *error_r = t_strdup_printf("mail_location not set and "
-                                       "autodetection failed: %s", error);
-               }
+       if (mail_storage_create(ns, NULL, 0, error_r) < 0) {
                mail_namespace_free(ns);
                return -1;
        }
        return mail_namespaces_init_finish(ns, error_r);
 }
 
+static int
+mail_namespaces_init_default_location(struct mail_user *user,
+                                     const char **error_r)
+{
+       const struct mail_storage_settings *mail_set;
+       const char *location, *location_source, *error;
+       bool default_location = FALSE;
+
+       mail_set = mail_user_set_get_storage_set(user);
+       if (*mail_set->mail_location != '\0') {
+               location_source = "mail_location setting";
+               location = mail_set->mail_location;
+               default_location = TRUE;
+       } else if ((location = getenv("MAIL")) != NULL) {
+               location_source = "environment MAIL";
+       } else if ((location = getenv("MAILDIR")) != NULL) {
+               location = p_strdup_printf(user->pool, "maildir:%s", location);
+               location_source = "environment MAILDIR";
+       } else {
+               location = "";
+               location_source = "autodetection";
+       }
+       if (mail_namespaces_init_location_full(user, location,
+                                              default_location, &error) == 0)
+               return 0;
+       else if (location[0] != '\0') {
+               *error_r = t_strdup_printf(
+                       "Initializing mail storage from %s failed: %s",
+                       location_source, error);
+               return -1;
+       } else {
+               *error_r = t_strdup_printf("mail_location not set and "
+                                          "autodetection failed: %s", error);
+               return -1;
+       }
+}
+
+int mail_namespaces_init_location(struct mail_user *user, const char *location,
+                                 const char **error_r)
+{
+       return mail_namespaces_init_location_full(user, location,
+                                                 FALSE, error_r);
+}
+
 struct mail_namespace *mail_namespaces_init_empty(struct mail_user *user)
 {
        struct mail_namespace *ns;
index d07b7e4226e2a60d043d3d6c7d7e5a892b19ac73..7dcd9d82eb69a8775fce0de34e69036eda770723 100644 (file)
@@ -94,7 +94,7 @@ int mail_namespace_alloc(struct mail_user *user,
 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);
+                                 const char **error_r);
 /* 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