From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp2 ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms11 with LMTPS id 3MFCImJV8l9hDQAA0tVLHw (envelope-from ) for ; Sun, 03 Jan 2021 23:38:10 +0000 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp2 with LMTPS id uCnEHWJV8l8MUgAAB5/wlQ (envelope-from ) for ; Sun, 03 Jan 2021 23:38:10 +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 CCA3E9403E6 for ; Sun, 3 Jan 2021 23:38:09 +0000 (UTC) Received: from nmbug.tethera.net (localhost [127.0.0.1]) by mail.notmuchmail.org (Postfix) with ESMTP id 99B7529E26; Sun, 3 Jan 2021 18:36:58 -0500 (EST) Received: from fethera.tethera.net (fethera.tethera.net [198.245.60.197]) by mail.notmuchmail.org (Postfix) with ESMTP id D7C5429D74 for ; Sun, 3 Jan 2021 18:36:19 -0500 (EST) Received: by fethera.tethera.net (Postfix, from userid 1001) id AD6635FF47; Sun, 3 Jan 2021 18:36:19 -0500 (EST) Received: (nullmailer pid 126262 invoked by uid 1000); Sun, 03 Jan 2021 23:35:56 -0000 From: David Bremner To: notmuch@notmuchmail.org Cc: David Bremner Subject: [PATCH 34/36] lib: introduce notmuch_database_create_with_config Date: Sun, 3 Jan 2021 19:35:45 -0400 Message-Id: <20210103233547.122707-35-david@tethera.net> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210103233547.122707-1-david@tethera.net> References: <20210103233547.122707-1-david@tethera.net> MIME-Version: 1.0 Message-ID-Hash: 5BZHJQ6YBAV44ZXKFKUQEZJX3WJKCW6W X-Message-ID-Hash: 5BZHJQ6YBAV44ZXKFKUQEZJX3WJKCW6W 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.88 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: CCA3E9403E6 X-Spam-Score: -0.88 X-Migadu-Scanner: scn1.migadu.com X-TUID: kGpUC9OpZg/h This takes a config path parameter, and can use that to decide the new database location. --- bindings/python-cffi/tests/test_database.py | 2 +- lib/notmuch.h | 28 ++++++++++++ lib/open.cc | 50 ++++++++++++--------- test/T560-lib-error.sh | 2 +- 4 files changed, 59 insertions(+), 23 deletions(-) diff --git a/bindings/python-cffi/tests/test_database.py b/bindings/python-cffi/tests/test_database.py index a2c69de6..9b3219c0 100644 --- a/bindings/python-cffi/tests/test_database.py +++ b/bindings/python-cffi/tests/test_database.py @@ -80,7 +80,7 @@ class TestCreate: db.create(tmppath) def test_create_existing(self, tmppath, db): - with pytest.raises(errors.FileError): + with pytest.raises(errors.DatabaseExistsError): dbmod.Database.create(path=tmppath) def test_close(self, db): diff --git a/lib/notmuch.h b/lib/notmuch.h index ac055193..e51b738d 100644 --- a/lib/notmuch.h +++ b/lib/notmuch.h @@ -442,6 +442,34 @@ notmuch_database_open_with_config (const char *database_path, const char *profile, notmuch_database_t **database, char **error_message); +/** + * Create a new notmuch database located at 'database_path', using + * configuration in 'config_path'. + * + * @param[in,out] is_new if NULL consider an existing database as an + * error. Otherwise report whether actually created. + * + * For description of other arguments, see notmuch_database_open_with_config + * + * @retval NOTMUCH_STATUS_SUCCESS: Successfully created the database. + * + * @retval NOTMUCH_STATUS_OUT_OF_MEMORY: Out of memory. + * + * @retval NOTMUCH_STATUS_FILE_ERROR: An error occurred trying to open the + * database or config file (such as permission denied, or file not found, + * etc.) + * + * @retval NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred. + * + * @since libnotmuch 5.4 (notmuch 0.32) + */ + +notmuch_status_t +notmuch_database_create_with_config (const char *database_path, + const char *config_path, + const char *profile, + notmuch_database_t **database, + char **error_message); /** * Retrieve last status string for given database. diff --git a/lib/open.cc b/lib/open.cc index 0f392ab9..92661271 100644 --- a/lib/open.cc +++ b/lib/open.cc @@ -362,30 +362,32 @@ notmuch_status_t notmuch_database_create_verbose (const char *path, notmuch_database_t **database, char **status_string) +{ + return notmuch_database_create_with_config (path, "", NULL, database, status_string); +} + +notmuch_status_t +notmuch_database_create_with_config (const char *database_path, + const char *config_path, + const char *profile, + notmuch_database_t **database, + char **status_string) { notmuch_status_t status = NOTMUCH_STATUS_SUCCESS; notmuch_database_t *notmuch = NULL; char *notmuch_path = NULL; char *message = NULL; + GKeyFile *key_file = NULL; struct stat st; int err; - if (path == NULL) { - message = strdup ("Error: Cannot create a database for a NULL path.\n"); - status = NOTMUCH_STATUS_NULL_POINTER; - goto DONE; - } - - if (path[0] != '/') { - message = strdup ("Error: Database path must be absolute.\n"); - status = NOTMUCH_STATUS_PATH_ERROR; + if ((status = _pre_open (config_path, profile, &key_file, &database_path, &message))) goto DONE; - } - err = stat (path, &st); + err = stat (database_path, &st); if (err) { IGNORE_RESULT (asprintf (&message, "Error: Cannot create database at %s: %s.\n", - path, strerror (errno))); + database_path, strerror (errno))); status = NOTMUCH_STATUS_FILE_ERROR; goto DONE; } @@ -393,25 +395,31 @@ notmuch_database_create_verbose (const char *path, if (! S_ISDIR (st.st_mode)) { IGNORE_RESULT (asprintf (&message, "Error: Cannot create database at %s: " "Not a directory.\n", - path)); + database_path)); status = NOTMUCH_STATUS_FILE_ERROR; goto DONE; } - notmuch_path = talloc_asprintf (NULL, "%s/%s", path, ".notmuch"); + notmuch_path = talloc_asprintf (NULL, "%s/%s", database_path, ".notmuch"); err = mkdir (notmuch_path, 0755); - if (err) { - IGNORE_RESULT (asprintf (&message, "Error: Cannot create directory %s: %s.\n", - notmuch_path, strerror (errno))); - status = NOTMUCH_STATUS_FILE_ERROR; + if (errno == EEXIST) { + status = NOTMUCH_STATUS_DATABASE_EXISTS; + } else { + IGNORE_RESULT (asprintf (&message, "Error: Cannot create directory %s: %s.\n", + notmuch_path, strerror (errno))); + status = NOTMUCH_STATUS_FILE_ERROR; + } goto DONE; } - status = notmuch_database_open_verbose (path, - NOTMUCH_DATABASE_MODE_READ_WRITE, - ¬much, &message); + /* 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, + ¬much, &message); if (status) goto DONE; diff --git a/test/T560-lib-error.sh b/test/T560-lib-error.sh index 260ac120..ade376ef 100755 --- a/test/T560-lib-error.sh +++ b/test/T560-lib-error.sh @@ -93,7 +93,7 @@ EOF cat <<'EOF' >EXPECTED == stdout == == stderr == -Error: Cannot create a database for a NULL path. +Error: Cannot open a database for a NULL path. EOF test_expect_equal_file EXPECTED OUTPUT -- 2.29.2