From 2bc82f0d7e717f600bcaaa15356cf9bfe26fb633 Mon Sep 17 00:00:00 2001 From: Aki Tuomi Date: Mon, 10 Apr 2017 20:53:29 +0300 Subject: [PATCH] fts: Initialize fts after namespaces have been added This way paths are correctly set, and fts indexes are written to correct place. This affects mbox with lucene. Fixes Panic: file mailbox-list.c: line 1158 (mailbox_list_try_mkdir_root): assertion failed (strncmp(root_dir, path, strlen(root_dir)) == 0) --- src/plugins/fts/fts-plugin.c | 1 + src/plugins/fts/fts-storage.c | 49 +++++++++++++++++++++++++---------- src/plugins/fts/fts-storage.h | 1 + 3 files changed, 38 insertions(+), 13 deletions(-) diff --git a/src/plugins/fts/fts-plugin.c b/src/plugins/fts/fts-plugin.c index 287b0491a7..c7fad64ae3 100644 --- a/src/plugins/fts/fts-plugin.c +++ b/src/plugins/fts/fts-plugin.c @@ -13,6 +13,7 @@ const char *fts_plugin_version = DOVECOT_ABI_VERSION; static struct mail_storage_hooks fts_mail_storage_hooks = { + .mail_namespaces_added = fts_mail_namespaces_added, .mailbox_list_created = fts_mailbox_list_created, .mailbox_allocated = fts_mailbox_allocated, .mail_allocated = fts_mail_allocated diff --git a/src/plugins/fts/fts-storage.c b/src/plugins/fts/fts-storage.c index 545579a110..32a49ade08 100644 --- a/src/plugins/fts/fts-storage.c +++ b/src/plugins/fts/fts-storage.c @@ -33,8 +33,11 @@ struct fts_mailbox_list { union mailbox_list_module_context module_ctx; struct fts_backend *backend; + const char *backend_name; struct fts_backend_update_context *update_ctx; unsigned int update_ctx_refcount; + + bool failed:1; }; struct fts_mailbox { @@ -799,7 +802,7 @@ void fts_mailbox_allocated(struct mailbox *box) struct mailbox_vfuncs *v = box->vlast; struct fts_mailbox *fbox; - if (flist == NULL) + if (flist == NULL || flist->failed) return; fbox = p_new(box->pool, struct fts_mailbox, 1); @@ -831,12 +834,41 @@ static void fts_mailbox_list_deinit(struct mailbox_list *list) flist->module_ctx.super.deinit(list); } +static int +fts_init_namespace(struct fts_mailbox_list *flist, struct mail_namespace *ns, + const char **error_r) +{ + struct fts_backend *backend; + if (fts_backend_init(flist->backend_name, ns, error_r, &backend) < 0) { + flist->failed = TRUE; + return -1; + } + flist->backend = backend; + if ((flist->backend->flags & FTS_BACKEND_FLAG_FUZZY_SEARCH) != 0) + ns->user->fuzzy_search = TRUE; + return 0; +} + +void fts_mail_namespaces_added(struct mail_namespace *ns) +{ + while(ns != NULL) { + struct fts_mailbox_list *flist = FTS_LIST_CONTEXT(ns->list); + const char *error; + + if (flist != NULL && !flist->failed && flist->backend == NULL && + fts_init_namespace(flist, ns, &error) < 0) { + i_error("fts: Failed to initialize backend '%s': %s", + flist->backend_name, error); + } + ns = ns->next; + } +} + void fts_mailbox_list_created(struct mailbox_list *list) { - struct fts_backend *backend; - const char *path, *error; const char *name = mail_user_plugin_getenv(list->ns->user, "fts"); + const char *path; if (name == NULL || name[0] == '\0') { if (list->mail_set->mail_debug) @@ -852,21 +884,12 @@ fts_mailbox_list_created(struct mailbox_list *list) return; } - if (fts_backend_init(name, list->ns, &error, &backend) < 0) { - i_error("fts: Failed to initialize backend '%s': %s", - name, error); - return; - } - struct fts_mailbox_list *flist; struct mailbox_list_vfuncs *v = list->vlast; - if ((backend->flags & FTS_BACKEND_FLAG_FUZZY_SEARCH) != 0) - list->ns->user->fuzzy_search = TRUE; - flist = p_new(list->pool, struct fts_mailbox_list, 1); flist->module_ctx.super = *v; - flist->backend = backend; + flist->backend_name = name; list->vlast = &flist->module_ctx.super; v->deinit = fts_mailbox_list_deinit; MODULE_CONTEXT_SET(list, fts_mailbox_list_module, flist); diff --git a/src/plugins/fts/fts-storage.h b/src/plugins/fts/fts-storage.h index ed79880cc7..3b7f117212 100644 --- a/src/plugins/fts/fts-storage.h +++ b/src/plugins/fts/fts-storage.h @@ -52,6 +52,7 @@ struct fts_backend *fts_mailbox_backend(struct mailbox *box); struct fts_backend *fts_list_backend(struct mailbox_list *list); void fts_mail_allocated(struct mail *mail); +void fts_mail_namespaces_added(struct mail_namespace *ns); void fts_mailbox_allocated(struct mailbox *box); void fts_mailbox_list_created(struct mailbox_list *list); #endif