Re: message-default-mail-headers not working in notmuch 0.22
[notmuch-archives.git] / 1f / 2b8774483d22689fb577fc584e2e3dc6b01537
1 Return-Path: <polatel@gmail.com>\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 03D0B429E32\r
6         for <notmuch@notmuchmail.org>; Sat,  1 Oct 2011 01:12:42 -0700 (PDT)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References"\r
9 X-Spam-Flag: NO\r
10 X-Spam-Score: 1.7\r
11 X-Spam-Level: *\r
12 X-Spam-Status: No, score=1.7 tagged_above=-999 required=5\r
13         tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1,\r
14         FREEMAIL_FROM=0.001, FREEMAIL_REPLY=2.499, RCVD_IN_DNSWL_LOW=-0.7]\r
15         autolearn=disabled\r
16 Received: from olra.theworths.org ([127.0.0.1])\r
17         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
18         with ESMTP id Ya6T5iSROiWU for <notmuch@notmuchmail.org>;\r
19         Sat,  1 Oct 2011 01:12:38 -0700 (PDT)\r
20 Received: from mail-bw0-f53.google.com (mail-bw0-f53.google.com\r
21         [209.85.214.53]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
22         (No client certificate requested)\r
23         by olra.theworths.org (Postfix) with ESMTPS id 8E3DF429E27\r
24         for <notmuch@notmuchmail.org>; Sat,  1 Oct 2011 01:12:37 -0700 (PDT)\r
25 Received: by mail-bw0-f53.google.com with SMTP id zt12so3139417bkb.26\r
26         for <notmuch@notmuchmail.org>; Sat, 01 Oct 2011 01:12:37 -0700 (PDT)\r
27 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma;\r
28         h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references\r
29         :in-reply-to:references:organization;\r
30         bh=hkdhO+3pQe698NimANvpGTGK7RhrGuIlerZfKPflliA=;\r
31         b=oHMNP94s0SEsGYD5NhoTPDRgkkZdNibcs9gnJ3MDpkwgP7c4mPTtCjTZF9cTxebJfj\r
32         vXkGddFKBEZ7oDucgxttczBYSuTDc+7Ji/lz8M8wWwnjkhtyP9JW49BNjNMh9sFYHgxK\r
33         ijpyQ6r+8ocFsJnpDHlGRQjNhpHcdgPiMwXi0=\r
34 Received: by 10.223.50.21 with SMTP id x21mr1107639faf.24.1317456757205;\r
35         Sat, 01 Oct 2011 01:12:37 -0700 (PDT)\r
36 Received: from localhost ([78.183.84.0])\r
37         by mx.google.com with ESMTPS id a7sm10379749fam.22.2011.10.01.01.12.35\r
38         (version=TLSv1/SSLv3 cipher=OTHER);\r
39         Sat, 01 Oct 2011 01:12:36 -0700 (PDT)\r
40 From: Ali Polatel <polatel@gmail.com>\r
41 To: Austin Clements <amdragon@MIT.EDU>\r
42 Subject: [PATCH] lib: make find_message{,by_filename) report errors\r
43 Date: Sat,  1 Oct 2011 11:12:24 +0300\r
44 Message-Id:\r
45  <7db57727233597b566579837eb653e8e6ba2aaa6.1317456435.git.alip@exherbo.org>\r
46 X-Mailer: git-send-email 1.7.6.1\r
47 In-Reply-To: <cover.1317456435.git.alip@exherbo.org>\r
48 References: <20110930064712.GA30012@hayalet>\r
49         <cover.1317456435.git.alip@exherbo.org>\r
50 In-Reply-To: <cover.1317456435.git.alip@exherbo.org>\r
51 References: <cover.1317456435.git.alip@exherbo.org>\r
52 Organization: Pink Floyd\r
53 Cc: Notmuch Mailing List <notmuch@notmuchmail.org>\r
54 X-BeenThere: notmuch@notmuchmail.org\r
55 X-Mailman-Version: 2.1.13\r
56 Precedence: list\r
57 List-Id: "Use and development of the notmuch mail system."\r
58         <notmuch.notmuchmail.org>\r
59 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
60         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
61 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
62 List-Post: <mailto:notmuch@notmuchmail.org>\r
63 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
64 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
65         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
66 X-List-Received-Date: Sat, 01 Oct 2011 08:12:42 -0000\r
67 \r
68 Previously, the functions notmuch_database_find_message() and\r
69 notmuch_database_find_message_by_filename() functions did not properly\r
70 report error condition to the library user.\r
71 \r
72 For more information, read the thread on the notmuch mailing list\r
73 starting with my mail "id:871uv2unfd.fsf@gmail.com"\r
74 \r
75 Make these functions accept a pointer to 'notmuch_message_t' as argument\r
76 and return notmuch_status_t which may be used to check for any error\r
77 condition.\r
78 \r
79 restore: Modify for the new notmuch_database_find_message()\r
80 new: Modify for the new notmuch_database_find_message_by_filename()\r
81 ---\r
82  lib/database.cc   |   89 ++++++++++++++++++++++++++++++++++------------------\r
83  lib/message.cc    |    6 ++--\r
84  lib/notmuch.h     |   61 +++++++++++++++++++++++++-----------\r
85  notmuch-new.c     |    4 ++-\r
86  notmuch-restore.c |   11 ++++--\r
87  5 files changed, 114 insertions(+), 57 deletions(-)\r
88 \r
89 diff --git a/lib/database.cc b/lib/database.cc\r
90 index 9299c8d..6641aa5 100644\r
91 --- a/lib/database.cc\r
92 +++ b/lib/database.cc\r
93 @@ -360,13 +360,17 @@ _message_id_compressed (void *ctx, const char *message_id)\r
94      return compressed;\r
95  }\r
96  \r
97 -notmuch_message_t *\r
98 +notmuch_status_t\r
99  notmuch_database_find_message (notmuch_database_t *notmuch,\r
100 -                              const char *message_id)\r
101 +                              const char *message_id,\r
102 +                              notmuch_message_t **message)\r
103  {\r
104      notmuch_private_status_t status;\r
105      unsigned int doc_id;\r
106  \r
107 +    if (message == NULL)\r
108 +           return NOTMUCH_STATUS_NULL_POINTER;\r
109 +\r
110      if (strlen (message_id) > NOTMUCH_MESSAGE_ID_MAX)\r
111         message_id = _message_id_compressed (notmuch, message_id);\r
112  \r
113 @@ -375,14 +379,20 @@ notmuch_database_find_message (notmuch_database_t *notmuch,\r
114                                                        message_id, &doc_id);\r
115  \r
116         if (status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND)\r
117 -           return NULL;\r
118 +           *message = NULL;\r
119 +       else {\r
120 +           *message = _notmuch_message_create (notmuch, notmuch, doc_id, NULL);\r
121 +           if (*message == NULL)\r
122 +                   return NOTMUCH_STATUS_OUT_OF_MEMORY;\r
123 +       }\r
124  \r
125 -       return _notmuch_message_create (notmuch, notmuch, doc_id, NULL);\r
126 +       return NOTMUCH_STATUS_SUCCESS;\r
127      } catch (const Xapian::Error &error) {\r
128         fprintf (stderr, "A Xapian exception occurred finding message: %s.\n",\r
129                  error.get_msg().c_str());\r
130         notmuch->exception_reported = TRUE;\r
131 -       return NULL;\r
132 +       *message = NULL;\r
133 +       return NOTMUCH_STATUS_XAPIAN_EXCEPTION;\r
134      }\r
135  }\r
136  \r
137 @@ -1311,7 +1321,8 @@ _get_metadata_thread_id_key (void *ctx, const char *message_id)\r
138  \r
139  /* Find the thread ID to which the message with 'message_id' belongs.\r
140   *\r
141 - * Always returns a newly talloced string belonging to 'ctx'.\r
142 + * Note: 'thread_id' must not be NULL!\r
143 + * On success '*thread_id' is set to a newly talloced string belonging to 'ctx'.\r
144   *\r
145   * Note: If there is no message in the database with the given\r
146   * 'message_id' then a new thread_id will be allocated for this\r
147 @@ -1319,25 +1330,29 @@ _get_metadata_thread_id_key (void *ctx, const char *message_id)\r
148   * thread ID can be looked up if the message is added to the database\r
149   * later).\r
150   */\r
151 -static const char *\r
152 +static notmuch_status_t\r
153  _resolve_message_id_to_thread_id (notmuch_database_t *notmuch,\r
154                                   void *ctx,\r
155 -                                 const char *message_id)\r
156 +                                 const char *message_id,\r
157 +                                 const char **thread_id)\r
158  {\r
159 +    notmuch_status_t status;\r
160      notmuch_message_t *message;\r
161      string thread_id_string;\r
162 -    const char *thread_id;\r
163      char *metadata_key;\r
164      Xapian::WritableDatabase *db;\r
165  \r
166 -    message = notmuch_database_find_message (notmuch, message_id);\r
167 +    status = notmuch_database_find_message (notmuch, message_id, &message);\r
168 +\r
169 +    if (status)\r
170 +       return status;\r
171  \r
172      if (message) {\r
173 -       thread_id = talloc_steal (ctx, notmuch_message_get_thread_id (message));\r
174 +       *thread_id = talloc_steal (ctx, notmuch_message_get_thread_id (message));\r
175  \r
176         notmuch_message_destroy (message);\r
177  \r
178 -       return thread_id;\r
179 +       return NOTMUCH_STATUS_SUCCESS;\r
180      }\r
181  \r
182      /* Message has not been seen yet.\r
183 @@ -1351,15 +1366,15 @@ _resolve_message_id_to_thread_id (notmuch_database_t *notmuch,\r
184      thread_id_string = notmuch->xapian_db->get_metadata (metadata_key);\r
185  \r
186      if (thread_id_string.empty()) {\r
187 -       thread_id = _notmuch_database_generate_thread_id (notmuch);\r
188 -       db->set_metadata (metadata_key, thread_id);\r
189 +       *thread_id = talloc_strdup(ctx, _notmuch_database_generate_thread_id (notmuch));\r
190 +       db->set_metadata (metadata_key, *thread_id);\r
191      } else {\r
192 -       thread_id = thread_id_string.c_str();\r
193 +       *thread_id = talloc_strdup(ctx, thread_id_string.c_str());\r
194      }\r
195  \r
196      talloc_free (metadata_key);\r
197  \r
198 -    return talloc_strdup (ctx, thread_id);\r
199 +    return NOTMUCH_STATUS_SUCCESS;\r
200  }\r
201  \r
202  static notmuch_status_t\r
203 @@ -1446,9 +1461,12 @@ _notmuch_database_link_message_to_parents (notmuch_database_t *notmuch,\r
204         _notmuch_message_add_term (message, "reference",\r
205                                    parent_message_id);\r
206  \r
207 -       parent_thread_id = _resolve_message_id_to_thread_id (notmuch,\r
208 -                                                            message,\r
209 -                                                            parent_message_id);\r
210 +       ret = _resolve_message_id_to_thread_id (notmuch,\r
211 +                                               message,\r
212 +                                               parent_message_id,\r
213 +                                               &parent_thread_id);\r
214 +       if (ret)\r
215 +           goto DONE;\r
216  \r
217         if (*thread_id == NULL) {\r
218             *thread_id = talloc_strdup (message, parent_thread_id);\r
219 @@ -1759,11 +1777,12 @@ notmuch_status_t\r
220  notmuch_database_remove_message (notmuch_database_t *notmuch,\r
221                                  const char *filename)\r
222  {\r
223 -    notmuch_message_t *message =\r
224 -       notmuch_database_find_message_by_filename (notmuch, filename);\r
225 -    notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;\r
226 +    notmuch_status_t status;\r
227 +    notmuch_message_t *message;\r
228  \r
229 -    if (message) {\r
230 +    status = notmuch_database_find_message_by_filename (notmuch, filename, &message);\r
231 +\r
232 +    if (status == NOTMUCH_STATUS_SUCCESS && message) {\r
233             status = _notmuch_message_remove_filename (message, filename);\r
234             if (status == NOTMUCH_STATUS_SUCCESS)\r
235                 _notmuch_message_delete (message);\r
236 @@ -1774,24 +1793,27 @@ notmuch_database_remove_message (notmuch_database_t *notmuch,\r
237      return status;\r
238  }\r
239  \r
240 -notmuch_message_t *\r
241 +notmuch_status_t\r
242  notmuch_database_find_message_by_filename (notmuch_database_t *notmuch,\r
243 -                                          const char *filename)\r
244 +                                          const char *filename,\r
245 +                                          notmuch_message_t **message)\r
246  {\r
247      void *local;\r
248      const char *prefix = _find_prefix ("file-direntry");\r
249      char *direntry, *term;\r
250      Xapian::PostingIterator i, end;\r
251 -    notmuch_message_t *message = NULL;\r
252      notmuch_status_t status;\r
253  \r
254 +    if (message == NULL)\r
255 +           return NOTMUCH_STATUS_NULL_POINTER;\r
256 +\r
257      local = talloc_new (notmuch);\r
258  \r
259      try {\r
260         status = _notmuch_database_filename_to_direntry (local, notmuch,\r
261                                                          filename, &direntry);\r
262         if (status)\r
263 -           return NULL;\r
264 +           goto DONE;\r
265  \r
266         term = talloc_asprintf (local, "%s%s", prefix, direntry);\r
267  \r
268 @@ -1800,19 +1822,24 @@ notmuch_database_find_message_by_filename (notmuch_database_t *notmuch,\r
269         if (i != end) {\r
270             notmuch_private_status_t private_status;\r
271  \r
272 -           message = _notmuch_message_create (notmuch, notmuch,\r
273 -                                              *i, &private_status);\r
274 +           *message = _notmuch_message_create (notmuch, notmuch,\r
275 +                                               *i, &private_status);\r
276 +           if (*message == NULL)\r
277 +                   status = NOTMUCH_STATUS_OUT_OF_MEMORY;\r
278         }\r
279      } catch (const Xapian::Error &error) {\r
280         fprintf (stderr, "Error: A Xapian exception occurred finding message by filename: %s\n",\r
281                  error.get_msg().c_str());\r
282         notmuch->exception_reported = TRUE;\r
283 -       message = NULL;\r
284 +       status = NOTMUCH_STATUS_OUT_OF_MEMORY;\r
285      }\r
286  \r
287 +  DONE:\r
288      talloc_free (local);\r
289  \r
290 -    return message;\r
291 +    if (status)\r
292 +           *message = NULL;\r
293 +    return status;\r
294  }\r
295  \r
296  notmuch_string_list_t *\r
297 diff --git a/lib/message.cc b/lib/message.cc\r
298 index 531d304..2a85744 100644\r
299 --- a/lib/message.cc\r
300 +++ b/lib/message.cc\r
301 @@ -216,11 +216,11 @@ _notmuch_message_create_for_message_id (notmuch_database_t *notmuch,\r
302      unsigned int doc_id;\r
303      char *term;\r
304  \r
305 -    *status_ret = NOTMUCH_PRIVATE_STATUS_SUCCESS;\r
306 -\r
307 -    message = notmuch_database_find_message (notmuch, message_id);\r
308 +    *status_ret = (notmuch_private_status_t) notmuch_database_find_message (notmuch, message_id, &message);\r
309      if (message)\r
310         return talloc_steal (notmuch, message);\r
311 +    else if (*status_ret)\r
312 +       return NULL;\r
313  \r
314      term = talloc_asprintf (NULL, "%s%s",\r
315                             _find_prefix ("id"), message_id);\r
316 diff --git a/lib/notmuch.h b/lib/notmuch.h\r
317 index 6d7a99f..08b4ce2 100644\r
318 --- a/lib/notmuch.h\r
319 +++ b/lib/notmuch.h\r
320 @@ -347,35 +347,60 @@ notmuch_database_remove_message (notmuch_database_t *database,\r
321  \r
322  /* Find a message with the given message_id.\r
323   *\r
324 - * If the database contains a message with the given message_id, then\r
325 - * a new notmuch_message_t object is returned. The caller should call\r
326 - * notmuch_message_destroy when done with the message.\r
327 + * If message with the given message_id is found then, on successful return\r
328 + * (NOTMUCH_STATUS_SUCCESS) '*message' will be initialized to a message object.\r
329 + * The user should call notmuch_message_destroy when done with the message.\r
330   *\r
331 - * This function returns NULL in the following situations:\r
332 + * On any failure or when the message is not found, this function initializes\r
333 + * '*message' to NULL. This means, when NOTMUCH_STATUS_SUCCESS is returned, the\r
334 + * user is supposed to check '*message' for NULL to find out whether the\r
335 + * message with the given message_id was found.\r
336   *\r
337 - *     * No message is found with the given message_id\r
338 - *     * An out-of-memory situation occurs\r
339 - *     * A Xapian exception occurs\r
340 + * Note: The argument 'message' must not be NULL!\r
341 + *\r
342 + * Return value:\r
343 + *\r
344 + * NOTMUCH_STATUS_SUCCESS: Successful return, check '*message'.\r
345 + *\r
346 + * NOTMUCH_STATUS_NULL_POINTER: The given 'message' argument is NULL\r
347 + *\r
348 + * NOTMUCH_STATUS_OUT_OF_MEMORY: Out of memory, creating message object\r
349 + *\r
350 + * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred\r
351   */\r
352 -notmuch_message_t *\r
353 +notmuch_status_t\r
354  notmuch_database_find_message (notmuch_database_t *database,\r
355 -                              const char *message_id);\r
356 +                              const char *message_id,\r
357 +                              notmuch_message_t **message);\r
358  \r
359  /* Find a message with the given filename.\r
360   *\r
361 - * If the database contains a message with the given filename, then a\r
362 - * new notmuch_message_t object is returned.  The caller should call \r
363 - * notmuch_message_destroy when done with the message.\r
364 + * If the database contains a message with the given filename then, on\r
365 + * successful return (NOTMUCH_STATUS_SUCCESS) '*message' will be initialized to\r
366 + * a message object. The user should call notmuch_message_destroy when done\r
367 + * with the message.\r
368   *\r
369 - * This function returns NULL in the following situations:\r
370 + * On any failure or when the message is not found, this function initializes\r
371 + * '*message' to NULL. This means, when NOTMUCH_STATUS_SUCCESS is returned, the\r
372 + * user is supposed to check '*message' for NULL to find out whether the\r
373 + * message with the given filename is found.\r
374   *\r
375 - *     * No message is found with the given filename\r
376 - *     * An out-of-memory situation occurs\r
377 - *     * A Xapian exception occurs\r
378 + * Note: The argument 'message' must not be NULL!\r
379 + *\r
380 + * Return value:\r
381 + *\r
382 + * NOTMUCH_STATUS_SUCCESS: Successful return, check '*message'\r
383 + *\r
384 + * NOTMUCH_STATUS_NULL_POINTER: The given 'message' argument is NULL\r
385 + *\r
386 + * NOTMUCH_STATUS_OUT_OF_MEMORY: Out of memory, creating the message object\r
387 + *\r
388 + * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred\r
389   */\r
390 -notmuch_message_t *\r
391 +notmuch_status_t\r
392  notmuch_database_find_message_by_filename (notmuch_database_t *notmuch,\r
393 -                                          const char *filename);\r
394 +                                          const char *filename,\r
395 +                                          notmuch_message_t **message);\r
396  \r
397  /* Return a list of all tags found in the database.\r
398   *\r
399 diff --git a/notmuch-new.c b/notmuch-new.c\r
400 index e79593c..96a1e31 100644\r
401 --- a/notmuch-new.c\r
402 +++ b/notmuch-new.c\r
403 @@ -743,7 +743,9 @@ remove_filename (notmuch_database_t *notmuch,\r
404      status = notmuch_database_begin_atomic (notmuch);\r
405      if (status)\r
406         return status;\r
407 -    message = notmuch_database_find_message_by_filename (notmuch, path);\r
408 +    status = notmuch_database_find_message_by_filename (notmuch, path, &message);\r
409 +    if (status || message == NULL)\r
410 +       return status;\r
411      status = notmuch_database_remove_message (notmuch, path);\r
412      if (status == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) {\r
413         add_files_state->renamed_messages++;\r
414 diff --git a/notmuch-restore.c b/notmuch-restore.c\r
415 index f095f64..3b0ae71 100644\r
416 --- a/notmuch-restore.c\r
417 +++ b/notmuch-restore.c\r
418 @@ -87,10 +87,13 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])\r
419         file_tags = xstrndup (line + match[2].rm_so,\r
420                               match[2].rm_eo - match[2].rm_so);\r
421  \r
422 -       message = notmuch_database_find_message (notmuch, message_id);\r
423 -       if (message == NULL) {\r
424 -           fprintf (stderr, "Warning: Cannot apply tags to missing message: %s\n",\r
425 -                    message_id);\r
426 +       status = notmuch_database_find_message (notmuch, message_id, &message);\r
427 +       if (status || message == NULL) {\r
428 +           fprintf (stderr, "Warning: Cannot apply tags to %smessage: %s\n",\r
429 +                    message ? "" : "missing ", message_id);\r
430 +           if (status)\r
431 +                   fprintf (stderr, "%s\n",\r
432 +                            notmuch_status_to_string(status));\r
433             goto NEXT_LINE;\r
434         }\r
435  \r
436 -- \r
437 1.7.6.1\r
438 \r