mail-storage.c revision d81bbb6948b4d758e87206acb8da4cc4366fd478
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen/* Copyright (c) 2002-2015 Dovecot authors, see the included COPYING file */
e4d34f2fbee451219599d71505594df704093ce3Timo Sirainenextern struct mail_search_register *mail_search_register_imap;
e4d34f2fbee451219599d71505594df704093ce3Timo Sirainenextern struct mail_search_register *mail_search_register_human;
9f431ccfb6932746db56245c8a3d3415717ef545Timo Sirainenstruct mail_storage_module_register mail_storage_module_register = { 0 };
b321df9603081896b70ec44635af96d674a9839aTimo Sirainenstruct mail_module_register mail_module_register = { 0 };
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainenstruct mail_storage_mail_index_module mail_storage_mail_index_module =
c02a056b724abd6578fb8c4e439de0e94eaea6feTimo Sirainen MODULE_CONTEXT_INIT(&mail_index_module_register);
2cfe9983ce7a6280636ee12beccc2e865111967bTimo Sirainen mail_search_register_deinit(&mail_search_register_human);
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen mail_search_register_deinit(&mail_search_register_imap);
f968e62caa52a8924bd05ebf76ff515b5c18e17bTimo Sirainenvoid mail_storage_class_register(struct mail_storage *storage_class)
f968e62caa52a8924bd05ebf76ff515b5c18e17bTimo Sirainen i_assert(mail_storage_find_class(storage_class->name) == NULL);
f968e62caa52a8924bd05ebf76ff515b5c18e17bTimo Sirainen /* append it after the list, so the autodetection order is correct */
f968e62caa52a8924bd05ebf76ff515b5c18e17bTimo Sirainen array_append(&mail_storage_classes, &storage_class, 1);
f968e62caa52a8924bd05ebf76ff515b5c18e17bTimo Sirainenvoid mail_storage_class_unregister(struct mail_storage *storage_class)
f968e62caa52a8924bd05ebf76ff515b5c18e17bTimo Sirainen unsigned int i, count;
f968e62caa52a8924bd05ebf76ff515b5c18e17bTimo Sirainen classes = array_get(&mail_storage_classes, &count);
f968e62caa52a8924bd05ebf76ff515b5c18e17bTimo Sirainen for (i = 0; i < count; i++) {
f968e62caa52a8924bd05ebf76ff515b5c18e17bTimo Sirainenstruct mail_storage *mail_storage_find_class(const char *name)
1e7252421b9c85b898fef7e75bd6422ef1f046e4Timo Sirainen unsigned int i, count;
f968e62caa52a8924bd05ebf76ff515b5c18e17bTimo Sirainen classes = array_get(&mail_storage_classes, &count);
17e09bf093bf968f383a90c559399656dffafe73Timo Sirainen for (i = 0; i < count; i++) {
f968e62caa52a8924bd05ebf76ff515b5c18e17bTimo Sirainenmail_storage_autodetect(const struct mail_namespace *ns,
f968e62caa52a8924bd05ebf76ff515b5c18e17bTimo Sirainen unsigned int i, count;
f968e62caa52a8924bd05ebf76ff515b5c18e17bTimo Sirainen classes = array_get(&mail_storage_classes, &count);
f968e62caa52a8924bd05ebf76ff515b5c18e17bTimo Sirainen for (i = 0; i < count; i++) {
f968e62caa52a8924bd05ebf76ff515b5c18e17bTimo Sirainenmail_storage_set_autodetection(const char **data, const char **driver)
f968e62caa52a8924bd05ebf76ff515b5c18e17bTimo Sirainen const char *p;
f968e62caa52a8924bd05ebf76ff515b5c18e17bTimo Sirainen /* check if data is in driver:data format (eg. mbox:~/mail) */
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen /* no autodetection if the storage driver is given. */
3c9783956dea385b322cd7fa6bf8c98c17a907a0Timo Sirainenmail_storage_get_class(struct mail_namespace *ns, const char *driver,
3c9783956dea385b322cd7fa6bf8c98c17a907a0Timo Sirainen enum mail_storage_flags flags, const char **error_r)
b321df9603081896b70ec44635af96d674a9839aTimo Sirainen /* no mail_location, autodetect */
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen /* explicit autodetection with "auto" driver. */
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen /* handle the same as with driver=NULL */
49e513d090753ccbf95560b2f3a21f081a5b6c51Timo Sirainen storage_class = mail_user_get_storage_class(ns->user, driver);
e4d34f2fbee451219599d71505594df704093ce3Timo Sirainen if (list_set->root_dir == NULL || *list_set->root_dir == '\0') {
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen /* no root directory given. is this allowed? */
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen (flags & MAIL_STORAGE_FLAG_NO_AUTODETECTION) == 0) {
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen /* autodetection should take care of this */
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen (storage_class->class_flags & MAIL_STORAGE_CLASS_FLAG_NO_ROOT) != 0) {
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen /* root not required for this storage */
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen (list->props & MAILBOX_LIST_PROP_NO_ROOT) != 0) {
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen /* root not required for this layout */
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen storage_class->v.get_list_settings(ns, list_set);
35136dd2baf8dc30e4e754294ed81ff48e8c1e64Timo Sirainen storage_class = mail_storage_autodetect(ns, list_set);
35136dd2baf8dc30e4e754294ed81ff48e8c1e64Timo Sirainen if (home == NULL || *home == '\0') home = "(not set)";
484e12acec34f16e5a8adc001e23ae48f1dda8c7Timo Sirainen if (ns->set->location == NULL || *ns->set->location == '\0') {
35136dd2baf8dc30e4e754294ed81ff48e8c1e64Timo Sirainen "Mail storage autodetection failed with home=%s", home);
35136dd2baf8dc30e4e754294ed81ff48e8c1e64Timo Sirainen } else if (strncmp(ns->set->location, "auto:", 5) == 0) {
35136dd2baf8dc30e4e754294ed81ff48e8c1e64Timo Sirainen "Autodetection failed for %s (home=%s)",
35136dd2baf8dc30e4e754294ed81ff48e8c1e64Timo Sirainen "Ambiguous mail location setting, "
35136dd2baf8dc30e4e754294ed81ff48e8c1e64Timo Sirainen "don't know what to do with it: %s "
35136dd2baf8dc30e4e754294ed81ff48e8c1e64Timo Sirainen "(try prefixing it with mbox: or maildir:)",
35136dd2baf8dc30e4e754294ed81ff48e8c1e64Timo Sirainenmail_storage_verify_root(const char *root_dir, bool autocreate,
35136dd2baf8dc30e4e754294ed81ff48e8c1e64Timo Sirainen const char **error_r)
35136dd2baf8dc30e4e754294ed81ff48e8c1e64Timo Sirainen *error_r = mail_error_eacces_msg("stat", root_dir);
35136dd2baf8dc30e4e754294ed81ff48e8c1e64Timo Sirainen } else if (errno != ENOENT && errno != ENOTDIR) {
35136dd2baf8dc30e4e754294ed81ff48e8c1e64Timo Sirainen *error_r = t_strdup_printf("stat(%s) failed: %m", root_dir);
35136dd2baf8dc30e4e754294ed81ff48e8c1e64Timo Sirainen } else if (!autocreate) {
35136dd2baf8dc30e4e754294ed81ff48e8c1e64Timo Sirainen "Root mail directory doesn't exist: %s", root_dir);
b321df9603081896b70ec44635af96d674a9839aTimo Sirainen /* doesn't exist */
b321df9603081896b70ec44635af96d674a9839aTimo Sirainenmail_storage_create_root(struct mailbox_list *list,
b321df9603081896b70ec44635af96d674a9839aTimo Sirainen enum mail_storage_flags flags, const char **error_r)
b321df9603081896b70ec44635af96d674a9839aTimo Sirainen if (!mailbox_list_get_root_path(list, MAILBOX_LIST_PATH_TYPE_MAILBOX,
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen /* storage doesn't use directories (e.g. shared root) */
b321df9603081896b70ec44635af96d674a9839aTimo Sirainen if ((flags & MAIL_STORAGE_FLAG_NO_AUTOVERIFY) != 0) {
b321df9603081896b70ec44635af96d674a9839aTimo Sirainen /* we don't need to verify, but since debugging is
b321df9603081896b70ec44635af96d674a9839aTimo Sirainen enabled, check and log if the root doesn't exist */
f8a86fdfb0048f9c87bf223373b35416ceb5856bTimo Sirainen if (mail_storage_verify_root(root_dir, FALSE, &error) < 0) {
b321df9603081896b70ec44635af96d674a9839aTimo Sirainen i_debug("Namespace %s: Creating storage despite: %s",
b321df9603081896b70ec44635af96d674a9839aTimo Sirainen autocreate = (flags & MAIL_STORAGE_FLAG_NO_AUTOCREATE) == 0;
b321df9603081896b70ec44635af96d674a9839aTimo Sirainen ret = mail_storage_verify_root(root_dir, autocreate, error_r);
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen ret = mailbox_list_try_mkdir_root(list, root_dir,
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainenmail_storage_match_class(struct mail_storage *storage,
35136dd2baf8dc30e4e754294ed81ff48e8c1e64Timo Sirainen if (strcmp(storage->name, storage_class->name) != 0)
6c2c5f20760b06bfb4a40b0ee2ef5ab016bc41f0Timo Sirainen if ((storage->class_flags & MAIL_STORAGE_CLASS_FLAG_UNIQUE_ROOT) != 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, ...)
const char *str;
const char *str;
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;
const char *errstr;
MAILBOX_SET_AUTO_SUBSCRIBE) == 0) {
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)
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;
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 (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 TRUE;
struct mail_namespace *
return FALSE;
i_unreached();
const char **str_r)
const unsigned char *data;
int ret;
return ret;
int ret;
return ret;
struct mailbox_attribute_iter *
const char *prefix)
struct mailbox_sync_context *
T_BEGIN {
} T_END;
return ctx;
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 *
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;
const char **path_r)
int ret;
if (ret < 0) {
return ret;
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;
unsigned int secs;
const char *error;