--- /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 059686DE1749\r
+ for <notmuch@notmuchmail.org>; Tue, 12 Jan 2016 19:10:28 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at cworth.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.31\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.31 tagged_above=-999 required=5 tests=[AWL=0.241, \r
+ RP_MATCHES_RCVD=-0.55, SPF_PASS=-0.001] 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 EwyVTSeXyHU4 for <notmuch@notmuchmail.org>;\r
+ Tue, 12 Jan 2016 19:10:25 -0800 (PST)\r
+Received: from fethera.tethera.net (fethera.tethera.net [198.245.60.197])\r
+ by arlo.cworth.org (Postfix) with ESMTPS id 6A6F66DE01FF\r
+ for <notmuch@notmuchmail.org>; Tue, 12 Jan 2016 19:10:22 -0800 (PST)\r
+Received: from remotemail by fethera.tethera.net with local (Exim 4.84)\r
+ (envelope-from <bremner@tethera.net>)\r
+ id 1aJBp6-0000yH-48; Tue, 12 Jan 2016 22:10:12 -0500\r
+Received: (nullmailer pid 23096 invoked by uid 1000);\r
+ Wed, 13 Jan 2016 03:10:18 -0000\r
+From: David Bremner <david@tethera.net>\r
+To: notmuch@notmuchmail.org\r
+Subject: [WIP2 1/4] lib: provide config API\r
+Date: Tue, 12 Jan 2016 23:10:07 -0400\r
+Message-Id: <1452654610-22864-2-git-send-email-david@tethera.net>\r
+X-Mailer: git-send-email 2.6.4\r
+In-Reply-To: <1452654610-22864-1-git-send-email-david@tethera.net>\r
+References: <1452654610-22864-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, 13 Jan 2016 03:10:28 -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 | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++\r
+ lib/notmuch.h | 20 +++++++++++\r
+ test/T590-libconfig.sh | 58 ++++++++++++++++++++++++++++++++\r
+ 4 files changed, 169 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 3a07090..eb442d1 100644\r
+--- a/lib/Makefile.local\r
++++ b/lib/Makefile.local\r
+@@ -48,6 +48,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..af00d6f\r
+--- /dev/null\r
++++ b/lib/config.cc\r
+@@ -0,0 +1,90 @@\r
++/* metadata.cc - API for database metadata\r
++ *\r
++ * Copyright © 2015 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
++ if (! notmuch->exception_reported) {\r
++ _notmuch_database_log (notmuch, "Error: A Xapian exception occurred setting metadata: %s\n",\r
++ error.get_msg().c_str());\r
++ }\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
++ if (! notmuch->exception_reported) {\r
++ _notmuch_database_log (notmuch, "Error: A Xapian exception occurred getting metadata: %s\n",\r
++ error.get_msg().c_str());\r
++ }\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
++ 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 310a8b8..c62223b 100644\r
+--- a/lib/notmuch.h\r
++++ b/lib/notmuch.h\r
+@@ -1829,6 +1829,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
+ \r
+ NOTMUCH_END_DECLS\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.6.4\r
+\r