lib/open: create database path in some cases

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.
This commit is contained in:
David Bremner 2022-07-29 08:31:37 -03:00
parent 8ba3057d01
commit 84e4e130e2
3 changed files with 57 additions and 7 deletions

View file

@ -262,6 +262,45 @@ _mkdir (const char *path, char **message)
return NOTMUCH_STATUS_SUCCESS; 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 * static notmuch_database_t *
_alloc_notmuch (const char *database_path, const char *config_path, const char *profile) _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; goto DONE;
} }
if ((status = _choose_database_path (notmuch, profile, key_file, 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))) &database_path, &message)))
goto DONE; goto DONE;
break;
default:
goto DONE;
}
_set_database_path (notmuch, database_path); _set_database_path (notmuch, database_path);

View file

@ -369,7 +369,6 @@ EOF
;& ;&
mailroot_only) mailroot_only)
test_begin_subtest "create database parent dir ($config)" test_begin_subtest "create database parent dir ($config)"
test_subtest_known_broken
rm -r ${DATABASE_PATH} rm -r ${DATABASE_PATH}
notmuch new notmuch new
test_expect_equal "$(xapian-metadata get ${XAPIAN_PATH} version)" 3 test_expect_equal "$(xapian-metadata get ${XAPIAN_PATH} version)" 3

View file

@ -102,16 +102,17 @@ test_C <<'EOF'
int main (int argc, char** argv) int main (int argc, char** argv)
{ {
notmuch_status_t stat; notmuch_status_t stat;
char *msg; char *msg = NULL;
stat = notmuch_database_create_with_config (NULL, "", NULL, NULL, &msg); stat = notmuch_database_create_with_config (NULL, "", NULL, NULL, &msg);
printf ("%s\n", notmuch_status_to_string (stat));
if (msg) fputs (msg, stderr); if (msg) fputs (msg, stderr);
} }
EOF EOF
cat <<'EOF' >EXPECTED cat <<'EOF' >EXPECTED
== stdout == == stdout ==
No mail root found
== stderr == == stderr ==
Error: could not locate database.
EOF EOF
test_expect_equal_file EXPECTED OUTPUT test_expect_equal_file EXPECTED OUTPUT
@ -123,16 +124,17 @@ int main (int argc, char** argv)
{ {
notmuch_database_t *db; notmuch_database_t *db;
notmuch_status_t stat; notmuch_status_t stat;
char *msg; char *msg = NULL;
stat = notmuch_database_create_with_config (argv[1], "", NULL, &db, &msg); stat = notmuch_database_create_with_config (argv[1], "", NULL, &db, &msg);
printf ("%d\n", stat == NOTMUCH_STATUS_SUCCESS);
if (msg) fputs (msg, stderr); if (msg) fputs (msg, stderr);
} }
EOF EOF
cat <<'EOF' >EXPECTED cat <<'EOF' >EXPECTED
== stdout == == stdout ==
1
== stderr == == stderr ==
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