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 21/21] CLI/new: use configuration variable for backup directory
Date: Wed, 17 Feb 2021 16:10:45 -0400	[thread overview]
Message-ID: <20210217201045.2021106-22-david@tethera.net> (raw)
In-Reply-To: <20210217201045.2021106-1-david@tethera.net>

The stat is dropped to avoid a race condition between stat and mkdir.
This changes the default location for backups to make things
tidier. Hopefully there is not user scripts relying on this location.
---
 notmuch-new.c            | 18 ++++++------------
 test/T055-path-config.sh | 15 +++++++++++++++
 test/T530-upgrade.sh     |  2 +-
 3 files changed, 22 insertions(+), 13 deletions(-)

diff --git a/notmuch-new.c b/notmuch-new.c
index 63ac10a6..8a8ff69a 100644
--- a/notmuch-new.c
+++ b/notmuch-new.c
@@ -1048,28 +1048,22 @@ _maybe_upgrade (notmuch_database_t *notmuch, add_files_state_t *state) {
     if (notmuch_database_needs_upgrade (notmuch)) {
 	time_t now = time (NULL);
 	struct tm *gm_time = gmtime (&now);
-	struct stat st;
 	int err;
 	notmuch_status_t status;
-	char *dot_notmuch_path = talloc_asprintf (notmuch, "%s/%s", state->db_path, ".notmuch");
-
+	const char *backup_dir = notmuch_config_get (notmuch, NOTMUCH_CONFIG_BACKUP_DIR);
 	const char *backup_name;
 
-	err = stat(dot_notmuch_path, &st);
-	if (err) {
-	    if (errno == ENOENT) {
-		dot_notmuch_path = NULL;
-	    } else {
-		fprintf(stderr, "Failed to stat %s: %s\n", dot_notmuch_path, strerror(errno));
-		return EXIT_FAILURE;
-	    }
+	err = mkdir (backup_dir, 0755);
+	if (err && errno != EEXIST) {
+	    fprintf(stderr, "Failed to create %s: %s\n", backup_dir, strerror(errno));
+	    return EXIT_FAILURE;
 	}
 
 	/* since dump files are written atomically, the amount of
 	 * harm from overwriting one within a second seems
 	 * relatively small. */
 	backup_name = talloc_asprintf (notmuch, "%s/dump-%04d%02d%02dT%02d%02d%02d.gz",
-				       dot_notmuch_path ? dot_notmuch_path : state->db_path,
+				       backup_dir,
 				       gm_time->tm_year + 1900,
 				       gm_time->tm_mon + 1,
 				       gm_time->tm_mday,
diff --git a/test/T055-path-config.sh b/test/T055-path-config.sh
index e4812c82..d8828342 100755
--- a/test/T055-path-config.sh
+++ b/test/T055-path-config.sh
@@ -2,6 +2,8 @@
 test_description='Configuration of mail-root and database path'
 . $(dirname "$0")/test-lib.sh || exit 1
 
+test_require_external_prereq xapian-metdata
+
 backup_config () {
     local test_name=$(basename $0 .sh)
     cp ${NOTMUCH_CONFIG} notmuch-config-backup.${test_name}
@@ -13,6 +15,7 @@ restore_config () {
     unset CONFIG_PATH
     unset DATABASE_PATH
     unset NOTMUCH_PROFILE
+    unset XAPIAN_PATH
     cp notmuch-config-backup.${test_name} ${NOTMUCH_CONFIG}
 }
 
@@ -25,6 +28,7 @@ split_config () {
     notmuch config set database.path $dir
     notmuch config set database.mail_root $MAIL_DIR
     DATABASE_PATH=$dir
+    XAPIAN_PATH="$dir/xapian"
 }
 
 symlink_config () {
@@ -34,6 +38,7 @@ symlink_config () {
     ln -s $MAIL_DIR $dir
     notmuch config set database.path $dir
     notmuch config set database.mail_root $MAIL_DIR
+    XAPIAN_PATH="$MAIL_DIR/.notmuch/xapian"
     unset DATABASE_PATH
 }
 
@@ -56,6 +61,7 @@ xdg_config () {
     mv ${NOTMUCH_CONFIG} $CONFIG_PATH
     unset NOTMUCH_CONFIG
 
+    XAPIAN_PATH="${DATABASE_PATH}/xapian"
     notmuch --config=${CONFIG_PATH} config set database.mail_root ${TMP_DIRECTORY}/mail
     notmuch --config=${CONFIG_PATH} config set database.path
 }
@@ -67,6 +73,7 @@ for config in traditional split XDG XDG+profile symlink; do
     case $config in
 	traditional)
 	    backup_config
+	    XAPIAN_PATH="$MAIL_DIR/.notmuch/xapian"
 	    ;;
 	split)
 	    split_config
@@ -184,6 +191,14 @@ EOF
     notmuch search --output=messages '*' | sort > OUTPUT
     test_expect_equal_file EXPECTED OUTPUT
 
+    test_begin_subtest "upgrade backup ($config)"
+    features=$(xapian-metadata get $XAPIAN_PATH features | grep -v "^relative directory paths")
+    xapian-metadata set $XAPIAN_PATH features "$features"
+    output=$(notmuch new | grep Welcome)
+    test_expect_equal \
+	"$output" \
+	"Welcome to a new version of notmuch! Your database will now be upgraded."
+
     restore_config
 done
 
diff --git a/test/T530-upgrade.sh b/test/T530-upgrade.sh
index c599dacf..cce29f45 100755
--- a/test/T530-upgrade.sh
+++ b/test/T530-upgrade.sh
@@ -5,7 +5,7 @@ test_description='database upgrades'
 test_require_external_prereq xapian-metadata
 
 XAPIAN_PATH=$MAIL_DIR/.notmuch/xapian
-BACKUP_PATH=$MAIL_DIR/.notmuch
+BACKUP_PATH=$MAIL_DIR/.notmuch/backups
 
 delete_feature () {
     local key=$1
-- 
2.30.0

  parent reply	other threads:[~2021-02-17 20:12 UTC|newest]

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