From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp2 ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms11 with LMTPS id 9FfoA8x4LWAPHgAA0tVLHw (envelope-from ) for ; Wed, 17 Feb 2021 20:13:00 +0000 Received: from aspmx1.migadu.com ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp2 with LMTPS id CBu2Ost4LWDvZAAAB5/wlQ (envelope-from ) for ; Wed, 17 Feb 2021 20:12:59 +0000 Received: from mail.notmuchmail.org (nmbug.tethera.net [IPv6:2607:5300:201:3100::1657]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 9D3DC214D5 for ; Wed, 17 Feb 2021 21:12:59 +0100 (CET) Received: from nmbug.tethera.net (localhost [127.0.0.1]) by mail.notmuchmail.org (Postfix) with ESMTP id C8B142706D; Wed, 17 Feb 2021 15:11:42 -0500 (EST) Received: from fethera.tethera.net (fethera.tethera.net [IPv6:2607:5300:60:c5::1]) by mail.notmuchmail.org (Postfix) with ESMTP id 8AB6826ACB for ; Wed, 17 Feb 2021 15:11:10 -0500 (EST) Received: by fethera.tethera.net (Postfix, from userid 1001) id 83AF76067D; Wed, 17 Feb 2021 15:11:10 -0500 (EST) Received: (nullmailer pid 2021425 invoked by uid 1000); Wed, 17 Feb 2021 20:10:50 -0000 From: David Bremner To: notmuch@notmuchmail.org Cc: David Bremner Subject: [PATCH 10/21] lib/open: factor out the second half of n_d_open_with_config Date: Wed, 17 Feb 2021 16:10:34 -0400 Message-Id: <20210217201045.2021106-11-david@tethera.net> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210217201045.2021106-1-david@tethera.net> References: <20210217201045.2021106-1-david@tethera.net> MIME-Version: 1.0 Message-ID-Hash: TN7F6URJVEFJVJQPZOGDLR5GTI2PU7RU X-Message-ID-Hash: TN7F6URJVEFJVJQPZOGDLR5GTI2PU7RU X-MailFrom: bremner@tethera.net X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-notmuch.notmuchmail.org-0; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; suspicious-header X-Mailman-Version: 3.2.1 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Help: List-Post: List-Subscribe: List-Unsubscribe: Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Migadu-Flow: FLOW_IN X-Migadu-Spam-Score: 0.56 Authentication-Results: aspmx1.migadu.com; dkim=none; dmarc=none; spf=pass (aspmx1.migadu.com: domain of notmuch-bounces@notmuchmail.org designates 2607:5300:201:3100::1657 as permitted sender) smtp.mailfrom=notmuch-bounces@notmuchmail.org X-Migadu-Queue-Id: 9D3DC214D5 X-Spam-Score: 0.56 X-Migadu-Scanner: scn1.migadu.com X-TUID: /62YJ5V59Jel The idea is to allow reuse in n_d_create_with_config. This is primarily code movement, with some changes in error messages to reduce the number of input parameters. --- lib/open.cc | 115 +++++++++++++++++++++++++++++++--------------------- 1 file changed, 68 insertions(+), 47 deletions(-) diff --git a/lib/open.cc b/lib/open.cc index 6a5c99dd..d052f95d 100644 --- a/lib/open.cc +++ b/lib/open.cc @@ -249,54 +249,18 @@ static void _init_libs () { } } -notmuch_status_t -notmuch_database_open_with_config (const char *database_path, - notmuch_database_mode_t mode, - const char *config_path, - const char *profile, - notmuch_database_t **database, - char **status_string) +static notmuch_status_t +_finish_open (notmuch_database_t *notmuch, + const char *profile, + notmuch_database_mode_t mode, + GKeyFile *key_file, + char **message_ptr) { notmuch_status_t status = NOTMUCH_STATUS_SUCCESS; - void *local = talloc_new (NULL); - notmuch_database_t *notmuch = NULL; - char *notmuch_path, *incompat_features; + char *incompat_features; char *message = NULL; unsigned int version; - GKeyFile *key_file = NULL; - - _init_libs (); - - 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; - - _set_database_path (notmuch, database_path); - - status = _db_dir_exists (database_path, &message); - if (status) - goto DONE; - - if (! (notmuch_path = talloc_asprintf (local, "%s/%s", database_path, ".notmuch"))) { - message = strdup ("Out of memory\n"); - status = NOTMUCH_STATUS_OUT_OF_MEMORY; - goto DONE; - } - - status = _db_dir_exists (notmuch_path, &message); - if (status) - goto DONE; - - if (! (notmuch->xapian_path = talloc_asprintf (local, "%s/%s", notmuch_path, "xapian"))) { - message = strdup ("Out of memory\n"); - status = NOTMUCH_STATUS_OUT_OF_MEMORY; - goto DONE; - } + const char *database_path = notmuch_database_get_path (notmuch); try { std::string last_thread_id; @@ -319,7 +283,7 @@ notmuch_database_open_with_config (const char *database_path, "Error: Notmuch database at %s\n" " has a newer database format version (%u) than supported by this\n" " version of notmuch (%u).\n", - notmuch_path, version, NOTMUCH_DATABASE_VERSION)); + database_path, version, NOTMUCH_DATABASE_VERSION)); notmuch_database_destroy (notmuch); notmuch = NULL; status = NOTMUCH_STATUS_FILE_ERROR; @@ -329,7 +293,7 @@ notmuch_database_open_with_config (const char *database_path, /* Check features. */ incompat_features = NULL; notmuch->features = _notmuch_database_parse_features ( - local, notmuch->xapian_db->get_metadata ("features").c_str (), + notmuch, notmuch->xapian_db->get_metadata ("features").c_str (), version, mode == NOTMUCH_DATABASE_MODE_READ_WRITE ? 'w' : 'r', &incompat_features); if (incompat_features) { @@ -337,7 +301,7 @@ notmuch_database_open_with_config (const char *database_path, "Error: Notmuch database at %s\n" " requires features (%s)\n" " not supported by this version of notmuch.\n", - notmuch_path, incompat_features)); + database_path, incompat_features)); notmuch_database_destroy (notmuch); notmuch = NULL; status = NOTMUCH_STATUS_FILE_ERROR; @@ -415,6 +379,63 @@ notmuch_database_open_with_config (const char *database_path, notmuch = NULL; status = NOTMUCH_STATUS_XAPIAN_EXCEPTION; } + DONE: + if (message_ptr) + *message_ptr = message; + return status; +} + +notmuch_status_t +notmuch_database_open_with_config (const char *database_path, + notmuch_database_mode_t mode, + const char *config_path, + const char *profile, + notmuch_database_t **database, + char **status_string) +{ + notmuch_status_t status = NOTMUCH_STATUS_SUCCESS; + void *local = talloc_new (NULL); + notmuch_database_t *notmuch = NULL; + char *notmuch_path; + char *message = NULL; + struct stat st; + int err; + GKeyFile *key_file = NULL; + + _init_libs (); + + 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; + + _set_database_path (notmuch, database_path); + + status = _db_dir_exists (database_path, &message); + if (status) + goto DONE; + + if (! (notmuch_path = talloc_asprintf (local, "%s/%s", database_path, ".notmuch"))) { + message = strdup ("Out of memory\n"); + status = NOTMUCH_STATUS_OUT_OF_MEMORY; + goto DONE; + } + + status = _db_dir_exists (notmuch_path, &message); + if (status) + goto DONE; + + if (! (notmuch->xapian_path = talloc_asprintf (local, "%s/%s", notmuch_path, "xapian"))) { + message = strdup ("Out of memory\n"); + status = NOTMUCH_STATUS_OUT_OF_MEMORY; + goto DONE; + } + + status = _finish_open (notmuch, profile, mode, key_file, &message); DONE: talloc_free (local); -- 2.30.0