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 ms0.migadu.com with LMTPS id MKmpE/vUc2GYAQAAgWs5BA (envelope-from ) for ; Sat, 23 Oct 2021 11:25:15 +0200 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 WMpuD/vUc2HZfgAA1q6Kng (envelope-from ) for ; Sat, 23 Oct 2021 09:25:15 +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) server-digest SHA256) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id DBAAF4ED6 for ; Sat, 23 Oct 2021 11:25:14 +0200 (CEST) Received: from nmbug.tethera.net (localhost [127.0.0.1]) by mail.notmuchmail.org (Postfix) with ESMTP id 94CB12C17D; Sat, 23 Oct 2021 05:25:03 -0400 (EDT) Received: from fethera.tethera.net (fethera.tethera.net [IPv6:2607:5300:60:c5::1]) by mail.notmuchmail.org (Postfix) with ESMTP id E341F2906D for ; Sat, 23 Oct 2021 05:24:56 -0400 (EDT) Received: by fethera.tethera.net (Postfix, from userid 1001) id DC3CB5FC48; Sat, 23 Oct 2021 05:24:56 -0400 (EDT) Received: (nullmailer pid 1403474 invoked by uid 1000); Sat, 23 Oct 2021 09:24:54 -0000 From: David Bremner To: Austin Ray , notmuch@notmuchmail.org Cc: David Bremner Subject: [PATCH 5/5] lib/load_config: document expectations for db on error, add tests Date: Sat, 23 Oct 2021 06:24:51 -0300 Message-Id: <20211023092451.1403141-6-david@tethera.net> X-Mailer: git-send-email 2.33.0 In-Reply-To: <20211023092451.1403141-1-david@tethera.net> References: <20211023042225.xt36nzpsukqs6kid@athena> <20211023092451.1403141-1-david@tethera.net> MIME-Version: 1.0 Message-ID-Hash: HBG5TM3ILCXVZA3SJA4PUC4JDKCRJCWR X-Message-ID-Hash: HBG5TM3ILCXVZA3SJA4PUC4JDKCRJCWR 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 ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1634981115; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=/7buC1CPop8QMM5Z7SxT5swPzvhyaTS3X3EfZpWepPY=; b=nY7uL6JhkHlnyVzmKblLex1jBJ8HyrAZVPM0+MgR/OWYcAu4PEkxkCR/Idrmt3dACL4w8+ hj43oquglwM5wwKkKMizVid7oftOBXzIkVfphznj6egl1FC2C+XCYEFarB8TQla0kNfQxc PAmpNMVd5Fi5VAu7M+zFXIo7Kdtx3O1nSJWfkxF5IPYKIjPJSoaJ+a2Rwludh4db6a2sVb wz29PJOJOwOnyDQGsQ3lR9lvk01jKaRYqXEguNPyY7DtaVT6FUNUYCe1VQWDWXWCoI+tMN KR+vaXCQJC5MB4vmTxBBulpRI3BP8M/4U9KS+40lRvEx6kpvRgO7dbA7O/A9vw== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1634981115; a=rsa-sha256; cv=none; b=GkHXWBvNbVVD5mppzzM0EGXroZLmxPyt+/9a6hmKzo24ItjlPqCp78+EkVWxIhH9JSkvjq pBLeW3qHWi2cjpZ0MXr6reeVOJZ0GZ/Lj6L8W24vielYBAY1rip5pwDJM4887m5CoMrqgU 72emo9MEouws06Os90jtEX6CBRwXq8cr3i6ogBkNGn2gk9t7T7IouKa55k8bXCmWm2rPPs FdsYZqDsG28fkP7fXcjSE03y3tFKci1Unaw4k/VFc+llj4F4XDdRrlnEKMVHf4RVOkONpP PrSbA583pnwVdh2CohJ4kJTO0QYE9Wz+b/lkZ+3imEa/UoS3r0dTqQwenqXHGA== ARC-Authentication-Results: i=1; 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-Spam-Score: -1.15 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: DBAAF4ED6 X-Spam-Score: -1.15 X-Migadu-Scanner: scn1.migadu.com X-TUID: WWjcevJBcCIT This is a bit different than n_d_{open,create}_with_config, since there are several non-zero status codes where we do want to return a non-NULL database structure. --- lib/notmuch.h | 2 ++ lib/open.cc | 7 +++++++ test/T590-libconfig.sh | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+) diff --git a/lib/notmuch.h b/lib/notmuch.h index 571b8a87..64f71280 100644 --- a/lib/notmuch.h +++ b/lib/notmuch.h @@ -458,6 +458,8 @@ notmuch_database_open_with_config (const char *database_path, * * For description of arguments, @see notmuch_database_open_with_config * + * For fatal errors, database is set to NULL. + * * @retval NOTMUCH_STATUS_SUCCESS: Successfully loaded configuration. * * @retval NOTMUCH_STATUS_NO_CONFIG: No config file was loaded. Not fatal. diff --git a/lib/open.cc b/lib/open.cc index 6fa00a84..ba32c2f1 100644 --- a/lib/open.cc +++ b/lib/open.cc @@ -871,6 +871,13 @@ notmuch_database_load_config (const char *database_path, if (status_string) *status_string = message; + if (status && + status != NOTMUCH_STATUS_NO_DATABASE + && status != NOTMUCH_STATUS_NO_CONFIG) { + notmuch_database_destroy (notmuch); + notmuch = NULL; + } + if (database) *database = notmuch; diff --git a/test/T590-libconfig.sh b/test/T590-libconfig.sh index cd7d6e24..e8fbd3d7 100755 --- a/test/T590-libconfig.sh +++ b/test/T590-libconfig.sh @@ -932,4 +932,44 @@ db == NULL: 1 EOF test_expect_equal_file EXPECTED OUTPUT +test_begin_subtest "load_config: database set non-null on missing config" +test_C ${MAIL_DIR} "/nonexistent" < +int main (int argc, char **argv) { + notmuch_status_t stat; + notmuch_database_t *db = NULL; + + notmuch_status_t st = notmuch_database_load_config(argv[1],argv[2], NULL, &db, NULL); + printf("db == NULL: %d\n", db == NULL); +} +EOF +cat < EXPECTED +== stdout == +db == NULL: 0 +== stderr == +EOF +test_expect_equal_file EXPECTED OUTPUT + +test_begin_subtest "load_config: database non-null on missing config (env)" +old_NOTMUCH_CONFIG=${NOTMUCH_CONFIG} +NOTMUCH_CONFIG="/nonexistent" +test_C ${MAIL_DIR} < +int main (int argc, char **argv) { + notmuch_status_t stat; + notmuch_database_t *db = NULL; + + notmuch_status_t st = notmuch_database_load_config(argv[1], NULL, NULL, &db, NULL); + printf("db == NULL: %d\n", db == NULL); +} +EOF +NOTMUCH_CONFIG=${old_NOTMUCH_CONFIG} +cat < EXPECTED +== stdout == +db == NULL: 0 +== stderr == +EOF +test_expect_equal_file EXPECTED OUTPUT + + test_done -- 2.33.0