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