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 oOeTICm0Ll/2dgAA0tVLHw (envelope-from ) for ; Sat, 08 Aug 2020 14:18:17 +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 uIZ8HCm0Ll8NOwAA1q6Kng (envelope-from ) for ; Sat, 08 Aug 2020 14:18:17 +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 09317940223 for ; Sat, 8 Aug 2020 14:18:17 +0000 (UTC) Received: from [144.217.243.247] (localhost [127.0.0.1]) by mail.notmuchmail.org (Postfix) with ESMTP id D29EE29B4F; Sat, 8 Aug 2020 10:17:42 -0400 (EDT) Received: from fethera.tethera.net (fethera.tethera.net [IPv6:2607:5300:60:c5::1]) by mail.notmuchmail.org (Postfix) with ESMTP id 07AEF29AE5 for ; Sat, 8 Aug 2020 10:17:19 -0400 (EDT) Received: by fethera.tethera.net (Postfix, from userid 1001) id E4AEA5FA66; Sat, 8 Aug 2020 10:17:19 -0400 (EDT) Received: (nullmailer pid 1124265 invoked by uid 1000); Sat, 08 Aug 2020 14:17:04 -0000 From: David Bremner To: notmuch@notmuchmail.org Cc: David Bremner Subject: [PATCH 05/19] WIP: add notmuch_config_set Date: Sat, 8 Aug 2020 11:16:39 -0300 Message-Id: <20200808141653.1124111-6-david@tethera.net> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200808141653.1124111-1-david@tethera.net> References: <20200808141653.1124111-1-david@tethera.net> MIME-Version: 1.0 Message-ID-Hash: LU32XTFPJVCJB2ZRH5D25PF3PI26KI2D X-Message-ID-Hash: LU32XTFPJVCJB2ZRH5D25PF3PI26KI2D 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-Scanner: scn0 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-Spam-Score: 0.03 X-TUID: nnTQDKmNV+YV To be decided: is the write_through paramater a good idea? Should we simplify the API? --- lib/config.cc | 16 ++++++++++++++++ lib/notmuch-private.h | 5 +++++ lib/notmuch.h | 5 +++++ lib/string-map.c | 16 ++++++++++++++++ test/T590-libconfig.sh | 30 ++++++++++++++++++++++++++++++ 5 files changed, 72 insertions(+) diff --git a/lib/config.cc b/lib/config.cc index 0a91ec03..2cfd2882 100644 --- a/lib/config.cc +++ b/lib/config.cc @@ -225,3 +225,19 @@ notmuch_config_get (notmuch_database_t *notmuch, const char *key) { return _notmuch_string_map_get (notmuch->config, key); } + +notmuch_status_t +notmuch_config_set (notmuch_database_t *notmuch, + const char *key, + const char *val, + notmuch_bool_t write_through) +{ + notmuch_status_t status = NOTMUCH_STATUS_SUCCESS; + + _notmuch_string_map_set (notmuch->config, key, val); + + if (write_through) + status = notmuch_database_set_config (notmuch, key, val); + + return status; +} diff --git a/lib/notmuch-private.h b/lib/notmuch-private.h index 0f26b371..b545fc46 100644 --- a/lib/notmuch-private.h +++ b/lib/notmuch-private.h @@ -638,6 +638,11 @@ _notmuch_string_map_append (notmuch_string_map_t *map, const char *key, const char *value); +void +_notmuch_string_map_set (notmuch_string_map_t *map, + const char *key, + const char *value); + const char * _notmuch_string_map_get (notmuch_string_map_t *map, const char *key); diff --git a/lib/notmuch.h b/lib/notmuch.h index 777116bb..e147d5e6 100644 --- a/lib/notmuch.h +++ b/lib/notmuch.h @@ -2400,6 +2400,11 @@ notmuch_config_list_destroy (notmuch_config_list_t *config_list); const char * notmuch_config_get (notmuch_database_t *notmuch, const char *key); +notmuch_status_t +notmuch_config_set (notmuch_database_t *notmuch, const char *key, + const char *val, + notmuch_bool_t write_through); + /** * get the current default indexing options for a given database. * diff --git a/lib/string-map.c b/lib/string-map.c index a88404c7..9774dbe8 100644 --- a/lib/string-map.c +++ b/lib/string-map.c @@ -143,6 +143,22 @@ bsearch_first (notmuch_string_pair_t *array, size_t len, const char *key, bool e } +void +_notmuch_string_map_set (notmuch_string_map_t *map, const char *key, const char *val) +{ + notmuch_string_pair_t *pair; + + /* this means that calling string_map_set invalidates iterators */ + _notmuch_string_map_sort (map); + pair = bsearch_first (map->pairs, map->length, key, true); + if (! pair) + _notmuch_string_map_append (map, key, val); + else { + talloc_free (pair->value); + pair->value = talloc_strdup (map->pairs, val); + } +} + const char * _notmuch_string_map_get (notmuch_string_map_t *map, const char *key) { diff --git a/test/T590-libconfig.sh b/test/T590-libconfig.sh index 8a4519e9..c2bce4a2 100755 --- a/test/T590-libconfig.sh +++ b/test/T590-libconfig.sh @@ -184,4 +184,34 @@ test.key2 = testvalue2 EOF test_expect_equal_file EXPECTED OUTPUT +backup_database +test_begin_subtest "notmuch_config_set" +cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR} +{ + char *val; + printf("test.key1 = %s\n", notmuch_config_get (db, "test.key1")); + EXPECT0(notmuch_config_set (db, "test.key1", "overridden", FALSE)); + printf("test.key1 = %s\n", notmuch_config_get (db, "test.key1")); + printf("test.key2 = %s\n", notmuch_config_get (db, "test.key2")); + EXPECT0(notmuch_database_get_config (db, "test.key1", &val)); + printf("test.key1 (db) = %s\n", val); + EXPECT0(notmuch_config_set (db, "test.key2", "overridden2", TRUE)); + printf("test.key2 = %s\n", notmuch_config_get (db, "test.key2")); + EXPECT0(notmuch_database_get_config (db, "test.key2", &val)); + printf("test.key2 (db) = %s\n", val); +} +EOF +cat <<'EOF' >EXPECTED +== stdout == +test.key1 = testvalue1 +test.key1 = overridden +test.key2 = testvalue2 +test.key1 (db) = testvalue1 +test.key2 = overridden2 +test.key2 (db) = overridden2 +== stderr == +EOF +test_expect_equal_file EXPECTED OUTPUT +restore_database + test_done -- 2.28.0