aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDavid Bremner <david@tethera.net>2022-07-29 08:31:37 -0300
committerDavid Bremner <david@tethera.net>2022-09-03 08:24:43 -0300
commit84e4e130e2c920b3dee91901582c4ab6276e2630 (patch)
tree7323895279f5fac2760306cfa9d76b17d86cebf5 /lib
parent8ba3057d01b11fb806581f8dc451a8891a4d4e0e (diff)
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.
Diffstat (limited to 'lib')
-rw-r--r--lib/open.cc53
1 files changed, 51 insertions, 2 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);