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 directory.
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
78 * Finally, the data portion of a mail document contains the path name
79 * of the mail message (relative to the database path).
83 * A directory document is used by a client of the notmuch library to
84 * maintain data necessary to allow for efficient polling of mail
87 * The directory document is indexed with a single prefixed term:
89 * directory: The directory path (relative to the database path)
91 * and has a single value:
93 * TIMESTAMP: The mtime of the directory (at last scan)
96 /* With these prefix values we follow the conventions published here:
98 * http://xapian.org/docs/omega/termprefixes.html
100 * as much as makes sense. Note that I took some liberty in matching
101 * the reserved prefix values to notmuch concepts, (for example, 'G'
102 * is documented as "newsGroup (or similar entity - e.g. a web forum
103 * name)", for which I think the thread is the closest analogue in
104 * notmuch. This in spite of the fact that we will eventually be
105 * storing mailing-list messages where 'G' for "mailing list name"
106 * might be even a closer analogue. I'm treating the single-character
107 * prefixes preferentially for core notmuch concepts (which will be
108 * nearly universal to all mail messages).
111 prefix_t BOOLEAN_PREFIX_INTERNAL[] = {
113 { "reference", "XREFERENCE" },
114 { "replyto", "XREPLYTO" },
115 /* XXX: Need a flag day to rename XTIMESTAMP. */
116 { "directory", "XTIMESTAMP" },
119 prefix_t BOOLEAN_PREFIX_EXTERNAL[] = {
125 prefix_t PROBABILISTIC_PREFIX[]= {
128 { "attachment", "XATTACHMENT" },
129 { "subject", "XSUBJECT"}
133 _internal_error (const char *format, ...)
137 va_start (va_args, format);
139 fprintf (stderr, "Internal error: ");
140 vfprintf (stderr, format, va_args);
148 _find_prefix (const char *name)
152 for (i = 0; i < ARRAY_SIZE (BOOLEAN_PREFIX_INTERNAL); i++) {
153 if (strcmp (name, BOOLEAN_PREFIX_INTERNAL[i].name) == 0)
154 return BOOLEAN_PREFIX_INTERNAL[i].prefix;
157 for (i = 0; i < ARRAY_SIZE (BOOLEAN_PREFIX_EXTERNAL); i++) {
158 if (strcmp (name, BOOLEAN_PREFIX_EXTERNAL[i].name) == 0)
159 return BOOLEAN_PREFIX_EXTERNAL[i].prefix;
162 for (i = 0; i < ARRAY_SIZE (PROBABILISTIC_PREFIX); i++) {
163 if (strcmp (name, PROBABILISTIC_PREFIX[i].name) == 0)
164 return PROBABILISTIC_PREFIX[i].prefix;
167 INTERNAL_ERROR ("No prefix exists for '%s'\n", name);
173 notmuch_status_to_string (notmuch_status_t status)
176 case NOTMUCH_STATUS_SUCCESS:
177 return "No error occurred";
178 case NOTMUCH_STATUS_OUT_OF_MEMORY:
179 return "Out of memory";
180 case NOTMUCH_STATUS_READONLY_DATABASE:
181 return "The database is read-only";
182 case NOTMUCH_STATUS_XAPIAN_EXCEPTION:
183 return "A Xapian exception occurred";
184 case NOTMUCH_STATUS_FILE_ERROR:
185 return "Something went wrong trying to read or write a file";
186 case NOTMUCH_STATUS_FILE_NOT_EMAIL:
187 return "File is not an email";
188 case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:
189 return "Message ID is identical to a message in database";
190 case NOTMUCH_STATUS_NULL_POINTER:
191 return "Erroneous NULL pointer";
192 case NOTMUCH_STATUS_TAG_TOO_LONG:
193 return "Tag value is too long (exceeds NOTMUCH_TAG_MAX)";
194 case NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW:
195 return "Unbalanced number of calls to notmuch_message_freeze/thaw";
197 case NOTMUCH_STATUS_LAST_STATUS:
198 return "Unknown error status value";
203 find_doc_ids (notmuch_database_t *notmuch,
204 const char *prefix_name,
206 Xapian::PostingIterator *begin,
207 Xapian::PostingIterator *end)
209 Xapian::PostingIterator i;
212 term = talloc_asprintf (notmuch, "%s%s",
213 _find_prefix (prefix_name), value);
215 *begin = notmuch->xapian_db->postlist_begin (term);
217 *end = notmuch->xapian_db->postlist_end (term);
222 static notmuch_private_status_t
223 find_unique_doc_id (notmuch_database_t *notmuch,
224 const char *prefix_name,
226 unsigned int *doc_id)
228 Xapian::PostingIterator i, end;
230 find_doc_ids (notmuch, prefix_name, value, &i, &end);
234 return NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
237 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
241 static Xapian::Document
242 find_document_for_doc_id (notmuch_database_t *notmuch, unsigned doc_id)
244 return notmuch->xapian_db->get_document (doc_id);
247 static notmuch_private_status_t
248 find_unique_document (notmuch_database_t *notmuch,
249 const char *prefix_name,
251 Xapian::Document *document,
252 unsigned int *doc_id)
254 notmuch_private_status_t status;
256 status = find_unique_doc_id (notmuch, prefix_name, value, doc_id);
259 *document = Xapian::Document ();
263 *document = find_document_for_doc_id (notmuch, *doc_id);
264 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
268 notmuch_database_find_message (notmuch_database_t *notmuch,
269 const char *message_id)
271 notmuch_private_status_t status;
274 status = find_unique_doc_id (notmuch, "id", message_id, &doc_id);
276 if (status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND)
279 return _notmuch_message_create (notmuch, notmuch, doc_id, NULL);
282 /* Advance 'str' past any whitespace or RFC 822 comments. A comment is
283 * a (potentially nested) parenthesized sequence with '\' used to
284 * escape any character (including parentheses).
286 * If the sequence to be skipped continues to the end of the string,
287 * then 'str' will be left pointing at the final terminating '\0'
291 skip_space_and_comments (const char **str)
296 while (*s && (isspace (*s) || *s == '(')) {
297 while (*s && isspace (*s))
302 while (*s && nesting) {
305 } else if (*s == ')') {
307 } else if (*s == '\\') {
319 /* Parse an RFC 822 message-id, discarding whitespace, any RFC 822
320 * comments, and the '<' and '>' delimeters.
322 * If not NULL, then *next will be made to point to the first character
323 * not parsed, (possibly pointing to the final '\0' terminator.
325 * Returns a newly talloc'ed string belonging to 'ctx'.
327 * Returns NULL if there is any error parsing the message-id. */
329 _parse_message_id (void *ctx, const char *message_id, const char **next)
334 if (message_id == NULL || *message_id == '\0')
339 skip_space_and_comments (&s);
341 /* Skip any unstructured text as well. */
342 while (*s && *s != '<')
353 skip_space_and_comments (&s);
356 while (*end && *end != '>')
365 if (end > s && *end == '>')
370 result = talloc_strndup (ctx, s, end - s + 1);
372 /* Finally, collapse any whitespace that is within the message-id
378 for (r = result, len = strlen (r); *r; r++, len--)
379 if (*r == ' ' || *r == '\t')
380 memmove (r, r+1, len);
386 /* Parse a References header value, putting a (talloc'ed under 'ctx')
387 * copy of each referenced message-id into 'hash'.
389 * We explicitly avoid including any reference identical to
390 * 'message_id' in the result (to avoid mass confusion when a single
391 * message references itself cyclically---and yes, mail messages are
392 * not infrequent in the wild that do this---don't ask me why).
395 parse_references (void *ctx,
396 const char *message_id,
402 if (refs == NULL || *refs == '\0')
406 ref = _parse_message_id (ctx, refs, &refs);
408 if (ref && strcmp (ref, message_id))
409 g_hash_table_insert (hash, ref, NULL);
414 notmuch_database_create (const char *path)
416 notmuch_database_t *notmuch = NULL;
417 char *notmuch_path = NULL;
422 fprintf (stderr, "Error: Cannot create a database for a NULL path.\n");
426 err = stat (path, &st);
428 fprintf (stderr, "Error: Cannot create database at %s: %s.\n",
429 path, strerror (errno));
433 if (! S_ISDIR (st.st_mode)) {
434 fprintf (stderr, "Error: Cannot create database at %s: Not a directory.\n",
439 notmuch_path = talloc_asprintf (NULL, "%s/%s", path, ".notmuch");
441 err = mkdir (notmuch_path, 0755);
444 fprintf (stderr, "Error: Cannot create directory %s: %s.\n",
445 notmuch_path, strerror (errno));
449 notmuch = notmuch_database_open (path,
450 NOTMUCH_DATABASE_MODE_READ_WRITE);
454 talloc_free (notmuch_path);
460 notmuch_database_open (const char *path,
461 notmuch_database_mode_t mode)
463 notmuch_database_t *notmuch = NULL;
464 char *notmuch_path = NULL, *xapian_path = NULL;
469 if (asprintf (¬much_path, "%s/%s", path, ".notmuch") == -1) {
471 fprintf (stderr, "Out of memory\n");
475 err = stat (notmuch_path, &st);
477 fprintf (stderr, "Error opening database at %s: %s\n",
478 notmuch_path, strerror (errno));
482 if (asprintf (&xapian_path, "%s/%s", notmuch_path, "xapian") == -1) {
484 fprintf (stderr, "Out of memory\n");
488 notmuch = talloc (NULL, notmuch_database_t);
489 notmuch->exception_reported = FALSE;
490 notmuch->path = talloc_strdup (notmuch, path);
492 if (notmuch->path[strlen (notmuch->path) - 1] == '/')
493 notmuch->path[strlen (notmuch->path) - 1] = '\0';
495 notmuch->mode = mode;
497 if (mode == NOTMUCH_DATABASE_MODE_READ_WRITE) {
498 notmuch->xapian_db = new Xapian::WritableDatabase (xapian_path,
499 Xapian::DB_CREATE_OR_OPEN);
501 notmuch->xapian_db = new Xapian::Database (xapian_path);
503 notmuch->query_parser = new Xapian::QueryParser;
504 notmuch->term_gen = new Xapian::TermGenerator;
505 notmuch->term_gen->set_stemmer (Xapian::Stem ("english"));
506 notmuch->value_range_processor = new Xapian::NumberValueRangeProcessor (NOTMUCH_VALUE_TIMESTAMP);
508 notmuch->query_parser->set_default_op (Xapian::Query::OP_AND);
509 notmuch->query_parser->set_database (*notmuch->xapian_db);
510 notmuch->query_parser->set_stemmer (Xapian::Stem ("english"));
511 notmuch->query_parser->set_stemming_strategy (Xapian::QueryParser::STEM_SOME);
512 notmuch->query_parser->add_valuerangeprocessor (notmuch->value_range_processor);
514 for (i = 0; i < ARRAY_SIZE (BOOLEAN_PREFIX_EXTERNAL); i++) {
515 prefix_t *prefix = &BOOLEAN_PREFIX_EXTERNAL[i];
516 notmuch->query_parser->add_boolean_prefix (prefix->name,
520 for (i = 0; i < ARRAY_SIZE (PROBABILISTIC_PREFIX); i++) {
521 prefix_t *prefix = &PROBABILISTIC_PREFIX[i];
522 notmuch->query_parser->add_prefix (prefix->name, prefix->prefix);
524 } catch (const Xapian::Error &error) {
525 fprintf (stderr, "A Xapian exception occurred opening database: %s\n",
526 error.get_msg().c_str());
540 notmuch_database_close (notmuch_database_t *notmuch)
543 if (notmuch->mode == NOTMUCH_DATABASE_MODE_READ_WRITE)
544 (static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db))->flush ();
545 } catch (const Xapian::Error &error) {
546 if (! notmuch->exception_reported) {
547 fprintf (stderr, "Error: A Xapian exception occurred flushing database: %s\n",
548 error.get_msg().c_str());
552 delete notmuch->term_gen;
553 delete notmuch->query_parser;
554 delete notmuch->xapian_db;
555 delete notmuch->value_range_processor;
556 talloc_free (notmuch);
560 notmuch_database_get_path (notmuch_database_t *notmuch)
562 return notmuch->path;
565 static notmuch_private_status_t
566 find_directory_document (notmuch_database_t *notmuch, const char *db_path,
567 Xapian::Document *doc, unsigned int *doc_id)
569 return find_unique_document (notmuch, "directory", db_path, doc, doc_id);
572 /* We allow the user to use arbitrarily long paths for directories. But
573 * we have a term-length limit. So if we exceed that, we'll use the
574 * SHA-1 of the path for the database term.
576 * Note: This function may return the original value of 'path'. If it
577 * does not, then the caller is responsible to free() the returned
581 directory_db_path (const char *path)
583 int term_len = strlen (_find_prefix ("directory")) + strlen (path);
585 if (term_len > NOTMUCH_TERM_MAX)
586 return notmuch_sha1_of_string (path);
591 /* Given a legal 'path' for the database, return the relative path.
593 * The return value will be a pointer to the originl path contents,
594 * and will be either the original string (if 'path' was relative) or
595 * a portion of the string (if path was absolute and begins with the
599 _notmuch_database_relative_path (notmuch_database_t *notmuch,
602 const char *db_path, *relative;
603 unsigned int db_path_len;
605 db_path = notmuch_database_get_path (notmuch);
606 db_path_len = strlen (db_path);
610 if (*relative == '/') {
611 while (*relative == '/' && *(relative+1) == '/')
614 if (strncmp (relative, db_path, db_path_len) == 0)
616 relative += db_path_len;
617 while (*relative == '/')
626 notmuch_database_set_directory_mtime (notmuch_database_t *notmuch,
630 Xapian::Document doc;
631 Xapian::WritableDatabase *db;
633 notmuch_private_status_t status;
634 notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
635 const char *db_path = NULL;
637 if (notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY) {
638 fprintf (stderr, "Attempted to update a read-only database.\n");
639 return NOTMUCH_STATUS_READONLY_DATABASE;
642 path = _notmuch_database_relative_path (notmuch, path);
644 db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
645 db_path = directory_db_path (path);
648 status = find_directory_document (notmuch, db_path, &doc, &doc_id);
650 doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
651 Xapian::sortable_serialise (mtime));
653 if (status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND) {
654 char *term = talloc_asprintf (NULL, "%s%s",
655 _find_prefix ("directory"), db_path);
659 db->add_document (doc);
661 db->replace_document (doc_id, doc);
664 } catch (const Xapian::Error &error) {
665 fprintf (stderr, "A Xapian exception occurred setting directory mtime: %s.\n",
666 error.get_msg().c_str());
667 notmuch->exception_reported = TRUE;
668 ret = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
672 free ((char *) db_path);
678 notmuch_database_get_directory_mtime (notmuch_database_t *notmuch,
681 Xapian::Document doc;
683 notmuch_private_status_t status;
684 const char *db_path = NULL;
687 db_path = directory_db_path (path);
690 status = find_directory_document (notmuch, db_path, &doc, &doc_id);
692 if (status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND)
695 ret = Xapian::sortable_unserialise (doc.get_value (NOTMUCH_VALUE_TIMESTAMP));
696 } catch (Xapian::Error &error) {
703 free ((char *) db_path);
708 /* Find the thread ID to which the message with 'message_id' belongs.
710 * Returns NULL if no message with message ID 'message_id' is in the
713 * Otherwise, returns a newly talloced string belonging to 'ctx'.
716 _resolve_message_id_to_thread_id (notmuch_database_t *notmuch,
718 const char *message_id)
720 notmuch_message_t *message;
721 const char *ret = NULL;
723 message = notmuch_database_find_message (notmuch, message_id);
727 ret = talloc_steal (ctx, notmuch_message_get_thread_id (message));
731 notmuch_message_destroy (message);
736 static notmuch_status_t
737 _merge_threads (notmuch_database_t *notmuch,
738 const char *winner_thread_id,
739 const char *loser_thread_id)
741 Xapian::PostingIterator loser, loser_end;
742 notmuch_message_t *message = NULL;
743 notmuch_private_status_t private_status;
744 notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
746 find_doc_ids (notmuch, "thread", loser_thread_id, &loser, &loser_end);
748 for ( ; loser != loser_end; loser++) {
749 message = _notmuch_message_create (notmuch, notmuch,
750 *loser, &private_status);
751 if (message == NULL) {
752 ret = COERCE_STATUS (private_status,
753 "Cannot find document for doc_id from query");
757 _notmuch_message_remove_term (message, "thread", loser_thread_id);
758 _notmuch_message_add_term (message, "thread", winner_thread_id);
759 _notmuch_message_sync (message);
761 notmuch_message_destroy (message);
767 notmuch_message_destroy (message);
773 _my_talloc_free_for_g_hash (void *ptr)
778 static notmuch_status_t
779 _notmuch_database_link_message_to_parents (notmuch_database_t *notmuch,
780 notmuch_message_t *message,
781 notmuch_message_file_t *message_file,
782 const char **thread_id)
784 GHashTable *parents = NULL;
785 const char *refs, *in_reply_to, *in_reply_to_message_id;
786 GList *l, *keys = NULL;
787 notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
789 parents = g_hash_table_new_full (g_str_hash, g_str_equal,
790 _my_talloc_free_for_g_hash, NULL);
792 refs = notmuch_message_file_get_header (message_file, "references");
793 parse_references (message, notmuch_message_get_message_id (message),
796 in_reply_to = notmuch_message_file_get_header (message_file, "in-reply-to");
797 parse_references (message, notmuch_message_get_message_id (message),
798 parents, in_reply_to);
800 /* Carefully avoid adding any self-referential in-reply-to term. */
801 in_reply_to_message_id = _parse_message_id (message, in_reply_to, NULL);
802 if (in_reply_to_message_id &&
803 strcmp (in_reply_to_message_id,
804 notmuch_message_get_message_id (message)))
806 _notmuch_message_add_term (message, "replyto",
807 _parse_message_id (message, in_reply_to, NULL));
810 keys = g_hash_table_get_keys (parents);
811 for (l = keys; l; l = l->next) {
812 char *parent_message_id;
813 const char *parent_thread_id;
815 parent_message_id = (char *) l->data;
816 parent_thread_id = _resolve_message_id_to_thread_id (notmuch,
820 if (parent_thread_id == NULL) {
821 _notmuch_message_add_term (message, "reference",
824 if (*thread_id == NULL) {
825 *thread_id = talloc_strdup (message, parent_thread_id);
826 _notmuch_message_add_term (message, "thread", *thread_id);
827 } else if (strcmp (*thread_id, parent_thread_id)) {
828 ret = _merge_threads (notmuch, *thread_id, parent_thread_id);
839 g_hash_table_unref (parents);
844 static notmuch_status_t
845 _notmuch_database_link_message_to_children (notmuch_database_t *notmuch,
846 notmuch_message_t *message,
847 const char **thread_id)
849 const char *message_id = notmuch_message_get_message_id (message);
850 Xapian::PostingIterator child, children_end;
851 notmuch_message_t *child_message = NULL;
852 const char *child_thread_id;
853 notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
854 notmuch_private_status_t private_status;
856 find_doc_ids (notmuch, "reference", message_id, &child, &children_end);
858 for ( ; child != children_end; child++) {
860 child_message = _notmuch_message_create (message, notmuch,
861 *child, &private_status);
862 if (child_message == NULL) {
863 ret = COERCE_STATUS (private_status,
864 "Cannot find document for doc_id from query");
868 child_thread_id = notmuch_message_get_thread_id (child_message);
869 if (*thread_id == NULL) {
870 *thread_id = talloc_strdup (message, child_thread_id);
871 _notmuch_message_add_term (message, "thread", *thread_id);
872 } else if (strcmp (*thread_id, child_thread_id)) {
873 _notmuch_message_remove_term (child_message, "reference",
875 _notmuch_message_sync (child_message);
876 ret = _merge_threads (notmuch, *thread_id, child_thread_id);
881 notmuch_message_destroy (child_message);
882 child_message = NULL;
887 notmuch_message_destroy (child_message);
892 /* Given a (mostly empty) 'message' and its corresponding
893 * 'message_file' link it to existing threads in the database.
895 * We first look at 'message_file' and its link-relevant headers
896 * (References and In-Reply-To) for message IDs. We also look in the
897 * database for existing message that reference 'message'.
899 * The end result is to call _notmuch_message_ensure_thread_id which
900 * generates a new thread ID if the message doesn't connect to any
903 static notmuch_status_t
904 _notmuch_database_link_message (notmuch_database_t *notmuch,
905 notmuch_message_t *message,
906 notmuch_message_file_t *message_file)
908 notmuch_status_t status;
909 const char *thread_id = NULL;
911 status = _notmuch_database_link_message_to_parents (notmuch, message,
917 status = _notmuch_database_link_message_to_children (notmuch, message,
922 if (thread_id == NULL)
923 _notmuch_message_ensure_thread_id (message);
925 return NOTMUCH_STATUS_SUCCESS;
929 notmuch_database_add_message (notmuch_database_t *notmuch,
930 const char *filename,
931 notmuch_message_t **message_ret)
933 notmuch_message_file_t *message_file;
934 notmuch_message_t *message = NULL;
935 notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
936 notmuch_private_status_t private_status;
938 const char *date, *header;
939 const char *from, *to, *subject;
940 char *message_id = NULL;
945 message_file = notmuch_message_file_open (filename);
946 if (message_file == NULL) {
947 ret = NOTMUCH_STATUS_FILE_ERROR;
951 notmuch_message_file_restrict_headers (message_file,
962 /* Before we do any real work, (especially before doing a
963 * potential SHA-1 computation on the entire file's contents),
964 * let's make sure that what we're looking at looks like an
965 * actual email message.
967 from = notmuch_message_file_get_header (message_file, "from");
968 subject = notmuch_message_file_get_header (message_file, "subject");
969 to = notmuch_message_file_get_header (message_file, "to");
971 if ((from == NULL || *from == '\0') &&
972 (subject == NULL || *subject == '\0') &&
973 (to == NULL || *to == '\0'))
975 ret = NOTMUCH_STATUS_FILE_NOT_EMAIL;
979 /* Now that we're sure it's mail, the first order of business
980 * is to find a message ID (or else create one ourselves). */
982 header = notmuch_message_file_get_header (message_file, "message-id");
983 if (header && *header != '\0') {
984 message_id = _parse_message_id (message_file, header, NULL);
986 /* So the header value isn't RFC-compliant, but it's
987 * better than no message-id at all. */
988 if (message_id == NULL)
989 message_id = talloc_strdup (message_file, header);
991 /* Reject a Message ID that's too long. */
992 if (message_id && strlen (message_id) + 1 > NOTMUCH_TERM_MAX) {
993 talloc_free (message_id);
998 if (message_id == NULL ) {
999 /* No message-id at all, let's generate one by taking a
1000 * hash over the file's contents. */
1001 char *sha1 = notmuch_sha1_of_file (filename);
1003 /* If that failed too, something is really wrong. Give up. */
1005 ret = NOTMUCH_STATUS_FILE_ERROR;
1009 message_id = talloc_asprintf (message_file,
1010 "notmuch-sha1-%s", sha1);
1014 /* Now that we have a message ID, we get a message object,
1015 * (which may or may not reference an existing document in the
1018 message = _notmuch_message_create_for_message_id (notmuch,
1022 talloc_free (message_id);
1024 if (message == NULL) {
1025 ret = COERCE_STATUS (private_status,
1026 "Unexpected status value from _notmuch_message_create_for_message_id");
1030 /* Is this a newly created message object? */
1031 if (private_status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND) {
1032 _notmuch_message_set_filename (message, filename);
1033 _notmuch_message_add_term (message, "type", "mail");
1035 ret = NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID;
1039 ret = _notmuch_database_link_message (notmuch, message, message_file);
1043 date = notmuch_message_file_get_header (message_file, "date");
1044 _notmuch_message_set_date (message, date);
1046 _notmuch_message_index_file (message, filename);
1048 _notmuch_message_sync (message);
1049 } catch (const Xapian::Error &error) {
1050 fprintf (stderr, "A Xapian exception occurred adding message: %s.\n",
1051 error.get_description().c_str());
1052 notmuch->exception_reported = TRUE;
1053 ret = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
1059 if (ret == NOTMUCH_STATUS_SUCCESS && message_ret)
1060 *message_ret = message;
1062 notmuch_message_destroy (message);
1066 notmuch_message_file_close (message_file);
1072 _notmuch_convert_tags (void *ctx, Xapian::TermIterator &i,
1073 Xapian::TermIterator &end)
1075 const char *prefix = _find_prefix ("tag");
1076 notmuch_tags_t *tags;
1079 /* Currently this iteration is written with the assumption that
1080 * "tag" has a single-character prefix. */
1081 assert (strlen (prefix) == 1);
1083 tags = _notmuch_tags_create (ctx);
1084 if (unlikely (tags == NULL))
1092 if (tag.empty () || tag[0] != *prefix)
1095 _notmuch_tags_add_tag (tags, tag.c_str () + 1);
1100 _notmuch_tags_prepare_iterator (tags);
1106 notmuch_database_get_all_tags (notmuch_database_t *db)
1108 Xapian::TermIterator i, end;
1109 i = db->xapian_db->allterms_begin();
1110 end = db->xapian_db->allterms_end();
1111 return _notmuch_convert_tags(db, i, end);