--- /dev/null
+Return-Path: <bremner@tethera.net>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+ by arlo.cworth.org (Postfix) with ESMTP id A219E6DE0939\r
+ for <notmuch@notmuchmail.org>; Wed, 25 May 2016 03:52:10 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at cworth.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.012\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.012 tagged_above=-999 required=5\r
+ tests=[AWL=-0.001, SPF_PASS=-0.001, T_RP_MATCHES_RCVD=-0.01]\r
+ autolearn=disabled\r
+Received: from arlo.cworth.org ([127.0.0.1])\r
+ by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024)\r
+ with ESMTP id eO5J30LvMHUX for <notmuch@notmuchmail.org>;\r
+ Wed, 25 May 2016 03:52:00 -0700 (PDT)\r
+Received: from fethera.tethera.net (fethera.tethera.net [198.245.60.197])\r
+ by arlo.cworth.org (Postfix) with ESMTPS id 3D6696DE02C2\r
+ for <notmuch@notmuchmail.org>; Wed, 25 May 2016 03:51:40 -0700 (PDT)\r
+Received: from remotemail by fethera.tethera.net with local (Exim 4.84)\r
+ (envelope-from <bremner@tethera.net>)\r
+ id 1b5WPS-0004UY-Nk; Wed, 25 May 2016 06:51:30 -0400\r
+Received: (nullmailer pid 23136 invoked by uid 1000);\r
+ Wed, 25 May 2016 10:51:31 -0000\r
+From: David Bremner <david@tethera.net>\r
+To: notmuch@notmuchmail.org\r
+Subject: [Patch v6 1/7] lib: provide config API\r
+Date: Wed, 25 May 2016 07:51:20 -0300\r
+Message-Id: <1464173486-23032-2-git-send-email-david@tethera.net>\r
+X-Mailer: git-send-email 2.8.1\r
+In-Reply-To: <1464173486-23032-1-git-send-email-david@tethera.net>\r
+References: <1464173486-23032-1-git-send-email-david@tethera.net>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=UTF-8\r
+Content-Transfer-Encoding: 8bit\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.20\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+ <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <https://notmuchmail.org/mailman/options/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch/>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <https://notmuchmail.org/mailman/listinfo/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Wed, 25 May 2016 10:52:10 -0000\r
+\r
+This is a thin wrapper around the Xapian metadata API. The job of this\r
+layer is to keep the config key value pairs from colliding with other\r
+metadata by transparently prefixing the keys, along with the usual glue\r
+to provide a C interface.\r
+\r
+The split of _get_config into two functions is to allow returning of the\r
+return value with different memory ownership semantics.\r
+---\r
+ lib/Makefile.local | 1 +\r
+ lib/config.cc | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++\r
+ lib/notmuch.h | 20 ++++++++++++\r
+ test/T590-libconfig.sh | 58 +++++++++++++++++++++++++++++++++\r
+ 4 files changed, 166 insertions(+)\r
+ create mode 100644 lib/config.cc\r
+ create mode 100755 test/T590-libconfig.sh\r
+\r
+diff --git a/lib/Makefile.local b/lib/Makefile.local\r
+index 36c3924..76b57cb 100644\r
+--- a/lib/Makefile.local\r
++++ b/lib/Makefile.local\r
+@@ -49,6 +49,7 @@ libnotmuch_cxx_srcs = \\r
+ $(dir)/index.cc \\r
+ $(dir)/message.cc \\r
+ $(dir)/query.cc \\r
++ $(dir)/config.cc \\r
+ $(dir)/thread.cc\r
+ \r
+ libnotmuch_modules := $(libnotmuch_c_srcs:.c=.o) $(libnotmuch_cxx_srcs:.cc=.o)\r
+diff --git a/lib/config.cc b/lib/config.cc\r
+new file mode 100644\r
+index 0000000..9ca74bf\r
+--- /dev/null\r
++++ b/lib/config.cc\r
+@@ -0,0 +1,87 @@\r
++/* config.cc - API for database metadata\r
++ *\r
++ * Copyright © 2016 David Bremner\r
++ *\r
++ * This program is free software: you can redistribute it and/or modify\r
++ * it under the terms of the GNU General Public License as published by\r
++ * the Free Software Foundation, either version 3 of the License, or\r
++ * (at your option) any later version.\r
++ *\r
++ * This program is distributed in the hope that it will be useful,\r
++ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
++ * GNU General Public License for more details.\r
++ *\r
++ * You should have received a copy of the GNU General Public License\r
++ * along with this program. If not, see http://www.gnu.org/licenses/ .\r
++ *\r
++ * Author: David Bremner <david@tethera.net>\r
++ */\r
++\r
++#include "notmuch.h"\r
++#include "notmuch-private.h"\r
++#include "database-private.h"\r
++\r
++static const std::string CONFIG_PREFIX = "C";\r
++\r
++notmuch_status_t\r
++notmuch_database_set_config (notmuch_database_t *notmuch,\r
++ const char *key,\r
++ const char *value)\r
++{\r
++ notmuch_status_t status;\r
++ Xapian::WritableDatabase *db;\r
++\r
++ status = _notmuch_database_ensure_writable (notmuch);\r
++ if (status)\r
++ return status;\r
++\r
++ try {\r
++ db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);\r
++ db->set_metadata (CONFIG_PREFIX + key, value);\r
++ } catch (const Xapian::Error &error) {\r
++ status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;\r
++ notmuch->exception_reported = TRUE;\r
++ _notmuch_database_log (notmuch, "Error: A Xapian exception occurred setting metadata: %s\n",\r
++ error.get_msg().c_str());\r
++ }\r
++ return NOTMUCH_STATUS_SUCCESS;\r
++}\r
++\r
++static notmuch_status_t\r
++_metadata_value (notmuch_database_t *notmuch,\r
++ const char *key,\r
++ std::string &value)\r
++{\r
++ notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;\r
++\r
++ try {\r
++ value = notmuch->xapian_db->get_metadata (CONFIG_PREFIX + key);\r
++ } catch (const Xapian::Error &error) {\r
++ status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;\r
++ notmuch->exception_reported = TRUE;\r
++ _notmuch_database_log (notmuch, "Error: A Xapian exception occurred getting metadata: %s\n",\r
++ error.get_msg().c_str());\r
++ }\r
++ return status;\r
++}\r
++\r
++notmuch_status_t\r
++notmuch_database_get_config (notmuch_database_t *notmuch,\r
++ const char *key,\r
++ char **value)\r
++{\r
++ std::string strval;\r
++ notmuch_status_t status;\r
++\r
++ if (! value)\r
++ return NOTMUCH_STATUS_NULL_POINTER;\r
++\r
++ status = _metadata_value (notmuch, key, strval);\r
++ if (status)\r
++ return status;\r
++\r
++ *value = strdup (strval.c_str ());\r
++\r
++ return NOTMUCH_STATUS_SUCCESS;\r
++}\r
+diff --git a/lib/notmuch.h b/lib/notmuch.h\r
+index 3a092ef..c827e02 100644\r
+--- a/lib/notmuch.h\r
++++ b/lib/notmuch.h\r
+@@ -1838,6 +1838,26 @@ notmuch_filenames_move_to_next (notmuch_filenames_t *filenames);\r
+ void\r
+ notmuch_filenames_destroy (notmuch_filenames_t *filenames);\r
+ \r
++\r
++/**\r
++ * set config 'key' to 'value'\r
++ *\r
++ */\r
++notmuch_status_t\r
++notmuch_database_set_config (notmuch_database_t *db, const char *key, const char *value);\r
++\r
++/**\r
++ * retrieve config item 'key', assign to 'value'\r
++ *\r
++ * keys which have not been previously set with n_d_set_config will\r
++ * return an empty string.\r
++ *\r
++ * return value is allocated by malloc and should be freed by the\r
++ * caller.\r
++ */\r
++notmuch_status_t\r
++notmuch_database_get_config (notmuch_database_t *db, const char *key, char **value);\r
++\r
+ /**\r
+ * interrogate the library for compile time features\r
+ */\r
+diff --git a/test/T590-libconfig.sh b/test/T590-libconfig.sh\r
+new file mode 100755\r
+index 0000000..85e4497\r
+--- /dev/null\r
++++ b/test/T590-libconfig.sh\r
+@@ -0,0 +1,58 @@\r
++#!/usr/bin/env bash\r
++test_description="library config API"\r
++\r
++. ./test-lib.sh || exit 1\r
++\r
++add_email_corpus\r
++\r
++cat <<EOF > c_head\r
++#include <stdio.h>\r
++#include <string.h>\r
++#include <stdlib.h>\r
++#include <notmuch.h>\r
++\r
++void run(int line, notmuch_status_t ret)\r
++{\r
++ if (ret) {\r
++ fprintf (stderr, "line %d: %s\n", line, ret);\r
++ exit (1);\r
++ }\r
++}\r
++\r
++#define RUN(v) run(__LINE__, v);\r
++\r
++int main (int argc, char** argv)\r
++{\r
++ notmuch_database_t *db;\r
++ char *val;\r
++ notmuch_status_t stat;\r
++\r
++ RUN(notmuch_database_open (argv[1], NOTMUCH_DATABASE_MODE_READ_WRITE, &db));\r
++\r
++EOF\r
++\r
++cat <<EOF > c_tail\r
++ RUN(notmuch_database_destroy(db));\r
++}\r
++EOF\r
++\r
++test_begin_subtest "notmuch_database_{set,get}_config"\r
++cat c_head - c_tail <<'EOF' | test_C ${MAIL_DIR}\r
++{\r
++ RUN(notmuch_database_set_config (db, "testkey1", "testvalue1"));\r
++ RUN(notmuch_database_set_config (db, "testkey2", "testvalue2"));\r
++ RUN(notmuch_database_get_config (db, "testkey1", &val));\r
++ printf("testkey1 = %s\n", val);\r
++ RUN(notmuch_database_get_config (db, "testkey2", &val));\r
++ printf("testkey2 = %s\n", val);\r
++}\r
++EOF\r
++cat <<'EOF' >EXPECTED\r
++== stdout ==\r
++testkey1 = testvalue1\r
++testkey2 = testvalue2\r
++== stderr ==\r
++EOF\r
++test_expect_equal_file EXPECTED OUTPUT\r
++\r
++test_done\r
+-- \r
+2.8.1\r
+\r