[PATCH 5/5] lib/open: support XDG_DATA_HOME as a fallback database location.

Subject: [PATCH 5/5] lib/open: support XDG_DATA_HOME as a fallback database location.

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

To: notmuch@notmuchmail.org

Cc: David Bremner

From: David Bremner


This changes some error reporting, either intentionally by reporting
the highest level missing directory, or by side effect from looking in
XDG locations when given null database location.
---
 lib/open.cc              | 25 +++++++++++++++++++------
 test/T055-path-config.sh | 11 +++++++++++
 test/T560-lib-error.sh   |  6 +++---
 test/T590-libconfig.sh   |  4 ++--
 4 files changed, 35 insertions(+), 11 deletions(-)

diff --git a/lib/open.cc b/lib/open.cc
index 91b24e38..bc97e178 100644
--- a/lib/open.cc
+++ b/lib/open.cc
@@ -116,7 +116,8 @@ DONE:
 }
 
 static notmuch_status_t
-_pre_open(const char *config_path,
+_pre_open(void *ctx,
+	  const char *config_path,
 	  const char *profile,
 	  GKeyFile **key_file,
 	  const char **database_path,
@@ -133,6 +134,10 @@ _pre_open(const char *config_path,
     if (! *database_path && *key_file)
 	*database_path = g_key_file_get_value (*key_file, "database", "path", NULL);
 
+    if (! *database_path) {
+	*database_path = _xdg_dir (ctx, "XDG_DATA_HOME", ".local/share", profile);
+    }
+
     if (*database_path == NULL) {
 	*message = strdup ("Error: Cannot open a database for a NULL path.\n");
 	return NOTMUCH_STATUS_NULL_POINTER;
@@ -169,7 +174,7 @@ notmuch_database_open_with_config (const char *database_path,
 	database_path = getenv ("NOTMUCH_DATABASE");
     }
 
-    if ((status = _pre_open (config_path, profile, &key_file, &database_path, &message)))
+    if ((status = _pre_open (local, config_path, profile, &key_file, &database_path, &message)))
 	goto DONE;
 
     /* Initialize the GLib type system and threads */
@@ -195,6 +200,14 @@ notmuch_database_open_with_config (const char *database_path,
     notmuch->view = 1;
     notmuch->xapian_db = NULL;
 
+    err = stat (database_path, &st);
+    if (err) {
+	IGNORE_RESULT (asprintf (&message, "Error opening database at %s: %s\n",
+				 database_path, strerror (errno)));
+	status = NOTMUCH_STATUS_FILE_ERROR;
+	goto DONE;
+    }
+
     try {
 	xapian_path = talloc_asprintf (notmuch,"%s/xapian",database_path);
 	notmuch->xapian_db = new Xapian::Database (xapian_path);
@@ -408,8 +421,9 @@ notmuch_database_create_with_config (const char *database_path,
     GKeyFile *key_file = NULL;
     struct stat st;
     int err;
+    void *local = talloc_new (NULL);
 
-    if ((status = _pre_open (config_path, profile, &key_file, &database_path, &message)))
+    if ((status = _pre_open (local, config_path, profile, &key_file, &database_path, &message)))
 	goto DONE;
 
     err = stat (database_path, &st);
@@ -428,7 +442,7 @@ notmuch_database_create_with_config (const char *database_path,
 	goto DONE;
     }
 
-    notmuch_path = talloc_asprintf (NULL, "%s/%s", database_path, ".notmuch");
+    notmuch_path = talloc_asprintf (local, "%s/%s", database_path, ".notmuch");
 
     err = mkdir (notmuch_path, 0755);
     if (err) {
@@ -464,8 +478,7 @@ notmuch_database_create_with_config (const char *database_path,
     }
 
   DONE:
-    if (notmuch_path)
-	talloc_free (notmuch_path);
+    talloc_free (local);
 
     if (message) {
 	if (status_string)
diff --git a/test/T055-path-config.sh b/test/T055-path-config.sh
index 72c369cb..b4bc6741 100755
--- a/test/T055-path-config.sh
+++ b/test/T055-path-config.sh
@@ -49,4 +49,15 @@ output=$(notmuch count '*')
 test_expect_equal "$output" '52'
 restore_database
 
+backup_database
+test_begin_subtest "xdg database location"
+notmuch config set database.mail_root `pwd`/mail
+notmuch config set database.path
+dir=home/.local/share/notmuch/default/
+mkdir -p $dir
+mv mail/.notmuch/xapian $dir
+output=$(notmuch count '*')
+test_expect_equal "$output" '52'
+restore_database
+
 test_done
diff --git a/test/T560-lib-error.sh b/test/T560-lib-error.sh
index ade376ef..9b59fb55 100755
--- a/test/T560-lib-error.sh
+++ b/test/T560-lib-error.sh
@@ -22,7 +22,7 @@ EOF
 cat <<'EOF' >EXPECTED
 == stdout ==
 == stderr ==
-Error: Cannot open a database for a NULL path.
+Error opening database at CWD/home/.local/share/notmuch/default: No such file or directory
 EOF
 test_expect_equal_file EXPECTED OUTPUT
 
@@ -76,7 +76,7 @@ EOF
 cat <<'EOF' >EXPECTED
 == stdout ==
 == stderr ==
-Error opening database at CWD/nonexistent/foo/.notmuch: No such file or directory
+Error opening database at CWD/nonexistent/foo: No such file or directory
 EOF
 test_expect_equal_file EXPECTED OUTPUT
 
@@ -93,7 +93,7 @@ EOF
 cat <<'EOF' >EXPECTED
 == stdout ==
 == stderr ==
-Error: Cannot open a database for a NULL path.
+Error: Cannot create database at CWD/home/.local/share/notmuch/default: No such file or directory.
 EOF
 test_expect_equal_file EXPECTED OUTPUT
 
diff --git a/test/T590-libconfig.sh b/test/T590-libconfig.sh
index b03ee0c6..256b9463 100755
--- a/test/T590-libconfig.sh
+++ b/test/T590-libconfig.sh
@@ -487,8 +487,8 @@ cat <<'EOF' >EXPECTED
 == stdout ==
 == stderr ==
 error opening database
-Erroneous NULL pointer
-Error: Cannot open a database for a NULL path.
+Something went wrong trying to read or write a file
+Error opening database at CWD/home/.local/share/notmuch/default: No such file or directory
 
 EOF
 test_expect_equal_file EXPECTED OUTPUT
-- 
2.29.2
_______________________________________________
notmuch mailing list -- notmuch@notmuchmail.org
To unsubscribe send an email to notmuch-leave@notmuchmail.org

Thread: