[PATCH 2/5] support splitting mail from database location.

Subject: [PATCH 2/5] support splitting mail from database location.

Date: Mon, 4 Jan 2021 22:23:47 -0400

To: notmuch@notmuchmail.org

Cc: David Bremner

From: David Bremner


Introduce a new configuration value for the mail root, and use it in
preference to the database.path (which previously implied the mail was
also in this location.
---
 doc/man1/notmuch-config.rst | 14 +++++++++---
 lib/config.cc               | 11 +++++++---
 lib/database.cc             |  2 +-
 lib/message-file.c          |  2 +-
 lib/message.cc              |  2 +-
 lib/notmuch.h               |  1 +
 lib/open.cc                 |  2 +-
 test/T055-path-config.sh    | 43 +++++++++++++++++++++++++++++++++++++
 test/T590-libconfig.sh      |  1 +
 9 files changed, 68 insertions(+), 10 deletions(-)
 create mode 100755 test/T055-path-config.sh

diff --git a/doc/man1/notmuch-config.rst b/doc/man1/notmuch-config.rst
index 769f336a..fb2b8671 100644
--- a/doc/man1/notmuch-config.rst
+++ b/doc/man1/notmuch-config.rst
@@ -47,12 +47,20 @@ programmatically as described in the SYNOPSIS above.
 The available configuration items are described below.
 
 **database.path**
+    Notmuch will store its database within a
+    sub-directory of the path configured here named ``.notmuch``.
+
+    Default: ``$MAILDIR`` variable if set, otherwise ``$HOME/mail``.
+
+**database.mail_root**
     The top-level directory where your mail currently exists and to
     where mail will be delivered in the future. Files should be
-    individual email messages. Notmuch will store its database within
-    a sub-directory of the path configured here named ``.notmuch``.
+    individual email messages.
 
-    Default: ``$MAILDIR`` variable if set, otherwise ``$HOME/mail``.
+    History: this configuration value was introduced in notmuch 0.32.
+
+    Default: For compatibility with older configurations, the value of
+    database.path is used if database.mail\_root is unset..
 
 **user.name**
     Your full name.
diff --git a/lib/config.cc b/lib/config.cc
index 7a1494be..a8aa151b 100644
--- a/lib/config.cc
+++ b/lib/config.cc
@@ -388,6 +388,8 @@ _notmuch_config_key_to_string (notmuch_config_key_t key) {
     switch (key) {
     case NOTMUCH_CONFIG_DATABASE_PATH:
 	return "database.path";
+    case NOTMUCH_CONFIG_MAIL_ROOT:
+	return "database.mail_root";
     case NOTMUCH_CONFIG_EXCLUDE_TAGS:
 	return "search.exclude_tags";
     case NOTMUCH_CONFIG_NEW_TAGS:
@@ -408,18 +410,21 @@ _notmuch_config_key_to_string (notmuch_config_key_t key) {
 }
 
 static const char *
-_notmuch_config_default (void *ctx, notmuch_config_key_t key) {
+_notmuch_config_default (notmuch_database_t *notmuch, notmuch_config_key_t key) {
     char *path;
 
     switch (key) {
     case NOTMUCH_CONFIG_DATABASE_PATH:
 	path = getenv ("MAILDIR");
 	if (path)
-	    path = talloc_strdup (ctx, path);
+	    path = talloc_strdup (notmuch, path);
 	else
-	    path = talloc_asprintf (ctx, "%s/mail",
+	    path = talloc_asprintf (notmuch, "%s/mail",
 				    getenv ("HOME"));
 	return path;
+    case NOTMUCH_CONFIG_MAIL_ROOT:
+	/* by default, mail root is the same as database path */
+	return notmuch_database_get_path (notmuch);
     case NOTMUCH_CONFIG_EXCLUDE_TAGS:
 	return "";
     case NOTMUCH_CONFIG_NEW_TAGS:
diff --git a/lib/database.cc b/lib/database.cc
index f96ba7c0..c646fee2 100644
--- a/lib/database.cc
+++ b/lib/database.cc
@@ -1367,7 +1367,7 @@ _notmuch_database_relative_path (notmuch_database_t *notmuch,
     const char *db_path, *relative;
     unsigned int db_path_len;
 
-    db_path = notmuch_database_get_path (notmuch);
+    db_path = notmuch_config_get (notmuch, NOTMUCH_CONFIG_MAIL_ROOT);
     db_path_len = strlen (db_path);
 
     relative = path;
diff --git a/lib/message-file.c b/lib/message-file.c
index 311bd478..a23493f1 100644
--- a/lib/message-file.c
+++ b/lib/message-file.c
@@ -64,7 +64,7 @@ _notmuch_message_file_open_ctx (notmuch_database_t *notmuch,
     if (unlikely (message == NULL))
 	return NULL;
 
-    const char *prefix = notmuch_database_get_path (notmuch);
+    const char *prefix = notmuch_config_get (notmuch, NOTMUCH_CONFIG_MAIL_ROOT);
     if (prefix == NULL)
 	goto FAIL;
 
diff --git a/lib/message.cc b/lib/message.cc
index fca99082..e596456c 100644
--- a/lib/message.cc
+++ b/lib/message.cc
@@ -1097,7 +1097,7 @@ _notmuch_message_ensure_filename_list (notmuch_message_t *message)
 
 	*colon = '\0';
 
-	db_path = notmuch_database_get_path (message->notmuch);
+	db_path = notmuch_config_get (message->notmuch, NOTMUCH_CONFIG_MAIL_ROOT);
 
 	directory = _notmuch_database_get_directory_path (local,
 							  message->notmuch,
diff --git a/lib/notmuch.h b/lib/notmuch.h
index e51b738d..f659d1a4 100644
--- a/lib/notmuch.h
+++ b/lib/notmuch.h
@@ -2461,6 +2461,7 @@ notmuch_config_list_destroy (notmuch_config_list_t *config_list);
 typedef enum _notmuch_config_key {
     NOTMUCH_CONFIG_FIRST,
     NOTMUCH_CONFIG_DATABASE_PATH = NOTMUCH_CONFIG_FIRST,
+    NOTMUCH_CONFIG_MAIL_ROOT,
     NOTMUCH_CONFIG_EXCLUDE_TAGS,
     NOTMUCH_CONFIG_NEW_TAGS,
     NOTMUCH_CONFIG_NEW_IGNORE,
diff --git a/lib/open.cc b/lib/open.cc
index acc9f7a4..12cbbbd0 100644
--- a/lib/open.cc
+++ b/lib/open.cc
@@ -203,8 +203,8 @@ notmuch_database_open_with_config (const char *database_path,
     notmuch = talloc_zero (NULL, notmuch_database_t);
     notmuch->exception_reported = false;
     notmuch->status_string = NULL;
-    notmuch->path = talloc_strdup (notmuch, database_path);
 
+    notmuch->path = talloc_strdup (notmuch, database_path);
     strip_trailing (notmuch->path, '/');
 
     notmuch->writable_xapian_db = NULL;
diff --git a/test/T055-path-config.sh b/test/T055-path-config.sh
new file mode 100755
index 00000000..190b3686
--- /dev/null
+++ b/test/T055-path-config.sh
@@ -0,0 +1,43 @@
+#!/usr/bin/env bash
+test_description='Configuration of mail-root and database path'
+. $(dirname "$0")/test-lib.sh || exit 1
+
+add_email_corpus
+
+test_begin_subtest "database is mail/.notmuch/xapian"
+output=$(notmuch count '*')
+test_expect_equal $output '52'
+
+backup_database
+test_begin_subtest "split (with prefix)"
+notmuch config set database.path `pwd`/database
+notmuch config set database.mail_root `pwd`/mail
+mkdir database
+mv mail/.notmuch database
+output=$(notmuch count '*')
+test_expect_equal "$output" '52'
+restore_database
+
+backup_database
+test_begin_subtest "split (read-write)"
+notmuch config set database.path `pwd`/database
+notmuch config set database.mail_root `pwd`/mail
+mkdir database
+mv mail/.notmuch database
+tag="tag${RANDOM}"
+notmuch tag +$tag '*'
+output=$(notmuch count tag:$tag)
+test_expect_equal "$output" '52'
+restore_database
+
+backup_database
+test_begin_subtest "create database in split configuration"
+notmuch config set database.path `pwd`/database
+notmuch config set database.mail_root `pwd`/mail
+rm -r mail/.notmuch/xapian
+notmuch new
+output=$(notmuch count '*')
+test_expect_equal "$output" '52'
+restore_database
+
+test_done
diff --git a/test/T590-libconfig.sh b/test/T590-libconfig.sh
index 7f187d36..b03ee0c6 100755
--- a/test/T590-libconfig.sh
+++ b/test/T590-libconfig.sh
@@ -333,6 +333,7 @@ EOF
 cat <<'EOF' >EXPECTED
 == stdout ==
 MAIL_DIR
+MAIL_DIR
 
 inbox;unread
 NULL
-- 
2.29.2
_______________________________________________
notmuch mailing list -- notmuch@notmuchmail.org
To unsubscribe send an email to notmuch-leave@notmuchmail.org

Thread: