]> git.notmuchmail.org Git - notmuch/commitdiff
lib/open: pull _load_key_file out of _choose_database_path
authorDavid Bremner <david@tethera.net>
Thu, 11 Feb 2021 00:02:37 +0000 (20:02 -0400)
committerDavid Bremner <david@tethera.net>
Sat, 27 Mar 2021 12:26:14 +0000 (09:26 -0300)
Although this increases code duplication, it also increases
flexibility in handling various combinations of missing config file
and missing database.

lib/open.cc

index dc191d64aad0489890aed9bc0af10fd50f7ea8a5..9bb313dadbcf7679e9a8ef37cc014f583afe09a4 100644 (file)
@@ -183,27 +183,18 @@ _db_dir_exists (const char *database_path, char **message)
 
 static notmuch_status_t
 _choose_database_path (void *ctx,
-                      const char *config_path,
                       const char *profile,
-                      GKeyFile **key_file,
+                      GKeyFile *key_file,
                       const char **database_path,
                       bool *split,
                       char **message)
 {
-    notmuch_status_t status;
-
-    status = _load_key_file (config_path, profile, key_file);
-    if (status) {
-       *message = strdup ("Error: cannot load config file.\n");
-       return status;
-    }
-
     if (! *database_path) {
        *database_path = getenv ("NOTMUCH_DATABASE");
     }
 
-    if (! *database_path && *key_file) {
-       char *path = g_key_file_get_value (*key_file, "database", "path", NULL);
+    if (! *database_path && key_file) {
+       char *path = g_key_file_get_value (key_file, "database", "path", NULL);
        if (path) {
            *database_path = talloc_strdup (ctx, path);
            g_free (path);
@@ -500,8 +491,14 @@ notmuch_database_open_with_config (const char *database_path,
        goto DONE;
     }
 
-    if ((status = _choose_database_path (local, config_path, profile,
-                                        &key_file, &database_path, &split,
+    status = _load_key_file (config_path, profile, &key_file);
+    if (status) {
+       message = strdup ("Error: cannot load config file.\n");
+       goto DONE;
+    }
+
+    if ((status = _choose_database_path (local, profile, key_file,
+                                        &database_path, &split,
                                         &message)))
        goto DONE;
 
@@ -591,11 +588,14 @@ notmuch_database_create_with_config (const char *database_path,
        goto DONE;
     }
 
-    _init_libs ();
+    status = _load_key_file (config_path, profile, &key_file);
+    if (status) {
+       message = strdup ("Error: cannot load config file.\n");
+       goto DONE;
+    }
 
-    if ((status = _choose_database_path (local, config_path, profile,
-                                        &key_file, &database_path, &split,
-                                        &message)))
+    if ((status = _choose_database_path (local, profile, key_file,
+                                        &database_path, &split, &message)))
        goto DONE;
 
     status = _db_dir_exists (database_path, &message);