lib/open: return non-SUCCESS on missing database path

This simplifies the logic of creating the directory path when it doesn't
exist.
This commit is contained in:
David Bremner 2022-07-29 08:23:11 -03:00
parent 25e2790e30
commit 8ba3057d01
2 changed files with 13 additions and 10 deletions

View file

@ -192,6 +192,8 @@ _choose_database_path (notmuch_database_t *notmuch,
const char **database_path, const char **database_path,
char **message) char **message)
{ {
notmuch_status_t status;
if (! *database_path) { if (! *database_path) {
*database_path = getenv ("NOTMUCH_DATABASE"); *database_path = getenv ("NOTMUCH_DATABASE");
} }
@ -207,8 +209,6 @@ _choose_database_path (notmuch_database_t *notmuch,
} }
} }
if (! *database_path) { if (! *database_path) {
notmuch_status_t status;
*database_path = _xdg_dir (notmuch, "XDG_DATA_HOME", ".local/share", profile); *database_path = _xdg_dir (notmuch, "XDG_DATA_HOME", ".local/share", profile);
status = _db_dir_exists (*database_path, message); status = _db_dir_exists (*database_path, message);
if (status) { if (status) {
@ -223,8 +223,6 @@ _choose_database_path (notmuch_database_t *notmuch,
} }
if (! *database_path) { if (! *database_path) {
notmuch_status_t status;
*database_path = talloc_asprintf (notmuch, "%s/mail", getenv ("HOME")); *database_path = talloc_asprintf (notmuch, "%s/mail", getenv ("HOME"));
status = _db_dir_exists (*database_path, message); status = _db_dir_exists (*database_path, message);
if (status) { if (status) {
@ -241,6 +239,15 @@ _choose_database_path (notmuch_database_t *notmuch,
*message = strdup ("Error: Database path must be absolute.\n"); *message = strdup ("Error: Database path must be absolute.\n");
return NOTMUCH_STATUS_PATH_ERROR; 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; return NOTMUCH_STATUS_SUCCESS;
} }
@ -638,10 +645,6 @@ notmuch_database_create_with_config (const char *database_path,
&database_path, &message))) &database_path, &message)))
goto DONE; goto DONE;
status = _db_dir_exists (database_path, &message);
if (status)
goto DONE;
_set_database_path (notmuch, database_path); _set_database_path (notmuch, database_path);
if (key_file && ! (notmuch->params & NOTMUCH_PARAM_SPLIT)) { if (key_file && ! (notmuch->params & NOTMUCH_PARAM_SPLIT)) {

View file

@ -91,7 +91,7 @@ EOF
cat <<'EOF' >EXPECTED cat <<'EOF' >EXPECTED
== stdout == == stdout ==
== stderr == == 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 EOF
test_expect_equal_file EXPECTED OUTPUT test_expect_equal_file EXPECTED OUTPUT
@ -132,7 +132,7 @@ EOF
cat <<'EOF' >EXPECTED cat <<'EOF' >EXPECTED
== stdout == == stdout ==
== stderr == == 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 EOF
test_expect_equal_file EXPECTED OUTPUT test_expect_equal_file EXPECTED OUTPUT