lib/open: use db struct as talloc ctx for choose_database_path

The extra talloc struct "local" was left over from before the notmuch
struct was allocated earlier. Having the notmuch struct available in
this function will allow more flexibility to track the configuration
variations (e.g. split vs. non-split).
This commit is contained in:
David Bremner 2021-12-25 09:33:02 -04:00
parent 237f803fdb
commit fd0edeb561

View file

@ -186,7 +186,7 @@ _db_dir_exists (const char *database_path, char **message)
} }
static notmuch_status_t static notmuch_status_t
_choose_database_path (void *ctx, _choose_database_path (notmuch_database_t *notmuch,
const char *profile, const char *profile,
GKeyFile *key_file, GKeyFile *key_file,
const char **database_path, const char **database_path,
@ -201,16 +201,16 @@ _choose_database_path (void *ctx,
char *path = g_key_file_get_string (key_file, "database", "path", NULL); char *path = g_key_file_get_string (key_file, "database", "path", NULL);
if (path) { if (path) {
if (path[0] == '/') if (path[0] == '/')
*database_path = talloc_strdup (ctx, path); *database_path = talloc_strdup (notmuch, path);
else else
*database_path = talloc_asprintf (ctx, "%s/%s", getenv ("HOME"), path); *database_path = talloc_asprintf (notmuch, "%s/%s", getenv ("HOME"), path);
g_free (path); g_free (path);
} }
} }
if (! *database_path) { if (! *database_path) {
notmuch_status_t status; notmuch_status_t status;
*database_path = _xdg_dir (ctx, "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) {
*database_path = NULL; *database_path = NULL;
@ -226,7 +226,7 @@ _choose_database_path (void *ctx,
if (! *database_path) { if (! *database_path) {
notmuch_status_t status; notmuch_status_t status;
*database_path = talloc_asprintf (ctx, "%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) {
*database_path = NULL; *database_path = NULL;
@ -510,7 +510,6 @@ notmuch_database_open_with_config (const char *database_path,
char **status_string) char **status_string)
{ {
notmuch_status_t status = NOTMUCH_STATUS_SUCCESS; notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
void *local = talloc_new (NULL);
notmuch_database_t *notmuch = NULL; notmuch_database_t *notmuch = NULL;
char *message = NULL; char *message = NULL;
GKeyFile *key_file = NULL; GKeyFile *key_file = NULL;
@ -530,7 +529,7 @@ notmuch_database_open_with_config (const char *database_path,
goto DONE; goto DONE;
} }
if ((status = _choose_database_path (local, profile, key_file, if ((status = _choose_database_path (notmuch, profile, key_file,
&database_path, &split, &database_path, &split,
&message))) &message)))
goto DONE; goto DONE;
@ -549,8 +548,6 @@ notmuch_database_open_with_config (const char *database_path,
status = _finish_open (notmuch, profile, mode, key_file, &message); status = _finish_open (notmuch, profile, mode, key_file, &message);
DONE: DONE:
talloc_free (local);
if (key_file) if (key_file)
g_key_file_free (key_file); g_key_file_free (key_file);
@ -612,7 +609,6 @@ notmuch_database_create_with_config (const char *database_path,
const char *notmuch_path = NULL; const char *notmuch_path = NULL;
char *message = NULL; char *message = NULL;
GKeyFile *key_file = NULL; GKeyFile *key_file = NULL;
void *local = talloc_new (NULL);
int err; int err;
bool split = false; bool split = false;
@ -630,7 +626,7 @@ notmuch_database_create_with_config (const char *database_path,
goto DONE; goto DONE;
} }
if ((status = _choose_database_path (local, profile, key_file, if ((status = _choose_database_path (notmuch, profile, key_file,
&database_path, &split, &message))) &database_path, &split, &message)))
goto DONE; goto DONE;
@ -654,7 +650,7 @@ notmuch_database_create_with_config (const char *database_path,
if (split) { if (split) {
notmuch_path = database_path; notmuch_path = database_path;
} else { } else {
if (! (notmuch_path = talloc_asprintf (local, "%s/%s", database_path, ".notmuch"))) { if (! (notmuch_path = talloc_asprintf (notmuch, "%s/%s", database_path, ".notmuch"))) {
status = NOTMUCH_STATUS_OUT_OF_MEMORY; status = NOTMUCH_STATUS_OUT_OF_MEMORY;
goto DONE; goto DONE;
} }
@ -707,8 +703,6 @@ notmuch_database_create_with_config (const char *database_path,
} }
DONE: DONE:
talloc_free (local);
if (key_file) if (key_file)
g_key_file_free (key_file); g_key_file_free (key_file);
@ -808,7 +802,6 @@ notmuch_database_load_config (const char *database_path,
char **status_string) char **status_string)
{ {
notmuch_status_t status = NOTMUCH_STATUS_SUCCESS, warning = NOTMUCH_STATUS_SUCCESS; notmuch_status_t status = NOTMUCH_STATUS_SUCCESS, warning = NOTMUCH_STATUS_SUCCESS;
void *local = talloc_new (NULL);
notmuch_database_t *notmuch = NULL; notmuch_database_t *notmuch = NULL;
char *message = NULL; char *message = NULL;
GKeyFile *key_file = NULL; GKeyFile *key_file = NULL;
@ -834,7 +827,7 @@ notmuch_database_load_config (const char *database_path,
goto DONE; goto DONE;
} }
status = _choose_database_path (local, profile, key_file, status = _choose_database_path (notmuch, profile, key_file,
&database_path, &split, &message); &database_path, &split, &message);
switch (status) { switch (status) {
case NOTMUCH_STATUS_NO_DATABASE: case NOTMUCH_STATUS_NO_DATABASE:
@ -870,8 +863,6 @@ notmuch_database_load_config (const char *database_path,
goto DONE; goto DONE;
DONE: DONE:
talloc_free (local);
if (status_string) if (status_string)
*status_string = message; *status_string = message;