Re: [WIP PATCH] emacs: query: completion for from: in searches
[notmuch-archives.git] / a3 / 3ddfdfad318fdf54c5b880e3d249e5334c3e96
1 Return-Path: <bremner@tethera.net>\r
2 X-Original-To: notmuch@notmuchmail.org\r
3 Delivered-To: notmuch@notmuchmail.org\r
4 Received: from localhost (localhost [127.0.0.1])\r
5  by arlo.cworth.org (Postfix) with ESMTP id CE2446DE18A6\r
6  for <notmuch@notmuchmail.org>; Sat, 12 Mar 2016 04:31:42 -0800 (PST)\r
7 X-Virus-Scanned: Debian amavisd-new at cworth.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: -0.033\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=-0.033 tagged_above=-999 required=5\r
12  tests=[AWL=-0.022, SPF_PASS=-0.001, T_RP_MATCHES_RCVD=-0.01]\r
13  autolearn=disabled\r
14 Received: from arlo.cworth.org ([127.0.0.1])\r
15  by localhost (arlo.cworth.org [127.0.0.1]) (amavisd-new, port 10024)\r
16  with ESMTP id duFu2a-b6Nzv for <notmuch@notmuchmail.org>;\r
17  Sat, 12 Mar 2016 04:31:39 -0800 (PST)\r
18 Received: from fethera.tethera.net (fethera.tethera.net [198.245.60.197])\r
19  by arlo.cworth.org (Postfix) with ESMTPS id 2C08B6DE182F\r
20  for <notmuch@notmuchmail.org>; Sat, 12 Mar 2016 04:31:38 -0800 (PST)\r
21 Received: from remotemail by fethera.tethera.net with local (Exim 4.84)\r
22  (envelope-from <bremner@tethera.net>)\r
23  id 1aeiiM-0004z0-T5; Sat, 12 Mar 2016 07:32:14 -0500\r
24 Received: (nullmailer pid 17185 invoked by uid 1000);\r
25  Sat, 12 Mar 2016 12:31:33 -0000\r
26 From: David Bremner <david@tethera.net>\r
27 To: David Bremner <david@tethera.net>, notmuch@notmuchmail.org\r
28 Subject: [PATCH 6/6] WIP: support XDG database directory\r
29 Date: Sat, 12 Mar 2016 08:31:30 -0400\r
30 Message-Id: <1457785890-17058-7-git-send-email-david@tethera.net>\r
31 X-Mailer: git-send-email 2.7.0\r
32 In-Reply-To: <1457785890-17058-1-git-send-email-david@tethera.net>\r
33 References: <1453561198-2893-1-git-send-email-david@tethera.net>\r
34  <1457785890-17058-1-git-send-email-david@tethera.net>\r
35 X-BeenThere: notmuch@notmuchmail.org\r
36 X-Mailman-Version: 2.1.20\r
37 Precedence: list\r
38 List-Id: "Use and development of the notmuch mail system."\r
39  <notmuch.notmuchmail.org>\r
40 List-Unsubscribe: <https://notmuchmail.org/mailman/options/notmuch>,\r
41  <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
42 List-Archive: <http://notmuchmail.org/pipermail/notmuch/>\r
43 List-Post: <mailto:notmuch@notmuchmail.org>\r
44 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
45 List-Subscribe: <https://notmuchmail.org/mailman/listinfo/notmuch>,\r
46  <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
47 X-List-Received-Date: Sat, 12 Mar 2016 12:31:43 -0000\r
48 \r
49 ---\r
50  lib/database.cc        | 66 +++++++++++++++++++++++++++++++++++++++++---------\r
51  test/T560-lib-error.sh |  2 +-\r
52  test/T590-libconfig.sh | 35 ++++++++++++++++++++++++++\r
53  3 files changed, 90 insertions(+), 13 deletions(-)\r
54 \r
55 diff --git a/lib/database.cc b/lib/database.cc\r
56 index 3b342f1..3d19bec 100644\r
57 --- a/lib/database.cc\r
58 +++ b/lib/database.cc\r
59 @@ -855,6 +855,23 @@ notmuch_database_open (const char *path,\r
60      return status;\r
61  }\r
62  \r
63 +static char *\r
64 +_xdg_database_path (void *ctx) {\r
65 +\r
66 +    const char *data_dir = NULL;\r
67 +\r
68 +    data_dir = getenv ("XDG_DATA_HOME");\r
69 +\r
70 +    if (! data_dir) {\r
71 +       const char *home = getenv ("HOME");\r
72 +       if (! home)\r
73 +           return NULL;\r
74 +\r
75 +       data_dir = talloc_asprintf (ctx, "%s/.local/share", home);\r
76 +    }\r
77 +    return talloc_asprintf (ctx, "%s/notmuch", data_dir);\r
78 +}\r
79 +\r
80  notmuch_status_t\r
81  notmuch_database_open_verbose (const char *path,\r
82                                notmuch_database_mode_t mode,\r
83 @@ -865,6 +882,7 @@ notmuch_database_open_verbose (const char *path,\r
84      void *local = talloc_new (NULL);\r
85      notmuch_database_t *notmuch = NULL;\r
86      char *notmuch_path, *xapian_path, *incompat_features;\r
87 +    char *xdg_path = NULL;\r
88      char *message = NULL;\r
89      struct stat st;\r
90      int err;\r
91 @@ -872,21 +890,29 @@ notmuch_database_open_verbose (const char *path,\r
92      static int initialized = 0;\r
93  \r
94      if (path == NULL) {\r
95 -       message = strdup ("Error: Cannot open a database for a NULL path.\n");\r
96 -       status = NOTMUCH_STATUS_NULL_POINTER;\r
97 -       goto DONE;\r
98 +       xdg_path = _xdg_database_path (local);\r
99 +       if (! xdg_path) {\r
100 +           message = strdup ("Error: NULL path, and cannot compute XDG_DATA_HOME.\n");\r
101 +           status = NOTMUCH_STATUS_NULL_POINTER;\r
102 +           goto DONE;\r
103 +       }\r
104      }\r
105  \r
106 -    if (path[0] != '/') {\r
107 +    if (path && path[0] != '/') {\r
108         message = strdup ("Error: Database path must be absolute.\n");\r
109         status = NOTMUCH_STATUS_PATH_ERROR;\r
110         goto DONE;\r
111      }\r
112  \r
113 -    if (! (notmuch_path = talloc_asprintf (local, "%s/%s", path, ".notmuch"))) {\r
114 -       message = strdup ("Out of memory\n");\r
115 -       status = NOTMUCH_STATUS_OUT_OF_MEMORY;\r
116 -       goto DONE;\r
117 +    if (xdg_path) {\r
118 +       notmuch_path = xdg_path;\r
119 +    } else {\r
120 +       notmuch_path = talloc_asprintf (local, "%s/%s", path, ".notmuch");\r
121 +       if (! (notmuch_path)) {\r
122 +           message = strdup ("Out of memory\n");\r
123 +           status = NOTMUCH_STATUS_OUT_OF_MEMORY;\r
124 +           goto DONE;\r
125 +       }\r
126      }\r
127  \r
128      err = stat (notmuch_path, &st);\r
129 @@ -917,10 +943,14 @@ notmuch_database_open_verbose (const char *path,\r
130      notmuch = talloc_zero (NULL, notmuch_database_t);\r
131      notmuch->exception_reported = FALSE;\r
132      notmuch->status_string = NULL;\r
133 -    notmuch->path = talloc_strdup (notmuch, path);\r
134 +    if (path) {\r
135 +       notmuch->path = talloc_strdup (notmuch, path);\r
136  \r
137 -    if (notmuch->path[strlen (notmuch->path) - 1] == '/')\r
138 -       notmuch->path[strlen (notmuch->path) - 1] = '\0';\r
139 +       if (notmuch->path[strlen (notmuch->path) - 1] == '/')\r
140 +           notmuch->path[strlen (notmuch->path) - 1] = '\0';\r
141 +    } else {\r
142 +       notmuch->path = NULL;\r
143 +    }\r
144  \r
145      notmuch->mode = mode;\r
146      notmuch->atomic_nesting = 0;\r
147 @@ -1303,7 +1333,19 @@ notmuch_database_destroy (notmuch_database_t *notmuch)\r
148  const char *\r
149  notmuch_database_get_path (notmuch_database_t *notmuch)\r
150  {\r
151 -    return notmuch->path;\r
152 +    char *path = NULL;\r
153 +    notmuch_status_t status;\r
154 +\r
155 +    if (notmuch->path)\r
156 +       return notmuch->path;\r
157 +\r
158 +    status = notmuch_database_get_config (notmuch, "maildir_root", &path);\r
159 +    if (status) {\r
160 +       _notmuch_database_log (notmuch, "unable to find maildir_root\n");\r
161 +       return NULL;\r
162 +    }\r
163 +\r
164 +    return path;\r
165  }\r
166  \r
167  unsigned int\r
168 diff --git a/test/T560-lib-error.sh b/test/T560-lib-error.sh\r
169 index 59a479c..8d4eaf5 100755\r
170 --- a/test/T560-lib-error.sh\r
171 +++ b/test/T560-lib-error.sh\r
172 @@ -21,7 +21,7 @@ EOF\r
173  cat <<'EOF' >EXPECTED\r
174  == stdout ==\r
175  == stderr ==\r
176 -Error: Cannot open a database for a NULL path.\r
177 +Error opening database at CWD/home/.local/share/notmuch: No such file or directory\r
178  EOF\r
179  test_expect_equal_file EXPECTED OUTPUT\r
180  \r
181 diff --git a/test/T590-libconfig.sh b/test/T590-libconfig.sh\r
182 index 9c1e566..52b06bb 100755\r
183 --- a/test/T590-libconfig.sh\r
184 +++ b/test/T590-libconfig.sh\r
185 @@ -143,4 +143,39 @@ notmuch restore --include=config <EXPECTED\r
186  notmuch dump --include=config >OUTPUT\r
187  test_expect_equal_file EXPECTED OUTPUT\r
188  \r
189 +XDG_DIR=$HOME/.local/share/notmuch\r
190 +test_begin_subtest "Split database and maildir"\r
191 +xapian-metadata set ${MAIL_DIR}/.notmuch/xapian Cmaildir_root ${MAIL_DIR}\r
192 +mkdir -p $XDG_DIR\r
193 +mv ${MAIL_DIR}/.notmuch/xapian $XDG_DIR\r
194 +test_C <<EOF >OUTPUT\r
195 +#include <stdio.h>\r
196 +#include <notmuch.h>\r
197 +\r
198 +int main (int argc, char** argv)\r
199 +{\r
200 +   notmuch_database_t *db;\r
201 +   char *val;\r
202 +   notmuch_status_t stat;\r
203 +   notmuch_message_t *message;\r
204 +\r
205 +   stat=notmuch_database_open (NULL, NOTMUCH_DATABASE_MODE_READ_WRITE, &db);\r
206 +   printf("database_open status = %d\n", stat);\r
207 +   stat = notmuch_database_find_message (db, "87ocn0qh6d.fsf@yoom.home.cworth.org", &message);\r
208 +   printf("find_message status = %d\n", stat);\r
209 +   printf("found message = %d\n", message != NULL);\r
210 +   printf("filename = %s\n",notmuch_message_get_filename (message));\r
211 +}\r
212 +EOF\r
213 +\r
214 +cat <<EOF >EXPECTED\r
215 +== stdout ==\r
216 +database_open status = 0\r
217 +find_message status = 0\r
218 +found message = 1\r
219 +filename = MAIL_DIR/cur/41:2,\r
220 +== stderr ==\r
221 +EOF\r
222 +test_expect_equal_file EXPECTED OUTPUT\r
223 +\r
224  test_done\r
225 -- \r
226 2.7.0\r
227 \r