From: David Bremner <david@tethera.net>
To: notmuch@notmuchmail.org
Cc: David Bremner <david@tethera.net>
Subject: [PATCH 5/5] lib/open: support XDG_DATA_HOME as a fallback database location.
Date: Mon, 4 Jan 2021 22:23:50 -0400 [thread overview]
Message-ID: <20210105022350.643917-6-david@tethera.net> (raw)
In-Reply-To: <20210105022350.643917-1-david@tethera.net>
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
prev parent reply other threads:[~2021-01-05 3:00 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-01-05 2:23 v1 flexible database location David Bremner
2021-01-05 2:23 ` [PATCH 1/5] lib/open: support NOTMUCH_DATABASE environment variable David Bremner
2021-01-05 2:23 ` [PATCH 2/5] support splitting mail from database location David Bremner
2021-01-07 11:50 ` [PATCH] fixup! " David Bremner
2021-01-05 2:23 ` [PATCH 3/5] lib/open: move path calculations after notmuch object creation David Bremner
2021-01-05 2:23 ` [PATCH 4/5] lib/open: open xapian database without .notmuch prefix David Bremner
2021-01-05 2:23 ` David Bremner [this message]
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=20210105022350.643917-6-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).