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 mLF3FjlIHWBHQwAA0tVLHw (envelope-from ) for ; Fri, 05 Feb 2021 13:29:29 +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 iJNPEjlIHWBfJQAA1q6Kng (envelope-from ) for ; Fri, 05 Feb 2021 13:29:29 +0000 Received: from mail.notmuchmail.org (nmbug.tethera.net [144.217.243.247]) (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 ADC2C9403D6 for ; Fri, 5 Feb 2021 13:29:28 +0000 (UTC) Received: from nmbug.tethera.net (localhost [127.0.0.1]) by mail.notmuchmail.org (Postfix) with ESMTP id 94E791FFC1; Fri, 5 Feb 2021 08:27:58 -0500 (EST) Received: from fethera.tethera.net (fethera.tethera.net [IPv6:2607:5300:60:c5::1]) by mail.notmuchmail.org (Postfix) with ESMTP id 14AB11FBC5 for ; Fri, 5 Feb 2021 08:27:24 -0500 (EST) Received: by fethera.tethera.net (Postfix, from userid 1001) id 0D1D66067F; Fri, 5 Feb 2021 08:27:24 -0500 (EST) Received: (nullmailer pid 3258439 invoked by uid 1000); Fri, 05 Feb 2021 13:27:02 -0000 From: David Bremner To: notmuch@notmuchmail.org Cc: David Bremner Subject: [PATCH 32/39] lib: introduce notmuch_database_create_with_config Date: Fri, 5 Feb 2021 09:26:47 -0400 Message-Id: <20210205132654.3258292-33-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: EICVWK6ZFXZDRI4FFN34RKMCUONWE3N7 X-Message-ID-Hash: EICVWK6ZFXZDRI4FFN34RKMCUONWE3N7 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: -1.04 Authentication-Results: aspmx1.migadu.com; dkim=none; dmarc=none; spf=pass (aspmx1.migadu.com: domain of notmuch-bounces@notmuchmail.org designates 144.217.243.247 as permitted sender) smtp.mailfrom=notmuch-bounces@notmuchmail.org X-Migadu-Queue-Id: ADC2C9403D6 X-Spam-Score: -1.04 X-Migadu-Scanner: scn0.migadu.com X-TUID: Z9cLs3RzvLNF 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 | 27 +++++++++++ lib/open.cc | 50 ++++++++++++--------- test/T560-lib-error.sh | 2 +- 4 files changed, 58 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 6d22d328..5e07a01a 100644 --- a/lib/notmuch.h +++ b/lib/notmuch.h @@ -442,6 +442,33 @@ 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'. + * + * For description of arguments, @see notmuch_database_open_with_config + * + * @retval NOTMUCH_STATUS_SUCCESS: Successfully created the database. + * + * @retval NOTMUCH_STATUS_DATABASE_EXISTS: Database already exists, not created + * + * @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 6046868a..577fc88a 100644 --- a/lib/open.cc +++ b/lib/open.cc @@ -363,30 +363,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 = _choose_database_path (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; } @@ -394,25 +396,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.30.0