]> git.notmuchmail.org Git - notmuch/blob - lib/message.cc
Avoid database corruption by not adding partially-constructed mail documents.
[notmuch] / lib / 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 <stdint.h>
25
26 #include <gmime/gmime.h>
27
28 #include <xapian.h>
29
30 struct _notmuch_message {
31     notmuch_database_t *notmuch;
32     Xapian::docid doc_id;
33     int frozen;
34     char *message_id;
35     char *thread_id;
36     char *in_reply_to;
37     char *filename;
38     char *author;
39     notmuch_message_file_t *message_file;
40     notmuch_message_list_t *replies;
41     unsigned long flags;
42
43     Xapian::Document doc;
44 };
45
46 /* We end up having to call the destructor explicitly because we had
47  * to use "placement new" in order to initialize C++ objects within a
48  * block that we allocated with talloc. So C++ is making talloc
49  * slightly less simple to use, (we wouldn't need
50  * talloc_set_destructor at all otherwise).
51  */
52 static int
53 _notmuch_message_destructor (notmuch_message_t *message)
54 {
55     message->doc.~Document ();
56
57     return 0;
58 }
59
60 static notmuch_message_t *
61 _notmuch_message_create_for_document (const void *talloc_owner,
62                                       notmuch_database_t *notmuch,
63                                       unsigned int doc_id,
64                                       Xapian::Document doc,
65                                       notmuch_private_status_t *status)
66 {
67     notmuch_message_t *message;
68
69     if (status)
70         *status = NOTMUCH_PRIVATE_STATUS_SUCCESS;
71
72     message = talloc (talloc_owner, notmuch_message_t);
73     if (unlikely (message == NULL)) {
74         if (status)
75             *status = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
76         return NULL;
77     }
78
79     message->notmuch = notmuch;
80     message->doc_id = doc_id;
81
82     message->frozen = 0;
83     message->flags = 0;
84
85     /* Each of these will be lazily created as needed. */
86     message->message_id = NULL;
87     message->thread_id = NULL;
88     message->in_reply_to = NULL;
89     message->filename = NULL;
90     message->message_file = NULL;
91     message->author = NULL;
92
93     message->replies = _notmuch_message_list_create (message);
94     if (unlikely (message->replies == NULL)) {
95         if (status)
96             *status = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
97         return NULL;
98     }
99
100     /* This is C++'s creepy "placement new", which is really just an
101      * ugly way to call a constructor for a pre-allocated object. So
102      * it's really not an error to not be checking for OUT_OF_MEMORY
103      * here, since this "new" isn't actually allocating memory. This
104      * is language-design comedy of the wrong kind. */
105
106     new (&message->doc) Xapian::Document;
107
108     talloc_set_destructor (message, _notmuch_message_destructor);
109
110     message->doc = doc;
111
112     return message;
113 }
114
115 /* Create a new notmuch_message_t object for an existing document in
116  * the database.
117  *
118  * Here, 'talloc owner' is an optional talloc context to which the new
119  * message will belong. This allows for the caller to not bother
120  * calling notmuch_message_destroy on the message, and no that all
121  * memory will be reclaimed with 'talloc_owner' is free. The caller
122  * still can call notmuch_message_destroy when finished with the
123  * message if desired.
124  *
125  * The 'talloc_owner' argument can also be NULL, in which case the
126  * caller *is* responsible for calling notmuch_message_destroy.
127  *
128  * If no document exists in the database with document ID of 'doc_id'
129  * then this function returns NULL and optionally sets *status to
130  * NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND.
131  *
132  * This function can also fail to due lack of available memory,
133  * returning NULL and optionally setting *status to
134  * NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY.
135  *
136  * The caller can pass NULL for status if uninterested in
137  * distinguishing these two cases.
138  */
139 notmuch_message_t *
140 _notmuch_message_create (const void *talloc_owner,
141                          notmuch_database_t *notmuch,
142                          unsigned int doc_id,
143                          notmuch_private_status_t *status)
144 {
145     Xapian::Document doc;
146
147     try {
148         doc = notmuch->xapian_db->get_document (doc_id);
149     } catch (const Xapian::DocNotFoundError &error) {
150         if (status)
151             *status = NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
152         return NULL;
153     }
154
155     return _notmuch_message_create_for_document (talloc_owner, notmuch,
156                                                  doc_id, doc, status);
157 }
158
159 /* Create a new notmuch_message_t object for a specific message ID,
160  * (which may or may not already exist in the database).
161  *
162  * The 'notmuch' database will be the talloc owner of the returned
163  * message.
164  *
165  * This function returns a valid notmuch_message_t whether or not
166  * there is already a document in the database with the given message
167  * ID. These two cases can be distinguished by the value of *status:
168  *
169  *
170  *   NOTMUCH_PRIVATE_STATUS_SUCCESS:
171  *
172  *     There is already a document with message ID 'message_id' in the
173  *     database. The returned message can be used to query/modify the
174  *     document.
175  *   NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND:
176  *
177  *     No document with 'message_id' exists in the database. The
178  *     returned message contains a newly created document (not yet
179  *     added to the database) and a document ID that is known not to
180  *     exist in the database. The caller can modify the message, and a
181  *     call to _notmuch_message_sync will add * the document to the
182  *     database.
183  *
184  * If an error occurs, this function will return NULL and *status
185  * will be set as appropriate. (The status pointer argument must
186  * not be NULL.)
187  */
188 notmuch_message_t *
189 _notmuch_message_create_for_message_id (notmuch_database_t *notmuch,
190                                         const char *message_id,
191                                         notmuch_private_status_t *status_ret)
192 {
193     notmuch_message_t *message;
194     Xapian::Document doc;
195     Xapian::WritableDatabase *db;
196     unsigned int doc_id;
197     char *term;
198
199     *status_ret = NOTMUCH_PRIVATE_STATUS_SUCCESS;
200
201     message = notmuch_database_find_message (notmuch, message_id);
202     if (message)
203         return talloc_steal (notmuch, message);
204
205     term = talloc_asprintf (NULL, "%s%s",
206                             _find_prefix ("id"), message_id);
207     if (term == NULL) {
208         *status_ret = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
209         return NULL;
210     }
211
212     if (notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)
213         INTERNAL_ERROR ("Failure to ensure database is writable.");
214
215     db = static_cast<Xapian::WritableDatabase *> (notmuch->xapian_db);
216     try {
217         doc.add_term (term, 0);
218         talloc_free (term);
219
220         doc.add_value (NOTMUCH_VALUE_MESSAGE_ID, message_id);
221
222         doc_id = _notmuch_database_generate_doc_id (notmuch);
223     } catch (const Xapian::Error &error) {
224         fprintf (stderr, "A Xapian exception occurred creating message: %s\n",
225                  error.get_msg().c_str());
226         notmuch->exception_reported = TRUE;
227         *status_ret = NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION;
228         return NULL;
229     }
230
231     message = _notmuch_message_create_for_document (notmuch, notmuch,
232                                                     doc_id, doc, status_ret);
233
234     /* We want to inform the caller that we had to create a new
235      * document. */
236     if (*status_ret == NOTMUCH_PRIVATE_STATUS_SUCCESS)
237         *status_ret = NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
238
239     return message;
240 }
241
242 const char *
243 notmuch_message_get_message_id (notmuch_message_t *message)
244 {
245     Xapian::TermIterator i;
246
247     if (message->message_id)
248         return message->message_id;
249
250     i = message->doc.termlist_begin ();
251     i.skip_to (_find_prefix ("id"));
252
253     if (i == message->doc.termlist_end ())
254         INTERNAL_ERROR ("Message with document ID of %d has no message ID.\n",
255                         message->doc_id);
256
257     message->message_id = talloc_strdup (message, (*i).c_str () + 1);
258
259 #if DEBUG_DATABASE_SANITY
260     i++;
261
262     if (i != message->doc.termlist_end () &&
263         strncmp ((*i).c_str (), _find_prefix ("id"),
264                  strlen (_find_prefix ("id"))) == 0)
265     {
266         INTERNAL_ERROR ("Mail (doc_id: %d) has duplicate message IDs",
267                         message->doc_id);
268     }
269 #endif
270
271     return message->message_id;
272 }
273
274 static void
275 _notmuch_message_ensure_message_file (notmuch_message_t *message)
276 {
277     const char *filename;
278
279     if (message->message_file)
280         return;
281
282     filename = notmuch_message_get_filename (message);
283     if (unlikely (filename == NULL))
284         return;
285
286     message->message_file = _notmuch_message_file_open_ctx (message, filename);
287 }
288
289 const char *
290 notmuch_message_get_header (notmuch_message_t *message, const char *header)
291 {
292     _notmuch_message_ensure_message_file (message);
293     if (message->message_file == NULL)
294         return NULL;
295
296     return notmuch_message_file_get_header (message->message_file, header);
297 }
298
299 /* Return the message ID from the In-Reply-To header of 'message'.
300  *
301  * Returns an empty string ("") if 'message' has no In-Reply-To
302  * header.
303  *
304  * Returns NULL if any error occurs.
305  */
306 const char *
307 _notmuch_message_get_in_reply_to (notmuch_message_t *message)
308 {
309     const char *prefix = _find_prefix ("replyto");
310     int prefix_len = strlen (prefix);
311     Xapian::TermIterator i;
312     std::string in_reply_to;
313
314     if (message->in_reply_to)
315         return message->in_reply_to;
316
317     i = message->doc.termlist_begin ();
318     i.skip_to (prefix);
319
320     if (i != message->doc.termlist_end ())
321         in_reply_to = *i;
322
323     /* It's perfectly valid for a message to have no In-Reply-To
324      * header. For these cases, we return an empty string. */
325     if (i == message->doc.termlist_end () ||
326         strncmp (in_reply_to.c_str (), prefix, prefix_len))
327     {
328         message->in_reply_to = talloc_strdup (message, "");
329         return message->in_reply_to;
330     }
331
332     message->in_reply_to = talloc_strdup (message,
333                                           in_reply_to.c_str () + prefix_len);
334
335 #if DEBUG_DATABASE_SANITY
336     i++;
337
338     in_reply_to = *i;
339
340     if (i != message->doc.termlist_end () &&
341         strncmp ((*i).c_str (), prefix, prefix_len) == 0)
342     {
343        INTERNAL_ERROR ("Message %s has duplicate In-Reply-To IDs: %s and %s\n",
344                         notmuch_message_get_message_id (message),
345                         message->in_reply_to,
346                         (*i).c_str () + prefix_len);
347     }
348 #endif
349
350     return message->in_reply_to;
351 }
352
353 const char *
354 notmuch_message_get_thread_id (notmuch_message_t *message)
355 {
356     const char *prefix = _find_prefix ("thread");
357     Xapian::TermIterator i;
358     std::string id;
359
360     /* This code is written with the assumption that "thread" has a
361      * single-character prefix. */
362     assert (strlen (prefix) == 1);
363
364     if (message->thread_id)
365         return message->thread_id;
366
367     i = message->doc.termlist_begin ();
368     i.skip_to (prefix);
369
370     if (i != message->doc.termlist_end ())
371         id = *i;
372
373     if (i == message->doc.termlist_end () || id[0] != *prefix)
374         INTERNAL_ERROR ("Message with document ID of %d has no thread ID.\n",
375                         message->doc_id);
376
377     message->thread_id = talloc_strdup (message, id.c_str () + 1);
378
379 #if DEBUG_DATABASE_SANITY
380     i++;
381     id = *i;
382
383     if (i != message->doc.termlist_end () && id[0] == *prefix)
384     {
385         INTERNAL_ERROR ("Message %s has duplicate thread IDs: %s and %s\n",
386                         notmuch_message_get_message_id (message),
387                         message->thread_id,
388                         id.c_str () + 1);
389     }
390 #endif
391
392     return message->thread_id;
393 }
394
395 void
396 _notmuch_message_add_reply (notmuch_message_t *message,
397                             notmuch_message_node_t *reply)
398 {
399     _notmuch_message_list_append (message->replies, reply);
400 }
401
402 notmuch_messages_t *
403 notmuch_message_get_replies (notmuch_message_t *message)
404 {
405     return _notmuch_messages_create (message->replies);
406 }
407
408 /* Add an additional 'filename' for 'message'.
409  *
410  * This change will not be reflected in the database until the next
411  * call to _notmuch_message_sync. */
412 notmuch_status_t
413 _notmuch_message_add_filename (notmuch_message_t *message,
414                                const char *filename)
415 {
416     notmuch_status_t status;
417     void *local = talloc_new (message);
418     char *direntry;
419
420     if (message->filename) {
421         talloc_free (message->filename);
422         message->filename = NULL;
423     }
424
425     if (filename == NULL)
426         INTERNAL_ERROR ("Message filename cannot be NULL.");
427
428     status = _notmuch_database_filename_to_direntry (local,
429                                                      message->notmuch,
430                                                      filename, &direntry);
431     if (status)
432         return status;
433
434     _notmuch_message_add_term (message, "file-direntry", direntry);
435
436     talloc_free (local);
437
438     return NOTMUCH_STATUS_SUCCESS;
439 }
440
441 char *
442 _notmuch_message_talloc_copy_data (notmuch_message_t *message)
443 {
444     return talloc_strdup (message, message->doc.get_data ().c_str ());
445 }
446
447 void
448 _notmuch_message_clear_data (notmuch_message_t *message)
449 {
450     message->doc.set_data ("");
451 }
452
453 const char *
454 notmuch_message_get_filename (notmuch_message_t *message)
455 {
456     const char *prefix = _find_prefix ("file-direntry");
457     int prefix_len = strlen (prefix);
458     Xapian::TermIterator i;
459     char *colon, *direntry = NULL;
460     const char *db_path, *directory, *basename;
461     unsigned int directory_id;
462     void *local = talloc_new (message);
463
464     if (message->filename)
465         return message->filename;
466
467     i = message->doc.termlist_begin ();
468     i.skip_to (prefix);
469
470     if (i != message->doc.termlist_end ())
471         direntry = talloc_strdup (local, (*i).c_str ());
472
473     if (i == message->doc.termlist_end () ||
474         strncmp (direntry, prefix, prefix_len))
475     {
476         /* A message document created by an old version of notmuch
477          * (prior to rename support) will have the filename in the
478          * data of the document rather than as a file-direntry term.
479          *
480          * It would be nice to do the upgrade of the document directly
481          * here, but the database is likely open in read-only mode. */
482         const char *data;
483
484         data = message->doc.get_data ().c_str ();
485
486         if (data == NULL)
487             INTERNAL_ERROR ("message with no filename");
488
489         message->filename = talloc_strdup (message, data);
490
491         return message->filename;
492     }
493
494     direntry += prefix_len;
495
496     directory_id = strtol (direntry, &colon, 10);
497
498     if (colon == NULL || *colon != ':')
499         INTERNAL_ERROR ("malformed direntry");
500
501     basename = colon + 1;
502
503     *colon = '\0';
504
505     db_path = notmuch_database_get_path (message->notmuch);
506
507     directory = _notmuch_database_get_directory_path (local,
508                                                       message->notmuch,
509                                                       directory_id);
510
511     if (strlen (directory))
512         message->filename = talloc_asprintf (message, "%s/%s/%s",
513                                              db_path, directory, basename);
514     else
515         message->filename = talloc_asprintf (message, "%s/%s",
516                                              db_path, basename);
517     talloc_free ((void *) directory);
518
519     talloc_free (local);
520
521     return message->filename;
522 }
523
524 notmuch_bool_t
525 notmuch_message_get_flag (notmuch_message_t *message,
526                           notmuch_message_flag_t flag)
527 {
528     return message->flags & (1 << flag);
529 }
530
531 void
532 notmuch_message_set_flag (notmuch_message_t *message,
533                           notmuch_message_flag_t flag, notmuch_bool_t enable)
534 {
535     if (enable)
536         message->flags |= (1 << flag);
537     else
538         message->flags &= ~(1 << flag);
539 }
540
541 time_t
542 notmuch_message_get_date (notmuch_message_t *message)
543 {
544     std::string value;
545
546     try {
547         value = message->doc.get_value (NOTMUCH_VALUE_TIMESTAMP);
548     } catch (Xapian::Error &error) {
549         INTERNAL_ERROR ("Failed to read timestamp value from document.");
550         return 0;
551     }
552
553     return Xapian::sortable_unserialise (value);
554 }
555
556 notmuch_tags_t *
557 notmuch_message_get_tags (notmuch_message_t *message)
558 {
559     Xapian::TermIterator i, end;
560     i = message->doc.termlist_begin();
561     end = message->doc.termlist_end();
562     return _notmuch_convert_tags(message, i, end);
563 }
564
565 const char *
566 notmuch_message_get_author (notmuch_message_t *message)
567 {
568     return message->author;
569 }
570
571 void
572 notmuch_message_set_author (notmuch_message_t *message,
573                             const char *author)
574 {
575     if (message->author)
576         talloc_free(message->author);
577     message->author = talloc_strdup(message, author);
578     return;
579 }
580
581 void
582 _notmuch_message_set_date (notmuch_message_t *message,
583                            const char *date)
584 {
585     time_t time_value;
586
587     /* GMime really doesn't want to see a NULL date, so protect its
588      * sensibilities. */
589     if (date == NULL || *date == '\0')
590         time_value = 0;
591     else
592         time_value = g_mime_utils_header_decode_date (date, NULL);
593
594     message->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
595                             Xapian::sortable_serialise (time_value));
596 }
597
598 /* Synchronize changes made to message->doc out into the database. */
599 void
600 _notmuch_message_sync (notmuch_message_t *message)
601 {
602     Xapian::WritableDatabase *db;
603
604     if (message->notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)
605         return;
606
607     db = static_cast <Xapian::WritableDatabase *> (message->notmuch->xapian_db);
608     db->replace_document (message->doc_id, message->doc);
609 }
610
611 /* Ensure that 'message' is not holding any file object open. Future
612  * calls to various functions will still automatically open the
613  * message file as needed.
614  */
615 void
616 _notmuch_message_close (notmuch_message_t *message)
617 {
618     if (message->message_file) {
619         notmuch_message_file_close (message->message_file);
620         message->message_file = NULL;
621     }
622 }
623
624 /* Add a name:value term to 'message', (the actual term will be
625  * encoded by prefixing the value with a short prefix). See
626  * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
627  * names to prefix values.
628  *
629  * This change will not be reflected in the database until the next
630  * call to _notmuch_message_sync. */
631 notmuch_private_status_t
632 _notmuch_message_add_term (notmuch_message_t *message,
633                            const char *prefix_name,
634                            const char *value)
635 {
636
637     char *term;
638
639     if (value == NULL)
640         return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
641
642     term = talloc_asprintf (message, "%s%s",
643                             _find_prefix (prefix_name), value);
644
645     if (strlen (term) > NOTMUCH_TERM_MAX)
646         return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
647
648     message->doc.add_term (term, 0);
649
650     talloc_free (term);
651
652     return NOTMUCH_PRIVATE_STATUS_SUCCESS;
653 }
654
655 /* Parse 'text' and add a term to 'message' for each parsed word. Each
656  * term will be added both prefixed (if prefix_name is not NULL) and
657  * also unprefixed). */
658 notmuch_private_status_t
659 _notmuch_message_gen_terms (notmuch_message_t *message,
660                             const char *prefix_name,
661                             const char *text)
662 {
663     Xapian::TermGenerator *term_gen = message->notmuch->term_gen;
664
665     if (text == NULL)
666         return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
667
668     term_gen->set_document (message->doc);
669
670     if (prefix_name) {
671         const char *prefix = _find_prefix (prefix_name);
672
673         term_gen->index_text (text, 1, prefix);
674     }
675
676     term_gen->index_text (text);
677
678     return NOTMUCH_PRIVATE_STATUS_SUCCESS;
679 }
680
681 /* Remove a name:value term from 'message', (the actual term will be
682  * encoded by prefixing the value with a short prefix). See
683  * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
684  * names to prefix values.
685  *
686  * This change will not be reflected in the database until the next
687  * call to _notmuch_message_sync. */
688 notmuch_private_status_t
689 _notmuch_message_remove_term (notmuch_message_t *message,
690                               const char *prefix_name,
691                               const char *value)
692 {
693     char *term;
694
695     if (value == NULL)
696         return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
697
698     term = talloc_asprintf (message, "%s%s",
699                             _find_prefix (prefix_name), value);
700
701     if (strlen (term) > NOTMUCH_TERM_MAX)
702         return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
703
704     try {
705         message->doc.remove_term (term);
706     } catch (const Xapian::InvalidArgumentError) {
707         /* We'll let the philosopher's try to wrestle with the
708          * question of whether failing to remove that which was not
709          * there in the first place is failure. For us, we'll silently
710          * consider it all good. */
711     }
712
713     talloc_free (term);
714
715     return NOTMUCH_PRIVATE_STATUS_SUCCESS;
716 }
717
718 notmuch_status_t
719 notmuch_message_add_tag (notmuch_message_t *message, const char *tag)
720 {
721     notmuch_private_status_t private_status;
722     notmuch_status_t status;
723
724     status = _notmuch_database_ensure_writable (message->notmuch);
725     if (status)
726         return status;
727
728     if (tag == NULL)
729         return NOTMUCH_STATUS_NULL_POINTER;
730
731     if (strlen (tag) > NOTMUCH_TAG_MAX)
732         return NOTMUCH_STATUS_TAG_TOO_LONG;
733
734     private_status = _notmuch_message_add_term (message, "tag", tag);
735     if (private_status) {
736         INTERNAL_ERROR ("_notmuch_message_add_term return unexpected value: %d\n",
737                         private_status);
738     }
739
740     if (! message->frozen)
741         _notmuch_message_sync (message);
742
743     return NOTMUCH_STATUS_SUCCESS;
744 }
745
746 notmuch_status_t
747 notmuch_message_remove_tag (notmuch_message_t *message, const char *tag)
748 {
749     notmuch_private_status_t private_status;
750     notmuch_status_t status;
751
752     status = _notmuch_database_ensure_writable (message->notmuch);
753     if (status)
754         return status;
755
756     if (tag == NULL)
757         return NOTMUCH_STATUS_NULL_POINTER;
758
759     if (strlen (tag) > NOTMUCH_TAG_MAX)
760         return NOTMUCH_STATUS_TAG_TOO_LONG;
761
762     private_status = _notmuch_message_remove_term (message, "tag", tag);
763     if (private_status) {
764         INTERNAL_ERROR ("_notmuch_message_remove_term return unexpected value: %d\n",
765                         private_status);
766     }
767
768     if (! message->frozen)
769         _notmuch_message_sync (message);
770
771     return NOTMUCH_STATUS_SUCCESS;
772 }
773
774 notmuch_status_t
775 notmuch_message_remove_all_tags (notmuch_message_t *message)
776 {
777     notmuch_private_status_t private_status;
778     notmuch_status_t status;
779     notmuch_tags_t *tags;
780     const char *tag;
781
782     status = _notmuch_database_ensure_writable (message->notmuch);
783     if (status)
784         return status;
785
786     for (tags = notmuch_message_get_tags (message);
787          notmuch_tags_valid (tags);
788          notmuch_tags_move_to_next (tags))
789     {
790         tag = notmuch_tags_get (tags);
791
792         private_status = _notmuch_message_remove_term (message, "tag", tag);
793         if (private_status) {
794             INTERNAL_ERROR ("_notmuch_message_remove_term return unexpected value: %d\n",
795                             private_status);
796         }
797     }
798
799     if (! message->frozen)
800         _notmuch_message_sync (message);
801
802     return NOTMUCH_STATUS_SUCCESS;
803 }
804
805 notmuch_status_t
806 notmuch_message_freeze (notmuch_message_t *message)
807 {
808     notmuch_status_t status;
809
810     status = _notmuch_database_ensure_writable (message->notmuch);
811     if (status)
812         return status;
813
814     message->frozen++;
815
816     return NOTMUCH_STATUS_SUCCESS;
817 }
818
819 notmuch_status_t
820 notmuch_message_thaw (notmuch_message_t *message)
821 {
822     notmuch_status_t status;
823
824     status = _notmuch_database_ensure_writable (message->notmuch);
825     if (status)
826         return status;
827
828     if (message->frozen > 0) {
829         message->frozen--;
830         if (message->frozen == 0)
831             _notmuch_message_sync (message);
832         return NOTMUCH_STATUS_SUCCESS;
833     } else {
834         return NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW;
835     }
836 }
837
838 void
839 notmuch_message_destroy (notmuch_message_t *message)
840 {
841     talloc_free (message);
842 }