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 44D6F429E21
\r
6 for <notmuch@notmuchmail.org>; Mon, 3 Oct 2011 09:49:37 -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 msIDsCPIiafs for <notmuch@notmuchmail.org>;
\r
19 Mon, 3 Oct 2011 09:49:36 -0700 (PDT)
\r
20 Received: from mail-dy0-f53.google.com (mail-dy0-f53.google.com
\r
21 [209.85.220.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 CB7ED431FB6
\r
24 for <notmuch@notmuchmail.org>; Mon, 3 Oct 2011 09:49:35 -0700 (PDT)
\r
25 Received: by dye4 with SMTP id 4so176309dye.26
\r
26 for <notmuch@notmuchmail.org>; Mon, 03 Oct 2011 09:49:33 -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=kSMXOME/xCGF22PVGo39cM0DkSIcWxa3XO+80rKctws=;
\r
31 b=r6walyAMkxFAXucLOejWOUrvpeSdc2uF/XLiL8H7088XXgGQp0BB6R9orZCntoBO40
\r
32 kZNGebetHzl3czpi1HDHu9A+bsEUFwFaoRJeaqtq9IVnycn9hmXRlf7I4yVP1ypPiMz3
\r
33 BagPDt9n97bKy1X7wfIZ/inTt3STWI5OM4T2g=
\r
34 Received: by 10.223.45.85 with SMTP id d21mr180774faf.63.1317660572942;
\r
35 Mon, 03 Oct 2011 09:49:32 -0700 (PDT)
\r
36 Received: from localhost ([88.236.39.101])
\r
37 by mx.google.com with ESMTPS id n1sm21888425fad.20.2011.10.03.09.49.31
\r
38 (version=TLSv1/SSLv3 cipher=OTHER);
\r
39 Mon, 03 Oct 2011 09:49:32 -0700 (PDT)
\r
40 From: Ali Polatel <polatel@gmail.com>
\r
41 To: Notmuch Mailing List <notmuch@notmuchmail.org>
\r
42 Subject: [PATCH v1 1/1] lib: make find_message{,by_filename) report errors
\r
43 Date: Mon, 3 Oct 2011 19:49:20 +0300
\r
45 <1218582065f35bfcc5b84dfc1fbce21fc05034a6.1317660324.git.alip@exherbo.org>
\r
46 X-Mailer: git-send-email 1.7.6.1
\r
47 In-Reply-To: <cover.1317660324.git.alip@exherbo.org>
\r
48 References: <cover.1317456435.git.alip@exherbo.org>
\r
49 <cover.1317660324.git.alip@exherbo.org>
\r
50 In-Reply-To: <cover.1317660324.git.alip@exherbo.org>
\r
51 References: <cover.1317660324.git.alip@exherbo.org>
\r
52 Organization: Pink Floyd
\r
53 Cc: Austin Clements <amdragon@MIT.EDU>
\r
54 X-BeenThere: notmuch@notmuchmail.org
\r
55 X-Mailman-Version: 2.1.13
\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: Mon, 03 Oct 2011 16:49:37 -0000
\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
72 For more information, read the thread on the notmuch mailing list
\r
73 starting with my mail "id:871uv2unfd.fsf@gmail.com"
\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
79 restore: Modify for the new notmuch_database_find_message()
\r
80 new: Modify for the new notmuch_database_find_message_by_filename()
\r
82 lib/database.cc | 90 ++++++++++++++++++++++++++++++++++------------------
\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, 115 insertions(+), 57 deletions(-)
\r
89 diff --git a/lib/database.cc b/lib/database.cc
\r
90 index 9299c8d..1705232 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
97 -notmuch_message_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
104 notmuch_private_status_t status;
\r
105 unsigned int doc_id;
\r
107 + if (message == NULL)
\r
108 + return NOTMUCH_STATUS_NULL_POINTER;
\r
110 if (strlen (message_id) > NOTMUCH_MESSAGE_ID_MAX)
\r
111 message_id = _message_id_compressed (notmuch, message_id);
\r
113 @@ -375,14 +379,21 @@ notmuch_database_find_message (notmuch_database_t *notmuch,
\r
114 message_id, &doc_id);
\r
116 if (status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND)
\r
120 + *message = _notmuch_message_create (notmuch, notmuch, doc_id,
\r
122 + if (*message == NULL)
\r
123 + return NOTMUCH_STATUS_OUT_OF_MEMORY;
\r
126 - return _notmuch_message_create (notmuch, notmuch, doc_id, NULL);
\r
127 + return NOTMUCH_STATUS_SUCCESS;
\r
128 } catch (const Xapian::Error &error) {
\r
129 fprintf (stderr, "A Xapian exception occurred finding message: %s.\n",
\r
130 error.get_msg().c_str());
\r
131 notmuch->exception_reported = TRUE;
\r
134 + return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
\r
138 @@ -1311,7 +1322,8 @@ _get_metadata_thread_id_key (void *ctx, const char *message_id)
\r
140 /* Find the thread ID to which the message with 'message_id' belongs.
\r
142 - * Always returns a newly talloced string belonging to 'ctx'.
\r
143 + * Note: 'thread_id' must not be NULL!
\r
144 + * On success '*thread_id' is set to a newly talloced string belonging to 'ctx'.
\r
146 * Note: If there is no message in the database with the given
\r
147 * 'message_id' then a new thread_id will be allocated for this
\r
148 @@ -1319,25 +1331,29 @@ _get_metadata_thread_id_key (void *ctx, const char *message_id)
\r
149 * thread ID can be looked up if the message is added to the database
\r
152 -static const char *
\r
153 +static notmuch_status_t
\r
154 _resolve_message_id_to_thread_id (notmuch_database_t *notmuch,
\r
156 - const char *message_id)
\r
157 + const char *message_id,
\r
158 + const char **thread_id)
\r
160 + notmuch_status_t status;
\r
161 notmuch_message_t *message;
\r
162 string thread_id_string;
\r
163 - const char *thread_id;
\r
164 char *metadata_key;
\r
165 Xapian::WritableDatabase *db;
\r
167 - message = notmuch_database_find_message (notmuch, message_id);
\r
168 + status = notmuch_database_find_message (notmuch, message_id, &message);
\r
174 - thread_id = talloc_steal (ctx, notmuch_message_get_thread_id (message));
\r
175 + *thread_id = talloc_steal (ctx, notmuch_message_get_thread_id (message));
\r
177 notmuch_message_destroy (message);
\r
179 - return thread_id;
\r
180 + return NOTMUCH_STATUS_SUCCESS;
\r
183 /* Message has not been seen yet.
\r
184 @@ -1351,15 +1367,15 @@ _resolve_message_id_to_thread_id (notmuch_database_t *notmuch,
\r
185 thread_id_string = notmuch->xapian_db->get_metadata (metadata_key);
\r
187 if (thread_id_string.empty()) {
\r
188 - thread_id = _notmuch_database_generate_thread_id (notmuch);
\r
189 - db->set_metadata (metadata_key, thread_id);
\r
190 + *thread_id = talloc_strdup (ctx, _notmuch_database_generate_thread_id (notmuch));
\r
191 + db->set_metadata (metadata_key, *thread_id);
\r
193 - thread_id = thread_id_string.c_str();
\r
194 + *thread_id = talloc_strdup(ctx, thread_id_string.c_str());
\r
197 talloc_free (metadata_key);
\r
199 - return talloc_strdup (ctx, thread_id);
\r
200 + return NOTMUCH_STATUS_SUCCESS;
\r
203 static notmuch_status_t
\r
204 @@ -1446,9 +1462,12 @@ _notmuch_database_link_message_to_parents (notmuch_database_t *notmuch,
\r
205 _notmuch_message_add_term (message, "reference",
\r
206 parent_message_id);
\r
208 - parent_thread_id = _resolve_message_id_to_thread_id (notmuch,
\r
210 - parent_message_id);
\r
211 + ret = _resolve_message_id_to_thread_id (notmuch,
\r
213 + parent_message_id,
\r
214 + &parent_thread_id);
\r
218 if (*thread_id == NULL) {
\r
219 *thread_id = talloc_strdup (message, parent_thread_id);
\r
220 @@ -1759,11 +1778,12 @@ notmuch_status_t
\r
221 notmuch_database_remove_message (notmuch_database_t *notmuch,
\r
222 const char *filename)
\r
224 - notmuch_message_t *message =
\r
225 - notmuch_database_find_message_by_filename (notmuch, filename);
\r
226 - notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
\r
227 + notmuch_status_t status;
\r
228 + notmuch_message_t *message;
\r
231 + status = notmuch_database_find_message_by_filename (notmuch, filename, &message);
\r
233 + if (status == NOTMUCH_STATUS_SUCCESS && message) {
\r
234 status = _notmuch_message_remove_filename (message, filename);
\r
235 if (status == NOTMUCH_STATUS_SUCCESS)
\r
236 _notmuch_message_delete (message);
\r
237 @@ -1774,24 +1794,27 @@ notmuch_database_remove_message (notmuch_database_t *notmuch,
\r
241 -notmuch_message_t *
\r
243 notmuch_database_find_message_by_filename (notmuch_database_t *notmuch,
\r
244 - const char *filename)
\r
245 + const char *filename,
\r
246 + notmuch_message_t **message)
\r
249 const char *prefix = _find_prefix ("file-direntry");
\r
250 char *direntry, *term;
\r
251 Xapian::PostingIterator i, end;
\r
252 - notmuch_message_t *message = NULL;
\r
253 notmuch_status_t status;
\r
255 + if (message == NULL)
\r
256 + return NOTMUCH_STATUS_NULL_POINTER;
\r
258 local = talloc_new (notmuch);
\r
261 status = _notmuch_database_filename_to_direntry (local, notmuch,
\r
262 filename, &direntry);
\r
267 term = talloc_asprintf (local, "%s%s", prefix, direntry);
\r
269 @@ -1800,19 +1823,24 @@ notmuch_database_find_message_by_filename (notmuch_database_t *notmuch,
\r
271 notmuch_private_status_t private_status;
\r
273 - message = _notmuch_message_create (notmuch, notmuch,
\r
274 - *i, &private_status);
\r
275 + *message = _notmuch_message_create (notmuch, notmuch,
\r
276 + *i, &private_status);
\r
277 + if (*message == NULL)
\r
278 + status = NOTMUCH_STATUS_OUT_OF_MEMORY;
\r
280 } catch (const Xapian::Error &error) {
\r
281 fprintf (stderr, "Error: A Xapian exception occurred finding message by filename: %s\n",
\r
282 error.get_msg().c_str());
\r
283 notmuch->exception_reported = TRUE;
\r
285 + status = NOTMUCH_STATUS_OUT_OF_MEMORY;
\r
289 talloc_free (local);
\r
297 notmuch_string_list_t *
\r
298 diff --git a/lib/message.cc b/lib/message.cc
\r
299 index 531d304..2a85744 100644
\r
300 --- a/lib/message.cc
\r
301 +++ b/lib/message.cc
\r
302 @@ -216,11 +216,11 @@ _notmuch_message_create_for_message_id (notmuch_database_t *notmuch,
\r
303 unsigned int doc_id;
\r
306 - *status_ret = NOTMUCH_PRIVATE_STATUS_SUCCESS;
\r
308 - message = notmuch_database_find_message (notmuch, message_id);
\r
309 + *status_ret = (notmuch_private_status_t) notmuch_database_find_message (notmuch, message_id, &message);
\r
311 return talloc_steal (notmuch, message);
\r
312 + else if (*status_ret)
\r
315 term = talloc_asprintf (NULL, "%s%s",
\r
316 _find_prefix ("id"), message_id);
\r
317 diff --git a/lib/notmuch.h b/lib/notmuch.h
\r
318 index 6d7a99f..08b4ce2 100644
\r
319 --- a/lib/notmuch.h
\r
320 +++ b/lib/notmuch.h
\r
321 @@ -347,35 +347,60 @@ notmuch_database_remove_message (notmuch_database_t *database,
\r
323 /* Find a message with the given message_id.
\r
325 - * If the database contains a message with the given message_id, then
\r
326 - * a new notmuch_message_t object is returned. The caller should call
\r
327 - * notmuch_message_destroy when done with the message.
\r
328 + * If message with the given message_id is found then, on successful return
\r
329 + * (NOTMUCH_STATUS_SUCCESS) '*message' will be initialized to a message object.
\r
330 + * The user should call notmuch_message_destroy when done with the message.
\r
332 - * This function returns NULL in the following situations:
\r
333 + * On any failure or when the message is not found, this function initializes
\r
334 + * '*message' to NULL. This means, when NOTMUCH_STATUS_SUCCESS is returned, the
\r
335 + * user is supposed to check '*message' for NULL to find out whether the
\r
336 + * message with the given message_id was found.
\r
338 - * * No message is found with the given message_id
\r
339 - * * An out-of-memory situation occurs
\r
340 - * * A Xapian exception occurs
\r
341 + * Note: The argument 'message' must not be NULL!
\r
345 + * NOTMUCH_STATUS_SUCCESS: Successful return, check '*message'.
\r
347 + * NOTMUCH_STATUS_NULL_POINTER: The given 'message' argument is NULL
\r
349 + * NOTMUCH_STATUS_OUT_OF_MEMORY: Out of memory, creating message object
\r
351 + * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred
\r
353 -notmuch_message_t *
\r
355 notmuch_database_find_message (notmuch_database_t *database,
\r
356 - const char *message_id);
\r
357 + const char *message_id,
\r
358 + notmuch_message_t **message);
\r
360 /* Find a message with the given filename.
\r
362 - * If the database contains a message with the given filename, then a
\r
363 - * new notmuch_message_t object is returned. The caller should call
\r
364 - * notmuch_message_destroy when done with the message.
\r
365 + * If the database contains a message with the given filename then, on
\r
366 + * successful return (NOTMUCH_STATUS_SUCCESS) '*message' will be initialized to
\r
367 + * a message object. The user should call notmuch_message_destroy when done
\r
368 + * with the message.
\r
370 - * This function returns NULL in the following situations:
\r
371 + * On any failure or when the message is not found, this function initializes
\r
372 + * '*message' to NULL. This means, when NOTMUCH_STATUS_SUCCESS is returned, the
\r
373 + * user is supposed to check '*message' for NULL to find out whether the
\r
374 + * message with the given filename is found.
\r
376 - * * No message is found with the given filename
\r
377 - * * An out-of-memory situation occurs
\r
378 - * * A Xapian exception occurs
\r
379 + * Note: The argument 'message' must not be NULL!
\r
383 + * NOTMUCH_STATUS_SUCCESS: Successful return, check '*message'
\r
385 + * NOTMUCH_STATUS_NULL_POINTER: The given 'message' argument is NULL
\r
387 + * NOTMUCH_STATUS_OUT_OF_MEMORY: Out of memory, creating the message object
\r
389 + * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred
\r
391 -notmuch_message_t *
\r
393 notmuch_database_find_message_by_filename (notmuch_database_t *notmuch,
\r
394 - const char *filename);
\r
395 + const char *filename,
\r
396 + notmuch_message_t **message);
\r
398 /* Return a list of all tags found in the database.
\r
400 diff --git a/notmuch-new.c b/notmuch-new.c
\r
401 index e79593c..96a1e31 100644
\r
402 --- a/notmuch-new.c
\r
403 +++ b/notmuch-new.c
\r
404 @@ -743,7 +743,9 @@ remove_filename (notmuch_database_t *notmuch,
\r
405 status = notmuch_database_begin_atomic (notmuch);
\r
408 - message = notmuch_database_find_message_by_filename (notmuch, path);
\r
409 + status = notmuch_database_find_message_by_filename (notmuch, path, &message);
\r
410 + if (status || message == NULL)
\r
412 status = notmuch_database_remove_message (notmuch, path);
\r
413 if (status == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) {
\r
414 add_files_state->renamed_messages++;
\r
415 diff --git a/notmuch-restore.c b/notmuch-restore.c
\r
416 index f095f64..e4a5355 100644
\r
417 --- a/notmuch-restore.c
\r
418 +++ b/notmuch-restore.c
\r
419 @@ -87,10 +87,13 @@ notmuch_restore_command (unused (void *ctx), int argc, char *argv[])
\r
420 file_tags = xstrndup (line + match[2].rm_so,
\r
421 match[2].rm_eo - match[2].rm_so);
\r
423 - message = notmuch_database_find_message (notmuch, message_id);
\r
424 - if (message == NULL) {
\r
425 - fprintf (stderr, "Warning: Cannot apply tags to missing message: %s\n",
\r
427 + status = notmuch_database_find_message (notmuch, message_id, &message);
\r
428 + if (status || message == NULL) {
\r
429 + fprintf (stderr, "Warning: Cannot apply tags to %smessage: %s\n",
\r
430 + message ? "" : "missing ", message_id);
\r
432 + fprintf (stderr, "%s\n",
\r
433 + notmuch_status_to_string(status));
\r