1 /* database.cc - The database interfaces of the notmuch mail library
3 * Copyright © 2009 Carl Worth
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.
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.
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/ .
18 * Author: Carl Worth <cworth@cworth.org>
21 #include "database-private.h"
27 #include <glib.h> /* g_free, GPtrArray, GHashTable */
31 #define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr[0]))
38 /* Here's the current schema for our database:
40 * We currently have two different types of documents: mail and timestamps.
44 * A mail document is associated with a particular email message file
45 * on disk. It is indexed with the following prefixed terms which the
46 * database uses to construct threads, etc.:
48 * Single terms of given prefix:
52 * id: Unique ID of mail, (from Message-ID header or generated
53 * as "notmuch-sha1-<sha1_sum_of_entire_file>.
55 * thread: The ID of the thread to which the mail belongs
57 * replyto: The ID from the In-Reply-To header of the mail (if any).
59 * Multiple terms of given prefix:
61 * reference: All message IDs from In-Reply-To and Re ferences
62 * headers in the message.
64 * tag: Any tags associated with this message by the user.
66 * A mail document also has two values:
68 * TIMESTAMP: The time_t value corresponding to the message's
71 * MESSAGE_ID: The unique ID of the mail mess (see "id" above)
73 * In addition, terms from the content of the message are added with
74 * "from", "to", "attachment", and "subject" prefixes for use by the
75 * user in searching. But the database doesn't really care itself
80 * A timestamp document is used by a client of the notmuch library to
81 * maintain data necessary to allow for efficient polling of mail
82 * directories. The notmuch library does no interpretation of
83 * timestamps, but merely allows the user to store and retrieve
84 * timestamps as name/value pairs.
86 * The timestamp document is indexed with a single prefixed term:
88 * timestamp: The user's key value (likely a directory name)
90 * and has a single value:
92 * TIMESTAMP: The time_t value from the user.
95 /* With these prefix values we follow the conventions published here:
97 * http://xapian.org/docs/omega/termprefixes.html
99 * as much as makes sense. Note that I took some liberty in matching
100 * the reserved prefix values to notmuch concepts, (for example, 'G'
101 * is documented as "newsGroup (or similar entity - e.g. a web forum
102 * name)", for which I think the thread is the closest analogue in
103 * notmuch. This in spite of the fact that we will eventually be
104 * storing mailing-list messages where 'G' for "mailing list name"
105 * might be even a closer analogue. I'm treating the single-character
106 * prefixes preferentially for core notmuch concepts (which will be
107 * nearly universal to all mail messages).
110 prefix_t BOOLEAN_PREFIX_INTERNAL[] = {
112 { "reference", "XREFERENCE" },
113 { "replyto", "XREPLYTO" },
114 { "timestamp", "XTIMESTAMP" },
117 prefix_t BOOLEAN_PREFIX_EXTERNAL[] = {
123 prefix_t PROBABILISTIC_PREFIX[]= {
126 { "attachment", "XATTACHMENT" },
127 { "subject", "XSUBJECT"}
131 _internal_error (const char *format, ...)
135 va_start (va_args, format);
137 fprintf (stderr, "Internal error: ");
138 vfprintf (stderr, format, va_args);
146 _find_prefix (const char *name)
150 for (i = 0; i < ARRAY_SIZE (BOOLEAN_PREFIX_INTERNAL); i++)
151 if (strcmp (name, BOOLEAN_PREFIX_INTERNAL[i].name) == 0)
152 return BOOLEAN_PREFIX_INTERNAL[i].prefix;
154 for (i = 0; i < ARRAY_SIZE (BOOLEAN_PREFIX_EXTERNAL); i++)
155 if (strcmp (name, BOOLEAN_PREFIX_EXTERNAL[i].name) == 0)
156 return BOOLEAN_PREFIX_EXTERNAL[i].prefix;
158 for (i = 0; i < ARRAY_SIZE (PROBABILISTIC_PREFIX); i++)
159 if (strcmp (name, PROBABILISTIC_PREFIX[i].name) == 0)
160 return PROBABILISTIC_PREFIX[i].prefix;
162 INTERNAL_ERROR ("No prefix exists for '%s'\n", name);
168 notmuch_status_to_string (notmuch_status_t status)
171 case NOTMUCH_STATUS_SUCCESS:
172 return "No error occurred";
173 case NOTMUCH_STATUS_OUT_OF_MEMORY:
174 return "Out of memory";
175 case NOTMUCH_STATUS_READONLY_DATABASE:
176 return "The database is read-only";
177 case NOTMUCH_STATUS_XAPIAN_EXCEPTION:
178 return "A Xapian exception occurred";
179 case NOTMUCH_STATUS_FILE_ERROR:
180 return "Something went wrong trying to read or write a file";
181 case NOTMUCH_STATUS_FILE_NOT_EMAIL:
182 return "File is not an email";
183 case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:
184 return "Message ID is identical to a message in database";
185 case NOTMUCH_STATUS_NULL_POINTER:
186 return "Erroneous NULL pointer";
187 case NOTMUCH_STATUS_TAG_TOO_LONG:
188 return "Tag value is too long (exceeds NOTMUCH_TAG_MAX)";
189 case NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW:
190 return "Unbalanced number of calls to notmuch_message_freeze/thaw";
192 case NOTMUCH_STATUS_LAST_STATUS:
193 return "Unknown error status value";
198 find_doc_ids (notmuch_database_t *notmuch,
199 const char *prefix_name,
201 Xapian::PostingIterator *begin,
202 Xapian::PostingIterator *end)
204 Xapian::PostingIterator i;
207 term = talloc_asprintf (notmuch, "%s%s",
208 _find_prefix (prefix_name), value);
210 *begin = notmuch->xapian_db->postlist_begin (term);
212 *end = notmuch->xapian_db->postlist_end (term);
217 static notmuch_private_status_t
218 find_unique_doc_id (notmuch_database_t *notmuch,
219 const char *prefix_name,
221 unsigned int *doc_id)
223 Xapian::PostingIterator i, end;
225 find_doc_ids (notmuch, prefix_name, value, &i, &end);
229 return NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
232 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
236 static Xapian::Document
237 find_document_for_doc_id (notmuch_database_t *notmuch, unsigned doc_id)
239 return notmuch->xapian_db->get_document (doc_id);
242 static notmuch_private_status_t
243 find_unique_document (notmuch_database_t *notmuch,
244 const char *prefix_name,
246 Xapian::Document *document,
247 unsigned int *doc_id)
249 notmuch_private_status_t status;
251 status = find_unique_doc_id (notmuch, prefix_name, value, doc_id);
254 *document = Xapian::Document ();
258 *document = find_document_for_doc_id (notmuch, *doc_id);
259 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
263 notmuch_database_find_message (notmuch_database_t *notmuch,
264 const char *message_id)
266 notmuch_private_status_t status;
269 status = find_unique_doc_id (notmuch, "id", message_id, &doc_id);
271 if (status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND)
274 return _notmuch_message_create (notmuch, notmuch, doc_id, NULL);
277 /* Advance 'str' past any whitespace or RFC 822 comments. A comment is
278 * a (potentially nested) parenthesized sequence with '\' used to
279 * escape any character (including parentheses).
281 * If the sequence to be skipped continues to the end of the string,
282 * then 'str' will be left pointing at the final terminating '\0'
286 skip_space_and_comments (const char **str)
291 while (*s && (isspace (*s) || *s == '(')) {
292 while (*s && isspace (*s))
297 while (*s && nesting) {
313 /* Parse an RFC 822 message-id, discarding whitespace, any RFC 822
314 * comments, and the '<' and '>' delimeters.
316 * If not NULL, then *next will be made to point to the first character
317 * not parsed, (possibly pointing to the final '\0' terminator.
319 * Returns a newly talloc'ed string belonging to 'ctx'.
321 * Returns NULL if there is any error parsing the message-id. */
323 _parse_message_id (void *ctx, const char *message_id, const char **next)
328 if (message_id == NULL || *message_id == '\0')
333 skip_space_and_comments (&s);
335 /* Skip any unstructured text as well. */
336 while (*s && *s != '<')
347 skip_space_and_comments (&s);
350 while (*end && *end != '>')
359 if (end > s && *end == '>')
364 result = talloc_strndup (ctx, s, end - s + 1);
366 /* Finally, collapse any whitespace that is within the message-id
372 for (r = result, len = strlen (r); *r; r++, len--)
373 if (*r == ' ' || *r == '\t')
374 memmove (r, r+1, len);
380 /* Parse a References header value, putting a (talloc'ed under 'ctx')
381 * copy of each referenced message-id into 'hash'.
383 * We explicitly avoid including any reference identical to
384 * 'message_id' in the result (to avoid mass confusion when a single
385 * message references itself cyclically---and yes, mail messages are
386 * not infrequent in the wild that do this---don't ask me why).
389 parse_references (void *ctx,
390 const char *message_id,
396 if (refs == NULL || *refs == '\0')
400 ref = _parse_message_id (ctx, refs, &refs);
402 if (ref && strcmp (ref, message_id))
403 g_hash_table_insert (hash, ref, NULL);
408 notmuch_database_create (const char *path)
410 notmuch_database_t *notmuch = NULL;
411 char *notmuch_path = NULL;
416 fprintf (stderr, "Error: Cannot create a database for a NULL path.\n");
420 err = stat (path, &st);
422 fprintf (stderr, "Error: Cannot create database at %s: %s.\n",
423 path, strerror (errno));
427 if (! S_ISDIR (st.st_mode)) {
428 fprintf (stderr, "Error: Cannot create database at %s: Not a directory.\n",
433 notmuch_path = talloc_asprintf (NULL, "%s/%s", path, ".notmuch");
435 err = mkdir (notmuch_path, 0755);
438 fprintf (stderr, "Error: Cannot create directory %s: %s.\n",
439 notmuch_path, strerror (errno));
443 notmuch = notmuch_database_open (path,
444 NOTMUCH_DATABASE_MODE_READ_WRITE);
448 talloc_free (notmuch_path);
454 notmuch_database_open (const char *path,
455 notmuch_database_mode_t mode)
457 notmuch_database_t *notmuch = NULL;
458 char *notmuch_path = NULL, *xapian_path = NULL;
463 if (asprintf (¬much_path, "%s/%s", path, ".notmuch") == -1) {
465 fprintf (stderr, "Out of memory\n");
469 err = stat (notmuch_path, &st);
471 fprintf (stderr, "Error opening database at %s: %s\n",
472 notmuch_path, strerror (errno));
476 if (asprintf (&xapian_path, "%s/%s", notmuch_path, "xapian") == -1) {
478 fprintf (stderr, "Out of memory\n");
482 notmuch = talloc (NULL, notmuch_database_t);
483 notmuch->exception_reported = FALSE;
484 notmuch->path = talloc_strdup (notmuch, path);
486 if (notmuch->path[strlen (notmuch->path) - 1] == '/')
487 notmuch->path[strlen (notmuch->path) - 1] = '\0';
489 notmuch->mode = mode;
491 if (mode == NOTMUCH_DATABASE_MODE_READ_WRITE) {
492 notmuch->xapian_db = new Xapian::WritableDatabase (xapian_path,
493 Xapian::DB_CREATE_OR_OPEN);
495 notmuch->xapian_db = new Xapian::Database (xapian_path);
497 notmuch->query_parser = new Xapian::QueryParser;
498 notmuch->term_gen = new Xapian::TermGenerator;
499 notmuch->term_gen->set_stemmer (Xapian::Stem ("english"));
501 notmuch->query_parser->set_default_op (Xapian::Query::OP_AND);
502 notmuch->query_parser->set_database (*notmuch->xapian_db);
503 notmuch->query_parser->set_stemmer (Xapian::Stem ("english"));
504 notmuch->query_parser->set_stemming_strategy (Xapian::QueryParser::STEM_SOME);
506 for (i = 0; i < ARRAY_SIZE (BOOLEAN_PREFIX_EXTERNAL); i++) {
507 prefix_t *prefix = &BOOLEAN_PREFIX_EXTERNAL[i];
508 notmuch->query_parser->add_boolean_prefix (prefix->name,
512 for (i = 0; i < ARRAY_SIZE (PROBABILISTIC_PREFIX); i++) {
513 prefix_t *prefix = &PROBABILISTIC_PREFIX[i];
514 notmuch->query_parser->add_prefix (prefix->name, prefix->prefix);
516 } catch (const Xapian::Error &error) {
517 fprintf (stderr, "A Xapian exception occurred opening database: %s\n",
518 error.get_msg().c_str());
532 notmuch_database_close (notmuch_database_t *notmuch)
535 if (notmuch->mode == NOTMUCH_DATABASE_MODE_READ_WRITE)
536 (static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db))->flush ();
537 } catch (const Xapian::Error &error) {
538 if (! notmuch->exception_reported) {
539 fprintf (stderr, "Error: A Xapian exception occurred flushing database: %s\n",
540 error.get_msg().c_str());
544 delete notmuch->term_gen;
545 delete notmuch->query_parser;
546 delete notmuch->xapian_db;
547 talloc_free (notmuch);
551 notmuch_database_get_path (notmuch_database_t *notmuch)
553 return notmuch->path;
556 static notmuch_private_status_t
557 find_timestamp_document (notmuch_database_t *notmuch, const char *db_key,
558 Xapian::Document *doc, unsigned int *doc_id)
560 return find_unique_document (notmuch, "timestamp", db_key, doc, doc_id);
563 /* We allow the user to use arbitrarily long keys for timestamps,
564 * (they're for filesystem paths after all, which have no limit we
565 * know about). But we have a term-length limit. So if we exceed that,
566 * we'll use the SHA-1 of the user's key as the actual key for
567 * constructing a database term.
569 * Caution: This function returns a newly allocated string which the
570 * caller should free() when finished.
573 timestamp_db_key (const char *key)
575 int term_len = strlen (_find_prefix ("timestamp")) + strlen (key);
577 if (term_len > NOTMUCH_TERM_MAX)
578 return notmuch_sha1_of_string (key);
584 notmuch_database_set_timestamp (notmuch_database_t *notmuch,
585 const char *key, time_t timestamp)
587 Xapian::Document doc;
588 Xapian::WritableDatabase *db;
590 notmuch_private_status_t status;
591 notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
594 if (notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY) {
595 fprintf (stderr, "Attempted to update a read-only database.\n");
596 return NOTMUCH_STATUS_READONLY_DATABASE;
599 db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
600 db_key = timestamp_db_key (key);
603 status = find_timestamp_document (notmuch, db_key, &doc, &doc_id);
605 doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
606 Xapian::sortable_serialise (timestamp));
608 if (status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND) {
609 char *term = talloc_asprintf (NULL, "%s%s",
610 _find_prefix ("timestamp"), db_key);
614 db->add_document (doc);
616 db->replace_document (doc_id, doc);
619 } catch (const Xapian::Error &error) {
620 fprintf (stderr, "A Xapian exception occurred setting timestamp: %s.\n",
621 error.get_msg().c_str());
622 notmuch->exception_reported = TRUE;
623 ret = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
633 notmuch_database_get_timestamp (notmuch_database_t *notmuch, const char *key)
635 Xapian::Document doc;
637 notmuch_private_status_t status;
641 db_key = timestamp_db_key (key);
644 status = find_timestamp_document (notmuch, db_key, &doc, &doc_id);
646 if (status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND)
649 ret = Xapian::sortable_unserialise (doc.get_value (NOTMUCH_VALUE_TIMESTAMP));
650 } catch (Xapian::Error &error) {
661 /* Find the thread ID to which the message with 'message_id' belongs.
663 * Returns NULL if no message with message ID 'message_id' is in the
666 * Otherwise, returns a newly talloced string belonging to 'ctx'.
669 _resolve_message_id_to_thread_id (notmuch_database_t *notmuch,
671 const char *message_id)
673 notmuch_message_t *message;
674 const char *ret = NULL;
676 message = notmuch_database_find_message (notmuch, message_id);
680 ret = talloc_steal (ctx, notmuch_message_get_thread_id (message));
684 notmuch_message_destroy (message);
689 static notmuch_status_t
690 _merge_threads (notmuch_database_t *notmuch,
691 const char *winner_thread_id,
692 const char *loser_thread_id)
694 Xapian::PostingIterator loser, loser_end;
695 notmuch_message_t *message = NULL;
696 notmuch_private_status_t private_status;
697 notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
699 find_doc_ids (notmuch, "thread", loser_thread_id, &loser, &loser_end);
701 for ( ; loser != loser_end; loser++) {
702 message = _notmuch_message_create (notmuch, notmuch,
703 *loser, &private_status);
704 if (message == NULL) {
705 ret = COERCE_STATUS (private_status,
706 "Cannot find document for doc_id from query");
710 _notmuch_message_remove_term (message, "thread", loser_thread_id);
711 _notmuch_message_add_term (message, "thread", winner_thread_id);
712 _notmuch_message_sync (message);
714 notmuch_message_destroy (message);
720 notmuch_message_destroy (message);
726 _my_talloc_free_for_g_hash (void *ptr)
731 static notmuch_status_t
732 _notmuch_database_link_message_to_parents (notmuch_database_t *notmuch,
733 notmuch_message_t *message,
734 notmuch_message_file_t *message_file,
735 const char **thread_id)
737 GHashTable *parents = NULL;
738 const char *refs, *in_reply_to, *in_reply_to_message_id;
739 GList *l, *keys = NULL;
740 notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
742 parents = g_hash_table_new_full (g_str_hash, g_str_equal,
743 _my_talloc_free_for_g_hash, NULL);
745 refs = notmuch_message_file_get_header (message_file, "references");
746 parse_references (message, notmuch_message_get_message_id (message),
749 in_reply_to = notmuch_message_file_get_header (message_file, "in-reply-to");
750 parse_references (message, notmuch_message_get_message_id (message),
751 parents, in_reply_to);
753 /* Carefully avoid adding any self-referential in-reply-to term. */
754 in_reply_to_message_id = _parse_message_id (message, in_reply_to, NULL);
755 if (in_reply_to_message_id &&
756 strcmp (in_reply_to_message_id,
757 notmuch_message_get_message_id (message)))
759 _notmuch_message_add_term (message, "replyto",
760 _parse_message_id (message, in_reply_to, NULL));
763 keys = g_hash_table_get_keys (parents);
764 for (l = keys; l; l = l->next) {
765 char *parent_message_id;
766 const char *parent_thread_id;
768 parent_message_id = (char *) l->data;
769 parent_thread_id = _resolve_message_id_to_thread_id (notmuch,
773 if (parent_thread_id == NULL) {
774 _notmuch_message_add_term (message, "reference",
777 if (*thread_id == NULL) {
778 *thread_id = talloc_strdup (message, parent_thread_id);
779 _notmuch_message_add_term (message, "thread", *thread_id);
780 } else if (strcmp (*thread_id, parent_thread_id)) {
781 ret = _merge_threads (notmuch, *thread_id, parent_thread_id);
792 g_hash_table_unref (parents);
797 static notmuch_status_t
798 _notmuch_database_link_message_to_children (notmuch_database_t *notmuch,
799 notmuch_message_t *message,
800 const char **thread_id)
802 const char *message_id = notmuch_message_get_message_id (message);
803 Xapian::PostingIterator child, children_end;
804 notmuch_message_t *child_message = NULL;
805 const char *child_thread_id;
806 notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
807 notmuch_private_status_t private_status;
809 find_doc_ids (notmuch, "reference", message_id, &child, &children_end);
811 for ( ; child != children_end; child++) {
813 child_message = _notmuch_message_create (message, notmuch,
814 *child, &private_status);
815 if (child_message == NULL) {
816 ret = COERCE_STATUS (private_status,
817 "Cannot find document for doc_id from query");
821 child_thread_id = notmuch_message_get_thread_id (child_message);
822 if (*thread_id == NULL) {
823 *thread_id = talloc_strdup (message, child_thread_id);
824 _notmuch_message_add_term (message, "thread", *thread_id);
825 } else if (strcmp (*thread_id, child_thread_id)) {
826 _notmuch_message_remove_term (child_message, "reference",
828 _notmuch_message_sync (child_message);
829 ret = _merge_threads (notmuch, *thread_id, child_thread_id);
834 notmuch_message_destroy (child_message);
835 child_message = NULL;
840 notmuch_message_destroy (child_message);
845 /* Given a (mostly empty) 'message' and its corresponding
846 * 'message_file' link it to existing threads in the database.
848 * We first look at 'message_file' and its link-relevant headers
849 * (References and In-Reply-To) for message IDs. We also look in the
850 * database for existing message that reference 'message'.p
852 * The end result is to call _notmuch_message_add_thread_id with one
853 * or more thread IDs to which this message belongs, (including
854 * generating a new thread ID if necessary if the message doesn't
855 * connect to any existing threads).
857 static notmuch_status_t
858 _notmuch_database_link_message (notmuch_database_t *notmuch,
859 notmuch_message_t *message,
860 notmuch_message_file_t *message_file)
862 notmuch_status_t status;
863 const char *thread_id = NULL;
865 status = _notmuch_database_link_message_to_parents (notmuch, message,
871 status = _notmuch_database_link_message_to_children (notmuch, message,
876 if (thread_id == NULL)
877 _notmuch_message_ensure_thread_id (message);
879 return NOTMUCH_STATUS_SUCCESS;
883 notmuch_database_add_message (notmuch_database_t *notmuch,
884 const char *filename,
885 notmuch_message_t **message_ret)
887 notmuch_message_file_t *message_file;
888 notmuch_message_t *message = NULL;
889 notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
890 notmuch_private_status_t private_status;
892 const char *date, *header;
893 const char *from, *to, *subject;
899 message_file = notmuch_message_file_open (filename);
900 if (message_file == NULL) {
901 ret = NOTMUCH_STATUS_FILE_ERROR;
905 notmuch_message_file_restrict_headers (message_file,
916 /* Before we do any real work, (especially before doing a
917 * potential SHA-1 computation on the entire file's contents),
918 * let's make sure that what we're looking at looks like an
919 * actual email message.
921 from = notmuch_message_file_get_header (message_file, "from");
922 subject = notmuch_message_file_get_header (message_file, "subject");
923 to = notmuch_message_file_get_header (message_file, "to");
925 if ((from == NULL || *from == '\0') &&
926 (subject == NULL || *subject == '\0') &&
927 (to == NULL || *to == '\0'))
929 ret = NOTMUCH_STATUS_FILE_NOT_EMAIL;
933 /* Now that we're sure it's mail, the first order of business
934 * is to find a message ID (or else create one ourselves). */
936 header = notmuch_message_file_get_header (message_file, "message-id");
937 if (header && *header != '\0') {
938 message_id = _parse_message_id (message_file, header, NULL);
939 /* So the header value isn't RFC-compliant, but it's
940 * better than no message-id at all. */
941 if (message_id == NULL)
942 message_id = talloc_strdup (message_file, header);
944 /* No message-id at all, let's generate one by taking a
945 * hash over the file's contents. */
946 char *sha1 = notmuch_sha1_of_file (filename);
948 /* If that failed too, something is really wrong. Give up. */
950 ret = NOTMUCH_STATUS_FILE_ERROR;
954 message_id = talloc_asprintf (message_file,
955 "notmuch-sha1-%s", sha1);
959 /* Now that we have a message ID, we get a message object,
960 * (which may or may not reference an existing document in the
963 message = _notmuch_message_create_for_message_id (notmuch,
967 talloc_free (message_id);
969 if (message == NULL) {
970 ret = COERCE_STATUS (private_status,
971 "Unexpected status value from _notmuch_message_create_for_message_id");
975 /* Is this a newly created message object? */
976 if (private_status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND) {
977 _notmuch_message_set_filename (message, filename);
978 _notmuch_message_add_term (message, "type", "mail");
980 ret = NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID;
984 ret = _notmuch_database_link_message (notmuch, message, message_file);
988 date = notmuch_message_file_get_header (message_file, "date");
989 _notmuch_message_set_date (message, date);
991 _notmuch_message_index_file (message, filename);
993 _notmuch_message_sync (message);
994 } catch (const Xapian::Error &error) {
995 fprintf (stderr, "A Xapian exception occurred adding message: %s.\n",
996 error.get_description().c_str());
997 notmuch->exception_reported = TRUE;
998 ret = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
1004 if (ret == NOTMUCH_STATUS_SUCCESS && message_ret)
1005 *message_ret = message;
1007 notmuch_message_destroy (message);
1011 notmuch_message_file_close (message_file);