changeset 21950:86b0d2fb44d3

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)
author Aki Tuomi <aki.tuomi@dovecot.fi>
date Mon, 10 Apr 2017 20:53:29 +0300
parents 669e17487d5d
children 3ef988b95d96
files src/plugins/fts/fts-plugin.c src/plugins/fts/fts-storage.c src/plugins/fts/fts-storage.h
diffstat 3 files changed, 38 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/src/plugins/fts/fts-plugin.c	Tue Apr 11 15:05:53 2017 +0300
+++ b/src/plugins/fts/fts-plugin.c	Mon Apr 10 20:53:29 2017 +0300
@@ -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
--- a/src/plugins/fts/fts-storage.c	Tue Apr 11 15:05:53 2017 +0300
+++ b/src/plugins/fts/fts-storage.c	Mon Apr 10 20:53:29 2017 +0300
@@ -33,8 +33,11 @@
 	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 @@
 	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 @@
 	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 @@
 		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);
--- a/src/plugins/fts/fts-storage.h	Tue Apr 11 15:05:53 2017 +0300
+++ b/src/plugins/fts/fts-storage.h	Mon Apr 10 20:53:29 2017 +0300
@@ -52,6 +52,7 @@
 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