From: Timo Sirainen Date: Wed, 1 Nov 2023 14:02:23 +0000 (+0200) Subject: lib-storage: Split off mail_namespaces_init_default_location() X-Git-Tag: 2.4.1~1310 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=35f8ed3af3ef729e1cc0c358909c781fa0f7809c;p=thirdparty%2Fdovecot%2Fcore.git lib-storage: Split off mail_namespaces_init_default_location() Now mail_namespaces_init_location() is required to have non-NULL location parameter. --- diff --git a/src/lib-storage/index/shared/shared-storage.c b/src/lib-storage/index/shared/shared-storage.c index d77984ca38..02b1b96395 100644 --- a/src/lib-storage/index/shared/shared-storage.c +++ b/src/lib-storage/index/shared/shared-storage.c @@ -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; diff --git a/src/lib-storage/mail-namespace.c b/src/lib-storage/mail-namespace.c index 08334b224e..536010d957 100644 --- a/src/lib-storage/mail-namespace.c +++ b/src/lib-storage/mail-namespace.c @@ -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; diff --git a/src/lib-storage/mail-namespace.h b/src/lib-storage/mail-namespace.h index d07b7e4226..7dcd9d82eb 100644 --- a/src/lib-storage/mail-namespace.h +++ b/src/lib-storage/mail-namespace.h @@ -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