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 8Dg2Htk15V9yFgAA0tVLHw (envelope-from ) for ; Fri, 25 Dec 2020 00:44:09 +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 CMj3Gdk15V/CZgAA1q6Kng (envelope-from ) for ; Fri, 25 Dec 2020 00:44:09 +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 23B309403A9 for ; Fri, 25 Dec 2020 00:44:09 +0000 (UTC) Received: from nmbug.tethera.net (localhost [127.0.0.1]) by mail.notmuchmail.org (Postfix) with ESMTP id 9B1CE29CD5; Thu, 24 Dec 2020 19:43:19 -0500 (EST) Received: from fethera.tethera.net (fethera.tethera.net [198.245.60.197]) by mail.notmuchmail.org (Postfix) with ESMTP id 231DD29CE7 for ; Thu, 24 Dec 2020 19:42:52 -0500 (EST) Received: by fethera.tethera.net (Postfix, from userid 1001) id A22A45FC36; Thu, 24 Dec 2020 19:42:51 -0500 (EST) Received: (nullmailer pid 650439 invoked by uid 1000); Fri, 25 Dec 2020 00:42:36 -0000 From: David Bremner To: notmuch@notmuchmail.org Cc: David Bremner Subject: [PATCH 06/24] lib/config: add notmuch_config_key_{get,set} Date: Thu, 24 Dec 2020 20:42:10 -0400 Message-Id: <20201225004228.647328-7-david@tethera.net> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201225004228.647328-1-david@tethera.net> References: <20201225004228.647328-1-david@tethera.net> MIME-Version: 1.0 Message-ID-Hash: LVE6DXPXV7QUNNDL4MUM7MA2CJGND5VQ X-Message-ID-Hash: LVE6DXPXV7QUNNDL4MUM7MA2CJGND5VQ 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.45 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: 23B309403A9 X-Spam-Score: 0.45 X-Migadu-Scanner: scn0.migadu.com X-TUID: xv6qjMYLRq9I By using an enum we can have better error detection than copy pasting key strings around. The question of what layer this belongs in is a bit tricky. Historically most of the keys are defined by the CLI. On the other hand features like excludes are supported in the library/bindings, and it makes sense to configure them from the library as well. The somewhat long prefix for notmuch_config_t is to avoid collisions with the existing usage in notmuch-client.h. --- lib/config.cc | 34 +++++++++++++++++++++++++ lib/notmuch.h | 56 ++++++++++++++++++++++++++++++++++++++++++ test/T590-libconfig.sh | 38 ++++++++++++++++++++++++++++ 3 files changed, 128 insertions(+) diff --git a/lib/config.cc b/lib/config.cc index 8bce7ba8..c92ffeef 100644 --- a/lib/config.cc +++ b/lib/config.cc @@ -279,3 +279,37 @@ _notmuch_config_load_from_file (notmuch_database_t *notmuch, DONE: return status; } + +const char * +_notmuch_config_key_to_string (notmuch_config_key_t key) { + switch (key) { + case NOTMUCH_CONFIG_DATABASE_PATH: + return "database.path"; + case NOTMUCH_CONFIG_EXCLUDE_TAGS: + return "search.exclude_tags"; + case NOTMUCH_CONFIG_NEW_TAGS: + return "new.tags"; + case NOTMUCH_CONFIG_SYNC_MAILDIR_FLAGS: + return "maildir.synchronize_flags"; + case NOTMUCH_CONFIG_PRIMARY_EMAIL: + return "user.primary_email"; + case NOTMUCH_CONFIG_OTHER_EMAIL: + return "user.other_email"; + case NOTMUCH_CONFIG_USER_NAME: + return "user.name"; + default: + return NULL; + } +} + +const char * +notmuch_config_get (notmuch_database_t *notmuch, notmuch_config_key_t key) { + + return _notmuch_string_map_get (notmuch->config, _notmuch_config_key_to_string (key)); +} + +notmuch_status_t +notmuch_config_set (notmuch_database_t *notmuch, notmuch_config_key_t key, const char *val) { + + return notmuch_database_set_config (notmuch, _notmuch_config_key_to_string (key), val); +} diff --git a/lib/notmuch.h b/lib/notmuch.h index 8e538cfb..c2c71623 100644 --- a/lib/notmuch.h +++ b/lib/notmuch.h @@ -2323,6 +2323,11 @@ notmuch_filenames_destroy (notmuch_filenames_t *filenames); * set config 'key' to 'value' * * @since libnotmuch 4.4 (notmuch 0.23) + * @retval #NOTMUCH_STATUS_READ_ONLY_DATABASE: Database was opened in + * read-only mode so message cannot be modified. + * @retval #NOTMUCH_STATUS_XAPIAN_EXCEPTION: an exception was thrown + * accessing the database. + * @retval #NOTMUCH_STATUS_SUCCESS */ notmuch_status_t notmuch_database_set_config (notmuch_database_t *db, const char *key, const char *value); @@ -2337,6 +2342,7 @@ notmuch_database_set_config (notmuch_database_t *db, const char *key, const char * caller. * * @since libnotmuch 4.4 (notmuch 0.23) + * */ notmuch_status_t notmuch_database_get_config (notmuch_database_t *db, const char *key, char **value); @@ -2398,6 +2404,56 @@ void notmuch_config_list_destroy (notmuch_config_list_t *config_list); +/** + * Configuration keys known to libnotmuch + */ +typedef enum _notmuch_config_key { + NOTMUCH_CONFIG_FIRST, + NOTMUCH_CONFIG_DATABASE_PATH = NOTMUCH_CONFIG_FIRST, + NOTMUCH_CONFIG_EXCLUDE_TAGS, + NOTMUCH_CONFIG_NEW_TAGS, + NOTMUCH_CONFIG_SYNC_MAILDIR_FLAGS, + NOTMUCH_CONFIG_PRIMARY_EMAIL, + NOTMUCH_CONFIG_OTHER_EMAIL, + NOTMUCH_CONFIG_USER_NAME, + NOTMUCH_CONFIG_LAST +} notmuch_config_key_t; + +/** + * get a configuration value from an open database. + * + * This value reflects all configuration information given at the time + * the database was opened. + * + * @param[in] notmuch database + * @param[in] key configuration key + * + * @since libnotmuch 5.4 (notmuch 0.32) + * + * @retval NULL if 'key' unknown or if no value is known for + * 'key'. Otherwise returns a string owned by notmuch which should + * not be modified nor freed by the caller. + */ +const char * +notmuch_config_get (notmuch_database_t *notmuch, notmuch_config_key_t key); + +/** + * set a configuration value from in an open database. + * + * This value reflects all configuration information given at the time + * the database was opened. + * + * @param[in,out] notmuch database open read/write + * @param[in] key configuration key + * @param[in] val configuration value + * + * @since libnotmuch 5.4 (notmuch 0.32) + * + * @retval returns any return value for notmuch_database_set_config. + */ +notmuch_status_t +notmuch_config_set (notmuch_database_t *notmuch, notmuch_config_key_t key, const char *val); + /** * get the current default indexing options for a given database. * diff --git a/test/T590-libconfig.sh b/test/T590-libconfig.sh index 2986284a..8bf43eed 100755 --- a/test/T590-libconfig.sh +++ b/test/T590-libconfig.sh @@ -201,6 +201,44 @@ EOF test_expect_equal_file EXPECTED OUTPUT restore_database +backup_database +test_begin_subtest "get config by key" +notmuch config set test.key1 overridden +cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR} ${NOTMUCH_CONFIG} +{ + printf("before = %s\n", notmuch_config_get (db, NOTMUCH_CONFIG_SYNC_MAILDIR_FLAGS)); + EXPECT0(notmuch_database_set_config (db, "maildir.synchronize_flags", "false")); + printf("after = %s\n", notmuch_config_get (db, NOTMUCH_CONFIG_SYNC_MAILDIR_FLAGS)); +} +EOF +cat <<'EOF' >EXPECTED +== stdout == +before = true +after = false +== stderr == +EOF +test_expect_equal_file EXPECTED OUTPUT +restore_database + +backup_database +test_begin_subtest "set config by key" +notmuch config set test.key1 overridden +cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR} ${NOTMUCH_CONFIG} +{ + printf("before = %s\n", notmuch_config_get (db, NOTMUCH_CONFIG_SYNC_MAILDIR_FLAGS)); + EXPECT0(notmuch_config_set (db, NOTMUCH_CONFIG_SYNC_MAILDIR_FLAGS, "false")); + printf("after = %s\n", notmuch_config_get (db, NOTMUCH_CONFIG_SYNC_MAILDIR_FLAGS)); +} +EOF +cat <<'EOF' >EXPECTED +== stdout == +before = true +after = false +== stderr == +EOF +test_expect_equal_file EXPECTED OUTPUT +restore_database + backup_database test_begin_subtest "override config from \${NOTMUCH_CONFIG}" notmuch config set test.key1 overridden -- 2.29.2