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