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 B72A4431FB6
\r
6 for <notmuch@notmuchmail.org>; Mon, 3 Oct 2011 22:06:31 -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
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
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 sHIRwsbxYxp7 for <notmuch@notmuchmail.org>;
\r
19 Mon, 3 Oct 2011 22:06:29 -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 84C8C429E21
\r
24 for <notmuch@notmuchmail.org>; Mon, 3 Oct 2011 22:06:29 -0700 (PDT)
\r
25 Received: by mail-bw0-f53.google.com with SMTP id zt12so224537bkb.26
\r
26 for <notmuch@notmuchmail.org>; Mon, 03 Oct 2011 22:06:29 -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=86KHwNgRSa0Anrzm0ZDo7mMdTnrNDhLYpZcl8oaqvWY=;
\r
31 b=nZ/s01tco/0hD40gfPA0PE3duBp31mn3m4LzmJGKVbB3+Ia7fRvweoIaF0BjBzc5M2
\r
32 rE81m2GM6kBGDY8ZRl3M7yMkWnr/jzyMRqUfGdpTzfVBAtycIkLV75zRexwsjvIS2Jlr
\r
33 TfddUdt+kwUA8Ody8wPBWAiuaBZqJ8K0dJo9o=
\r
34 Received: by 10.223.29.203 with SMTP id r11mr1137448fac.10.1317704789016;
\r
35 Mon, 03 Oct 2011 22:06:29 -0700 (PDT)
\r
36 Received: from localhost ([88.236.39.101])
\r
37 by mx.google.com with ESMTPS id f25sm24008333faf.7.2011.10.03.22.06.27
\r
38 (version=TLSv1/SSLv3 cipher=OTHER);
\r
39 Mon, 03 Oct 2011 22:06:28 -0700 (PDT)
\r
40 From: Ali Polatel <polatel@gmail.com>
\r
41 To: David Bremner <david@tethera.net>
\r
42 Subject: [PATCH v3 2/2] lib: make find_message{,by_filename) report errors
\r
43 Date: Tue, 4 Oct 2011 08:06:09 +0300
\r
45 <02a30767116ad8abcbd0a3351f2e4d43bbbd655f.1317704225.git.alip@exherbo.org>
\r
46 X-Mailer: git-send-email 1.7.6.1
\r
47 In-Reply-To: <cover.1317704225.git.alip@exherbo.org>
\r
48 References: <20111004050046.GA6048@hayalet>
\r
49 <cover.1317704225.git.alip@exherbo.org>
\r
50 In-Reply-To: <cover.1317704225.git.alip@exherbo.org>
\r
51 References: <cover.1317704225.git.alip@exherbo.org>
\r
52 Organization: Pink Floyd
\r
53 Cc: Ali Polatel <alip@exherbo.org>,
\r
54 Notmuch Mailing List <notmuch@notmuchmail.org>,
\r
55 Austin Clements <amdragon@MIT.EDU>
\r
56 X-BeenThere: notmuch@notmuchmail.org
\r
57 X-Mailman-Version: 2.1.13
\r
59 List-Id: "Use and development of the notmuch mail system."
\r
60 <notmuch.notmuchmail.org>
\r
61 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,
\r
62 <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>
\r
63 List-Archive: <http://notmuchmail.org/pipermail/notmuch>
\r
64 List-Post: <mailto:notmuch@notmuchmail.org>
\r
65 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>
\r
66 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,
\r
67 <mailto:notmuch-request@notmuchmail.org?subject=subscribe>
\r
68 X-List-Received-Date: Tue, 04 Oct 2011 05:06:31 -0000
\r
70 From: Ali Polatel <alip@exherbo.org>
\r
72 Previously, the functions notmuch_database_find_message() and
\r
73 notmuch_database_find_message_by_filename() functions did not properly
\r
74 report error condition to the library user.
\r
76 For more information, read the thread on the notmuch mailing list
\r
77 starting with my mail "id:871uv2unfd.fsf@gmail.com"
\r
79 Make these functions accept a pointer to 'notmuch_message_t' as argument
\r
80 and return notmuch_status_t which may be used to check for any error
\r
83 restore: Modify for the new notmuch_database_find_message()
\r
84 new: Modify for the new notmuch_database_find_message_by_filename()
\r
86 lib/database.cc | 96 ++++++++++++++++++++++++++++++++++++-----------------
\r
87 lib/message.cc | 8 +++--
\r
88 lib/notmuch.h | 58 ++++++++++++++++++++++----------
\r
89 notmuch-new.c | 4 ++-
\r
90 notmuch-restore.c | 11 ++++--
\r
91 5 files changed, 120 insertions(+), 57 deletions(-)
\r
93 diff --git a/lib/database.cc b/lib/database.cc
\r
94 index d43e114..e77fd53 100644
\r
95 --- a/lib/database.cc
\r
96 +++ b/lib/database.cc
\r
97 @@ -360,13 +360,17 @@ _message_id_compressed (void *ctx, const char *message_id)
\r
101 -notmuch_message_t *
\r
103 notmuch_database_find_message (notmuch_database_t *notmuch,
\r
104 - const char *message_id)
\r
105 + const char *message_id,
\r
106 + notmuch_message_t **message_ret)
\r
108 notmuch_private_status_t status;
\r
109 unsigned int doc_id;
\r
111 + if (message_ret == NULL)
\r
112 + return NOTMUCH_STATUS_NULL_POINTER;
\r
114 if (strlen (message_id) > NOTMUCH_MESSAGE_ID_MAX)
\r
115 message_id = _message_id_compressed (notmuch, message_id);
\r
117 @@ -375,14 +379,21 @@ notmuch_database_find_message (notmuch_database_t *notmuch,
\r
118 message_id, &doc_id);
\r
120 if (status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND)
\r
122 + *message_ret = NULL;
\r
124 + *message_ret = _notmuch_message_create (notmuch, notmuch, doc_id,
\r
126 + if (*message_ret == NULL)
\r
127 + return NOTMUCH_STATUS_OUT_OF_MEMORY;
\r
130 - return _notmuch_message_create (notmuch, notmuch, doc_id, NULL);
\r
131 + return NOTMUCH_STATUS_SUCCESS;
\r
132 } catch (const Xapian::Error &error) {
\r
133 fprintf (stderr, "A Xapian exception occurred finding message: %s.\n",
\r
134 error.get_msg().c_str());
\r
135 notmuch->exception_reported = TRUE;
\r
137 + *message_ret = NULL;
\r
138 + return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
\r
142 @@ -1311,7 +1322,9 @@ _get_metadata_thread_id_key (void *ctx, const char *message_id)
\r
144 /* Find the thread ID to which the message with 'message_id' belongs.
\r
146 - * Always returns a newly talloced string belonging to 'ctx'.
\r
147 + * Note: 'thread_id_ret' must not be NULL!
\r
148 + * On success '*thread_id_ret' is set to a newly talloced string belonging to
\r
151 * Note: If there is no message in the database with the given
\r
152 * 'message_id' then a new thread_id will be allocated for this
\r
153 @@ -1319,25 +1332,30 @@ _get_metadata_thread_id_key (void *ctx, const char *message_id)
\r
154 * thread ID can be looked up if the message is added to the database
\r
157 -static const char *
\r
158 +static notmuch_status_t
\r
159 _resolve_message_id_to_thread_id (notmuch_database_t *notmuch,
\r
161 - const char *message_id)
\r
162 + const char *message_id,
\r
163 + const char **thread_id_ret)
\r
165 + notmuch_status_t status;
\r
166 notmuch_message_t *message;
\r
167 string thread_id_string;
\r
168 - const char *thread_id;
\r
169 char *metadata_key;
\r
170 Xapian::WritableDatabase *db;
\r
172 - message = notmuch_database_find_message (notmuch, message_id);
\r
173 + status = notmuch_database_find_message (notmuch, message_id, &message);
\r
179 - thread_id = talloc_steal (ctx, notmuch_message_get_thread_id (message));
\r
180 + *thread_id_ret = talloc_steal (ctx,
\r
181 + notmuch_message_get_thread_id (message));
\r
183 notmuch_message_destroy (message);
\r
185 - return thread_id;
\r
186 + return NOTMUCH_STATUS_SUCCESS;
\r
189 /* Message has not been seen yet.
\r
190 @@ -1351,15 +1369,16 @@ _resolve_message_id_to_thread_id (notmuch_database_t *notmuch,
\r
191 thread_id_string = notmuch->xapian_db->get_metadata (metadata_key);
\r
193 if (thread_id_string.empty()) {
\r
194 - thread_id = _notmuch_database_generate_thread_id (notmuch);
\r
195 - db->set_metadata (metadata_key, thread_id);
\r
196 + *thread_id_ret = talloc_strdup (ctx,
\r
197 + _notmuch_database_generate_thread_id (notmuch));
\r
198 + db->set_metadata (metadata_key, *thread_id_ret);
\r
200 - thread_id = thread_id_string.c_str();
\r
201 + *thread_id_ret = talloc_strdup (ctx, thread_id_string.c_str());
\r
204 talloc_free (metadata_key);
\r
206 - return talloc_strdup (ctx, thread_id);
\r
207 + return NOTMUCH_STATUS_SUCCESS;
\r
210 static notmuch_status_t
\r
211 @@ -1446,9 +1465,12 @@ _notmuch_database_link_message_to_parents (notmuch_database_t *notmuch,
\r
212 _notmuch_message_add_term (message, "reference",
\r
213 parent_message_id);
\r
215 - parent_thread_id = _resolve_message_id_to_thread_id (notmuch,
\r
217 - parent_message_id);
\r
218 + ret = _resolve_message_id_to_thread_id (notmuch,
\r
220 + parent_message_id,
\r
221 + &parent_thread_id);
\r
225 if (*thread_id == NULL) {
\r
226 *thread_id = talloc_strdup (message, parent_thread_id);
\r
227 @@ -1759,11 +1781,13 @@ notmuch_status_t
\r
228 notmuch_database_remove_message (notmuch_database_t *notmuch,
\r
229 const char *filename)
\r
231 - notmuch_message_t *message =
\r
232 - notmuch_database_find_message_by_filename (notmuch, filename);
\r
233 - notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
\r
234 + notmuch_status_t status;
\r
235 + notmuch_message_t *message;
\r
238 + status = notmuch_database_find_message_by_filename (notmuch, filename,
\r
241 + if (status == NOTMUCH_STATUS_SUCCESS && message) {
\r
242 status = _notmuch_message_remove_filename (message, filename);
\r
243 if (status == NOTMUCH_STATUS_SUCCESS)
\r
244 _notmuch_message_delete (message);
\r
245 @@ -1776,24 +1800,27 @@ notmuch_database_remove_message (notmuch_database_t *notmuch,
\r
249 -notmuch_message_t *
\r
251 notmuch_database_find_message_by_filename (notmuch_database_t *notmuch,
\r
252 - const char *filename)
\r
253 + const char *filename,
\r
254 + notmuch_message_t **message_ret)
\r
257 const char *prefix = _find_prefix ("file-direntry");
\r
258 char *direntry, *term;
\r
259 Xapian::PostingIterator i, end;
\r
260 - notmuch_message_t *message = NULL;
\r
261 notmuch_status_t status;
\r
263 + if (message_ret == NULL)
\r
264 + return NOTMUCH_STATUS_NULL_POINTER;
\r
266 local = talloc_new (notmuch);
\r
269 status = _notmuch_database_filename_to_direntry (local, notmuch,
\r
270 filename, &direntry);
\r
275 term = talloc_asprintf (local, "%s%s", prefix, direntry);
\r
277 @@ -1802,19 +1829,26 @@ notmuch_database_find_message_by_filename (notmuch_database_t *notmuch,
\r
279 notmuch_private_status_t private_status;
\r
281 - message = _notmuch_message_create (notmuch, notmuch,
\r
282 - *i, &private_status);
\r
283 + *message_ret = _notmuch_message_create (notmuch, notmuch, *i,
\r
284 + &private_status);
\r
285 + if (*message_ret == NULL)
\r
286 + status = NOTMUCH_STATUS_OUT_OF_MEMORY;
\r
288 } catch (const Xapian::Error &error) {
\r
289 fprintf (stderr, "Error: A Xapian exception occurred finding message by filename: %s\n",
\r
290 error.get_msg().c_str());
\r
291 notmuch->exception_reported = TRUE;
\r
293 + status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
\r
297 talloc_free (local);
\r
300 + if (status && *message_ret) {
\r
301 + notmuch_message_destroy (*message_ret);
\r
302 + *message_ret = NULL;
\r
307 notmuch_string_list_t *
\r
308 diff --git a/lib/message.cc b/lib/message.cc
\r
309 index 531d304..8f22e02 100644
\r
310 --- a/lib/message.cc
\r
311 +++ b/lib/message.cc
\r
312 @@ -216,11 +216,13 @@ _notmuch_message_create_for_message_id (notmuch_database_t *notmuch,
\r
313 unsigned int doc_id;
\r
316 - *status_ret = NOTMUCH_PRIVATE_STATUS_SUCCESS;
\r
318 - message = notmuch_database_find_message (notmuch, message_id);
\r
319 + *status_ret = (notmuch_private_status_t) notmuch_database_find_message (notmuch,
\r
323 return talloc_steal (notmuch, message);
\r
324 + else if (*status_ret)
\r
327 term = talloc_asprintf (NULL, "%s%s",
\r
328 _find_prefix ("id"), message_id);
\r
329 diff --git a/lib/notmuch.h b/lib/notmuch.h
\r
330 index 6d7a99f..c4330e4 100644
\r
331 --- a/lib/notmuch.h
\r
332 +++ b/lib/notmuch.h
\r
333 @@ -347,35 +347,57 @@ notmuch_database_remove_message (notmuch_database_t *database,
\r
335 /* Find a message with the given message_id.
\r
337 - * If the database contains a message with the given message_id, then
\r
338 - * a new notmuch_message_t object is returned. The caller should call
\r
339 - * notmuch_message_destroy when done with the message.
\r
340 + * If a message with the given message_id is found then, on successful return
\r
341 + * (NOTMUCH_STATUS_SUCCESS) '*message' will be initialized to a message
\r
342 + * object. The caller should call notmuch_message_destroy when done with the
\r
345 - * This function returns NULL in the following situations:
\r
346 + * On any failure or when the message is not found, this function initializes
\r
347 + * '*message' to NULL. This means, when NOTMUCH_STATUS_SUCCESS is returned, the
\r
348 + * caller is supposed to check '*message' for NULL to find out whether the
\r
349 + * message with the given message_id was found.
\r
351 - * * No message is found with the given message_id
\r
352 - * * An out-of-memory situation occurs
\r
353 - * * A Xapian exception occurs
\r
356 + * NOTMUCH_STATUS_SUCCESS: Successful return, check '*message'.
\r
358 + * NOTMUCH_STATUS_NULL_POINTER: The given 'message' argument is NULL
\r
360 + * NOTMUCH_STATUS_OUT_OF_MEMORY: Out of memory, creating message object
\r
362 + * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred
\r
364 -notmuch_message_t *
\r
366 notmuch_database_find_message (notmuch_database_t *database,
\r
367 - const char *message_id);
\r
368 + const char *message_id,
\r
369 + notmuch_message_t **message);
\r
371 /* Find a message with the given filename.
\r
373 - * If the database contains a message with the given filename, then a
\r
374 - * new notmuch_message_t object is returned. The caller should call
\r
375 - * notmuch_message_destroy when done with the message.
\r
376 + * If the database contains a message with the given filename then, on
\r
377 + * successful return (NOTMUCH_STATUS_SUCCESS) '*message' will be initialized to
\r
378 + * a message object. The caller should call notmuch_message_destroy when done
\r
379 + * with the message.
\r
381 - * This function returns NULL in the following situations:
\r
382 + * On any failure or when the message is not found, this function initializes
\r
383 + * '*message' to NULL. This means, when NOTMUCH_STATUS_SUCCESS is returned, the
\r
384 + * caller is supposed to check '*message' for NULL to find out whether the
\r
385 + * message with the given filename is found.
\r
387 - * * No message is found with the given filename
\r
388 - * * An out-of-memory situation occurs
\r
389 - * * A Xapian exception occurs
\r
392 + * NOTMUCH_STATUS_SUCCESS: Successful return, check '*message'
\r
394 + * NOTMUCH_STATUS_NULL_POINTER: The given 'message' argument is NULL
\r
396 + * NOTMUCH_STATUS_OUT_OF_MEMORY: Out of memory, creating the message object
\r
398 + * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred
\r
400 -notmuch_message_t *
\r
402 notmuch_database_find_message_by_filename (notmuch_database_t *notmuch,
\r
403 - const char *filename);
\r
404 + const char *filename,
\r
405 + notmuch_message_t **message);
\r
407 /* Return a list of all tags found in the database.
\r
409 diff --git a/notmuch-new.c b/notmuch-new.c
\r
410 index e79593c..96a1e31 100644
\r
411 --- a/notmuch-new.c
\r
412 +++ b/notmuch-new.c
\r
413 @@ -743,7 +743,9 @@ remove_filename (notmuch_database_t *notmuch,
\r
414 status = notmuch_database_begin_atomic (notmuch);
\r
417 - message = notmuch_database_find_message_by_filename (notmuch, path);
\r
418 + status = notmuch_database_find_message_by_filename (notmuch, path, &message);
\r
419 + if (status || message == NULL)
\r
421 status = notmuch_database_remove_message (notmuch, path);
\r
422 if (status == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) {
\r
423 add_files_state->renamed_messages++;
\r
424 diff --git a/notmuch-restore.c b/notmuch-restore.c
\r
425 index f095f64..e4a5355 100644
\r
426 --- a/notmuch-restore.c
\r
427 +++ b/notmuch-restore.c
\r
428 @@ -87,10 +87,13 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])
\r
429 file_tags = xstrndup (line + match[2].rm_so,
\r
430 match[2].rm_eo - match[2].rm_so);
\r
432 - message = notmuch_database_find_message (notmuch, message_id);
\r
433 - if (message == NULL) {
\r
434 - fprintf (stderr, "Warning: Cannot apply tags to missing message: %s\n",
\r
436 + status = notmuch_database_find_message (notmuch, message_id, &message);
\r
437 + if (status || message == NULL) {
\r
438 + fprintf (stderr, "Warning: Cannot apply tags to %smessage: %s\n",
\r
439 + message ? "" : "missing ", message_id);
\r
441 + fprintf (stderr, "%s\n",
\r
442 + notmuch_status_to_string(status));
\r