[PATCH 08/19] WIP: add _notmuch_config_load_from_file

Subject: [PATCH 08/19] WIP: add _notmuch_config_load_from_file

Date: Sat, 8 Aug 2020 11:16:42 -0300

To: notmuch@notmuchmail.org

Cc: David Bremner

From: David Bremner


---
 lib/config.cc          | 34 ++++++++++++++++++++++++++++++++++
 lib/database.cc        |  4 ++++
 lib/notmuch-private.h  |  3 +++
 test/T590-libconfig.sh | 23 ++++++++++++++++++++++-
 4 files changed, 63 insertions(+), 1 deletion(-)

diff --git a/lib/config.cc b/lib/config.cc
index 2cfd2882..f5def3aa 100644
--- a/lib/config.cc
+++ b/lib/config.cc
@@ -241,3 +241,37 @@ notmuch_config_set (notmuch_database_t *notmuch,
 
     return status;
 }
+
+notmuch_status_t
+_notmuch_config_load_from_file (notmuch_database_t *notmuch,
+				GKeyFile *file)
+{
+    notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
+    gchar **groups,**keys, *val;
+
+    if (notmuch->config == NULL)
+	notmuch->config = _notmuch_string_map_create (notmuch);
+
+    if (unlikely(notmuch->config == NULL)) {
+	status = NOTMUCH_STATUS_OUT_OF_MEMORY;
+	goto DONE;
+    }
+
+    for (groups = g_key_file_get_groups (file, NULL); *groups; groups++) {
+	for (keys = g_key_file_get_keys (file, *groups, NULL, NULL); *keys; keys++) {
+	    char *absolute_key = talloc_asprintf(notmuch, "%s.%s", *groups,  *keys);
+	    val = g_key_file_get_value (file, *groups, *keys, NULL);
+	    if (! val) {
+		status = NOTMUCH_STATUS_FILE_ERROR;
+		goto DONE;
+	    }
+	    status = notmuch_config_set (notmuch, absolute_key, val, FALSE);
+	    talloc_free (absolute_key);
+	    if (status)
+		goto DONE;
+	}
+    }
+
+ DONE:
+    return status;
+}
diff --git a/lib/database.cc b/lib/database.cc
index 2ab9170d..93fef107 100644
--- a/lib/database.cc
+++ b/lib/database.cc
@@ -1104,6 +1104,10 @@ notmuch_database_open_with_config (const char *database_path,
 
 	if (status == NOTMUCH_STATUS_SUCCESS)
 	    status = _notmuch_config_load_from_database (notmuch);
+
+	if (status == NOTMUCH_STATUS_SUCCESS && key_file)
+	    status = _notmuch_config_load_from_file (notmuch, key_file);
+
     } catch (const Xapian::Error &error) {
 	IGNORE_RESULT (asprintf (&message, "A Xapian exception occurred opening database: %s\n",
 				 error.get_msg ().c_str ()));
diff --git a/lib/notmuch-private.h b/lib/notmuch-private.h
index b545fc46..192a0771 100644
--- a/lib/notmuch-private.h
+++ b/lib/notmuch-private.h
@@ -707,6 +707,9 @@ struct _notmuch_indexopts {
 /* config.cc */
 notmuch_status_t
 _notmuch_config_load_from_database (notmuch_database_t * db);
+
+notmuch_status_t
+_notmuch_config_load_from_file (notmuch_database_t * db, GKeyFile *file);
 NOTMUCH_END_DECLS
 
 #ifdef __cplusplus
diff --git a/test/T590-libconfig.sh b/test/T590-libconfig.sh
index a34eae0b..a303319d 100755
--- a/test/T590-libconfig.sh
+++ b/test/T590-libconfig.sh
@@ -128,7 +128,7 @@ EOF
 test_expect_equal_file EXPECTED OUTPUT
 
 test_begin_subtest "notmuch_database_get_config_list: one prefix"
-cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}
+cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR} ${NOTMUCH_CONFIG}
 {
    notmuch_config_list_t *list;
    EXPECT0(notmuch_database_get_config_list (db, "test.key", &list));
@@ -219,4 +219,25 @@ EOF
 test_expect_equal_file EXPECTED OUTPUT
 restore_database
 
+backup_database
+test_begin_subtest "override config from file"
+notmuch config set test.key1 overridden
+cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR} ${NOTMUCH_CONFIG}
+{
+   printf("test.key1 = %s\n", notmuch_config_get (db, "test.key1"));
+   EXPECT0(notmuch_database_get_config (db, "test.key1", &val));
+   printf("test.key1 (db) = %s\n", val);
+   printf("test.key2 = %s\n", notmuch_config_get (db, "test.key2"));
+}
+EOF
+cat <<'EOF' >EXPECTED
+== stdout ==
+test.key1 = overridden
+test.key1 (db) = testvalue1
+test.key2 = testvalue2
+== stderr ==
+EOF
+test_expect_equal_file EXPECTED OUTPUT
+restore_database
+
 test_done
-- 
2.28.0
_______________________________________________
notmuch mailing list -- notmuch@notmuchmail.org
To unsubscribe send an email to notmuch-leave@notmuchmail.org

Thread: