]> git.notmuchmail.org Git - notmuch/blob - lib/message.cc
database: Allowing storing multiple filenames for a single message ID.
[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     if (notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY) {
178         *status_ret = NOTMUCH_PRIVATE_STATUS_READONLY_DATABASE;
179         return NULL;
180     }
181
182     *status_ret = NOTMUCH_PRIVATE_STATUS_SUCCESS;
183
184     message = notmuch_database_find_message (notmuch, message_id);
185     if (message)
186         return talloc_steal (notmuch, message);
187
188     term = talloc_asprintf (NULL, "%s%s",
189                             _find_prefix ("id"), message_id);
190     if (term == NULL) {
191         *status_ret = NOTMUCH_PRIVATE_STATUS_OUT_OF_MEMORY;
192         return NULL;
193     }
194
195     db = static_cast<Xapian::WritableDatabase *> (notmuch->xapian_db);
196     try {
197         doc.add_term (term);
198         talloc_free (term);
199
200         doc.add_value (NOTMUCH_VALUE_MESSAGE_ID, message_id);
201
202         doc_id = db->add_document (doc);
203     } catch (const Xapian::Error &error) {
204         fprintf (stderr, "A Xapian exception occurred creating message: %s\n",
205                  error.get_msg().c_str());
206         notmuch->exception_reported = TRUE;
207         *status_ret = NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION;
208         return NULL;
209     }
210
211     message = _notmuch_message_create (notmuch, notmuch,
212                                        doc_id, status_ret);
213
214     /* We want to inform the caller that we had to create a new
215      * document. */
216     if (*status_ret == NOTMUCH_PRIVATE_STATUS_SUCCESS)
217         *status_ret = NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
218
219     return message;
220 }
221
222 const char *
223 notmuch_message_get_message_id (notmuch_message_t *message)
224 {
225     Xapian::TermIterator i;
226
227     if (message->message_id)
228         return message->message_id;
229
230     i = message->doc.termlist_begin ();
231     i.skip_to (_find_prefix ("id"));
232
233     if (i == message->doc.termlist_end ())
234         INTERNAL_ERROR ("Message with document ID of %d has no message ID.\n",
235                         message->doc_id);
236
237     message->message_id = talloc_strdup (message, (*i).c_str () + 1);
238
239 #if DEBUG_DATABASE_SANITY
240     i++;
241
242     if (i != message->doc.termlist_end () &&
243         strncmp ((*i).c_str (), _find_prefix ("id"),
244                  strlen (_find_prefix ("id"))) == 0)
245     {
246         INTERNAL_ERROR ("Mail (doc_id: %d) has duplicate message IDs",
247                         message->doc_id);
248     }
249 #endif
250
251     return message->message_id;
252 }
253
254 static void
255 _notmuch_message_ensure_message_file (notmuch_message_t *message)
256 {
257     const char *filename;
258
259     if (message->message_file)
260         return;
261
262     filename = notmuch_message_get_filename (message);
263     if (unlikely (filename == NULL))
264         return;
265
266     message->message_file = _notmuch_message_file_open_ctx (message, filename);
267 }
268
269 const char *
270 notmuch_message_get_header (notmuch_message_t *message, const char *header)
271 {
272     _notmuch_message_ensure_message_file (message);
273     if (message->message_file == NULL)
274         return NULL;
275
276     return notmuch_message_file_get_header (message->message_file, header);
277 }
278
279 /* Return the message ID from the In-Reply-To header of 'message'.
280  *
281  * Returns an empty string ("") if 'message' has no In-Reply-To
282  * header.
283  *
284  * Returns NULL if any error occurs.
285  */
286 const char *
287 _notmuch_message_get_in_reply_to (notmuch_message_t *message)
288 {
289     const char *prefix = _find_prefix ("replyto");
290     int prefix_len = strlen (prefix);
291     Xapian::TermIterator i;
292     std::string in_reply_to;
293
294     if (message->in_reply_to)
295         return message->in_reply_to;
296
297     i = message->doc.termlist_begin ();
298     i.skip_to (prefix);
299
300     if (i != message->doc.termlist_end ())
301         in_reply_to = *i;
302
303     /* It's perfectly valid for a message to have no In-Reply-To
304      * header. For these cases, we return an empty string. */
305     if (i == message->doc.termlist_end () ||
306         strncmp (in_reply_to.c_str (), prefix, prefix_len))
307     {
308         message->in_reply_to = talloc_strdup (message, "");
309         return message->in_reply_to;
310     }
311
312     message->in_reply_to = talloc_strdup (message,
313                                           in_reply_to.c_str () + prefix_len);
314
315 #if DEBUG_DATABASE_SANITY
316     i++;
317
318     in_reply_to = *i;
319
320     if (i != message->doc.termlist_end () &&
321         strncmp ((*i).c_str (), prefix, prefix_len) == 0)
322     {
323        INTERNAL_ERROR ("Message %s has duplicate In-Reply-To IDs: %s and %s\n",
324                         notmuch_message_get_message_id (message),
325                         message->in_reply_to,
326                         (*i).c_str () + prefix_len);
327     }
328 #endif
329
330     return message->in_reply_to;
331 }
332
333 const char *
334 notmuch_message_get_thread_id (notmuch_message_t *message)
335 {
336     const char *prefix = _find_prefix ("thread");
337     Xapian::TermIterator i;
338     std::string id;
339
340     /* This code is written with the assumption that "thread" has a
341      * single-character prefix. */
342     assert (strlen (prefix) == 1);
343
344     if (message->thread_id)
345         return message->thread_id;
346
347     i = message->doc.termlist_begin ();
348     i.skip_to (prefix);
349
350     if (i != message->doc.termlist_end ())
351         id = *i;
352
353     if (i == message->doc.termlist_end () || id[0] != *prefix)
354         INTERNAL_ERROR ("Message with document ID of %d has no thread ID.\n",
355                         message->doc_id);
356
357     message->thread_id = talloc_strdup (message, id.c_str () + 1);
358
359 #if DEBUG_DATABASE_SANITY
360     i++;
361     id = *i;
362
363     if (i != message->doc.termlist_end () && id[0] == *prefix)
364     {
365         INTERNAL_ERROR ("Message %s has duplicate thread IDs: %s and %s\n",
366                         notmuch_message_get_message_id (message),
367                         message->thread_id,
368                         id.c_str () + 1);
369     }
370 #endif
371
372     return message->thread_id;
373 }
374
375 void
376 _notmuch_message_add_reply (notmuch_message_t *message,
377                             notmuch_message_node_t *reply)
378 {
379     _notmuch_message_list_append (message->replies, reply);
380 }
381
382 notmuch_messages_t *
383 notmuch_message_get_replies (notmuch_message_t *message)
384 {
385     return _notmuch_messages_create (message->replies);
386 }
387
388 /* Add an additional 'filename' for 'message'.
389  *
390  * This change will not be reflected in the database until the next
391  * call to _notmuch_message_set_sync. */
392 notmuch_status_t
393 _notmuch_message_add_filename (notmuch_message_t *message,
394                                const char *filename)
395 {
396     const char *relative, *directory, *basename;
397     char *term;
398     Xapian::docid directory_id;
399     notmuch_status_t status;
400     void *local = talloc_new (message);
401
402     if (message->filename) {
403         talloc_free (message->filename);
404         message->filename = NULL;
405     }
406
407     if (filename == NULL)
408         INTERNAL_ERROR ("Message filename cannot be NULL.");
409
410     relative = _notmuch_database_relative_path (message->notmuch, filename);
411
412     status = _notmuch_database_split_path (local, relative,
413                                            &directory, &basename);
414     if (status)
415         return status;
416
417     status = _notmuch_database_find_directory_id (message->notmuch, directory,
418                                                   &directory_id);
419     if (status)
420         return status;
421
422     term = talloc_asprintf (local, "%s%u:%s",
423                             _find_prefix ("direntry"), directory_id, basename);
424
425     message->doc.add_term (term);
426
427     talloc_free (local);
428
429     return NOTMUCH_STATUS_SUCCESS;
430 }
431
432 const char *
433 notmuch_message_get_filename (notmuch_message_t *message)
434 {
435     const char *prefix = _find_prefix ("direntry");
436     int prefix_len = strlen (prefix);
437     Xapian::TermIterator i;
438     char *direntry, *colon;
439     const char *db_path, *directory, *basename;
440     unsigned int directory_id;
441     void *local = talloc_new (message);
442
443     if (message->filename)
444         return message->filename;
445
446     i = message->doc.termlist_begin ();
447     i.skip_to (prefix);
448
449     if (i != message->doc.termlist_end ())
450         direntry = talloc_strdup (local, (*i).c_str ());
451
452     if (i == message->doc.termlist_end () ||
453         strncmp (direntry, prefix, prefix_len))
454     {
455         INTERNAL_ERROR ("message with no filename");
456     }
457
458     direntry += prefix_len;
459
460     directory_id = strtol (direntry, &colon, 10);
461
462     if (colon == NULL || *colon != ':')
463         INTERNAL_ERROR ("malformed direntry");
464
465     basename = colon + 1;
466
467     *colon = '\0';
468
469     db_path = notmuch_database_get_path (message->notmuch);
470
471     directory = _notmuch_database_get_directory_path (local,
472                                                       message->notmuch,
473                                                       directory_id);
474
475     if (strlen (directory))
476         message->filename = talloc_asprintf (message, "%s/%s/%s",
477                                              db_path, directory, basename);
478     else
479         message->filename = talloc_asprintf (message, "%s/%s",
480                                              db_path, basename);
481     talloc_free ((void *) directory);
482
483     talloc_free (local);
484
485     return message->filename;
486 }
487
488 notmuch_bool_t
489 notmuch_message_get_flag (notmuch_message_t *message,
490                           notmuch_message_flag_t flag)
491 {
492     return message->flags & (1 << flag);
493 }
494
495 void
496 notmuch_message_set_flag (notmuch_message_t *message,
497                           notmuch_message_flag_t flag, notmuch_bool_t enable)
498 {
499     if (enable)
500         message->flags |= (1 << flag);
501     else
502         message->flags &= ~(1 << flag);
503 }
504
505 time_t
506 notmuch_message_get_date (notmuch_message_t *message)
507 {
508     std::string value;
509
510     try {
511         value = message->doc.get_value (NOTMUCH_VALUE_TIMESTAMP);
512     } catch (Xapian::Error &error) {
513         INTERNAL_ERROR ("Failed to read timestamp value from document.");
514         return 0;
515     }
516
517     return Xapian::sortable_unserialise (value);
518 }
519
520 notmuch_tags_t *
521 notmuch_message_get_tags (notmuch_message_t *message)
522 {
523     Xapian::TermIterator i, end;
524     i = message->doc.termlist_begin();
525     end = message->doc.termlist_end();
526     return _notmuch_convert_tags(message, i, end);
527 }
528
529 void
530 _notmuch_message_set_date (notmuch_message_t *message,
531                            const char *date)
532 {
533     time_t time_value;
534
535     /* GMime really doesn't want to see a NULL date, so protect its
536      * sensibilities. */
537     if (date == NULL || *date == '\0')
538         time_value = 0;
539     else
540         time_value = g_mime_utils_header_decode_date (date, NULL);
541
542     message->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
543                             Xapian::sortable_serialise (time_value));
544 }
545
546 static void
547 thread_id_generate (thread_id_t *thread_id)
548 {
549     static int seeded = 0;
550     FILE *dev_random;
551     uint32_t value;
552     char *s;
553     int i;
554
555     if (! seeded) {
556         dev_random = fopen ("/dev/random", "r");
557         if (dev_random == NULL) {
558             srand (time (NULL));
559         } else {
560             fread ((void *) &value, sizeof (value), 1, dev_random);
561             srand (value);
562             fclose (dev_random);
563         }
564         seeded = 1;
565     }
566
567     s = thread_id->str;
568     for (i = 0; i < NOTMUCH_THREAD_ID_DIGITS; i += 8) {
569         value = rand ();
570         sprintf (s, "%08x", value);
571         s += 8;
572     }
573 }
574
575 void
576 _notmuch_message_ensure_thread_id (notmuch_message_t *message)
577 {
578     /* If not part of any existing thread, generate a new thread_id. */
579     thread_id_t thread_id;
580
581     thread_id_generate (&thread_id);
582     _notmuch_message_add_term (message, "thread", thread_id.str);
583 }
584
585 /* Synchronize changes made to message->doc out into the database. */
586 void
587 _notmuch_message_sync (notmuch_message_t *message)
588 {
589     Xapian::WritableDatabase *db;
590
591     if (message->notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)
592         return;
593
594     db = static_cast <Xapian::WritableDatabase *> (message->notmuch->xapian_db);
595     db->replace_document (message->doc_id, message->doc);
596 }
597
598 /* Ensure that 'message' is not holding any file object open. Future
599  * calls to various functions will still automatically open the
600  * message file as needed.
601  */
602 void
603 _notmuch_message_close (notmuch_message_t *message)
604 {
605     if (message->message_file) {
606         notmuch_message_file_close (message->message_file);
607         message->message_file = NULL;
608     }
609 }
610
611 /* Add a name:value term to 'message', (the actual term will be
612  * encoded by prefixing the value with a short prefix). See
613  * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
614  * names to prefix values.
615  *
616  * This change will not be reflected in the database until the next
617  * call to _notmuch_message_set_sync. */
618 notmuch_private_status_t
619 _notmuch_message_add_term (notmuch_message_t *message,
620                            const char *prefix_name,
621                            const char *value)
622 {
623
624     char *term;
625
626     if (value == NULL)
627         return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
628
629     term = talloc_asprintf (message, "%s%s",
630                             _find_prefix (prefix_name), value);
631
632     if (strlen (term) > NOTMUCH_TERM_MAX)
633         return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
634
635     message->doc.add_term (term);
636
637     talloc_free (term);
638
639     return NOTMUCH_PRIVATE_STATUS_SUCCESS;
640 }
641
642 /* Parse 'text' and add a term to 'message' for each parsed word. Each
643  * term will be added both prefixed (if prefix_name is not NULL) and
644  * also unprefixed). */
645 notmuch_private_status_t
646 _notmuch_message_gen_terms (notmuch_message_t *message,
647                             const char *prefix_name,
648                             const char *text)
649 {
650     Xapian::TermGenerator *term_gen = message->notmuch->term_gen;
651
652     if (text == NULL)
653         return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
654
655     term_gen->set_document (message->doc);
656
657     if (prefix_name) {
658         const char *prefix = _find_prefix (prefix_name);
659
660         term_gen->index_text (text, 1, prefix);
661     }
662
663     term_gen->index_text (text);
664
665     return NOTMUCH_PRIVATE_STATUS_SUCCESS;
666 }
667
668 /* Remove a name:value term from 'message', (the actual term will be
669  * encoded by prefixing the value with a short prefix). See
670  * NORMAL_PREFIX and BOOLEAN_PREFIX arrays for the mapping of term
671  * names to prefix values.
672  *
673  * This change will not be reflected in the database until the next
674  * call to _notmuch_message_set_sync. */
675 notmuch_private_status_t
676 _notmuch_message_remove_term (notmuch_message_t *message,
677                               const char *prefix_name,
678                               const char *value)
679 {
680     char *term;
681
682     if (value == NULL)
683         return NOTMUCH_PRIVATE_STATUS_NULL_POINTER;
684
685     term = talloc_asprintf (message, "%s%s",
686                             _find_prefix (prefix_name), value);
687
688     if (strlen (term) > NOTMUCH_TERM_MAX)
689         return NOTMUCH_PRIVATE_STATUS_TERM_TOO_LONG;
690
691     try {
692         message->doc.remove_term (term);
693     } catch (const Xapian::InvalidArgumentError) {
694         /* We'll let the philosopher's try to wrestle with the
695          * question of whether failing to remove that which was not
696          * there in the first place is failure. For us, we'll silently
697          * consider it all good. */
698     }
699
700     talloc_free (term);
701
702     return NOTMUCH_PRIVATE_STATUS_SUCCESS;
703 }
704
705 notmuch_status_t
706 notmuch_message_add_tag (notmuch_message_t *message, const char *tag)
707 {
708     notmuch_private_status_t status;
709
710     if (tag == NULL)
711         return NOTMUCH_STATUS_NULL_POINTER;
712
713     if (strlen (tag) > NOTMUCH_TAG_MAX)
714         return NOTMUCH_STATUS_TAG_TOO_LONG;
715
716     status = _notmuch_message_add_term (message, "tag", tag);
717     if (status) {
718         INTERNAL_ERROR ("_notmuch_message_add_term return unexpected value: %d\n",
719                         status);
720     }
721
722     if (! message->frozen)
723         _notmuch_message_sync (message);
724
725     return NOTMUCH_STATUS_SUCCESS;
726 }
727
728 notmuch_status_t
729 notmuch_message_remove_tag (notmuch_message_t *message, const char *tag)
730 {
731     notmuch_private_status_t status;
732
733     if (tag == NULL)
734         return NOTMUCH_STATUS_NULL_POINTER;
735
736     if (strlen (tag) > NOTMUCH_TAG_MAX)
737         return NOTMUCH_STATUS_TAG_TOO_LONG;
738
739     status = _notmuch_message_remove_term (message, "tag", tag);
740     if (status) {
741         INTERNAL_ERROR ("_notmuch_message_remove_term return unexpected value: %d\n",
742                         status);
743     }
744
745     if (! message->frozen)
746         _notmuch_message_sync (message);
747
748     return NOTMUCH_STATUS_SUCCESS;
749 }
750
751 void
752 notmuch_message_remove_all_tags (notmuch_message_t *message)
753 {
754     notmuch_private_status_t status;
755     notmuch_tags_t *tags;
756     const char *tag;
757
758     for (tags = notmuch_message_get_tags (message);
759          notmuch_tags_has_more (tags);
760          notmuch_tags_advance (tags))
761     {
762         tag = notmuch_tags_get (tags);
763
764         status = _notmuch_message_remove_term (message, "tag", tag);
765         if (status) {
766             INTERNAL_ERROR ("_notmuch_message_remove_term return unexpected value: %d\n",
767                             status);
768         }
769     }
770
771     if (! message->frozen)
772         _notmuch_message_sync (message);
773 }
774
775 void
776 notmuch_message_freeze (notmuch_message_t *message)
777 {
778     message->frozen++;
779 }
780
781 notmuch_status_t
782 notmuch_message_thaw (notmuch_message_t *message)
783 {
784     if (message->frozen > 0) {
785         message->frozen--;
786         if (message->frozen == 0)
787             _notmuch_message_sync (message);
788         return NOTMUCH_STATUS_SUCCESS;
789     } else {
790         return NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW;
791     }
792 }
793
794 void
795 notmuch_message_destroy (notmuch_message_t *message)
796 {
797     talloc_free (message);
798 }