Re: Flat search and threaded views
[notmuch-archives.git] / 35 / a3959ca1aaa5f9d65084ceca76a4fbcd3714b5
1 Return-Path: <bremner@tesseract.cs.unb.ca>\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 olra.theworths.org (Postfix) with ESMTP id C528A431E84\r
6         for <notmuch@notmuchmail.org>; Sat, 14 Mar 2015 10:05:24 -0700 (PDT)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: 2.438\r
10 X-Spam-Level: **\r
11 X-Spam-Status: No, score=2.438 tagged_above=-999 required=5\r
12         tests=[DNS_FROM_AHBL_RHSBL=2.438] autolearn=disabled\r
13 Received: from olra.theworths.org ([127.0.0.1])\r
14         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
15         with ESMTP id 6SjYlH5m0XF8 for <notmuch@notmuchmail.org>;\r
16         Sat, 14 Mar 2015 10:05:23 -0700 (PDT)\r
17 Received: from mx.xen14.node3324.gplhost.com (gitolite.debian.net\r
18         [87.98.215.224])\r
19         (using TLSv1 with cipher DHE-RSA-AES128-SHA (128/128 bits))\r
20         (No client certificate requested)\r
21         by olra.theworths.org (Postfix) with ESMTPS id 51DC4431E64\r
22         for <notmuch@notmuchmail.org>; Sat, 14 Mar 2015 10:05:23 -0700 (PDT)\r
23 Received: from remotemail by mx.xen14.node3324.gplhost.com with local (Exim\r
24         4.80) (envelope-from <bremner@tesseract.cs.unb.ca>)\r
25         id 1YWpUP-000293-Ou; Sat, 14 Mar 2015 17:04:41 +0000\r
26 Received: (nullmailer pid 4987 invoked by uid 1000); Sat, 14 Mar 2015\r
27         17:02:41 -0000\r
28 From: David Bremner <david@tethera.net>\r
29 To: notmuch@notmuchmail.org\r
30 Subject: [Patch v4 4/9] lib: add "verbose" versions of\r
31         notmuch_database_{open,create}\r
32 Date: Sat, 14 Mar 2015 18:02:29 +0100\r
33 Message-Id: <1426352554-4383-5-git-send-email-david@tethera.net>\r
34 X-Mailer: git-send-email 2.1.4\r
35 In-Reply-To: <1426352554-4383-1-git-send-email-david@tethera.net>\r
36 References: <1426352554-4383-1-git-send-email-david@tethera.net>\r
37 X-BeenThere: notmuch@notmuchmail.org\r
38 X-Mailman-Version: 2.1.13\r
39 Precedence: list\r
40 List-Id: "Use and development of the notmuch mail system."\r
41         <notmuch.notmuchmail.org>\r
42 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
43         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
44 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
45 List-Post: <mailto:notmuch@notmuchmail.org>\r
46 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
47 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
48         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
49 X-List-Received-Date: Sat, 14 Mar 2015 17:05:24 -0000\r
50 \r
51 The compatibility wrapper ensures that clients calling\r
52 notmuch_database_open will receive consistent output for now.\r
53 \r
54 The stdargs based infrastucture will be used in following commits for\r
55 a more general logging mechanism.\r
56 \r
57 The changes to notmuch-{new,search} and test/symbol-test are just to\r
58 make the test suite pass.\r
59 ---\r
60  lib/database.cc     | 96 ++++++++++++++++++++++++++++++++++++++++++++---------\r
61  lib/notmuch.h       | 21 ++++++++++++\r
62  notmuch-new.c       |  8 +++--\r
63  notmuch-search.c    |  8 +++--\r
64  test/symbol-test.cc |  6 +++-\r
65  5 files changed, 119 insertions(+), 20 deletions(-)\r
66 \r
67 diff --git a/lib/database.cc b/lib/database.cc\r
68 index 3974e2e..b774edc 100644\r
69 --- a/lib/database.cc\r
70 +++ b/lib/database.cc\r
71 @@ -349,6 +349,35 @@ notmuch_status_to_string (notmuch_status_t status)\r
72  }\r
73  \r
74  static void\r
75 +vlog_to_string (void *ctx,\r
76 +              char **status_string,\r
77 +              const char *format,\r
78 +              va_list va_args)\r
79 +{\r
80 +    if (!status_string)\r
81 +       return;\r
82 +\r
83 +    if (*status_string)\r
84 +       talloc_free (*status_string);\r
85 +\r
86 +    *status_string = talloc_vasprintf (ctx, format, va_args);\r
87 +}\r
88 +\r
89 +static void\r
90 +log_to_string (char **str,\r
91 +              const char *format,\r
92 +              ...)\r
93 +{\r
94 +    va_list va_args;\r
95 +\r
96 +    va_start (va_args, format);\r
97 +\r
98 +    vlog_to_string (NULL, str, format, va_args);\r
99 +\r
100 +    va_end (va_args);\r
101 +}\r
102 +\r
103 +static void\r
104  find_doc_ids_for_term (notmuch_database_t *notmuch,\r
105                        const char *term,\r
106                        Xapian::PostingIterator *begin,\r
107 @@ -608,28 +637,37 @@ parse_references (void *ctx,\r
108  notmuch_status_t\r
109  notmuch_database_create (const char *path, notmuch_database_t **database)\r
110  {\r
111 +    return notmuch_database_create_verbose (path, database, NULL);\r
112 +}\r
113 +\r
114 +notmuch_status_t\r
115 +notmuch_database_create_verbose (const char *path,\r
116 +                                notmuch_database_t **database,\r
117 +                                char **status_string)\r
118 +{\r
119      notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;\r
120      notmuch_database_t *notmuch = NULL;\r
121      char *notmuch_path = NULL;\r
122 +    char *message = NULL;\r
123      struct stat st;\r
124      int err;\r
125  \r
126      if (path == NULL) {\r
127 -       fprintf (stderr, "Error: Cannot create a database for a NULL path.\n");\r
128 +       log_to_string (&message, "Error: Cannot create a database for a NULL path.\n");\r
129         status = NOTMUCH_STATUS_NULL_POINTER;\r
130         goto DONE;\r
131      }\r
132  \r
133      err = stat (path, &st);\r
134      if (err) {\r
135 -       fprintf (stderr, "Error: Cannot create database at %s: %s.\n",\r
136 +       log_to_string (&message, "Error: Cannot create database at %s: %s.\n",\r
137                  path, strerror (errno));\r
138         status = NOTMUCH_STATUS_FILE_ERROR;\r
139         goto DONE;\r
140      }\r
141  \r
142      if (! S_ISDIR (st.st_mode)) {\r
143 -       fprintf (stderr, "Error: Cannot create database at %s: Not a directory.\n",\r
144 +       log_to_string (&message, "Error: Cannot create database at %s: Not a directory.\n",\r
145                  path);\r
146         status = NOTMUCH_STATUS_FILE_ERROR;\r
147         goto DONE;\r
148 @@ -640,15 +678,15 @@ notmuch_database_create (const char *path, notmuch_database_t **database)\r
149      err = mkdir (notmuch_path, 0755);\r
150  \r
151      if (err) {\r
152 -       fprintf (stderr, "Error: Cannot create directory %s: %s.\n",\r
153 +       log_to_string (&message, "Error: Cannot create directory %s: %s.\n",\r
154                  notmuch_path, strerror (errno));\r
155         status = NOTMUCH_STATUS_FILE_ERROR;\r
156         goto DONE;\r
157      }\r
158  \r
159 -    status = notmuch_database_open (path,\r
160 -                                   NOTMUCH_DATABASE_MODE_READ_WRITE,\r
161 -                                   &notmuch);\r
162 +    status = notmuch_database_open_verbose (path,\r
163 +                                           NOTMUCH_DATABASE_MODE_READ_WRITE,\r
164 +                                           &notmuch, &message);\r
165      if (status)\r
166         goto DONE;\r
167  \r
168 @@ -667,6 +705,8 @@ notmuch_database_create (const char *path, notmuch_database_t **database)\r
169      if (notmuch_path)\r
170         talloc_free (notmuch_path);\r
171  \r
172 +    if (message)\r
173 +       *status_string = strdup(message);\r
174      if (database)\r
175         *database = notmuch;\r
176      else\r
177 @@ -767,37 +807,55 @@ notmuch_database_open (const char *path,\r
178                        notmuch_database_mode_t mode,\r
179                        notmuch_database_t **database)\r
180  {\r
181 +    char *status_string = NULL;\r
182 +    notmuch_status_t status;\r
183 +\r
184 +    status = notmuch_database_open_verbose(path, mode, database,\r
185 +                                          &status_string);\r
186 +\r
187 +    if (status_string) fputs(status_string, stderr);\r
188 +\r
189 +    return status;\r
190 +}\r
191 +\r
192 +notmuch_status_t\r
193 +notmuch_database_open_verbose (const char *path,\r
194 +                              notmuch_database_mode_t mode,\r
195 +                              notmuch_database_t **database,\r
196 +                              char **status_string)\r
197 +{\r
198      notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;\r
199      void *local = talloc_new (NULL);\r
200      notmuch_database_t *notmuch = NULL;\r
201      char *notmuch_path, *xapian_path, *incompat_features;\r
202 +    char *message = NULL;\r
203      struct stat st;\r
204      int err;\r
205      unsigned int i, version;\r
206      static int initialized = 0;\r
207  \r
208      if (path == NULL) {\r
209 -       fprintf (stderr, "Error: Cannot open a database for a NULL path.\n");\r
210 +       log_to_string (&message, "Error: Cannot open a database for a NULL path.\n");\r
211         status = NOTMUCH_STATUS_NULL_POINTER;\r
212         goto DONE;\r
213      }\r
214  \r
215      if (! (notmuch_path = talloc_asprintf (local, "%s/%s", path, ".notmuch"))) {\r
216 -       fprintf (stderr, "Out of memory\n");\r
217 +       log_to_string (&message, "Out of memory\n");\r
218         status = NOTMUCH_STATUS_OUT_OF_MEMORY;\r
219         goto DONE;\r
220      }\r
221  \r
222      err = stat (notmuch_path, &st);\r
223      if (err) {\r
224 -       fprintf (stderr, "Error opening database at %s: %s\n",\r
225 +       log_to_string (&message, "Error opening database at %s: %s\n",\r
226                  notmuch_path, strerror (errno));\r
227         status = NOTMUCH_STATUS_FILE_ERROR;\r
228         goto DONE;\r
229      }\r
230  \r
231      if (! (xapian_path = talloc_asprintf (local, "%s/%s", notmuch_path, "xapian"))) {\r
232 -       fprintf (stderr, "Out of memory\n");\r
233 +       log_to_string (&message, "Out of memory\n");\r
234         status = NOTMUCH_STATUS_OUT_OF_MEMORY;\r
235         goto DONE;\r
236      }\r
237 @@ -837,7 +895,7 @@ notmuch_database_open (const char *path,\r
238          * means a dramatically incompatible change. */\r
239         version = notmuch_database_get_version (notmuch);\r
240         if (version > NOTMUCH_DATABASE_VERSION) {\r
241 -           fprintf (stderr,\r
242 +           log_to_string (&message,\r
243                      "Error: Notmuch database at %s\n"\r
244                      "       has a newer database format version (%u) than supported by this\n"\r
245                      "       version of notmuch (%u).\n",\r
246 @@ -856,7 +914,7 @@ notmuch_database_open (const char *path,\r
247             version, mode == NOTMUCH_DATABASE_MODE_READ_WRITE ? 'w' : 'r',\r
248             &incompat_features);\r
249         if (incompat_features) {\r
250 -           fprintf (stderr,\r
251 +           log_to_string (&message,\r
252                      "Error: Notmuch database at %s\n"\r
253                      "       requires features (%s)\n"\r
254                      "       not supported by this version of notmuch.\n",\r
255 @@ -906,7 +964,7 @@ notmuch_database_open (const char *path,\r
256             notmuch->query_parser->add_prefix (prefix->name, prefix->prefix);\r
257         }\r
258      } catch (const Xapian::Error &error) {\r
259 -       fprintf (stderr, "A Xapian exception occurred opening database: %s\n",\r
260 +       log_to_string (&message, "A Xapian exception occurred opening database: %s\n",\r
261                  error.get_msg().c_str());\r
262         notmuch_database_destroy (notmuch);\r
263         notmuch = NULL;\r
264 @@ -916,6 +974,9 @@ notmuch_database_open (const char *path,\r
265    DONE:\r
266      talloc_free (local);\r
267  \r
268 +    if (status_string && message)\r
269 +       *status_string = strdup (message);\r
270 +\r
271      if (database)\r
272         *database = notmuch;\r
273      else\r
274 @@ -1039,13 +1100,18 @@ notmuch_database_compact (const char *path,\r
275      notmuch_database_t *notmuch = NULL;\r
276      struct stat statbuf;\r
277      notmuch_bool_t keep_backup;\r
278 +    char *message = NULL;\r
279  \r
280      local = talloc_new (NULL);\r
281      if (! local)\r
282         return NOTMUCH_STATUS_OUT_OF_MEMORY;\r
283  \r
284 -    ret = notmuch_database_open (path, NOTMUCH_DATABASE_MODE_READ_WRITE, &notmuch);\r
285 +    ret = notmuch_database_open_verbose (path,\r
286 +                                        NOTMUCH_DATABASE_MODE_READ_WRITE,\r
287 +                                        &notmuch,\r
288 +                                        &message);\r
289      if (ret) {\r
290 +       if (status_cb) status_cb (message, closure);\r
291         goto DONE;\r
292      }\r
293  \r
294 diff --git a/lib/notmuch.h b/lib/notmuch.h\r
295 index f066245..c671d82 100644\r
296 --- a/lib/notmuch.h\r
297 +++ b/lib/notmuch.h\r
298 @@ -230,6 +230,16 @@ notmuch_status_t\r
299  notmuch_database_create (const char *path, notmuch_database_t **database);\r
300  \r
301  /**\r
302 + * Like notmuch_database_create, except optionally return an error\r
303 + * message. This message is allocated by malloc and should be freed by\r
304 + * the caller.\r
305 + */\r
306 +notmuch_status_t\r
307 +notmuch_database_create_verbose (const char *path,\r
308 +                                notmuch_database_t **database,\r
309 +                                char **error_message);\r
310 +\r
311 +/**\r
312   * Database open mode for notmuch_database_open.\r
313   */\r
314  typedef enum {\r
315 @@ -279,6 +289,17 @@ notmuch_status_t\r
316  notmuch_database_open (const char *path,\r
317                        notmuch_database_mode_t mode,\r
318                        notmuch_database_t **database);\r
319 +/**\r
320 + * Like notmuch_database_open, except optionally return an error\r
321 + * message. This message is allocated by malloc and should be freed by\r
322 + * the caller.\r
323 + */\r
324 +\r
325 +notmuch_status_t\r
326 +notmuch_database_open_verbose (const char *path,\r
327 +                              notmuch_database_mode_t mode,\r
328 +                              notmuch_database_t **database,\r
329 +                              char **error_message);\r
330  \r
331  /**\r
332   * Commit changes and close the given notmuch database.\r
333 diff --git a/notmuch-new.c b/notmuch-new.c\r
334 index ddf42c1..93b70bf 100644\r
335 --- a/notmuch-new.c\r
336 +++ b/notmuch-new.c\r
337 @@ -985,9 +985,13 @@ notmuch_new_command (notmuch_config_t *config, int argc, char *argv[])\r
338             return EXIT_FAILURE;\r
339         add_files_state.total_files = count;\r
340      } else {\r
341 -       if (notmuch_database_open (db_path, NOTMUCH_DATABASE_MODE_READ_WRITE,\r
342 -                                  &notmuch))\r
343 +       char *status_string = NULL;\r
344 +       if (notmuch_database_open_verbose (db_path, NOTMUCH_DATABASE_MODE_READ_WRITE,\r
345 +                                          &notmuch, &status_string)) {\r
346 +           if (status_string) fputs (status_string, stderr);\r
347 +\r
348             return EXIT_FAILURE;\r
349 +       }\r
350  \r
351         if (notmuch_database_needs_upgrade (notmuch)) {\r
352             time_t now = time (NULL);\r
353 diff --git a/notmuch-search.c b/notmuch-search.c\r
354 index a591d45..d012af3 100644\r
355 --- a/notmuch-search.c\r
356 +++ b/notmuch-search.c\r
357 @@ -545,6 +545,7 @@ _notmuch_search_prepare (search_context_t *ctx, notmuch_config_t *config, int ar\r
358  {\r
359      char *query_str;\r
360      unsigned int i;\r
361 +    char *status_string = NULL;\r
362  \r
363      switch (ctx->format_sel) {\r
364      case NOTMUCH_FORMAT_TEXT:\r
365 @@ -570,9 +571,12 @@ _notmuch_search_prepare (search_context_t *ctx, notmuch_config_t *config, int ar\r
366  \r
367      notmuch_exit_if_unsupported_format ();\r
368  \r
369 -    if (notmuch_database_open (notmuch_config_get_database_path (config),\r
370 -                              NOTMUCH_DATABASE_MODE_READ_ONLY, &ctx->notmuch))\r
371 +    if (notmuch_database_open_verbose (\r
372 +           notmuch_config_get_database_path (config),\r
373 +           NOTMUCH_DATABASE_MODE_READ_ONLY, &ctx->notmuch, &status_string)) {\r
374 +       if (status_string) fputs (status_string, stderr);\r
375         return EXIT_FAILURE;\r
376 +    }\r
377  \r
378      query_str = query_string_from_args (ctx->notmuch, argc, argv);\r
379      if (query_str == NULL) {\r
380 diff --git a/test/symbol-test.cc b/test/symbol-test.cc\r
381 index 3e96c03..9f8eea7 100644\r
382 --- a/test/symbol-test.cc\r
383 +++ b/test/symbol-test.cc\r
384 @@ -5,7 +5,11 @@\r
385  \r
386  int main() {\r
387    notmuch_database_t *notmuch;\r
388 -  notmuch_database_open("fakedb", NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch);\r
389 +  char *message = NULL;\r
390 +\r
391 +  if (notmuch_database_open_verbose  ("fakedb", NOTMUCH_DATABASE_MODE_READ_ONLY, &notmuch, &message))\r
392 +      if (message) fputs (message, stderr);\r
393 +\r
394  \r
395    try {\r
396      (void) new Xapian::WritableDatabase("./nonexistant", Xapian::DB_OPEN);\r
397 -- \r
398 2.1.4\r
399 \r