From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp0 ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms11 with LMTPS id YD8kKuJHHWBHQwAA0tVLHw (envelope-from ) for ; Fri, 05 Feb 2021 13:28:02 +0000 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp0 with LMTPS id UASkJeJHHWBnGQAA1q6Kng (envelope-from ) for ; Fri, 05 Feb 2021 13:28:02 +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) server-signature RSA-PSS (2048 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 37A4D9403CD for ; Fri, 5 Feb 2021 13:28:02 +0000 (UTC) Received: from nmbug.tethera.net (localhost [127.0.0.1]) by mail.notmuchmail.org (Postfix) with ESMTP id B55A61FBD0; Fri, 5 Feb 2021 08:27:22 -0500 (EST) Received: from fethera.tethera.net (fethera.tethera.net [198.245.60.197]) by mail.notmuchmail.org (Postfix) with ESMTP id 22F3A1FBB1 for ; Fri, 5 Feb 2021 08:27:10 -0500 (EST) Received: by fethera.tethera.net (Postfix, from userid 1001) id 1B4CA6067F; Fri, 5 Feb 2021 08:27:10 -0500 (EST) Received: (nullmailer pid 3258431 invoked by uid 1000); Fri, 05 Feb 2021 13:27:02 -0000 From: David Bremner To: notmuch@notmuchmail.org Cc: David Bremner Subject: [PATCH 28/39] lib/open: factor out choosing database path Date: Fri, 5 Feb 2021 09:26:43 -0400 Message-Id: <20210205132654.3258292-29-david@tethera.net> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210205132654.3258292-1-david@tethera.net> References: <20210205132654.3258292-1-david@tethera.net> MIME-Version: 1.0 Message-ID-Hash: 3P3Y6OXZLG4HQGWPYBHLJKRROOSRMMD2 X-Message-ID-Hash: 3P3Y6OXZLG4HQGWPYBHLJKRROOSRMMD2 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.83 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: 37A4D9403CD X-Spam-Score: -0.83 X-Migadu-Scanner: scn1.migadu.com X-TUID: xMZymhW1q/ez The plan is to share code with a new database creation function that has a similar API to n_d_open_with_config. --- lib/open.cc | 50 +++++++++++++++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 19 deletions(-) diff --git a/lib/open.cc b/lib/open.cc index ad74ccd6..6e8e0f6f 100644 --- a/lib/open.cc +++ b/lib/open.cc @@ -114,6 +114,36 @@ DONE: return status; } +static notmuch_status_t +_choose_database_path (const char *config_path, + const char *profile, + GKeyFile **key_file, + const char **database_path, + 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 && *key_file) + *database_path = g_key_file_get_value (*key_file, "database", "path", NULL); + + if (*database_path == NULL) { + *message = strdup ("Error: Cannot open a database for a NULL path.\n"); + return NOTMUCH_STATUS_NULL_POINTER; + } + + if (*database_path[0] != '/') { + *message = strdup ("Error: Database path must be absolute.\n"); + return NOTMUCH_STATUS_PATH_ERROR; + } + return NOTMUCH_STATUS_SUCCESS; +} + notmuch_status_t notmuch_database_open_with_config (const char *database_path, notmuch_database_mode_t mode, @@ -133,26 +163,8 @@ notmuch_database_open_with_config (const char *database_path, GKeyFile *key_file = NULL; static int initialized = 0; - status = _load_key_file (config_path, profile, &key_file); - if (status) { - message = strdup ("Error: cannot load config file"); - goto DONE; - } - - if (! database_path && key_file) - database_path = g_key_file_get_value (key_file, "database", "path", NULL); - - if (database_path == NULL) { - message = strdup ("Error: Cannot open a database for a NULL path.\n"); - status = NOTMUCH_STATUS_NULL_POINTER; + if ((status = _choose_database_path (config_path, profile, &key_file, &database_path, &message))) goto DONE; - } - - if (database_path[0] != '/') { - message = strdup ("Error: Database path must be absolute.\n"); - status = NOTMUCH_STATUS_PATH_ERROR; - goto DONE; - } if (! (notmuch_path = talloc_asprintf (local, "%s/%s", database_path, ".notmuch"))) { message = strdup ("Out of memory\n"); -- 2.30.0