unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
From: David Bremner <david@tethera.net>
To: notmuch@notmuchmail.org
Cc: spwhitton@spwhitton.name
Subject: [PATCH 6/6] lib/open: create database path in some cases
Date: Fri, 29 Jul 2022 09:20:01 -0300	[thread overview]
Message-ID: <20220729122001.1556787-7-david@tethera.net> (raw)
In-Reply-To: <20220729122001.1556787-1-david@tethera.net>

There is some duplication of code here, but not all of the locations
valid to find a database make sense to create. Furthermore we nead two
passes, so the control flow in _choose_database_path would get a bit
convoluted.
---
 lib/open.cc              | 53 ++++++++++++++++++++++++++++++++++++++--
 test/T055-path-config.sh |  1 -
 test/T560-lib-error.sh   | 10 +++++---
 3 files changed, 57 insertions(+), 7 deletions(-)

diff --git a/lib/open.cc b/lib/open.cc
index bc450555..caa58ff6 100644
--- a/lib/open.cc
+++ b/lib/open.cc
@@ -262,6 +262,45 @@ _mkdir (const char *path, char **message)
     return NOTMUCH_STATUS_SUCCESS;
 }
 
+static notmuch_status_t
+_create_database_path (notmuch_database_t *notmuch,
+		       const char *profile,
+		       GKeyFile *key_file,
+		       const char **database_path,
+		       char **message)
+{
+    notmuch_status_t status;
+
+    if (! *database_path) {
+	*database_path = getenv ("NOTMUCH_DATABASE");
+    }
+
+    if (! *database_path && key_file) {
+	char *path = g_key_file_get_string (key_file, "database", "path", NULL);
+	if (path) {
+	    if (path[0] == '/')
+		*database_path = talloc_strdup (notmuch, path);
+	    else
+		*database_path = talloc_asprintf (notmuch, "%s/%s", getenv ("HOME"), path);
+	    g_free (path);
+	}
+    }
+
+    if (! *database_path) {
+	*database_path = _xdg_dir (notmuch, "XDG_DATA_HOME", ".local/share", profile);
+	notmuch->params |= NOTMUCH_PARAM_SPLIT;
+    }
+
+    if (*database_path[0] != '/') {
+	*message = strdup ("Error: Database path must be absolute.\n");
+	return NOTMUCH_STATUS_PATH_ERROR;
+    }
+
+    if ((status = _mkdir (*database_path, message)))
+	return status;
+
+    return NOTMUCH_STATUS_SUCCESS;
+}
 
 static notmuch_database_t *
 _alloc_notmuch (const char *database_path, const char *config_path, const char *profile)
@@ -641,9 +680,19 @@ notmuch_database_create_with_config (const char *database_path,
 	goto DONE;
     }
 
-    if ((status = _choose_database_path (notmuch, profile, key_file,
-					 &database_path, &message)))
+    status = _choose_database_path (notmuch, profile, key_file,
+				    &database_path, &message);
+    switch (status) {
+    case NOTMUCH_STATUS_SUCCESS:
+	break;
+    case NOTMUCH_STATUS_NO_DATABASE:
+	if ((status = _create_database_path (notmuch, profile, key_file,
+					     &database_path, &message)))
+	    goto DONE;
+	break;
+    default:
 	goto DONE;
+    }
 
     _set_database_path (notmuch, database_path);
 
diff --git a/test/T055-path-config.sh b/test/T055-path-config.sh
index 149aa6a1..fe295324 100755
--- a/test/T055-path-config.sh
+++ b/test/T055-path-config.sh
@@ -369,7 +369,6 @@ EOF
 	   ;&
        mailroot_only)
 	   test_begin_subtest "create database parent dir ($config)"
-	   test_subtest_known_broken
 	   rm -r ${DATABASE_PATH}
 	   notmuch new
 	   test_expect_equal "$(xapian-metadata get ${XAPIAN_PATH} version)" 3
diff --git a/test/T560-lib-error.sh b/test/T560-lib-error.sh
index 30cce943..78cae1cd 100755
--- a/test/T560-lib-error.sh
+++ b/test/T560-lib-error.sh
@@ -102,16 +102,17 @@ test_C <<'EOF'
 int main (int argc, char** argv)
 {
     notmuch_status_t stat;
-    char *msg;
+    char *msg = NULL;
 
     stat = notmuch_database_create_with_config (NULL, "", NULL, NULL, &msg);
+    printf ("%s\n", notmuch_status_to_string (stat));
     if (msg) fputs (msg, stderr);
 }
 EOF
 cat <<'EOF' >EXPECTED
 == stdout ==
+No mail root found
 == stderr ==
-Error: could not locate database.
 EOF
 test_expect_equal_file EXPECTED OUTPUT
 
@@ -123,16 +124,17 @@ int main (int argc, char** argv)
 {
     notmuch_database_t *db;
     notmuch_status_t stat;
-    char *msg;
+    char *msg = NULL;
 
     stat = notmuch_database_create_with_config (argv[1], "", NULL, &db, &msg);
+    printf ("%d\n", stat == NOTMUCH_STATUS_SUCCESS);
     if (msg) fputs (msg, stderr);
 }
 EOF
 cat <<'EOF' >EXPECTED
 == stdout ==
+1
 == stderr ==
-Error: database path 'CWD/nonexistent/foo' does not exist or is not a directory.
 EOF
 test_expect_equal_file EXPECTED OUTPUT
 
-- 
2.35.2

      parent reply	other threads:[~2022-07-29 12:20 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-07-29 12:19 v1 Create database directory David Bremner
2022-07-29 12:19 ` [PATCH 1/6] tests: remove dead code from T055-path-config.sh David Bremner
2022-08-06 12:19   ` David Bremner
2022-07-29 12:19 ` [PATCH 2/6] test: add test for creating database in various configurations David Bremner
2022-09-03 11:33   ` David Bremner
2022-07-29 12:19 ` [PATCH 3/6] test/path-config: set database.mail_root but not database.path David Bremner
2022-07-29 12:19 ` [PATCH 4/6] lib/open: refactor call to mkdir into function David Bremner
2022-07-29 12:20 ` [PATCH 5/6] lib/open: return non-SUCCESS on missing database path David Bremner
2022-07-29 12:20 ` 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=20220729122001.1556787-7-david@tethera.net \
    --to=david@tethera.net \
    --cc=notmuch@notmuchmail.org \
    --cc=spwhitton@spwhitton.name \
    /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).