]> git.notmuchmail.org Git - notmuch/blob - message.cc
add_message: Propagate error status from notmuch_message_create_for_message_id
[notmuch] / message.cc
1 /* message.cc - Results of message-based searches from a notmuch database
2  *
3  * Copyright © 2009 Carl Worth
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see http://www.gnu.org/licenses/ .
17  *
18  * Author: Carl Worth <cworth@cworth.org>
19  */
20
21 #include "notmuch-private.h"
22 #include "database-private.h"
23
24 #include <xapian.h>
25
26 struct _notmuch_message {
27     notmuch_database_t *notmuch;
28     Xapian::docid doc_id;
29     char *message_id;
30     char *filename;
31     Xapian::Document doc;
32 };
33
34 typedef struct _notmuch_terms {
35     char prefix_char;
36     Xapian::TermIterator iterator;
37     Xapian::TermIterator iterator_end;
38 } notmuch_terms_t;
39
40 struct _notmuch_tags {
41     notmuch_terms_t terms;
42 };
43
44 struct _notmuch_thread_ids {
45     notmuch_terms_t terms;
46 };
47
48 /* "128 bits of thread-id ought to be enough for anybody" */
49 #define NOTMUCH_THREAD_ID_BITS   128
50 #define NOTMUCH_THREAD_ID_DIGITS (NOTMUCH_THREAD_ID_BITS / 4)
51 typedef struct _thread_id {
52     char str[NOTMUCH_THREAD_ID_DIGITS + 1];
53 } thread_id_t;
54
55 /* We end up having to call the destructor explicitly because we had
56  * to use "placement new" in order to initialize C++ objects within a
57  * block that we allocated with talloc. So C++ is making talloc
58  * slightly less simple to use, (we wouldn't need
59  * talloc_set_destructor at all otherwise).
60  */
61 static int
62 _notmuch_message_destructor (notmuch_message_t *message)
63 {
64     message->doc.~Document ();
65
66     return 0;
67 }
68
69 /* Create a new notmuch_message_t object for an existing document in
70  * the database.
71  *
72  * Here, 'talloc owner' is an optional talloc context to which the new
73  * message will belong. This allows for the caller to not bother
74  * calling notmuch_message_destroy on the message, and no that all
75  * memory will be reclaimed with 'talloc_owner' is free. The caller
76  * still can call notmuch_message_destroy when finished with the
77  * message if desired.
78  *
79  * The 'talloc_owner' argument can also be NULL, in which case the
80  * caller *is* responsible for calling notmuch_message_destroy.
81  *
82  * If no document exists in the database with document ID of 'doc_id'
83  * then this function returns NULL and sets *status to
84  * NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND.
85  *
86  * This function can also fail to due lack of available memory,
87  * returning NULL and optionally setting *status to
88  * NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY. Caller can pass NULL
89  * for status if uninterested in distinguishing these two cases.
90  */
91 notmuch_message_t *
92 _notmuch_message_create (const void *talloc_owner,
93                          notmuch_database_t *notmuch,
94                          unsigned int doc_id,
95                          notmuch_private_status_t *status)
96 {
97     notmuch_message_t *message;
98
99     if (status)
100         *status = NOTMUCH_PRIVATE_STATUS_SUCCESS;
101
102     message = talloc (talloc_owner, notmuch_message_t);
103     if (unlikely (message == NULL)) {
104         if (status)
105             *status = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
106         return NULL;
107     }
108
109     message->notmuch = notmuch;
110     message->doc_id = doc_id;
111     message->message_id = NULL; /* lazily created */
112     message->filename = NULL; /* lazily created */
113
114     /* This is C++'s creepy "placement new", which is really just an
115      * ugly way to call a constructor for a pre-allocated object. So
116      * it's really not an error to not be checking for OUT_OF_MEMORY
117      * here, since this "new" isn't actually allocating memory. This
118      * is language-design comedy of the wrong kind. */
119
120     new (&message->doc) Xapian::Document;
121
122     talloc_set_destructor (message, _notmuch_message_destructor);
123
124     try {
125         message->doc = notmuch->xapian_db->get_document (doc_id);
126     } catch (const Xapian::DocNotFoundError &error) {
127         talloc_free (message);
128         if (status)
129             *status = NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
130         return NULL;
131     }
132
133     return message;
134 }
135
136 /* Create a new notmuch_message_t object for a specific message ID,
137  * (which may or may not already exist in the databas).
138  *
139  * Here, 'talloc owner' is an optional talloc context to which the new
140  * message will belong. This allows for the caller to not bother
141  * calling notmuch_message_destroy on the message, and no that all
142  * memory will be reclaimed with 'talloc_owner' is free. The caller
143  * still can call notmuch_message_destroy when finished with the
144  * message if desired.
145  *
146  * The 'talloc_owner' argument can also be NULL, in which case the
147  * caller *is* responsible for calling notmuch_message_destroy.
148  *
149  * If there is already a document with message ID 'message_id' in the
150  * database, then the returned message can be used to query/modify the
151  * document. Otherwise, a new document will be inserted into the
152  * database before this function returns.
153  *
154  * If an error occurs, this function will return NULL and *status
155  * will be set as appropriate. (The status pointer argument must
156  * not be NULL.)
157  */
158 notmuch_message_t *
159 _notmuch_message_create_for_message_id (const void *talloc_owner,
160                                         notmuch_database_t *notmuch,
161                                         const char *message_id,
162                                         notmuch_status_t *status)
163 {
164     notmuch_private_status_t private_status;
165     notmuch_message_t *message;
166     Xapian::Document doc;
167     unsigned int doc_id;
168     char *term;
169
170     *status = NOTMUCH_STATUS_SUCCESS;
171
172     message = notmuch_database_find_message (notmuch, message_id);
173     if (message)
174         return talloc_steal (talloc_owner, message);
175
176     term = talloc_asprintf (NULL, "%s%s",
177                             _find_prefix ("id"), message_id);
178     if (term == NULL) {
179         *status = NOTMUCH_STATUS_OUT_OF_MEMORY;
180         return NULL;
181     }
182
183     try {
184         doc.add_term (term);
185         talloc_free (term);
186
187         doc.add_value (NOTMUCH_VALUE_MESSAGE_ID, message_id);
188
189         doc_id = notmuch->xapian_db->add_document (doc);
190     } catch (const Xapian::Error &error) {
191         *status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
192         return NULL;
193     }
194
195     message = _notmuch_message_create (talloc_owner, notmuch,
196                                        doc_id, &private_status);
197
198     if (private_status >= (notmuch_private_status_t) NOTMUCH_STATUS_LAST_STATUS)
199     {
200         fprintf (stderr, "Internal error: Failed to find document immediately after adding it.\n");
201         exit (1);
202     }
203
204     *status = (notmuch_status_t) private_status;
205
206     return message;
207 }
208
209 const char *
210 notmuch_message_get_message_id (notmuch_message_t *message)
211 {
212     Xapian::TermIterator i;
213
214     if (message->message_id)
215         return message->message_id;
216
217     i = message->doc.termlist_begin ();
218     i.skip_to (_find_prefix ("id"));
219
220     if (i == message->doc.termlist_end ()) {
221         fprintf (stderr, "Internal error: Message with document ID of %d has no message ID.\n",
222                  message->doc_id);
223         exit (1);
224     }
225
226     message->message_id = talloc_strdup (message, (*i).c_str () + 1);
227     return message->message_id;
228 }
229
230 /* Set the filename for 'message' to 'filename'.
231  *
232  * XXX: We should still figure out what we want to do for multiple
233  * files with identical message IDs. We will probably want to store a
234  * list of filenames here, (so that this will be "add_filename"
235  * instead of "set_filename"). Which would make this very similar to
236  * add_thread_ids.
237  *
238  * This change will not be reflected in the database until the next
239  * call to _notmuch_message_set_sync. */
240 void
241 _notmuch_message_set_filename (notmuch_message_t *message,
242                                const char *filename)
243 {
244     if (message->filename)
245         talloc_free (message->filename);
246     message->doc.set_data (filename);
247 }
248
249 const char *
250 notmuch_message_get_filename (notmuch_message_t *message)
251 {
252     std::string filename_str;
253
254     if (message->filename)
255         return message->filename;
256
257     filename_str = message->doc.get_data ();
258     message->filename = talloc_strdup (message, filename_str.c_str ());
259
260     return message->filename;
261 }
262
263 /* We end up having to call the destructors explicitly because we had
264  * to use "placement new" in order to initialize C++ objects within a
265  * block that we allocated with talloc. So C++ is making talloc
266  * slightly less simple to use, (we wouldn't need
267  * talloc_set_destructor at all otherwise).
268  */
269 static int
270 _notmuch_terms_destructor (notmuch_terms_t *terms)
271 {
272     terms->iterator.~TermIterator ();
273     terms->iterator_end.~TermIterator ();
274
275     return 0;
276 }
277
278 notmuch_terms_t *
279 _notmuch_terms_create (void *ctx,
280                        Xapian::Document doc,
281                        const char *prefix_name)
282 {
283     notmuch_terms_t *terms;
284     const char *prefix = _find_prefix (prefix_name);
285
286     /* Currently, notmuch_terms_t is written with the assumption that
287      * any prefix its derivatives use will be only a single
288      * character. */
289     assert (strlen (prefix) == 1);
290
291     terms = talloc (ctx, notmuch_terms_t);
292     if (unlikely (terms == NULL))
293         return NULL;
294
295     terms->prefix_char = *prefix;
296     new (&terms->iterator) Xapian::TermIterator;
297     new (&terms->iterator_end) Xapian::TermIterator;
298
299     talloc_set_destructor (terms, _notmuch_terms_destructor);
300
301     terms->iterator = doc.termlist_begin ();
302     terms->iterator.skip_to (prefix);
303     terms->iterator_end = doc.termlist_end ();
304
305     return terms;
306 }
307
308 /* The assertion is to ensure that 'type' is a derivative of
309  * notmuch_terms_t in that it contains a notmuch_terms_t as its first
310  * member. We do this by name of 'terms' as opposed to type, because
311  * that's as clever as I've been so far. */
312 #define _notmuch_terms_create_type(ctx, doc, prefix_name, type) \
313     (COMPILE_TIME_ASSERT(offsetof(type, terms) == 0),           \
314      (type *) _notmuch_terms_create (ctx, doc, prefix_name))
315
316 notmuch_tags_t *
317 notmuch_message_get_tags (notmuch_message_t *message)
318 {
319     return _notmuch_terms_create_type (message, message->doc, "tag",
320                                        notmuch_tags_t);
321 }
322
323 notmuch_thread_ids_t *
324 notmuch_message_get_thread_ids (notmuch_message_t *message)
325 {
326     return _notmuch_terms_create_type (message, message->doc, "thread",
327                                        notmuch_thread_ids_t);
328 }
329
330 void
331 _notmuch_message_set_date (notmuch_message_t *message,
332                            const char *date)
333 {
334     time_t time_value;
335
336     time_value = notmuch_parse_date (date, NULL);
337
338     message->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
339                             Xapian::sortable_serialise (time_value));
340 }
341
342 void
343 _notmuch_message_add_thread_id (notmuch_message_t *message,
344                                 const char *thread_id)
345 {
346     _notmuch_message_add_term (message, "thread", thread_id);
347 }
348
349 static void
350 thread_id_generate (thread_id_t *thread_id)
351 {
352     static int seeded = 0;
353     FILE *dev_random;
354     uint32_t value;
355     char *s;
356     int i;
357
358     if (! seeded) {
359         dev_random = fopen ("/dev/random", "r");
360         if (dev_random == NULL) {
361             srand (time (NULL));
362         } else {
363             fread ((void *) &value, sizeof (value), 1, dev_random);
364             srand (value);
365             fclose (dev_random);
366         }
367         seeded = 1;
368     }
369
370     s = thread_id->str;
371     for (i = 0; i < NOTMUCH_THREAD_ID_DIGITS; i += 8) {
372         value = rand ();
373         sprintf (s, "%08x", value);
374         s += 8;
375     }
376 }
377
378 void
379 _notmuch_message_ensure_thread_id (notmuch_message_t *message)
380 {
381     /* If not part of any existing thread, generate a new thread_id. */
382     thread_id_t thread_id;
383
384     thread_id_generate (&thread_id);
385     _notmuch_message_add_term (message, "thread", thread_id.str);
386 }
387
388 /* Synchronize changes made to message->doc out into the database. */
389 void
390 _notmuch_message_sync (notmuch_message_t *message)
391 {
392     Xapian::WritableDatabase *db = message->notmuch->xapian_db;
393
394     db->replace_document (message->doc_id, message->doc);
395 }
396
397 /* Add a name:value term to 'message', (the actual term will be
398  * encoded by prefixing the value with a short prefix). See
399  * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
400  * names to prefix values.
401  *
402  * This change will not be reflected in the database until the next
403  * call to _notmuch_message_set_sync. */
404 notmuch_private_status_t
405 _notmuch_message_add_term (notmuch_message_t *message,
406                            const char *prefix_name,
407                            const char *value)
408 {
409
410     char *term;
411
412     if (value == NULL)
413         return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
414
415     term = talloc_asprintf (message, "%s%s",
416                             _find_prefix (prefix_name), value);
417
418     if (strlen (term) > NOTMUCH_TERM_MAX)
419         return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
420
421     message->doc.add_term (term);
422
423     talloc_free (term);
424
425     return NOTMUCH_PRIVATE_STATUS_SUCCESS;
426 }
427
428 /* Remove a name:value term from 'message', (the actual term will be
429  * encoded by prefixing the value with a short prefix). See
430  * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
431  * names to prefix values.
432  *
433  * This change will not be reflected in the database until the next
434  * call to _notmuch_message_set_sync. */
435 notmuch_private_status_t
436 _notmuch_message_remove_term (notmuch_message_t *message,
437                               const char *prefix_name,
438                               const char *value)
439 {
440     char *term;
441
442     if (value == NULL)
443         return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
444
445     term = talloc_asprintf (message, "%s%s",
446                             _find_prefix (prefix_name), value);
447
448     if (strlen (term) > NOTMUCH_TERM_MAX)
449         return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
450
451     message->doc.remove_term (term);
452
453     talloc_free (term);
454
455     return NOTMUCH_PRIVATE_STATUS_SUCCESS;
456 }
457
458 notmuch_status_t
459 notmuch_message_add_tag (notmuch_message_t *message, const char *tag)
460 {
461     notmuch_private_status_t status;
462
463     if (tag == NULL)
464         return NOTMUCH_STATUS_NULL_POINTER;
465
466     if (strlen (tag) > NOTMUCH_TAG_MAX)
467         return NOTMUCH_STATUS_TAG_TOO_LONG;
468
469     status = _notmuch_message_add_term (message, "tag", tag);
470     if (status) {
471         fprintf (stderr, "Internal error: _notmuch_message_add_term return unexpected value: %d\n",
472                  status);
473         exit (1);
474     }
475
476     _notmuch_message_sync (message);
477
478     return NOTMUCH_STATUS_SUCCESS;
479 }
480
481 notmuch_status_t
482 notmuch_message_remove_tag (notmuch_message_t *message, const char *tag)
483 {
484     notmuch_private_status_t status;
485
486     if (tag == NULL)
487         return NOTMUCH_STATUS_NULL_POINTER;
488
489     if (strlen (tag) > NOTMUCH_TAG_MAX)
490         return NOTMUCH_STATUS_TAG_TOO_LONG;
491
492     status = _notmuch_message_remove_term (message, "tag", tag);
493     if (status) {
494         fprintf (stderr, "Internal error: _notmuch_message_remove_term return unexpected value: %d\n",
495                  status);
496         exit (1);
497     }
498
499     _notmuch_message_sync (message);
500
501     return NOTMUCH_STATUS_SUCCESS;
502 }
503
504 void
505 notmuch_message_destroy (notmuch_message_t *message)
506 {
507     talloc_free (message);
508 }
509
510 notmuch_bool_t
511 _notmuch_terms_has_more (notmuch_terms_t *terms)
512 {
513     std::string s;
514
515     if (terms->iterator == terms->iterator_end)
516         return FALSE;
517
518     s = *terms->iterator;
519     if (! s.empty () && s[0] == terms->prefix_char)
520         return TRUE;
521     else
522         return FALSE;
523 }
524
525 const char *
526 _notmuch_terms_get (notmuch_terms_t *terms)
527 {
528     return talloc_strdup (terms, (*terms->iterator).c_str () + 1);
529 }
530
531 void
532 _notmuch_terms_advance (notmuch_terms_t *terms)
533 {
534     terms->iterator++;
535 }
536
537 void
538 _notmuch_terms_destroy (notmuch_terms_t *terms)
539 {
540     talloc_free (terms);
541 }
542
543 notmuch_bool_t
544 notmuch_tags_has_more (notmuch_tags_t *tags)
545 {
546     return _notmuch_terms_has_more (&tags->terms);
547 }
548
549 const char *
550 notmuch_tags_get (notmuch_tags_t *tags)
551 {
552     return _notmuch_terms_get (&tags->terms);
553 }
554
555 void
556 notmuch_tags_advance (notmuch_tags_t *tags)
557 {
558     return _notmuch_terms_advance (&tags->terms);
559 }
560
561 void
562 notmuch_tags_destroy (notmuch_tags_t *tags)
563 {
564     return _notmuch_terms_destroy (&tags->terms);
565 }
566
567 notmuch_bool_t
568 notmuch_thread_ids_has_more (notmuch_thread_ids_t *thread_ids)
569 {
570     return _notmuch_terms_has_more (&thread_ids->terms);
571 }
572
573 const char *
574 notmuch_thread_ids_get (notmuch_thread_ids_t *thread_ids)
575 {
576     return _notmuch_terms_get (&thread_ids->terms);
577 }
578
579 void
580 notmuch_thread_ids_advance (notmuch_thread_ids_t *thread_ids)
581 {
582     return _notmuch_terms_advance (&thread_ids->terms);
583 }
584
585 void
586 notmuch_thread_ids_destroy (notmuch_thread_ids_t *thread_ids)
587 {
588     return _notmuch_terms_destroy (&thread_ids->terms);
589 }