--- /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 CE2446DE18A6\r
+ for <notmuch@notmuchmail.org>; Sat, 12 Mar 2016 04:31:42 -0800 (PST)\r
+X-Virus-Scanned: Debian amavisd-new at cworth.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.033\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.033 tagged_above=-999 required=5\r
+ tests=[AWL=-0.022, 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 duFu2a-b6Nzv for <notmuch@notmuchmail.org>;\r
+ Sat, 12 Mar 2016 04:31:39 -0800 (PST)\r
+Received: from fethera.tethera.net (fethera.tethera.net [198.245.60.197])\r
+ by arlo.cworth.org (Postfix) with ESMTPS id 2C08B6DE182F\r
+ for <notmuch@notmuchmail.org>; Sat, 12 Mar 2016 04:31:38 -0800 (PST)\r
+Received: from remotemail by fethera.tethera.net with local (Exim 4.84)\r
+ (envelope-from <bremner@tethera.net>)\r
+ id 1aeiiM-0004z0-T5; Sat, 12 Mar 2016 07:32:14 -0500\r
+Received: (nullmailer pid 17185 invoked by uid 1000);\r
+ Sat, 12 Mar 2016 12:31:33 -0000\r
+From: David Bremner <david@tethera.net>\r
+To: David Bremner <david@tethera.net>, notmuch@notmuchmail.org\r
+Subject: [PATCH 6/6] WIP: support XDG database directory\r
+Date: Sat, 12 Mar 2016 08:31:30 -0400\r
+Message-Id: <1457785890-17058-7-git-send-email-david@tethera.net>\r
+X-Mailer: git-send-email 2.7.0\r
+In-Reply-To: <1457785890-17058-1-git-send-email-david@tethera.net>\r
+References: <1453561198-2893-1-git-send-email-david@tethera.net>\r
+ <1457785890-17058-1-git-send-email-david@tethera.net>\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: Sat, 12 Mar 2016 12:31:43 -0000\r
+\r
+---\r
+ lib/database.cc | 66 +++++++++++++++++++++++++++++++++++++++++---------\r
+ test/T560-lib-error.sh | 2 +-\r
+ test/T590-libconfig.sh | 35 ++++++++++++++++++++++++++\r
+ 3 files changed, 90 insertions(+), 13 deletions(-)\r
+\r
+diff --git a/lib/database.cc b/lib/database.cc\r
+index 3b342f1..3d19bec 100644\r
+--- a/lib/database.cc\r
++++ b/lib/database.cc\r
+@@ -855,6 +855,23 @@ notmuch_database_open (const char *path,\r
+ return status;\r
+ }\r
+ \r
++static char *\r
++_xdg_database_path (void *ctx) {\r
++\r
++ const char *data_dir = NULL;\r
++\r
++ data_dir = getenv ("XDG_DATA_HOME");\r
++\r
++ if (! data_dir) {\r
++ const char *home = getenv ("HOME");\r
++ if (! home)\r
++ return NULL;\r
++\r
++ data_dir = talloc_asprintf (ctx, "%s/.local/share", home);\r
++ }\r
++ return talloc_asprintf (ctx, "%s/notmuch", data_dir);\r
++}\r
++\r
+ notmuch_status_t\r
+ notmuch_database_open_verbose (const char *path,\r
+ notmuch_database_mode_t mode,\r
+@@ -865,6 +882,7 @@ notmuch_database_open_verbose (const char *path,\r
+ void *local = talloc_new (NULL);\r
+ notmuch_database_t *notmuch = NULL;\r
+ char *notmuch_path, *xapian_path, *incompat_features;\r
++ char *xdg_path = NULL;\r
+ char *message = NULL;\r
+ struct stat st;\r
+ int err;\r
+@@ -872,21 +890,29 @@ notmuch_database_open_verbose (const char *path,\r
+ static int initialized = 0;\r
+ \r
+ if (path == NULL) {\r
+- message = strdup ("Error: Cannot open a database for a NULL path.\n");\r
+- status = NOTMUCH_STATUS_NULL_POINTER;\r
+- goto DONE;\r
++ xdg_path = _xdg_database_path (local);\r
++ if (! xdg_path) {\r
++ message = strdup ("Error: NULL path, and cannot compute XDG_DATA_HOME.\n");\r
++ status = NOTMUCH_STATUS_NULL_POINTER;\r
++ goto DONE;\r
++ }\r
+ }\r
+ \r
+- if (path[0] != '/') {\r
++ if (path && path[0] != '/') {\r
+ message = strdup ("Error: Database path must be absolute.\n");\r
+ status = NOTMUCH_STATUS_PATH_ERROR;\r
+ goto DONE;\r
+ }\r
+ \r
+- if (! (notmuch_path = talloc_asprintf (local, "%s/%s", path, ".notmuch"))) {\r
+- message = strdup ("Out of memory\n");\r
+- status = NOTMUCH_STATUS_OUT_OF_MEMORY;\r
+- goto DONE;\r
++ if (xdg_path) {\r
++ notmuch_path = xdg_path;\r
++ } else {\r
++ notmuch_path = talloc_asprintf (local, "%s/%s", path, ".notmuch");\r
++ if (! (notmuch_path)) {\r
++ message = strdup ("Out of memory\n");\r
++ status = NOTMUCH_STATUS_OUT_OF_MEMORY;\r
++ goto DONE;\r
++ }\r
+ }\r
+ \r
+ err = stat (notmuch_path, &st);\r
+@@ -917,10 +943,14 @@ notmuch_database_open_verbose (const char *path,\r
+ notmuch = talloc_zero (NULL, notmuch_database_t);\r
+ notmuch->exception_reported = FALSE;\r
+ notmuch->status_string = NULL;\r
+- notmuch->path = talloc_strdup (notmuch, path);\r
++ if (path) {\r
++ notmuch->path = talloc_strdup (notmuch, path);\r
+ \r
+- if (notmuch->path[strlen (notmuch->path) - 1] == '/')\r
+- notmuch->path[strlen (notmuch->path) - 1] = '\0';\r
++ if (notmuch->path[strlen (notmuch->path) - 1] == '/')\r
++ notmuch->path[strlen (notmuch->path) - 1] = '\0';\r
++ } else {\r
++ notmuch->path = NULL;\r
++ }\r
+ \r
+ notmuch->mode = mode;\r
+ notmuch->atomic_nesting = 0;\r
+@@ -1303,7 +1333,19 @@ notmuch_database_destroy (notmuch_database_t *notmuch)\r
+ const char *\r
+ notmuch_database_get_path (notmuch_database_t *notmuch)\r
+ {\r
+- return notmuch->path;\r
++ char *path = NULL;\r
++ notmuch_status_t status;\r
++\r
++ if (notmuch->path)\r
++ return notmuch->path;\r
++\r
++ status = notmuch_database_get_config (notmuch, "maildir_root", &path);\r
++ if (status) {\r
++ _notmuch_database_log (notmuch, "unable to find maildir_root\n");\r
++ return NULL;\r
++ }\r
++\r
++ return path;\r
+ }\r
+ \r
+ unsigned int\r
+diff --git a/test/T560-lib-error.sh b/test/T560-lib-error.sh\r
+index 59a479c..8d4eaf5 100755\r
+--- a/test/T560-lib-error.sh\r
++++ b/test/T560-lib-error.sh\r
+@@ -21,7 +21,7 @@ EOF\r
+ cat <<'EOF' >EXPECTED\r
+ == stdout ==\r
+ == stderr ==\r
+-Error: Cannot open a database for a NULL path.\r
++Error opening database at CWD/home/.local/share/notmuch: No such file or directory\r
+ EOF\r
+ test_expect_equal_file EXPECTED OUTPUT\r
+ \r
+diff --git a/test/T590-libconfig.sh b/test/T590-libconfig.sh\r
+index 9c1e566..52b06bb 100755\r
+--- a/test/T590-libconfig.sh\r
++++ b/test/T590-libconfig.sh\r
+@@ -143,4 +143,39 @@ notmuch restore --include=config <EXPECTED\r
+ notmuch dump --include=config >OUTPUT\r
+ test_expect_equal_file EXPECTED OUTPUT\r
+ \r
++XDG_DIR=$HOME/.local/share/notmuch\r
++test_begin_subtest "Split database and maildir"\r
++xapian-metadata set ${MAIL_DIR}/.notmuch/xapian Cmaildir_root ${MAIL_DIR}\r
++mkdir -p $XDG_DIR\r
++mv ${MAIL_DIR}/.notmuch/xapian $XDG_DIR\r
++test_C <<EOF >OUTPUT\r
++#include <stdio.h>\r
++#include <notmuch.h>\r
++\r
++int main (int argc, char** argv)\r
++{\r
++ notmuch_database_t *db;\r
++ char *val;\r
++ notmuch_status_t stat;\r
++ notmuch_message_t *message;\r
++\r
++ stat=notmuch_database_open (NULL, NOTMUCH_DATABASE_MODE_READ_WRITE, &db);\r
++ printf("database_open status = %d\n", stat);\r
++ stat = notmuch_database_find_message (db, "87ocn0qh6d.fsf@yoom.home.cworth.org", &message);\r
++ printf("find_message status = %d\n", stat);\r
++ printf("found message = %d\n", message != NULL);\r
++ printf("filename = %s\n",notmuch_message_get_filename (message));\r
++}\r
++EOF\r
++\r
++cat <<EOF >EXPECTED\r
++== stdout ==\r
++database_open status = 0\r
++find_message status = 0\r
++found message = 1\r
++filename = MAIL_DIR/cur/41:2,\r
++== stderr ==\r
++EOF\r
++test_expect_equal_file EXPECTED OUTPUT\r
++\r
+ test_done\r
+-- \r
+2.7.0\r
+\r