[PATCH 11/23] lib/open: use _finish_open in n_d_create_with_config

Subject: [PATCH 11/23] lib/open: use _finish_open in n_d_create_with_config

Date: Sun, 7 Feb 2021 20:40:57 -0400

To: notmuch@notmuchmail.org

Cc: David Bremner

From: David Bremner


This avoids reading the configuration file twice.
---
 lib/open.cc | 34 +++++++++++++++++++++++++---------
 1 file changed, 25 insertions(+), 9 deletions(-)

diff --git a/lib/open.cc b/lib/open.cc
index 74fac37d..f4690763 100644
--- a/lib/open.cc
+++ b/lib/open.cc
@@ -459,14 +459,25 @@ notmuch_database_create_with_config (const char *database_path,
     notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
     notmuch_database_t *notmuch = NULL;
     char *notmuch_path = NULL;
+    char *xapian_path = NULL;
     char *message = NULL;
     GKeyFile *key_file = NULL;
     struct stat st;
     int err;
+    void *local = talloc_new (NULL);
+
+    notmuch = _alloc_notmuch ();
+    if (! notmuch) {
+	status = NOTMUCH_STATUS_OUT_OF_MEMORY;
+	goto DONE;
+    }
 
     if ((status = _choose_database_path (config_path, profile, &key_file, &database_path, &message)))
 	goto DONE;
 
+    notmuch->path = talloc_strdup (notmuch, database_path);
+    strip_trailing (notmuch->path, '/');
+
     err = stat (database_path, &st);
     if (err) {
 	IGNORE_RESULT (asprintf (&message, "Error: Cannot create database at %s: %s.\n",
@@ -483,12 +494,14 @@ notmuch_database_create_with_config (const char *database_path,
 	goto DONE;
     }
 
-    notmuch_path = talloc_asprintf (NULL, "%s/%s", database_path, ".notmuch");
+    notmuch_path = talloc_asprintf (local, "%s/%s", database_path, ".notmuch");
 
     err = mkdir (notmuch_path, 0755);
     if (err) {
 	if (errno == EEXIST) {
 	    status = NOTMUCH_STATUS_DATABASE_EXISTS;
+	    talloc_free (notmuch);
+	    notmuch = NULL;
 	} else {
 	    IGNORE_RESULT (asprintf (&message, "Error: Cannot create directory %s: %s.\n",
 				     notmuch_path, strerror (errno)));
@@ -497,12 +510,16 @@ notmuch_database_create_with_config (const char *database_path,
 	goto DONE;
     }
 
-    /* XXX this reads the config file twice, which is a bit wasteful */
-    status = notmuch_database_open_with_config (database_path,
-						NOTMUCH_DATABASE_MODE_READ_WRITE,
-						config_path,
-						profile,
-						&notmuch, &message);
+    if (! (notmuch->xapian_path = talloc_asprintf (notmuch, "%s/%s", notmuch_path, "xapian"))) {
+	status = NOTMUCH_STATUS_OUT_OF_MEMORY;
+	goto DONE;
+    }
+
+    status = _finish_open (notmuch,
+			   profile,
+			   NOTMUCH_DATABASE_MODE_READ_WRITE,
+			   key_file,
+			   &message);
     if (status)
 	goto DONE;
 
@@ -519,8 +536,7 @@ notmuch_database_create_with_config (const char *database_path,
     }
 
   DONE:
-    if (notmuch_path)
-	talloc_free (notmuch_path);
+    talloc_free(local);
 
     if (message) {
 	if (status_string)
-- 
2.30.0
_______________________________________________
notmuch mailing list -- notmuch@notmuchmail.org
To unsubscribe send an email to notmuch-leave@notmuchmail.org

Thread: