unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
* v1 Create database directory
@ 2022-07-29 12:19 David Bremner
  2022-07-29 12:19 ` [PATCH 1/6] tests: remove dead code from T055-path-config.sh David Bremner
                   ` (5 more replies)
  0 siblings, 6 replies; 8+ messages in thread
From: David Bremner @ 2022-07-29 12:19 UTC (permalink / raw)
  To: notmuch; +Cc: spwhitton

In [1] Sean reported the database being created in the wrong
location. In order to fix this, this database creates the parent
directory for the database in more cases.

[1]: id:87y1wqkw13.fsf@athena.silentflame.com 


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH 1/6] tests: remove dead code from T055-path-config.sh
  2022-07-29 12:19 v1 Create database directory David Bremner
@ 2022-07-29 12:19 ` 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
                   ` (4 subsequent siblings)
  5 siblings, 1 reply; 8+ messages in thread
From: David Bremner @ 2022-07-29 12:19 UTC (permalink / raw)
  To: notmuch; +Cc: spwhitton

This case statement does nothing.
---
 test/T055-path-config.sh | 3 ---
 1 file changed, 3 deletions(-)

diff --git a/test/T055-path-config.sh b/test/T055-path-config.sh
index 63dd90fd..58c824a2 100755
--- a/test/T055-path-config.sh
+++ b/test/T055-path-config.sh
@@ -359,9 +359,6 @@ EOF
 	   ;;
    esac
 
-   case $config in
-       split|XDG*)
-   esac
    restore_config
    rm -rf home/.local
    rm -rf home/.config
-- 
2.35.2

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH 2/6] test: add test for creating database in various configurations.
  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-07-29 12:19 ` David Bremner
  2022-07-29 12:19 ` [PATCH 3/6] test/path-config: set database.mail_root but not database.path David Bremner
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 8+ messages in thread
From: David Bremner @ 2022-07-29 12:19 UTC (permalink / raw)
  To: notmuch; +Cc: spwhitton

The existing database creation (via add_email_corpus) was always done
in the traditional configuration. The use of xapian-metadata is just
to portably ensure that there is a database created where we expect
there to be.
---
 test/T055-path-config.sh | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/test/T055-path-config.sh b/test/T055-path-config.sh
index 58c824a2..45545d88 100755
--- a/test/T055-path-config.sh
+++ b/test/T055-path-config.sh
@@ -313,6 +313,12 @@ user.other_email
 user.primary_email
 EOF
    test_expect_equal_file EXPECTED OUTPUT
+
+   test_begin_subtest "create database ($config)"
+   rm -r ${XAPIAN_PATH}
+   notmuch new
+   test_expect_equal "$(xapian-metadata get ${XAPIAN_PATH} version)" 3
+
    case $config in
        XDG*)
 	   test_begin_subtest "Set shadowed config value in database ($config)"
-- 
2.35.2

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH 3/6] test/path-config: set database.mail_root but not database.path
  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-07-29 12:19 ` [PATCH 2/6] test: add test for creating database in various configurations David Bremner
@ 2022-07-29 12:19 ` David Bremner
  2022-07-29 12:19 ` [PATCH 4/6] lib/open: refactor call to mkdir into function David Bremner
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 8+ messages in thread
From: David Bremner @ 2022-07-29 12:19 UTC (permalink / raw)
  To: notmuch; +Cc: spwhitton

The failing "create database" test replicates a bug reported by Sean
Whitton [1]. The other two failures also look related to the database
being (re)created in the wrong place.

[1]: id:87y1wqkw13.fsf@athena.silentflame.com.
---
 test/T055-path-config.sh | 30 ++++++++++++++++++++++++------
 1 file changed, 24 insertions(+), 6 deletions(-)

diff --git a/test/T055-path-config.sh b/test/T055-path-config.sh
index 45545d88..149aa6a1 100755
--- a/test/T055-path-config.sh
+++ b/test/T055-path-config.sh
@@ -92,7 +92,20 @@ xdg_config () {
     notmuch --config=${CONFIG_PATH} config set database.path
 }
 
-for config in traditional split XDG XDG+profile symlink home_mail maildir_env; do
+mailroot_only_config () {
+    local dir
+
+    backup_config
+    notmuch config set database.mail_root ${TMP_DIRECTORY}/mail
+    notmuch config set database.path
+    DATABASE_PATH="${HOME}/.local/share/notmuch/default"
+    rm -rf $DATABASE_PATH
+    mkdir -p $DATABASE_PATH
+    XAPIAN_PATH="${DATABASE_PATH}/xapian"
+    mv mail/.notmuch/xapian $DATABASE_PATH
+}
+
+for config in traditional split XDG XDG+profile symlink home_mail maildir_env mailroot_only; do
     #start each set of tests with an known set of messages
     add_email_corpus
 
@@ -122,6 +135,9 @@ for config in traditional split XDG XDG+profile symlink home_mail maildir_env; d
 	maildir_env)
 	    maildir_env_config
 	    ;;
+	mailroot_only)
+	    mailroot_only_config
+	    ;;
     esac
 
     test_begin_subtest "count ($config)"
@@ -314,11 +330,6 @@ user.primary_email
 EOF
    test_expect_equal_file EXPECTED OUTPUT
 
-   test_begin_subtest "create database ($config)"
-   rm -r ${XAPIAN_PATH}
-   notmuch new
-   test_expect_equal "$(xapian-metadata get ${XAPIAN_PATH} version)" 3
-
    case $config in
        XDG*)
 	   test_begin_subtest "Set shadowed config value in database ($config)"
@@ -355,6 +366,13 @@ Added 1 new message to the database.
 thread:XXX   2001-01-05 [1/1] Notmuch Test Suite; Do not ignore, very important (inbox unread)
 EOF
 	   test_expect_equal_file EXPECTED OUTPUT
+	   ;&
+       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
 	   ;;
        *)
 	   backup_database
-- 
2.35.2

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH 4/6] lib/open: refactor call to mkdir into function
  2022-07-29 12:19 v1 Create database directory David Bremner
                   ` (2 preceding siblings ...)
  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 ` 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 ` [PATCH 6/6] lib/open: create database path in some cases David Bremner
  5 siblings, 0 replies; 8+ messages in thread
From: David Bremner @ 2022-07-29 12:19 UTC (permalink / raw)
  To: notmuch; +Cc: spwhitton

This makes the error handling available for re-use. Using
g_mkdir_with_parents also handles the case of a pre-existing
directory. This introduces new functionality, namely creating the
parent directories, which will be useful for creating directories like
'.local/share/notmuch/default'.
---
 lib/open.cc | 25 +++++++++++++++----------
 1 file changed, 15 insertions(+), 10 deletions(-)

diff --git a/lib/open.cc b/lib/open.cc
index 30cfcf9e..85e46dc7 100644
--- a/lib/open.cc
+++ b/lib/open.cc
@@ -244,6 +244,18 @@ _choose_database_path (notmuch_database_t *notmuch,
     return NOTMUCH_STATUS_SUCCESS;
 }
 
+static notmuch_status_t
+_mkdir (const char *path, char **message)
+{
+    if (g_mkdir_with_parents (path, 0755)) {
+	IGNORE_RESULT (asprintf (message, "Error: Cannot create directory %s: %s.\n",
+				 path, strerror (errno)));
+	return NOTMUCH_STATUS_FILE_ERROR;
+    }
+    return NOTMUCH_STATUS_SUCCESS;
+}
+
+
 static notmuch_database_t *
 _alloc_notmuch (const char *database_path, const char *config_path, const char *profile)
 {
@@ -607,7 +619,6 @@ notmuch_database_create_with_config (const char *database_path,
     const char *notmuch_path = NULL;
     char *message = NULL;
     GKeyFile *key_file = NULL;
-    int err;
 
     _notmuch_init ();
 
@@ -653,15 +664,9 @@ notmuch_database_create_with_config (const char *database_path,
 	    goto DONE;
 	}
 
-	err = mkdir (notmuch_path, 0755);
-	if (err) {
-	    if (errno != EEXIST) {
-		IGNORE_RESULT (asprintf (&message, "Error: Cannot create directory %s: %s.\n",
-					 notmuch_path, strerror (errno)));
-		status = NOTMUCH_STATUS_FILE_ERROR;
-		goto DONE;
-	    }
-	}
+	status = _mkdir (notmuch_path, &message);
+	if (status)
+	    goto DONE;
     }
 
     if (! (notmuch->xapian_path = talloc_asprintf (notmuch, "%s/%s", notmuch_path, "xapian"))) {
-- 
2.35.2

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH 5/6] lib/open: return non-SUCCESS on missing database path
  2022-07-29 12:19 v1 Create database directory David Bremner
                   ` (3 preceding siblings ...)
  2022-07-29 12:19 ` [PATCH 4/6] lib/open: refactor call to mkdir into function David Bremner
@ 2022-07-29 12:20 ` David Bremner
  2022-07-29 12:20 ` [PATCH 6/6] lib/open: create database path in some cases David Bremner
  5 siblings, 0 replies; 8+ messages in thread
From: David Bremner @ 2022-07-29 12:20 UTC (permalink / raw)
  To: notmuch; +Cc: spwhitton

This simplifies the logic of creating the directory path when it doesn't
exist.
---
 lib/open.cc            | 19 +++++++++++--------
 test/T560-lib-error.sh |  4 ++--
 2 files changed, 13 insertions(+), 10 deletions(-)

diff --git a/lib/open.cc b/lib/open.cc
index 85e46dc7..bc450555 100644
--- a/lib/open.cc
+++ b/lib/open.cc
@@ -192,6 +192,8 @@ _choose_database_path (notmuch_database_t *notmuch,
 		       const char **database_path,
 		       char **message)
 {
+    notmuch_status_t status;
+
     if (! *database_path) {
 	*database_path = getenv ("NOTMUCH_DATABASE");
     }
@@ -207,8 +209,6 @@ _choose_database_path (notmuch_database_t *notmuch,
 	}
     }
     if (! *database_path) {
-	notmuch_status_t status;
-
 	*database_path = _xdg_dir (notmuch, "XDG_DATA_HOME", ".local/share", profile);
 	status = _db_dir_exists (*database_path, message);
 	if (status) {
@@ -223,8 +223,6 @@ _choose_database_path (notmuch_database_t *notmuch,
     }
 
     if (! *database_path) {
-	notmuch_status_t status;
-
 	*database_path = talloc_asprintf (notmuch, "%s/mail", getenv ("HOME"));
 	status = _db_dir_exists (*database_path, message);
 	if (status) {
@@ -241,6 +239,15 @@ _choose_database_path (notmuch_database_t *notmuch,
 	*message = strdup ("Error: Database path must be absolute.\n");
 	return NOTMUCH_STATUS_PATH_ERROR;
     }
+
+    status = _db_dir_exists (*database_path, message);
+    if (status) {
+	IGNORE_RESULT (asprintf (message,
+				 "Error: database path '%s' does not exist or is not a directory.\n",
+				 *database_path));
+	return NOTMUCH_STATUS_NO_DATABASE;
+    }
+
     return NOTMUCH_STATUS_SUCCESS;
 }
 
@@ -638,10 +645,6 @@ notmuch_database_create_with_config (const char *database_path,
 					 &database_path, &message)))
 	goto DONE;
 
-    status = _db_dir_exists (database_path, &message);
-    if (status)
-	goto DONE;
-
     _set_database_path (notmuch, database_path);
 
     if (key_file && ! (notmuch->params & NOTMUCH_PARAM_SPLIT)) {
diff --git a/test/T560-lib-error.sh b/test/T560-lib-error.sh
index a2901ff6..30cce943 100755
--- a/test/T560-lib-error.sh
+++ b/test/T560-lib-error.sh
@@ -91,7 +91,7 @@ EOF
 cat <<'EOF' >EXPECTED
 == stdout ==
 == stderr ==
-Error: Cannot open database at CWD/nonexistent/foo: No such file or directory.
+Error: database path 'CWD/nonexistent/foo' does not exist or is not a directory.
 EOF
 test_expect_equal_file EXPECTED OUTPUT
 
@@ -132,7 +132,7 @@ EOF
 cat <<'EOF' >EXPECTED
 == stdout ==
 == stderr ==
-Error: Cannot open database at CWD/nonexistent/foo: No such file or directory.
+Error: database path 'CWD/nonexistent/foo' does not exist or is not a directory.
 EOF
 test_expect_equal_file EXPECTED OUTPUT
 
-- 
2.35.2

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH 6/6] lib/open: create database path in some cases
  2022-07-29 12:19 v1 Create database directory David Bremner
                   ` (4 preceding siblings ...)
  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
  5 siblings, 0 replies; 8+ messages in thread
From: David Bremner @ 2022-07-29 12:20 UTC (permalink / raw)
  To: notmuch; +Cc: spwhitton

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

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 1/6] tests: remove dead code from T055-path-config.sh
  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
  0 siblings, 0 replies; 8+ messages in thread
From: David Bremner @ 2022-08-06 12:19 UTC (permalink / raw)
  To: notmuch; +Cc: spwhitton

David Bremner <david@tethera.net> writes:

> This case statement does nothing.
> ---
>  test/T055-path-config.sh | 3 ---
>  1 file changed, 3 deletions(-)

applied to master.

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2022-08-06 12:20 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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-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 ` [PATCH 6/6] lib/open: create database path in some cases David Bremner

Code repositories for project(s) associated with this inbox:

	notmuch.git.git (no URL configured)

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).