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 67347429E21
\r
6 for <notmuch@notmuchmail.org>; Mon, 3 Oct 2011 13:40:24 -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 iSTx70XEEwrl for <notmuch@notmuchmail.org>;
\r
19 Mon, 3 Oct 2011 13:40:23 -0700 (PDT)
\r
20 Received: from mail-ww0-f45.google.com (mail-ww0-f45.google.com
\r
21 [74.125.82.45]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) (No client
\r
22 certificate requested) by olra.theworths.org (Postfix) with ESMTPS id
\r
23 C413F431FB6 for <notmuch@notmuchmail.org>; Mon, 3 Oct 2011 13:40:22 -0700
\r
25 Received: by wwi36 with SMTP id 36so5446910wwi.2
\r
26 for <notmuch@notmuchmail.org>; Mon, 03 Oct 2011 13:40:21 -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=2O6Gw1JrMJQVh6hteqk3uEYcduz6xZrgv7ckvk4H1WQ=;
\r
31 b=iAJagCGMpwqYvwCSHb00npk30qOzqkxPMwgHpLMZbOyPJowga33Mz8kjP+C5e/HCuN
\r
32 qp8STpFCuOYkEC3s7vObM32+PQMVFUFv+hKEsyzuTbgCodq/F3eY3edTV7QrSEGjPwoW
\r
33 s9HK/wGgwRec/0BJrVZm4wbtTgFnyKtwIFOdE=
\r
34 Received: by 10.223.55.146 with SMTP id u18mr454440fag.108.1317674420320;
\r
35 Mon, 03 Oct 2011 13:40:20 -0700 (PDT)
\r
36 Received: from localhost ([85.104.4.184])
\r
37 by mx.google.com with ESMTPS id a7sm22603106fam.22.2011.10.03.13.40.18
\r
38 (version=TLSv1/SSLv3 cipher=OTHER);
\r
39 Mon, 03 Oct 2011 13:40:19 -0700 (PDT)
\r
40 From: Ali Polatel <polatel@gmail.com>
\r
41 To: Austin Clements <amdragon@MIT.EDU>
\r
42 Subject: [PATCH v2 2/2] lib: make find_message{,by_filename) report errors
\r
43 Date: Mon, 3 Oct 2011 23:40:15 +0300
\r
45 <56f9435dd2b7c1a359852d37df8f997a42e9756d.1317674063.git.alip@exherbo.org>
\r
46 X-Mailer: git-send-email 1.7.6.1
\r
47 In-Reply-To: <cover.1317674063.git.alip@exherbo.org>
\r
48 References: <20111003203837.GA22365@hayalet>
\r
49 <cover.1317674063.git.alip@exherbo.org>
\r
50 In-Reply-To: <cover.1317674063.git.alip@exherbo.org>
\r
51 References: <cover.1317674063.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 X-BeenThere: notmuch@notmuchmail.org
\r
56 X-Mailman-Version: 2.1.13
\r
58 List-Id: "Use and development of the notmuch mail system."
\r
59 <notmuch.notmuchmail.org>
\r
60 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,
\r
61 <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>
\r
62 List-Archive: <http://notmuchmail.org/pipermail/notmuch>
\r
63 List-Post: <mailto:notmuch@notmuchmail.org>
\r
64 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>
\r
65 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,
\r
66 <mailto:notmuch-request@notmuchmail.org?subject=subscribe>
\r
67 X-List-Received-Date: Mon, 03 Oct 2011 20:40:24 -0000
\r
69 From: Ali Polatel <alip@exherbo.org>
\r
71 Previously, the functions notmuch_database_find_message() and
\r
72 notmuch_database_find_message_by_filename() functions did not properly
\r
73 report error condition to the library user.
\r
75 For more information, read the thread on the notmuch mailing list
\r
76 starting with my mail "id:871uv2unfd.fsf@gmail.com"
\r
78 Make these functions accept a pointer to 'notmuch_message_t' as argument
\r
79 and return notmuch_status_t which may be used to check for any error
\r
82 restore: Modify for the new notmuch_database_find_message()
\r
83 new: Modify for the new notmuch_database_find_message_by_filename()
\r
85 lib/database.cc | 93 +++++++++++++++++++++++++++++++++++-----------------
\r
86 lib/message.cc | 8 +++--
\r
87 lib/notmuch.h | 60 +++++++++++++++++++++++-----------
\r
88 notmuch-new.c | 4 ++-
\r
89 notmuch-restore.c | 11 ++++--
\r
90 5 files changed, 118 insertions(+), 58 deletions(-)
\r
92 diff --git a/lib/database.cc b/lib/database.cc
\r
93 index d43e114..cf0cc8a 100644
\r
94 --- a/lib/database.cc
\r
95 +++ b/lib/database.cc
\r
96 @@ -360,13 +360,17 @@ _message_id_compressed (void *ctx, const char *message_id)
\r
100 -notmuch_message_t *
\r
102 notmuch_database_find_message (notmuch_database_t *notmuch,
\r
103 - const char *message_id)
\r
104 + const char *message_id,
\r
105 + notmuch_message_t **message_ret)
\r
107 notmuch_private_status_t status;
\r
108 unsigned int doc_id;
\r
110 + if (message_ret == NULL)
\r
111 + return NOTMUCH_STATUS_NULL_POINTER;
\r
113 if (strlen (message_id) > NOTMUCH_MESSAGE_ID_MAX)
\r
114 message_id = _message_id_compressed (notmuch, message_id);
\r
116 @@ -375,14 +379,21 @@ notmuch_database_find_message (notmuch_database_t *notmuch,
\r
117 message_id, &doc_id);
\r
119 if (status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND)
\r
121 + *message_ret = NULL;
\r
123 + *message_ret = _notmuch_message_create (notmuch, notmuch, doc_id,
\r
125 + if (*message_ret == NULL)
\r
126 + return NOTMUCH_STATUS_OUT_OF_MEMORY;
\r
129 - return _notmuch_message_create (notmuch, notmuch, doc_id, NULL);
\r
130 + return NOTMUCH_STATUS_SUCCESS;
\r
131 } catch (const Xapian::Error &error) {
\r
132 fprintf (stderr, "A Xapian exception occurred finding message: %s.\n",
\r
133 error.get_msg().c_str());
\r
134 notmuch->exception_reported = TRUE;
\r
136 + *message_ret = NULL;
\r
137 + return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
\r
141 @@ -1311,7 +1322,8 @@ _get_metadata_thread_id_key (void *ctx, const char *message_id)
\r
143 /* Find the thread ID to which the message with 'message_id' belongs.
\r
145 - * Always returns a newly talloced string belonging to 'ctx'.
\r
146 + * Note: 'thread_id' must not be NULL!
\r
147 + * On success '*thread_id' is set to a newly talloced string belonging to 'ctx'.
\r
149 * Note: If there is no message in the database with the given
\r
150 * 'message_id' then a new thread_id will be allocated for this
\r
151 @@ -1319,25 +1331,30 @@ _get_metadata_thread_id_key (void *ctx, const char *message_id)
\r
152 * thread ID can be looked up if the message is added to the database
\r
155 -static const char *
\r
156 +static notmuch_status_t
\r
157 _resolve_message_id_to_thread_id (notmuch_database_t *notmuch,
\r
159 - const char *message_id)
\r
160 + const char *message_id,
\r
161 + const char **thread_id_ret)
\r
163 + notmuch_status_t status;
\r
164 notmuch_message_t *message;
\r
165 string thread_id_string;
\r
166 - const char *thread_id;
\r
167 char *metadata_key;
\r
168 Xapian::WritableDatabase *db;
\r
170 - message = notmuch_database_find_message (notmuch, message_id);
\r
171 + status = notmuch_database_find_message (notmuch, message_id, &message);
\r
177 - thread_id = talloc_steal (ctx, notmuch_message_get_thread_id (message));
\r
178 + *thread_id_ret = talloc_steal (ctx,
\r
179 + notmuch_message_get_thread_id (message));
\r
181 notmuch_message_destroy (message);
\r
183 - return thread_id;
\r
184 + return NOTMUCH_STATUS_SUCCESS;
\r
187 /* Message has not been seen yet.
\r
188 @@ -1351,15 +1368,16 @@ _resolve_message_id_to_thread_id (notmuch_database_t *notmuch,
\r
189 thread_id_string = notmuch->xapian_db->get_metadata (metadata_key);
\r
191 if (thread_id_string.empty()) {
\r
192 - thread_id = _notmuch_database_generate_thread_id (notmuch);
\r
193 - db->set_metadata (metadata_key, thread_id);
\r
194 + *thread_id_ret = talloc_strdup (ctx,
\r
195 + _notmuch_database_generate_thread_id (notmuch));
\r
196 + db->set_metadata (metadata_key, *thread_id_ret);
\r
198 - thread_id = thread_id_string.c_str();
\r
199 + *thread_id_ret = talloc_strdup (ctx, thread_id_string.c_str());
\r
202 talloc_free (metadata_key);
\r
204 - return talloc_strdup (ctx, thread_id);
\r
205 + return NOTMUCH_STATUS_SUCCESS;
\r
208 static notmuch_status_t
\r
209 @@ -1446,9 +1464,12 @@ _notmuch_database_link_message_to_parents (notmuch_database_t *notmuch,
\r
210 _notmuch_message_add_term (message, "reference",
\r
211 parent_message_id);
\r
213 - parent_thread_id = _resolve_message_id_to_thread_id (notmuch,
\r
215 - parent_message_id);
\r
216 + ret = _resolve_message_id_to_thread_id (notmuch,
\r
218 + parent_message_id,
\r
219 + &parent_thread_id);
\r
223 if (*thread_id == NULL) {
\r
224 *thread_id = talloc_strdup (message, parent_thread_id);
\r
225 @@ -1759,11 +1780,13 @@ notmuch_status_t
\r
226 notmuch_database_remove_message (notmuch_database_t *notmuch,
\r
227 const char *filename)
\r
229 - notmuch_message_t *message =
\r
230 - notmuch_database_find_message_by_filename (notmuch, filename);
\r
231 - notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
\r
232 + notmuch_status_t status;
\r
233 + notmuch_message_t *message;
\r
236 + status = notmuch_database_find_message_by_filename (notmuch, filename,
\r
239 + if (status == NOTMUCH_STATUS_SUCCESS && message) {
\r
240 status = _notmuch_message_remove_filename (message, filename);
\r
241 if (status == NOTMUCH_STATUS_SUCCESS)
\r
242 _notmuch_message_delete (message);
\r
243 @@ -1776,24 +1799,27 @@ notmuch_database_remove_message (notmuch_database_t *notmuch,
\r
247 -notmuch_message_t *
\r
249 notmuch_database_find_message_by_filename (notmuch_database_t *notmuch,
\r
250 - const char *filename)
\r
251 + const char *filename,
\r
252 + notmuch_message_t **message_ret)
\r
255 const char *prefix = _find_prefix ("file-direntry");
\r
256 char *direntry, *term;
\r
257 Xapian::PostingIterator i, end;
\r
258 - notmuch_message_t *message = NULL;
\r
259 notmuch_status_t status;
\r
261 + if (message_ret == NULL)
\r
262 + return NOTMUCH_STATUS_NULL_POINTER;
\r
264 local = talloc_new (notmuch);
\r
267 status = _notmuch_database_filename_to_direntry (local, notmuch,
\r
268 filename, &direntry);
\r
273 term = talloc_asprintf (local, "%s%s", prefix, direntry);
\r
275 @@ -1802,19 +1828,24 @@ notmuch_database_find_message_by_filename (notmuch_database_t *notmuch,
\r
277 notmuch_private_status_t private_status;
\r
279 - message = _notmuch_message_create (notmuch, notmuch,
\r
280 - *i, &private_status);
\r
281 + *message_ret = _notmuch_message_create (notmuch, notmuch, *i,
\r
282 + &private_status);
\r
283 + if (*message_ret == NULL)
\r
284 + status = NOTMUCH_STATUS_OUT_OF_MEMORY;
\r
286 } catch (const Xapian::Error &error) {
\r
287 fprintf (stderr, "Error: A Xapian exception occurred finding message by filename: %s\n",
\r
288 error.get_msg().c_str());
\r
289 notmuch->exception_reported = TRUE;
\r
291 + status = NOTMUCH_STATUS_OUT_OF_MEMORY;
\r
295 talloc_free (local);
\r
299 + *message_ret = NULL;
\r
303 notmuch_string_list_t *
\r
304 diff --git a/lib/message.cc b/lib/message.cc
\r
305 index 531d304..8f22e02 100644
\r
306 --- a/lib/message.cc
\r
307 +++ b/lib/message.cc
\r
308 @@ -216,11 +216,13 @@ _notmuch_message_create_for_message_id (notmuch_database_t *notmuch,
\r
309 unsigned int doc_id;
\r
312 - *status_ret = NOTMUCH_PRIVATE_STATUS_SUCCESS;
\r
314 - message = notmuch_database_find_message (notmuch, message_id);
\r
315 + *status_ret = (notmuch_private_status_t) notmuch_database_find_message (notmuch,
\r
319 return talloc_steal (notmuch, message);
\r
320 + else if (*status_ret)
\r
323 term = talloc_asprintf (NULL, "%s%s",
\r
324 _find_prefix ("id"), message_id);
\r
325 diff --git a/lib/notmuch.h b/lib/notmuch.h
\r
326 index 6d7a99f..9d7f8f5 100644
\r
327 --- a/lib/notmuch.h
\r
328 +++ b/lib/notmuch.h
\r
329 @@ -286,7 +286,7 @@ notmuch_database_get_directory (notmuch_database_t *database,
\r
330 * If 'message' is not NULL, then, on successful return
\r
331 * (NOTMUCH_STATUS_SUCCESS or NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) '*message'
\r
332 * will be initialized to a message object that can be used for things
\r
333 - * such as adding tags to the just-added message. The user should call
\r
334 + * such as adding tags to the just-added message. The caller should call
\r
335 * notmuch_message_destroy when done with the message. On any failure
\r
336 * '*message' will be set to NULL.
\r
338 @@ -347,35 +347,57 @@ notmuch_database_remove_message (notmuch_database_t *database,
\r
340 /* Find a message with the given message_id.
\r
342 - * If the database contains a message with the given message_id, then
\r
343 - * a new notmuch_message_t object is returned. The caller should call
\r
344 - * notmuch_message_destroy when done with the message.
\r
345 + * If a message with the given message_id is found then, on successful return
\r
346 + * (NOTMUCH_STATUS_SUCCESS) '*message' will be initialized to a message
\r
347 + * object. The caller should call notmuch_message_destroy when done with the
\r
350 - * This function returns NULL in the following situations:
\r
351 + * On any failure or when the message is not found, this function initializes
\r
352 + * '*message' to NULL. This means, when NOTMUCH_STATUS_SUCCESS is returned, the
\r
353 + * caller is supposed to check '*message' for NULL to find out whether the
\r
354 + * message with the given message_id was found.
\r
356 - * * No message is found with the given message_id
\r
357 - * * An out-of-memory situation occurs
\r
358 - * * A Xapian exception occurs
\r
361 + * NOTMUCH_STATUS_SUCCESS: Successful return, check '*message'.
\r
363 + * NOTMUCH_STATUS_NULL_POINTER: The given 'message' argument is NULL
\r
365 + * NOTMUCH_STATUS_OUT_OF_MEMORY: Out of memory, creating message object
\r
367 + * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred
\r
369 -notmuch_message_t *
\r
371 notmuch_database_find_message (notmuch_database_t *database,
\r
372 - const char *message_id);
\r
373 + const char *message_id,
\r
374 + notmuch_message_t **message);
\r
376 /* Find a message with the given filename.
\r
378 - * If the database contains a message with the given filename, then a
\r
379 - * new notmuch_message_t object is returned. The caller should call
\r
380 - * notmuch_message_destroy when done with the message.
\r
381 + * If the database contains a message with the given filename then, on
\r
382 + * successful return (NOTMUCH_STATUS_SUCCESS) '*message' will be initialized to
\r
383 + * a message object. The caller should call notmuch_message_destroy when done
\r
384 + * with the message.
\r
386 - * This function returns NULL in the following situations:
\r
387 + * On any failure or when the message is not found, this function initializes
\r
388 + * '*message' to NULL. This means, when NOTMUCH_STATUS_SUCCESS is returned, the
\r
389 + * caller is supposed to check '*message' for NULL to find out whether the
\r
390 + * message with the given filename is found.
\r
392 - * * No message is found with the given filename
\r
393 - * * An out-of-memory situation occurs
\r
394 - * * A Xapian exception occurs
\r
397 + * NOTMUCH_STATUS_SUCCESS: Successful return, check '*message'
\r
399 + * NOTMUCH_STATUS_NULL_POINTER: The given 'message' argument is NULL
\r
401 + * NOTMUCH_STATUS_OUT_OF_MEMORY: Out of memory, creating the message object
\r
403 + * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred
\r
405 -notmuch_message_t *
\r
407 notmuch_database_find_message_by_filename (notmuch_database_t *notmuch,
\r
408 - const char *filename);
\r
409 + const char *filename,
\r
410 + notmuch_message_t **message);
\r
412 /* Return a list of all tags found in the database.
\r
414 diff --git a/notmuch-new.c b/notmuch-new.c
\r
415 index e79593c..96a1e31 100644
\r
416 --- a/notmuch-new.c
\r
417 +++ b/notmuch-new.c
\r
418 @@ -743,7 +743,9 @@ remove_filename (notmuch_database_t *notmuch,
\r
419 status = notmuch_database_begin_atomic (notmuch);
\r
422 - message = notmuch_database_find_message_by_filename (notmuch, path);
\r
423 + status = notmuch_database_find_message_by_filename (notmuch, path, &message);
\r
424 + if (status || message == NULL)
\r
426 status = notmuch_database_remove_message (notmuch, path);
\r
427 if (status == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) {
\r
428 add_files_state->renamed_messages++;
\r
429 diff --git a/notmuch-restore.c b/notmuch-restore.c
\r
430 index f095f64..e4a5355 100644
\r
431 --- a/notmuch-restore.c
\r
432 +++ b/notmuch-restore.c
\r
433 @@ -87,10 +87,13 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])
\r
434 file_tags = xstrndup (line + match[2].rm_so,
\r
435 match[2].rm_eo - match[2].rm_so);
\r
437 - message = notmuch_database_find_message (notmuch, message_id);
\r
438 - if (message == NULL) {
\r
439 - fprintf (stderr, "Warning: Cannot apply tags to missing message: %s\n",
\r
441 + status = notmuch_database_find_message (notmuch, message_id, &message);
\r
442 + if (status || message == NULL) {
\r
443 + fprintf (stderr, "Warning: Cannot apply tags to %smessage: %s\n",
\r
444 + message ? "" : "missing ", message_id);
\r
446 + fprintf (stderr, "%s\n",
\r
447 + notmuch_status_to_string(status));
\r