From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp10.migadu.com ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms5.migadu.com with LMTPS id 8KwqAUYKjmKDOwAAbAwnHQ (envelope-from ) for ; Wed, 25 May 2022 12:51:50 +0200 Received: from aspmx1.migadu.com ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp10.migadu.com with LMTPS id uMc0AEYKjmIxbQEAG6o9tA (envelope-from ) for ; Wed, 25 May 2022 12:51:50 +0200 Received: from mail.notmuchmail.org (yantan.tethera.net [IPv6:2a01:4f9:c011:7a79::1]) (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 8CEC02922E for ; Wed, 25 May 2022 12:51:49 +0200 (CEST) Received: from yantan.tethera.net (localhost [127.0.0.1]) by mail.notmuchmail.org (Postfix) with ESMTP id BBE085F799; Wed, 25 May 2022 10:51:29 +0000 (UTC) Received: from fethera.tethera.net (fethera.tethera.net [IPv6:2607:5300:60:c5::1]) by mail.notmuchmail.org (Postfix) with ESMTP id 77C255F6BD for ; Wed, 25 May 2022 10:51:26 +0000 (UTC) Received: by fethera.tethera.net (Postfix, from userid 1001) id E15905FBD7; Wed, 25 May 2022 06:51:25 -0400 (EDT) Received: (nullmailer pid 10855 invoked by uid 1000); Wed, 25 May 2022 10:51:20 -0000 From: David Bremner To: notmuch@notmuchmail.org Subject: [PATCH 5/6] lib: add NOTMUCH_STATUS_CLOSED_DATABASE, use in _n_d_ensure_writable Date: Wed, 25 May 2022 07:51:16 -0300 Message-Id: <20220525105117.10709-6-david@tethera.net> X-Mailer: git-send-email 2.35.2 In-Reply-To: <20220525105117.10709-1-david@tethera.net> References: <20220525105117.10709-1-david@tethera.net> MIME-Version: 1.0 Message-ID-Hash: ZOMNAZJBWXMAUF7BXJIBTWWYMD2COEE3 X-Message-ID-Hash: ZOMNAZJBWXMAUF7BXJIBTWWYMD2COEE3 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; digests; suspicious-header X-Mailman-Version: 3.3.3 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Migadu-Flow: FLOW_IN X-Migadu-To: larch@yhetil.org X-Migadu-Country: DE ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1653475909; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to: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-owner:list-unsubscribe:list-subscribe:list-post; bh=XSdRXBNyda9kh+rQ4z/P2aLXRCAycsIeA5+nD3JR4HI=; b=CVgr5Ol+cCnDJccd71CfT0e1q8ROXhTdYehrxmJD8y6DHYwPB8OfMO+c/BLGDOT/t+UuMY bsViHz692cxc2gb4mgJqHqH90eOYreNA0smnQsMmI5Jyu8J7dYlN5x5nnd0VQuiT6hzyVI wKhWab/0j1betzxvL5DNjUD7sUe5DhQXD2vjkT6y3k3X558iTbBnNTygH6FZ4VBqO21pLN mSODKuQ3iV8ALe9CwBBXOOPUtFN2sIjMHStTM5mOt527KqtW6sVQctLO6m3vaykUcLbHbW b0fbXcEZ2qenO8PszF1gdv73ckCQZuopRPngfIDe255WQjD4FfhdqT1ZvvqRNQ== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1653475909; a=rsa-sha256; cv=none; b=uG4v6AcSQRb3Hre0ifLT6yHKBGdoMWfYFZ6TACp5qhf+Wy+kEEA0jOhKAjGEnsPYKZ2FNU yN5Zv6GqtdbvZnMoY8hueUBc1BYWgXM81YRXw3AHL36XhoRb/4fvi+DvTnAh1pGZH9Zhne mq8YSDEj2hyNDC6u+9l1RXk1IeNfkIZvfuLlk0c5RTUIaurxN7WZvYWoDtZk2XAVWqW+7m NEOWP4H3UVsMkIcBzf0+6vRbGQxw2yZPooBPomhwg0+s4/vWN7OqEsC8k69Zhna4kDkCJz 1XGX056Zt1CVyod+g01AHpAT1wnm9mfnTzWV30CjK94xoqrntKY0kNKg1lCHzQ== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=none; dmarc=none; spf=pass (aspmx1.migadu.com: domain of notmuch-bounces@notmuchmail.org designates 2a01:4f9:c011:7a79::1 as permitted sender) smtp.mailfrom=notmuch-bounces@notmuchmail.org X-Migadu-Spam-Score: -1.02 Authentication-Results: aspmx1.migadu.com; dkim=none; dmarc=none; spf=pass (aspmx1.migadu.com: domain of notmuch-bounces@notmuchmail.org designates 2a01:4f9:c011:7a79::1 as permitted sender) smtp.mailfrom=notmuch-bounces@notmuchmail.org X-Migadu-Queue-Id: 8CEC02922E X-Spam-Score: -1.02 X-Migadu-Scanner: scn0.migadu.com X-TUID: DSrFcNkGSUpr In order for a database to actually be writeable, it must be the case that it is open, not just the correct type of Xapian object. By explicitely checking, we are able to provide better error reporting, in particular for the previously broken test in T566-lib-message. --- bindings/python-cffi/notmuch2/_build.py | 1 + lib/database.cc | 10 +++++++--- lib/notmuch-private.h | 1 + lib/notmuch.h | 4 ++++ test/T562-lib-database.sh | 8 ++++---- test/T563-lib-directory.sh | 26 ++++--------------------- test/T566-lib-message.sh | 10 +++++----- 7 files changed, 26 insertions(+), 34 deletions(-) diff --git a/bindings/python-cffi/notmuch2/_build.py b/bindings/python-cffi/notmuch2/_build.py index 349bb79d..65d7dcb6 100644 --- a/bindings/python-cffi/notmuch2/_build.py +++ b/bindings/python-cffi/notmuch2/_build.py @@ -55,6 +55,7 @@ ffibuilder.cdef( NOTMUCH_STATUS_DATABASE_EXISTS, NOTMUCH_STATUS_BAD_QUERY_SYNTAX, NOTMUCH_STATUS_NO_MAIL_ROOT, + NOTMUCH_STATUS_CLOSED_DATABASE, NOTMUCH_STATUS_LAST_STATUS } notmuch_status_t; typedef enum { diff --git a/lib/database.cc b/lib/database.cc index df83e204..c05d70d3 100644 --- a/lib/database.cc +++ b/lib/database.cc @@ -476,6 +476,11 @@ _notmuch_database_ensure_writable (notmuch_database_t *notmuch) return NOTMUCH_STATUS_READ_ONLY_DATABASE; } + if (! notmuch->open) { + _notmuch_database_log (notmuch, "Cannot write to a closed database.\n"); + return NOTMUCH_STATUS_CLOSED_DATABASE; + } + return NOTMUCH_STATUS_SUCCESS; } @@ -852,9 +857,8 @@ notmuch_database_upgrade (notmuch_database_t *notmuch, notmuch_query_t *query = NULL; unsigned int count = 0, total = 0; - status = _notmuch_database_ensure_writable (notmuch); - if (status) - return status; + if (_notmuch_database_mode (notmuch) != NOTMUCH_DATABASE_MODE_READ_WRITE) + return NOTMUCH_STATUS_READ_ONLY_DATABASE; db = notmuch->writable_xapian_db; diff --git a/lib/notmuch-private.h b/lib/notmuch-private.h index 69debcfe..1d3d2b0c 100644 --- a/lib/notmuch-private.h +++ b/lib/notmuch-private.h @@ -146,6 +146,7 @@ typedef enum { NOTMUCH_PRIVATE_STATUS_DATABASE_EXISTS = NOTMUCH_STATUS_DATABASE_EXISTS, NOTMUCH_PRIVATE_STATUS_NO_MAIL_ROOT = NOTMUCH_STATUS_NO_MAIL_ROOT, NOTMUCH_PRIVATE_STATUS_BAD_QUERY_SYNTAX = NOTMUCH_STATUS_BAD_QUERY_SYNTAX, + NOTMUCH_PRIVATE_STATUS_CLOSED_DATABASE = NOTMUCH_STATUS_CLOSED_DATABASE, /* Then add our own private values. */ NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG = NOTMUCH_STATUS_LAST_STATUS, diff --git a/lib/notmuch.h b/lib/notmuch.h index 44263a66..0b0540b1 100644 --- a/lib/notmuch.h +++ b/lib/notmuch.h @@ -228,6 +228,10 @@ typedef enum { * No mail root could be deduced from parameters and environment */ NOTMUCH_STATUS_NO_MAIL_ROOT, + /** + * Database is not fully opened, or has been closed + */ + NOTMUCH_STATUS_CLOSED_DATABASE, /** * Not an actual status value. Just a way to find out how many * valid status values there are. diff --git a/test/T562-lib-database.sh b/test/T562-lib-database.sh index 2314efd2..324b4544 100755 --- a/test/T562-lib-database.sh +++ b/test/T562-lib-database.sh @@ -241,14 +241,14 @@ cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR} const char *path = talloc_asprintf(db, "%s/01:2,", argv[1]); EXPECT0(notmuch_database_close (db)); stat = notmuch_database_index_file (db, path, NULL, &msg); - printf ("%d\n", stat == NOTMUCH_STATUS_XAPIAN_EXCEPTION); + printf ("%d\n", stat == NOTMUCH_STATUS_CLOSED_DATABASE); } EOF cat < EXPECTED == stdout == 1 == stderr == -A Xapian exception occurred finding message: Database has been closed. +Cannot write to a closed database. EOF test_expect_equal_file EXPECTED OUTPUT @@ -356,14 +356,14 @@ cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR} { EXPECT0(notmuch_database_close (db)); stat = notmuch_database_set_config (db, "foo", "bar"); - printf("%d\n", stat == NOTMUCH_STATUS_XAPIAN_EXCEPTION); + printf("%d\n", stat == NOTMUCH_STATUS_CLOSED_DATABASE); } EOF cat < EXPECTED == stdout == 1 == stderr == -Error: A Xapian exception occurred setting metadata: Database has been closed +Cannot write to a closed database. EOF test_expect_equal_file EXPECTED OUTPUT diff --git a/test/T563-lib-directory.sh b/test/T563-lib-directory.sh index ebd7fcb2..9f07101b 100755 --- a/test/T563-lib-directory.sh +++ b/test/T563-lib-directory.sh @@ -77,14 +77,14 @@ test_begin_subtest "delete directory document for a closed db" cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR} { stat = notmuch_directory_delete (dir); - printf ("%d\n", stat == NOTMUCH_STATUS_XAPIAN_EXCEPTION); + printf ("%d\n", stat == NOTMUCH_STATUS_CLOSED_DATABASE); } EOF cat < EXPECTED == stdout == 1 == stderr == -A Xapian exception occurred deleting directory entry: Database has been closed. +Cannot write to a closed database. EOF test_expect_equal_file EXPECTED OUTPUT restore_database @@ -95,32 +95,14 @@ cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR} { time_t stamp = notmuch_directory_get_mtime (dir); stat = notmuch_directory_set_mtime (dir, stamp); - printf ("%d\n", stat == NOTMUCH_STATUS_XAPIAN_EXCEPTION); + printf ("%d\n", stat == NOTMUCH_STATUS_CLOSED_DATABASE); } EOF cat < EXPECTED == stdout == 1 == stderr == -A Xapian exception occurred setting directory mtime: Database has been closed. -EOF -test_expect_equal_file EXPECTED OUTPUT -restore_database - -backup_database -test_begin_subtest "get/set mtime of directory for a closed db" -cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR} - { - time_t stamp = notmuch_directory_get_mtime (dir); - stat = notmuch_directory_set_mtime (dir, stamp); - printf ("%d\n", stat == NOTMUCH_STATUS_XAPIAN_EXCEPTION); - } -EOF -cat < EXPECTED -== stdout == -1 -== stderr == -A Xapian exception occurred setting directory mtime: Database has been closed. +Cannot write to a closed database. EOF test_expect_equal_file EXPECTED OUTPUT restore_database diff --git a/test/T566-lib-message.sh b/test/T566-lib-message.sh index 2ad24543..3a5e9607 100755 --- a/test/T566-lib-message.sh +++ b/test/T566-lib-message.sh @@ -229,7 +229,7 @@ cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR} { notmuch_status_t status; status = notmuch_message_add_tag (message, "boom"); - printf("%d\n%d\n", message != NULL, status == NOTMUCH_STATUS_XAPIAN_EXCEPTION); + printf("%d\n%d\n", message != NULL, status == NOTMUCH_STATUS_CLOSED_DATABASE); } EOF cat < EXPECTED @@ -245,7 +245,7 @@ cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR} { notmuch_status_t status; status = notmuch_message_remove_tag (message, "boom"); - printf("%d\n%d\n", message != NULL, status == NOTMUCH_STATUS_XAPIAN_EXCEPTION); + printf("%d\n%d\n", message != NULL, status == NOTMUCH_STATUS_CLOSED_DATABASE); } EOF cat < EXPECTED @@ -425,7 +425,7 @@ cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR} { notmuch_status_t status; status = notmuch_message_remove_all_tags (message); - printf("%d\n%d\n", message != NULL, status == NOTMUCH_STATUS_XAPIAN_EXCEPTION); + printf("%d\n%d\n", message != NULL, status == NOTMUCH_STATUS_CLOSED_DATABASE); } EOF cat < EXPECTED @@ -441,7 +441,7 @@ cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR} { notmuch_status_t status; status = notmuch_message_freeze (message); - printf("%d\n%d\n", message != NULL, status == NOTMUCH_STATUS_SUCCESS); + printf("%d\n%d\n", message != NULL, status == NOTMUCH_STATUS_CLOSED_DATABASE); } EOF cat < EXPECTED @@ -457,7 +457,7 @@ cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR} { notmuch_status_t status; status = notmuch_message_thaw (message); - printf("%d\n%d\n", message != NULL, status == NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW); + printf("%d\n%d\n", message != NULL, status == NOTMUCH_STATUS_CLOSED_DATABASE); } EOF cat < EXPECTED -- 2.35.2