mail-storage.c revision a0169b23c31e3f2b6196685a7cb8cd69c6490f43
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen/* Copyright (c) 2002-2018 Dovecot authors, see the included COPYING file */
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainenextern struct mail_search_register *mail_search_register_imap;
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainenextern struct mail_search_register *mail_search_register_human;
4ba3717a04823875c2a1d60ff9dc8177ae033d12Timo Sirainenstruct event_category event_category_storage = {
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainenstruct event_category event_category_mailbox = {
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainenstruct mail_storage_module_register mail_storage_module_register = { 0 };
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainenstruct mail_module_register mail_module_register = { 0 };
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainenstruct mail_storage_mail_index_module mail_storage_mail_index_module =
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen MODULE_CONTEXT_INIT(&mail_index_module_register);
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen mail_search_register_deinit(&mail_search_register_human);
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen mail_search_register_deinit(&mail_search_register_imap);
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainenvoid mail_storage_class_register(struct mail_storage *storage_class)
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen i_assert(mail_storage_find_class(storage_class->name) == NULL);
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen /* append it after the list, so the autodetection order is correct */
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen array_append(&mail_storage_classes, &storage_class, 1);
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainenvoid mail_storage_class_unregister(struct mail_storage *storage_class)
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen unsigned int i, count;
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen classes = array_get(&mail_storage_classes, &count);
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen for (i = 0; i < count; i++) {
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainenstruct mail_storage *mail_storage_find_class(const char *name)
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila unsigned int i, count;
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila classes = array_get(&mail_storage_classes, &count);
957b0b4c9aeff7153bb9ebf91d8aea550bd07865Teemu Huovila for (i = 0; i < count; i++) {
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovilamail_storage_autodetect(const struct mail_namespace *ns,
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila unsigned int i, count;
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila classes = array_get(&mail_storage_classes, &count);
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila for (i = 0; i < count; i++) {
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovilamail_storage_set_autodetection(const char **data, const char **driver)
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila const char *p;
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila /* check if data is in driver:data format (eg. mbox:~/mail) */
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila /* no autodetection if the storage driver is given. */
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovilamail_storage_get_class(struct mail_namespace *ns, const char *driver,
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila enum mail_storage_flags flags, const char **error_r)
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila /* no mail_location, autodetect */
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila /* explicit autodetection with "auto" driver. */
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila /* handle the same as with driver=NULL */
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila storage_class = mail_user_get_storage_class(ns->user, driver);
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila if (list_set->root_dir == NULL || *list_set->root_dir == '\0') {
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila /* no root directory given. is this allowed? */
09aed882b99e865ff6d7140ae2f77a42c9e7d1a7Teemu Huovila (flags & MAIL_STORAGE_FLAG_NO_AUTODETECTION) == 0) {
09aed882b99e865ff6d7140ae2f77a42c9e7d1a7Teemu Huovila /* autodetection should take care of this */
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen (storage_class->class_flags & MAIL_STORAGE_CLASS_FLAG_NO_ROOT) != 0) {
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen /* root not required for this storage */
09aed882b99e865ff6d7140ae2f77a42c9e7d1a7Teemu Huovila (list->props & MAILBOX_LIST_PROP_NO_ROOT) != 0) {
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen /* root not required for this layout */
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila storage_class->v.get_list_settings(ns, list_set);
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila storage_class = mail_storage_autodetect(ns, list_set);
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila if (home == NULL || *home == '\0') home = "(not set)";
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila if (ns->set->location == NULL || *ns->set->location == '\0') {
09aed882b99e865ff6d7140ae2f77a42c9e7d1a7Teemu Huovila "Mail storage autodetection failed with home=%s", home);
09aed882b99e865ff6d7140ae2f77a42c9e7d1a7Teemu Huovila } else if (strncmp(ns->set->location, "auto:", 5) == 0) {
09aed882b99e865ff6d7140ae2f77a42c9e7d1a7Teemu Huovila "Autodetection failed for %s (home=%s)",
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen "Ambiguous mail location setting, "
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen "don't know what to do with it: %s "
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen "(try prefixing it with mbox: or maildir:)",
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainenmail_storage_verify_root(const char *root_dir, const char *dir_type,
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen const char **error_r)
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen "Root mail directory is a file: %s", root_dir);
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen *error_r = mail_error_eacces_msg("stat", root_dir);
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen *error_r = t_strdup_printf("stat(%s) failed: %m", root_dir);
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen "Root %s directory doesn't exist: %s", dir_type, root_dir);
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainenmail_storage_create_root(struct mailbox_list *list,
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen enum mail_storage_flags flags, const char **error_r)
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen if (!mailbox_list_get_root_path(list, type, &root_dir)) {
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen /* storage doesn't use directories (e.g. shared root) */
4ef1f9f3293965734e6e3c38c191ceb2246a721fTeemu Huovila if ((flags & MAIL_STORAGE_FLAG_NO_AUTOVERIFY) != 0) {
eeb03434472130f9631f164440566fd8d09e6380Timo Sirainen /* we don't need to verify, but since debugging is
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen enabled, check and log if the root doesn't exist */
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen if (mail_storage_verify_root(root_dir, type_name, &error) < 0) {
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen i_debug("Namespace %s: Creating storage despite: %s",
4ba3717a04823875c2a1d60ff9dc8177ae033d12Timo Sirainen if ((flags & MAIL_STORAGE_FLAG_NO_AUTOCREATE) == 0) {
1537d20b852cbbf0d6971790b84e0cce5ca61307Timo Sirainen /* If the directories don't exist, we'll just autocreate them
09aed882b99e865ff6d7140ae2f77a42c9e7d1a7Teemu Huovila return mail_storage_verify_root(root_dir, type_name, error_r);
4ba3717a04823875c2a1d60ff9dc8177ae033d12Timo Sirainenmail_storage_match_class(struct mail_storage *storage,
eeb03434472130f9631f164440566fd8d09e6380Timo Sirainen if (strcmp(storage->name, storage_class->name) != 0)
afbd17685101cc2bbd0614e85558bb167ab15096Timo Sirainen if ((storage->class_flags & MAIL_STORAGE_CLASS_FLAG_UNIQUE_ROOT) != 0 &&
eeb03434472130f9631f164440566fd8d09e6380Timo Sirainen (set->root_dir != NULL ? set->root_dir : "")) != 0)
return FALSE;
return FALSE;
return TRUE;
static struct mail_storage *
return storage;
return NULL;
const char **error_r)
error_r) < 0)
error_r);
T_BEGIN {
} T_END;
const char *str;
const char *fmt, ...)
T_BEGIN {
} T_END;
T_BEGIN {
} T_END;
error_r);
const char *str;
const char *str;
const char *index_error;
const struct mail_storage_settings *
void *context)
return error;
const char *error_string;
return FALSE;
return FALSE;
return TRUE;
const struct mailbox_settings *
return NULL;
return *box_set;
return NULL;
T_BEGIN {
if (open_error != 0)
} T_END;
return box;
const char *vname;
&metadata) < 0) {
return box;
return box;
return TRUE;
return TRUE;
return TRUE;
const char *errstr;
errstr);
int ret;
return ret;
const char **error_r)
if (prev_sep) {
return FALSE;
if (prev_sep && i > 0) {
return FALSE;
return TRUE;
vname++;
const char *path;
&path) < 0)
return TRUE;
return FALSE;
const char *name;
name++;
const char *name)
NAMESPACE_FLAG_LIST_CHILDREN)) == 0)
return TRUE;
return FALSE;
case MAIL_ERROR_NOTFOUND:
int ret;
case MAIL_ERROR_NOTFOUND:
MAIL_STORAGE_CLASS_FLAG_OPEN_STREAMS) == 0) {
T_BEGIN {
} T_END;
} T_END;
if (ret < 0) {
return FALSE;
return FALSE;
return FALSE;
if (ret < 0)
return FALSE;
return TRUE;
const char *index_dir;
int ret;
&index_dir);
if (ret <= 0)
int ret;
return ret;
const char *name1;
return FALSE;
return TRUE;
bool directory)
int ret;
if (ret == 0) {
return ret;
int ret;
return ret;
int ret;
if (ret < 0)
if (del)
if (del) {
if (ret < 0)
int ret;
return ret;
int ret;
return ret;
const char **error_r)
return TRUE;
return FALSE;
return FALSE;
return TRUE;
const char **error_r)
return FALSE;
return FALSE;
return FALSE;
return FALSE;
return TRUE;
int ret = 0;
return ret;
struct mail_namespace *
return FALSE;
i_unreached();
struct mailbox_sync_context *
return ctx;
T_BEGIN {
} T_END;
return ctx;
return FALSE;
const char *errormsg;
int ret;
if (ret == 0)
return ret;
struct mail_search_context *
int ret;
return ret;
bool tryagain;
if (!tryagain)
return FALSE;
return TRUE;
return FALSE;
return TRUE;
int ret;
if (ret < 0)
return ret;
struct mailbox_transaction_context *
const char *reason)
return trans;
int ret;
return ret;
int ret;
T_BEGIN {
} T_END;
return ret;
struct mailbox *
return t->box;
struct mail_save_context *
T_BEGIN {
} T_END;
return ctx;
const char *const *keywords_list;
const char *envelope)
unsigned int order)
int ret;
} else T_BEGIN {
} T_END;
if (ret < 0) {
int ret;
T_BEGIN {
} T_END;
return ret;
int ret;
T_BEGIN {
} T_END;
if (pvt_flags != 0)
t->save_count++;
return ret;
T_BEGIN {
} T_END;
struct mailbox_transaction_context *
int ret;
T_BEGIN {
} T_END;
if (ret == 0) {
if (pvt_flags != 0)
t->save_count++;
return ret;
int ret;
return ret;
int ret;
if (ret < 0) {
return ret;
const char **path_r)
int *fd_r)
int fd;
const char *root_dir;
int ret;
return ret;
&mail_dir) < 0)
unsigned int secs)
#ifndef MMAP_CONFLICTS_WRITE
return index_flags;
bool *utc_r)
unsigned int secs;
const char *error;
T_BEGIN {
} T_END;
if (ret <= 0) {
return ret;
bool created;
const char **error_r)
const char *lock_path;