unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
From: David Bremner <david@tethera.net>
To: notmuch@notmuchmail.org
Cc: David Bremner <david@tethera.net>
Subject: [PATCH 14/22] support splitting mail from database location.
Date: Thu,  4 Mar 2021 09:31:17 -0400	[thread overview]
Message-ID: <20210304133125.4050229-15-david@tethera.net> (raw)
In-Reply-To: <20210304133125.4050229-1-david@tethera.net>

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.

Further changes to the CLI will be needed to work in this split
configuration.
---
 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                 |  5 +++
 test/T055-path-config.sh    | 90 +++++++++++++++++++++++++++++++++++++
 test/T590-libconfig.sh      |  1 +
 9 files changed, 119 insertions(+), 9 deletions(-)
 create mode 100755 test/T055-path-config.sh

diff --git a/doc/man1/notmuch-config.rst b/doc/man1/notmuch-config.rst
index bc597957..99030a41 100644
--- a/doc/man1/notmuch-config.rst
+++ b/doc/man1/notmuch-config.rst
@@ -43,12 +43,20 @@ configuration file and corresponding database.
 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.
 
 **database.hook_dir**
 
diff --git a/lib/config.cc b/lib/config.cc
index 99b75e7b..3b8f1092 100644
--- a/lib/config.cc
+++ b/lib/config.cc
@@ -390,6 +390,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_HOOK_DIR:
 	return "database.hook_dir";
     case NOTMUCH_CONFIG_EXCLUDE_TAGS:
@@ -412,18 +414,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 afa8b754..1d8839a5 100644
--- a/lib/database.cc
+++ b/lib/database.cc
@@ -1345,7 +1345,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 1bea90f0..35f0b87a 100644
--- a/lib/message.cc
+++ b/lib/message.cc
@@ -1098,7 +1098,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 37d7581f..374d3af9 100644
--- a/lib/notmuch.h
+++ b/lib/notmuch.h
@@ -2473,6 +2473,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_HOOK_DIR,
     NOTMUCH_CONFIG_EXCLUDE_TAGS,
     NOTMUCH_CONFIG_NEW_TAGS,
diff --git a/lib/open.cc b/lib/open.cc
index 4e8ab3b4..2f65198d 100644
--- a/lib/open.cc
+++ b/lib/open.cc
@@ -252,6 +252,11 @@ _choose_xapian_path (void *ctx, const char *database_path, const char **xapian_p
     if (status)
 	goto DONE;
 
+    trial_path = talloc_asprintf (ctx, "%s/xapian", database_path);
+    status = _trial_open (trial_path, message_ptr);
+    if (status != NOTMUCH_STATUS_PATH_ERROR)
+	goto DONE;
+
     notmuch_path = talloc_asprintf (ctx, "%s/.notmuch", database_path);
     status = _db_dir_exists (notmuch_path, message_ptr);
     if (status)
diff --git a/test/T055-path-config.sh b/test/T055-path-config.sh
new file mode 100755
index 00000000..c6920ca9
--- /dev/null
+++ b/test/T055-path-config.sh
@@ -0,0 +1,90 @@
+#!/usr/bin/env bash
+test_description='Configuration of mail-root and database path'
+. $(dirname "$0")/test-lib.sh || exit 1
+
+backup_config () {
+    local test_name=$(basename $0 .sh)
+    cp ${NOTMUCH_CONFIG} notmuch-config-backup.${test_name}
+}
+
+restore_config () {
+    local test_name=$(basename $0 .sh)
+    export NOTMUCH_CONFIG="${TMP_DIRECTORY}/notmuch-config"
+    unset CONFIG_PATH
+    unset DATABASE_PATH
+    unset NOTMUCH_PROFILE
+    cp notmuch-config-backup.${test_name} ${NOTMUCH_CONFIG}
+}
+
+split_config () {
+    local dir
+    backup_config
+    dir="$TMP_DIRECTORY/database.$test_count"
+    rm -rf $dir
+    mkdir $dir
+    notmuch config set database.path $dir
+    notmuch config set database.mail_root $MAIL_DIR
+    DATABASE_PATH=$dir
+}
+
+
+
+for config in traditional split; do
+    # start each set of tests with a known set of messages
+    add_email_corpus
+
+    case $config in
+	traditional)
+	    backup_config
+	    ;;
+	split)
+	    split_config
+	    mv mail/.notmuch/xapian $DATABASE_PATH
+	    ;;
+    esac
+
+    test_begin_subtest "count ($config)"
+    output=$(notmuch count '*')
+    test_expect_equal "$output" '52'
+
+    test_begin_subtest "count+tag ($config)"
+    tag="tag${RANDOM}"
+    notmuch tag +$tag '*'
+    output=$(notmuch count tag:$tag)
+    notmuch tag -$tag '*'
+    test_expect_equal "$output" '52'
+
+    test_begin_subtest "address ($config)"
+    notmuch address --deduplicate=no --sort=newest-first --output=sender --output=recipients path:foo >OUTPUT
+    cat <<EOF >EXPECTED
+Carl Worth <cworth@cworth.org>
+notmuch@notmuchmail.org
+EOF
+    test_expect_equal_file EXPECTED OUTPUT
+
+    test_begin_subtest "dump ($config)"
+    notmuch dump is:attachment and is:signed | sort > OUTPUT
+    cat <<EOF > EXPECTED
+#notmuch-dump batch-tag:3 config,properties,tags
++attachment +inbox +signed +unread -- id:20091118005829.GB25380@dottiness.seas.harvard.edu
++attachment +inbox +signed +unread -- id:20091118010116.GC25380@dottiness.seas.harvard.edu
+EOF
+    test_expect_equal_file EXPECTED OUTPUT
+
+    test_begin_subtest "dump + tag + restore ($config)"
+    notmuch dump '*' > EXPECTED
+    notmuch tag -inbox '*'
+    notmuch restore < EXPECTED
+    notmuch dump > OUTPUT
+    test_expect_equal_file EXPECTED OUTPUT
+
+    test_begin_subtest "reindex ($config)"
+    notmuch search --output=messages '*' > EXPECTED
+    notmuch reindex '*'
+    notmuch search --output=messages '*' > OUTPUT
+    test_expect_equal_file EXPECTED OUTPUT
+
+    restore_config
+done
+
+test_done
diff --git a/test/T590-libconfig.sh b/test/T590-libconfig.sh
index 4e510e97..5cf70987 100755
--- a/test/T590-libconfig.sh
+++ b/test/T590-libconfig.sh
@@ -364,6 +364,7 @@ EOF
 cat <<'EOF' >EXPECTED
 == stdout ==
 MAIL_DIR
+MAIL_DIR
 MAIL_DIR/.notmuch/hooks
 
 inbox;unread
-- 
2.30.1

  parent reply	other threads:[~2021-03-04 13:33 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-04 13:31 v2 support flexible database location David Bremner
2021-03-04 13:31 ` [PATCH 01/22] test/setup: add check for config exists, but no database David Bremner
2021-03-04 13:31 ` [PATCH 02/22] lib: publish API for notmuch_database_reopen David Bremner
2021-03-04 13:31 ` [PATCH 03/22] lib: save path of xapian database in notmuch struct David Bremner
2021-03-04 13:31 ` [PATCH 04/22] lib: support reopening databases for write access David Bremner
2021-03-04 13:31 ` [PATCH 05/22] CLI/show: complete conversion to new configuration framework David Bremner
2021-03-04 13:31 ` [PATCH 06/22] lib/open: support NOTMUCH_DATABASE environment variable David Bremner
2021-03-04 13:31 ` [PATCH 07/22] lib/open: allocate notmuch_t struct early David Bremner
2021-03-04 13:31 ` [PATCH 08/22] lib: remove "path" from notmuch struct David Bremner
2021-03-04 13:31 ` [PATCH 09/22] lib/open: factor out library initialization David Bremner
2021-03-04 13:31 ` [PATCH 10/22] lib/open: reuse directory checks from n_d_c_with_config David Bremner
2021-03-04 13:31 ` [PATCH 11/22] lib/open: factor out the second half of n_d_open_with_config David Bremner
2021-03-04 13:31 ` [PATCH 12/22] lib/open: use _finish_open in n_d_create_with_config David Bremner
2021-03-04 13:31 ` [PATCH 13/22] lib/open: Use check for existing database by trial opening David Bremner
2021-03-04 13:31 ` David Bremner [this message]
2021-03-04 13:31 ` [PATCH 15/22] lib/open: check for split configuration when creating database David Bremner
2021-03-04 13:31 ` [PATCH 16/22] CLI/new: support split database and mail location David Bremner
2021-03-04 13:31 ` [PATCH 17/22] lib/open: support XDG_DATA_HOME as a fallback database location David Bremner
2021-03-04 13:31 ` [PATCH 18/22] CLI/insert: support split database and mail root David Bremner
2021-03-04 13:31 ` [PATCH 19/22] lib/compact: enable split config David Bremner
2021-03-04 13:31 ` [PATCH 20/22] lib/open: fix hook directory calculation in split configuration David Bremner
2021-03-04 13:31 ` [PATCH 21/22] lib/config: add configuration variable for backup directory David Bremner
2021-03-04 13:31 ` [PATCH 22/22] CLI/new: use " David Bremner
  -- strict thread matches above, loose matches on Subject: below --
2021-03-07 17:27 v3 support flexible database location David Bremner
2021-03-07 17:27 ` [PATCH 14/22] support splitting mail from " David Bremner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://notmuchmail.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210304133125.4050229-15-david@tethera.net \
    --to=david@tethera.net \
    --cc=notmuch@notmuchmail.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).