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"
22 #include "parse-time-vrp.h"
29 #include <glib.h> /* g_free, GPtrArray, GHashTable */
30 #include <glib-object.h> /* g_type_init */
32 #include <gmime/gmime.h> /* g_mime_init */
36 #define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr[0]))
43 #define NOTMUCH_DATABASE_VERSION 1
45 #define STRINGIFY(s) _SUB_STRINGIFY(s)
46 #define _SUB_STRINGIFY(s) #s
48 /* Here's the current schema for our database (for NOTMUCH_DATABASE_VERSION):
50 * We currently have two different types of documents (mail and
51 * directory) and also some metadata.
55 * A mail document is associated with a particular email message file
56 * on disk. It is indexed with the following prefixed terms which the
57 * database uses to construct threads, etc.:
59 * Single terms of given prefix:
63 * id: Unique ID of mail. This is from the Message-ID header
64 * if present and not too long (see NOTMUCH_MESSAGE_ID_MAX).
65 * If it's present and too long, then we use
66 * "notmuch-sha1-<sha1_sum_of_message_id>".
67 * If this header is not present, we use
68 * "notmuch-sha1-<sha1_sum_of_entire_file>".
70 * thread: The ID of the thread to which the mail belongs
72 * replyto: The ID from the In-Reply-To header of the mail (if any).
74 * Multiple terms of given prefix:
76 * reference: All message IDs from In-Reply-To and References
77 * headers in the message.
79 * tag: Any tags associated with this message by the user.
81 * file-direntry: A colon-separated pair of values
82 * (INTEGER:STRING), where INTEGER is the
83 * document ID of a directory document, and
84 * STRING is the name of a file within that
85 * directory for this mail message.
87 * A mail document also has four values:
89 * TIMESTAMP: The time_t value corresponding to the message's
92 * MESSAGE_ID: The unique ID of the mail mess (see "id" above)
94 * FROM: The value of the "From" header
96 * SUBJECT: The value of the "Subject" header
98 * In addition, terms from the content of the message are added with
99 * "from", "to", "attachment", and "subject" prefixes for use by the
100 * user in searching. Similarly, terms from the path of the mail
101 * message are added with a "folder" prefix. But the database doesn't
102 * really care itself about any of these.
104 * The data portion of a mail document is empty.
108 * A directory document is used by a client of the notmuch library to
109 * maintain data necessary to allow for efficient polling of mail
112 * All directory documents contain one term:
114 * directory: The directory path (relative to the database path)
115 * Or the SHA1 sum of the directory path (if the
116 * path itself is too long to fit in a Xapian
119 * And all directory documents for directories other than top-level
120 * directories also contain the following term:
122 * directory-direntry: A colon-separated pair of values
123 * (INTEGER:STRING), where INTEGER is the
124 * document ID of the parent directory
125 * document, and STRING is the name of this
126 * directory within that parent.
128 * All directory documents have a single value:
130 * TIMESTAMP: The mtime of the directory (at last scan)
132 * The data portion of a directory document contains the path of the
133 * directory (relative to the database path).
137 * Xapian allows us to store arbitrary name-value pairs as
138 * "metadata". We currently use the following metadata names with the
141 * version The database schema version, (which is distinct
142 * from both the notmuch package version (see
143 * notmuch --version) and the libnotmuch library
144 * version. The version is stored as an base-10
145 * ASCII integer. The initial database version
146 * was 1, (though a schema existed before that
147 * were no "version" database value existed at
148 * all). Successive versions are allocated as
149 * changes are made to the database (such as by
150 * indexing new fields).
152 * last_thread_id The last thread ID generated. This is stored
153 * as a 16-byte hexadecimal ASCII representation
154 * of a 64-bit unsigned integer. The first ID
155 * generated is 1 and the value will be
156 * incremented for each thread ID.
158 * thread_id_* A pre-allocated thread ID for a particular
159 * message. This is actually an arbitrarily large
160 * family of metadata name. Any particular name is
161 * formed by concatenating "thread_id_" with a message
162 * ID (or the SHA1 sum of a message ID if it is very
163 * long---see description of 'id' in the mail
164 * document). The value stored is a thread ID.
166 * These thread ID metadata values are stored
167 * whenever a message references a parent message
168 * that does not yet exist in the database. A
169 * thread ID will be allocated and stored, and if
170 * the message is later added, the stored thread
171 * ID will be used (and the metadata value will
174 * Even before a message is added, it's
175 * pre-allocated thread ID is useful so that all
176 * descendant messages that reference this common
177 * parent can be recognized as belonging to the
181 /* With these prefix values we follow the conventions published here:
183 * http://xapian.org/docs/omega/termprefixes.html
185 * as much as makes sense. Note that I took some liberty in matching
186 * the reserved prefix values to notmuch concepts, (for example, 'G'
187 * is documented as "newsGroup (or similar entity - e.g. a web forum
188 * name)", for which I think the thread is the closest analogue in
189 * notmuch. This in spite of the fact that we will eventually be
190 * storing mailing-list messages where 'G' for "mailing list name"
191 * might be even a closer analogue. I'm treating the single-character
192 * prefixes preferentially for core notmuch concepts (which will be
193 * nearly universal to all mail messages).
196 static prefix_t BOOLEAN_PREFIX_INTERNAL[] = {
198 { "reference", "XREFERENCE" },
199 { "replyto", "XREPLYTO" },
200 { "directory", "XDIRECTORY" },
201 { "file-direntry", "XFDIRENTRY" },
202 { "directory-direntry", "XDDIRENTRY" },
205 static prefix_t BOOLEAN_PREFIX_EXTERNAL[] = {
212 static prefix_t PROBABILISTIC_PREFIX[]= {
215 { "attachment", "XATTACHMENT" },
216 { "subject", "XSUBJECT"},
217 { "folder", "XFOLDER"}
221 _find_prefix (const char *name)
225 for (i = 0; i < ARRAY_SIZE (BOOLEAN_PREFIX_INTERNAL); i++) {
226 if (strcmp (name, BOOLEAN_PREFIX_INTERNAL[i].name) == 0)
227 return BOOLEAN_PREFIX_INTERNAL[i].prefix;
230 for (i = 0; i < ARRAY_SIZE (BOOLEAN_PREFIX_EXTERNAL); i++) {
231 if (strcmp (name, BOOLEAN_PREFIX_EXTERNAL[i].name) == 0)
232 return BOOLEAN_PREFIX_EXTERNAL[i].prefix;
235 for (i = 0; i < ARRAY_SIZE (PROBABILISTIC_PREFIX); i++) {
236 if (strcmp (name, PROBABILISTIC_PREFIX[i].name) == 0)
237 return PROBABILISTIC_PREFIX[i].prefix;
240 INTERNAL_ERROR ("No prefix exists for '%s'\n", name);
246 notmuch_status_to_string (notmuch_status_t status)
249 case NOTMUCH_STATUS_SUCCESS:
250 return "No error occurred";
251 case NOTMUCH_STATUS_OUT_OF_MEMORY:
252 return "Out of memory";
253 case NOTMUCH_STATUS_READ_ONLY_DATABASE:
254 return "Attempt to write to a read-only database";
255 case NOTMUCH_STATUS_XAPIAN_EXCEPTION:
256 return "A Xapian exception occurred";
257 case NOTMUCH_STATUS_FILE_ERROR:
258 return "Something went wrong trying to read or write a file";
259 case NOTMUCH_STATUS_FILE_NOT_EMAIL:
260 return "File is not an email";
261 case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:
262 return "Message ID is identical to a message in database";
263 case NOTMUCH_STATUS_NULL_POINTER:
264 return "Erroneous NULL pointer";
265 case NOTMUCH_STATUS_TAG_TOO_LONG:
266 return "Tag value is too long (exceeds NOTMUCH_TAG_MAX)";
267 case NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW:
268 return "Unbalanced number of calls to notmuch_message_freeze/thaw";
269 case NOTMUCH_STATUS_UNBALANCED_ATOMIC:
270 return "Unbalanced number of calls to notmuch_database_begin_atomic/end_atomic";
272 case NOTMUCH_STATUS_LAST_STATUS:
273 return "Unknown error status value";
278 find_doc_ids_for_term (notmuch_database_t *notmuch,
280 Xapian::PostingIterator *begin,
281 Xapian::PostingIterator *end)
283 *begin = notmuch->xapian_db->postlist_begin (term);
285 *end = notmuch->xapian_db->postlist_end (term);
289 find_doc_ids (notmuch_database_t *notmuch,
290 const char *prefix_name,
292 Xapian::PostingIterator *begin,
293 Xapian::PostingIterator *end)
297 term = talloc_asprintf (notmuch, "%s%s",
298 _find_prefix (prefix_name), value);
300 find_doc_ids_for_term (notmuch, term, begin, end);
305 notmuch_private_status_t
306 _notmuch_database_find_unique_doc_id (notmuch_database_t *notmuch,
307 const char *prefix_name,
309 unsigned int *doc_id)
311 Xapian::PostingIterator i, end;
313 find_doc_ids (notmuch, prefix_name, value, &i, &end);
317 return NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
322 #if DEBUG_DATABASE_SANITY
326 INTERNAL_ERROR ("Term %s:%s is not unique as expected.\n",
330 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
333 static Xapian::Document
334 find_document_for_doc_id (notmuch_database_t *notmuch, unsigned doc_id)
336 return notmuch->xapian_db->get_document (doc_id);
339 /* Generate a compressed version of 'message_id' of the form:
341 * notmuch-sha1-<sha1_sum_of_message_id>
344 _message_id_compressed (void *ctx, const char *message_id)
346 char *sha1, *compressed;
348 sha1 = notmuch_sha1_of_string (message_id);
350 compressed = talloc_asprintf (ctx, "notmuch-sha1-%s", sha1);
357 notmuch_database_find_message (notmuch_database_t *notmuch,
358 const char *message_id,
359 notmuch_message_t **message_ret)
361 notmuch_private_status_t status;
364 if (message_ret == NULL)
365 return NOTMUCH_STATUS_NULL_POINTER;
367 if (strlen (message_id) > NOTMUCH_MESSAGE_ID_MAX)
368 message_id = _message_id_compressed (notmuch, message_id);
371 status = _notmuch_database_find_unique_doc_id (notmuch, "id",
372 message_id, &doc_id);
374 if (status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND)
377 *message_ret = _notmuch_message_create (notmuch, notmuch, doc_id,
379 if (*message_ret == NULL)
380 return NOTMUCH_STATUS_OUT_OF_MEMORY;
383 return NOTMUCH_STATUS_SUCCESS;
384 } catch (const Xapian::Error &error) {
385 fprintf (stderr, "A Xapian exception occurred finding message: %s.\n",
386 error.get_msg().c_str());
387 notmuch->exception_reported = TRUE;
389 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
393 /* Advance 'str' past any whitespace or RFC 822 comments. A comment is
394 * a (potentially nested) parenthesized sequence with '\' used to
395 * escape any character (including parentheses).
397 * If the sequence to be skipped continues to the end of the string,
398 * then 'str' will be left pointing at the final terminating '\0'
402 skip_space_and_comments (const char **str)
407 while (*s && (isspace (*s) || *s == '(')) {
408 while (*s && isspace (*s))
413 while (*s && nesting) {
416 } else if (*s == ')') {
418 } else if (*s == '\\') {
430 /* Parse an RFC 822 message-id, discarding whitespace, any RFC 822
431 * comments, and the '<' and '>' delimiters.
433 * If not NULL, then *next will be made to point to the first character
434 * not parsed, (possibly pointing to the final '\0' terminator.
436 * Returns a newly talloc'ed string belonging to 'ctx'.
438 * Returns NULL if there is any error parsing the message-id. */
440 _parse_message_id (void *ctx, const char *message_id, const char **next)
445 if (message_id == NULL || *message_id == '\0')
450 skip_space_and_comments (&s);
452 /* Skip any unstructured text as well. */
453 while (*s && *s != '<')
464 skip_space_and_comments (&s);
467 while (*end && *end != '>')
476 if (end > s && *end == '>')
481 result = talloc_strndup (ctx, s, end - s + 1);
483 /* Finally, collapse any whitespace that is within the message-id
489 for (r = result, len = strlen (r); *r; r++, len--)
490 if (*r == ' ' || *r == '\t')
491 memmove (r, r+1, len);
497 /* Parse a References header value, putting a (talloc'ed under 'ctx')
498 * copy of each referenced message-id into 'hash'.
500 * We explicitly avoid including any reference identical to
501 * 'message_id' in the result (to avoid mass confusion when a single
502 * message references itself cyclically---and yes, mail messages are
503 * not infrequent in the wild that do this---don't ask me why).
505 * Return the last reference parsed, if it is not equal to message_id.
508 parse_references (void *ctx,
509 const char *message_id,
515 if (refs == NULL || *refs == '\0')
519 ref = _parse_message_id (ctx, refs, &refs);
521 if (ref && strcmp (ref, message_id))
522 g_hash_table_insert (hash, ref, NULL);
525 /* The return value of this function is used to add a parent
526 * reference to the database. We should avoid making a message
527 * its own parent, thus the following check.
530 if (ref && strcmp(ref, message_id)) {
538 notmuch_database_create (const char *path, notmuch_database_t **database)
540 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
541 notmuch_database_t *notmuch = NULL;
542 char *notmuch_path = NULL;
547 fprintf (stderr, "Error: Cannot create a database for a NULL path.\n");
548 status = NOTMUCH_STATUS_NULL_POINTER;
552 err = stat (path, &st);
554 fprintf (stderr, "Error: Cannot create database at %s: %s.\n",
555 path, strerror (errno));
556 status = NOTMUCH_STATUS_FILE_ERROR;
560 if (! S_ISDIR (st.st_mode)) {
561 fprintf (stderr, "Error: Cannot create database at %s: Not a directory.\n",
563 status = NOTMUCH_STATUS_FILE_ERROR;
567 notmuch_path = talloc_asprintf (NULL, "%s/%s", path, ".notmuch");
569 err = mkdir (notmuch_path, 0755);
572 fprintf (stderr, "Error: Cannot create directory %s: %s.\n",
573 notmuch_path, strerror (errno));
574 status = NOTMUCH_STATUS_FILE_ERROR;
578 status = notmuch_database_open (path,
579 NOTMUCH_DATABASE_MODE_READ_WRITE,
583 status = notmuch_database_upgrade (notmuch, NULL, NULL);
585 notmuch_database_close(notmuch);
591 talloc_free (notmuch_path);
596 talloc_free (notmuch);
601 _notmuch_database_ensure_writable (notmuch_database_t *notmuch)
603 if (notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY) {
604 fprintf (stderr, "Cannot write to a read-only database.\n");
605 return NOTMUCH_STATUS_READ_ONLY_DATABASE;
608 return NOTMUCH_STATUS_SUCCESS;
612 notmuch_database_open (const char *path,
613 notmuch_database_mode_t mode,
614 notmuch_database_t **database)
616 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
617 void *local = talloc_new (NULL);
618 notmuch_database_t *notmuch = NULL;
619 char *notmuch_path, *xapian_path;
622 unsigned int i, version;
623 static int initialized = 0;
626 fprintf (stderr, "Error: Cannot open a database for a NULL path.\n");
627 status = NOTMUCH_STATUS_NULL_POINTER;
631 if (! (notmuch_path = talloc_asprintf (local, "%s/%s", path, ".notmuch"))) {
632 fprintf (stderr, "Out of memory\n");
633 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
637 err = stat (notmuch_path, &st);
639 fprintf (stderr, "Error opening database at %s: %s\n",
640 notmuch_path, strerror (errno));
641 status = NOTMUCH_STATUS_FILE_ERROR;
645 if (! (xapian_path = talloc_asprintf (local, "%s/%s", notmuch_path, "xapian"))) {
646 fprintf (stderr, "Out of memory\n");
647 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
651 /* Initialize the GLib type system and threads */
654 /* Initialize gmime */
660 notmuch = talloc_zero (NULL, notmuch_database_t);
661 notmuch->exception_reported = FALSE;
662 notmuch->path = talloc_strdup (notmuch, path);
664 if (notmuch->path[strlen (notmuch->path) - 1] == '/')
665 notmuch->path[strlen (notmuch->path) - 1] = '\0';
667 notmuch->needs_upgrade = FALSE;
668 notmuch->mode = mode;
669 notmuch->atomic_nesting = 0;
671 string last_thread_id;
673 if (mode == NOTMUCH_DATABASE_MODE_READ_WRITE) {
674 notmuch->xapian_db = new Xapian::WritableDatabase (xapian_path,
675 Xapian::DB_CREATE_OR_OPEN);
676 version = notmuch_database_get_version (notmuch);
678 if (version > NOTMUCH_DATABASE_VERSION) {
680 "Error: Notmuch database at %s\n"
681 " has a newer database format version (%u) than supported by this\n"
682 " version of notmuch (%u). Refusing to open this database in\n"
683 " read-write mode.\n",
684 notmuch_path, version, NOTMUCH_DATABASE_VERSION);
685 notmuch->mode = NOTMUCH_DATABASE_MODE_READ_ONLY;
686 notmuch_database_destroy (notmuch);
688 status = NOTMUCH_STATUS_FILE_ERROR;
692 if (version < NOTMUCH_DATABASE_VERSION)
693 notmuch->needs_upgrade = TRUE;
695 notmuch->xapian_db = new Xapian::Database (xapian_path);
696 version = notmuch_database_get_version (notmuch);
697 if (version > NOTMUCH_DATABASE_VERSION)
700 "Warning: Notmuch database at %s\n"
701 " has a newer database format version (%u) than supported by this\n"
702 " version of notmuch (%u). Some operations may behave incorrectly,\n"
703 " (but the database will not be harmed since it is being opened\n"
704 " in read-only mode).\n",
705 notmuch_path, version, NOTMUCH_DATABASE_VERSION);
709 notmuch->last_doc_id = notmuch->xapian_db->get_lastdocid ();
710 last_thread_id = notmuch->xapian_db->get_metadata ("last_thread_id");
711 if (last_thread_id.empty ()) {
712 notmuch->last_thread_id = 0;
717 str = last_thread_id.c_str ();
718 notmuch->last_thread_id = strtoull (str, &end, 16);
720 INTERNAL_ERROR ("Malformed database last_thread_id: %s", str);
723 notmuch->query_parser = new Xapian::QueryParser;
724 notmuch->term_gen = new Xapian::TermGenerator;
725 notmuch->term_gen->set_stemmer (Xapian::Stem ("english"));
726 notmuch->value_range_processor = new Xapian::NumberValueRangeProcessor (NOTMUCH_VALUE_TIMESTAMP);
727 notmuch->date_range_processor = new ParseTimeValueRangeProcessor (NOTMUCH_VALUE_TIMESTAMP);
729 notmuch->query_parser->set_default_op (Xapian::Query::OP_AND);
730 notmuch->query_parser->set_database (*notmuch->xapian_db);
731 notmuch->query_parser->set_stemmer (Xapian::Stem ("english"));
732 notmuch->query_parser->set_stemming_strategy (Xapian::QueryParser::STEM_SOME);
733 notmuch->query_parser->add_valuerangeprocessor (notmuch->value_range_processor);
734 notmuch->query_parser->add_valuerangeprocessor (notmuch->date_range_processor);
736 for (i = 0; i < ARRAY_SIZE (BOOLEAN_PREFIX_EXTERNAL); i++) {
737 prefix_t *prefix = &BOOLEAN_PREFIX_EXTERNAL[i];
738 notmuch->query_parser->add_boolean_prefix (prefix->name,
742 for (i = 0; i < ARRAY_SIZE (PROBABILISTIC_PREFIX); i++) {
743 prefix_t *prefix = &PROBABILISTIC_PREFIX[i];
744 notmuch->query_parser->add_prefix (prefix->name, prefix->prefix);
746 } catch (const Xapian::Error &error) {
747 fprintf (stderr, "A Xapian exception occurred opening database: %s\n",
748 error.get_msg().c_str());
749 notmuch_database_destroy (notmuch);
751 status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
760 talloc_free (notmuch);
765 notmuch_database_close (notmuch_database_t *notmuch)
768 if (notmuch->xapian_db != NULL &&
769 notmuch->mode == NOTMUCH_DATABASE_MODE_READ_WRITE)
770 (static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db))->flush ();
771 } catch (const Xapian::Error &error) {
772 if (! notmuch->exception_reported) {
773 fprintf (stderr, "Error: A Xapian exception occurred flushing database: %s\n",
774 error.get_msg().c_str());
778 /* Many Xapian objects (and thus notmuch objects) hold references to
779 * the database, so merely deleting the database may not suffice to
780 * close it. Thus, we explicitly close it here. */
781 if (notmuch->xapian_db != NULL) {
783 notmuch->xapian_db->close();
784 } catch (const Xapian::Error &error) {
789 delete notmuch->term_gen;
790 notmuch->term_gen = NULL;
791 delete notmuch->query_parser;
792 notmuch->query_parser = NULL;
793 delete notmuch->xapian_db;
794 notmuch->xapian_db = NULL;
795 delete notmuch->value_range_processor;
796 notmuch->value_range_processor = NULL;
797 delete notmuch->date_range_processor;
798 notmuch->date_range_processor = NULL;
802 notmuch_database_destroy (notmuch_database_t *notmuch)
804 notmuch_database_close (notmuch);
805 talloc_free (notmuch);
809 notmuch_database_get_path (notmuch_database_t *notmuch)
811 return notmuch->path;
815 notmuch_database_get_version (notmuch_database_t *notmuch)
817 unsigned int version;
818 string version_string;
822 version_string = notmuch->xapian_db->get_metadata ("version");
823 if (version_string.empty ())
826 str = version_string.c_str ();
827 if (str == NULL || *str == '\0')
830 version = strtoul (str, &end, 10);
832 INTERNAL_ERROR ("Malformed database version: %s", str);
838 notmuch_database_needs_upgrade (notmuch_database_t *notmuch)
840 return notmuch->needs_upgrade;
843 static volatile sig_atomic_t do_progress_notify = 0;
846 handle_sigalrm (unused (int signal))
848 do_progress_notify = 1;
851 /* Upgrade the current database.
853 * After opening a database in read-write mode, the client should
854 * check if an upgrade is needed (notmuch_database_needs_upgrade) and
855 * if so, upgrade with this function before making any modifications.
857 * The optional progress_notify callback can be used by the caller to
858 * provide progress indication to the user. If non-NULL it will be
859 * called periodically with 'count' as the number of messages upgraded
860 * so far and 'total' the overall number of messages that will be
864 notmuch_database_upgrade (notmuch_database_t *notmuch,
865 void (*progress_notify) (void *closure,
869 Xapian::WritableDatabase *db;
870 struct sigaction action;
871 struct itimerval timerval;
872 notmuch_bool_t timer_is_active = FALSE;
873 unsigned int version;
874 notmuch_status_t status;
875 unsigned int count = 0, total = 0;
877 status = _notmuch_database_ensure_writable (notmuch);
881 db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
883 version = notmuch_database_get_version (notmuch);
885 if (version >= NOTMUCH_DATABASE_VERSION)
886 return NOTMUCH_STATUS_SUCCESS;
888 if (progress_notify) {
889 /* Setup our handler for SIGALRM */
890 memset (&action, 0, sizeof (struct sigaction));
891 action.sa_handler = handle_sigalrm;
892 sigemptyset (&action.sa_mask);
893 action.sa_flags = SA_RESTART;
894 sigaction (SIGALRM, &action, NULL);
896 /* Then start a timer to send SIGALRM once per second. */
897 timerval.it_interval.tv_sec = 1;
898 timerval.it_interval.tv_usec = 0;
899 timerval.it_value.tv_sec = 1;
900 timerval.it_value.tv_usec = 0;
901 setitimer (ITIMER_REAL, &timerval, NULL);
903 timer_is_active = TRUE;
906 /* Before version 1, each message document had its filename in the
907 * data field. Copy that into the new format by calling
908 * notmuch_message_add_filename.
911 notmuch_query_t *query = notmuch_query_create (notmuch, "");
912 notmuch_messages_t *messages;
913 notmuch_message_t *message;
915 Xapian::TermIterator t, t_end;
917 total = notmuch_query_count_messages (query);
919 for (messages = notmuch_query_search_messages (query);
920 notmuch_messages_valid (messages);
921 notmuch_messages_move_to_next (messages))
923 if (do_progress_notify) {
924 progress_notify (closure, (double) count / total);
925 do_progress_notify = 0;
928 message = notmuch_messages_get (messages);
930 filename = _notmuch_message_talloc_copy_data (message);
931 if (filename && *filename != '\0') {
932 _notmuch_message_add_filename (message, filename);
933 _notmuch_message_sync (message);
935 talloc_free (filename);
937 notmuch_message_destroy (message);
942 notmuch_query_destroy (query);
944 /* Also, before version 1 we stored directory timestamps in
945 * XTIMESTAMP documents instead of the current XDIRECTORY
946 * documents. So copy those as well. */
948 t_end = notmuch->xapian_db->allterms_end ("XTIMESTAMP");
950 for (t = notmuch->xapian_db->allterms_begin ("XTIMESTAMP");
954 Xapian::PostingIterator p, p_end;
955 std::string term = *t;
957 p_end = notmuch->xapian_db->postlist_end (term);
959 for (p = notmuch->xapian_db->postlist_begin (term);
963 Xapian::Document document;
965 notmuch_directory_t *directory;
967 if (do_progress_notify) {
968 progress_notify (closure, (double) count / total);
969 do_progress_notify = 0;
972 document = find_document_for_doc_id (notmuch, *p);
973 mtime = Xapian::sortable_unserialise (
974 document.get_value (NOTMUCH_VALUE_TIMESTAMP));
976 directory = _notmuch_directory_create (notmuch, term.c_str() + 10,
977 NOTMUCH_FIND_CREATE, &status);
978 notmuch_directory_set_mtime (directory, mtime);
979 notmuch_directory_destroy (directory);
984 db->set_metadata ("version", STRINGIFY (NOTMUCH_DATABASE_VERSION));
987 /* Now that the upgrade is complete we can remove the old data
988 * and documents that are no longer needed. */
990 notmuch_query_t *query = notmuch_query_create (notmuch, "");
991 notmuch_messages_t *messages;
992 notmuch_message_t *message;
995 for (messages = notmuch_query_search_messages (query);
996 notmuch_messages_valid (messages);
997 notmuch_messages_move_to_next (messages))
999 if (do_progress_notify) {
1000 progress_notify (closure, (double) count / total);
1001 do_progress_notify = 0;
1004 message = notmuch_messages_get (messages);
1006 filename = _notmuch_message_talloc_copy_data (message);
1007 if (filename && *filename != '\0') {
1008 _notmuch_message_clear_data (message);
1009 _notmuch_message_sync (message);
1011 talloc_free (filename);
1013 notmuch_message_destroy (message);
1016 notmuch_query_destroy (query);
1020 Xapian::TermIterator t, t_end;
1022 t_end = notmuch->xapian_db->allterms_end ("XTIMESTAMP");
1024 for (t = notmuch->xapian_db->allterms_begin ("XTIMESTAMP");
1028 Xapian::PostingIterator p, p_end;
1029 std::string term = *t;
1031 p_end = notmuch->xapian_db->postlist_end (term);
1033 for (p = notmuch->xapian_db->postlist_begin (term);
1037 if (do_progress_notify) {
1038 progress_notify (closure, (double) count / total);
1039 do_progress_notify = 0;
1042 db->delete_document (*p);
1047 if (timer_is_active) {
1048 /* Now stop the timer. */
1049 timerval.it_interval.tv_sec = 0;
1050 timerval.it_interval.tv_usec = 0;
1051 timerval.it_value.tv_sec = 0;
1052 timerval.it_value.tv_usec = 0;
1053 setitimer (ITIMER_REAL, &timerval, NULL);
1055 /* And disable the signal handler. */
1056 action.sa_handler = SIG_IGN;
1057 sigaction (SIGALRM, &action, NULL);
1060 return NOTMUCH_STATUS_SUCCESS;
1064 notmuch_database_begin_atomic (notmuch_database_t *notmuch)
1066 if (notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY ||
1067 notmuch->atomic_nesting > 0)
1071 (static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db))->begin_transaction (false);
1072 } catch (const Xapian::Error &error) {
1073 fprintf (stderr, "A Xapian exception occurred beginning transaction: %s.\n",
1074 error.get_msg().c_str());
1075 notmuch->exception_reported = TRUE;
1076 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
1080 notmuch->atomic_nesting++;
1081 return NOTMUCH_STATUS_SUCCESS;
1085 notmuch_database_end_atomic (notmuch_database_t *notmuch)
1087 Xapian::WritableDatabase *db;
1089 if (notmuch->atomic_nesting == 0)
1090 return NOTMUCH_STATUS_UNBALANCED_ATOMIC;
1092 if (notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY ||
1093 notmuch->atomic_nesting > 1)
1096 db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
1098 db->commit_transaction ();
1100 /* This is a hack for testing. Xapian never flushes on a
1101 * non-flushed commit, even if the flush threshold is 1.
1102 * However, we rely on flushing to test atomicity. */
1103 const char *thresh = getenv ("XAPIAN_FLUSH_THRESHOLD");
1104 if (thresh && atoi (thresh) == 1)
1106 } catch (const Xapian::Error &error) {
1107 fprintf (stderr, "A Xapian exception occurred committing transaction: %s.\n",
1108 error.get_msg().c_str());
1109 notmuch->exception_reported = TRUE;
1110 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
1114 notmuch->atomic_nesting--;
1115 return NOTMUCH_STATUS_SUCCESS;
1118 /* We allow the user to use arbitrarily long paths for directories. But
1119 * we have a term-length limit. So if we exceed that, we'll use the
1120 * SHA-1 of the path for the database term.
1122 * Note: This function may return the original value of 'path'. If it
1123 * does not, then the caller is responsible to free() the returned
1127 _notmuch_database_get_directory_db_path (const char *path)
1129 int term_len = strlen (_find_prefix ("directory")) + strlen (path);
1131 if (term_len > NOTMUCH_TERM_MAX)
1132 return notmuch_sha1_of_string (path);
1137 /* Given a path, split it into two parts: the directory part is all
1138 * components except for the last, and the basename is that last
1139 * component. Getting the return-value for either part is optional
1140 * (the caller can pass NULL).
1142 * The original 'path' can represent either a regular file or a
1143 * directory---the splitting will be carried out in the same way in
1144 * either case. Trailing slashes on 'path' will be ignored, and any
1145 * cases of multiple '/' characters appearing in series will be
1146 * treated as a single '/'.
1148 * Allocation (if any) will have 'ctx' as the talloc owner. But
1149 * pointers will be returned within the original path string whenever
1152 * Note: If 'path' is non-empty and contains no non-trailing slash,
1153 * (that is, consists of a filename with no parent directory), then
1154 * the directory returned will be an empty string. However, if 'path'
1155 * is an empty string, then both directory and basename will be
1159 _notmuch_database_split_path (void *ctx,
1161 const char **directory,
1162 const char **basename)
1166 if (path == NULL || *path == '\0') {
1171 return NOTMUCH_STATUS_SUCCESS;
1174 /* Find the last slash (not counting a trailing slash), if any. */
1176 slash = path + strlen (path) - 1;
1178 /* First, skip trailing slashes. */
1179 while (slash != path) {
1186 /* Then, find a slash. */
1187 while (slash != path) {
1197 /* Finally, skip multiple slashes. */
1198 while (slash != path) {
1205 if (slash == path) {
1207 *directory = talloc_strdup (ctx, "");
1212 *directory = talloc_strndup (ctx, path, slash - path + 1);
1215 return NOTMUCH_STATUS_SUCCESS;
1218 /* Find the document ID of the specified directory.
1220 * If (flags & NOTMUCH_FIND_CREATE), a new directory document will be
1221 * created if one does not exist for 'path'. Otherwise, if the
1222 * directory document does not exist, this sets *directory_id to
1223 * ((unsigned int)-1) and returns NOTMUCH_STATUS_SUCCESS.
1226 _notmuch_database_find_directory_id (notmuch_database_t *notmuch,
1228 notmuch_find_flags_t flags,
1229 unsigned int *directory_id)
1231 notmuch_directory_t *directory;
1232 notmuch_status_t status;
1236 return NOTMUCH_STATUS_SUCCESS;
1239 directory = _notmuch_directory_create (notmuch, path, flags, &status);
1240 if (status || !directory) {
1245 *directory_id = _notmuch_directory_get_document_id (directory);
1247 notmuch_directory_destroy (directory);
1249 return NOTMUCH_STATUS_SUCCESS;
1253 _notmuch_database_get_directory_path (void *ctx,
1254 notmuch_database_t *notmuch,
1255 unsigned int doc_id)
1257 Xapian::Document document;
1259 document = find_document_for_doc_id (notmuch, doc_id);
1261 return talloc_strdup (ctx, document.get_data ().c_str ());
1264 /* Given a legal 'filename' for the database, (either relative to
1265 * database path or absolute with initial components identical to
1266 * database path), return a new string (with 'ctx' as the talloc
1267 * owner) suitable for use as a direntry term value.
1269 * If (flags & NOTMUCH_FIND_CREATE), the necessary directory documents
1270 * will be created in the database as needed. Otherwise, if the
1271 * necessary directory documents do not exist, this sets
1272 * *direntry to NULL and returns NOTMUCH_STATUS_SUCCESS.
1275 _notmuch_database_filename_to_direntry (void *ctx,
1276 notmuch_database_t *notmuch,
1277 const char *filename,
1278 notmuch_find_flags_t flags,
1281 const char *relative, *directory, *basename;
1282 Xapian::docid directory_id;
1283 notmuch_status_t status;
1285 relative = _notmuch_database_relative_path (notmuch, filename);
1287 status = _notmuch_database_split_path (ctx, relative,
1288 &directory, &basename);
1292 status = _notmuch_database_find_directory_id (notmuch, directory, flags,
1294 if (status || directory_id == (unsigned int)-1) {
1299 *direntry = talloc_asprintf (ctx, "%u:%s", directory_id, basename);
1301 return NOTMUCH_STATUS_SUCCESS;
1304 /* Given a legal 'path' for the database, return the relative path.
1306 * The return value will be a pointer to the original path contents,
1307 * and will be either the original string (if 'path' was relative) or
1308 * a portion of the string (if path was absolute and begins with the
1312 _notmuch_database_relative_path (notmuch_database_t *notmuch,
1315 const char *db_path, *relative;
1316 unsigned int db_path_len;
1318 db_path = notmuch_database_get_path (notmuch);
1319 db_path_len = strlen (db_path);
1323 if (*relative == '/') {
1324 while (*relative == '/' && *(relative+1) == '/')
1327 if (strncmp (relative, db_path, db_path_len) == 0)
1329 relative += db_path_len;
1330 while (*relative == '/')
1339 notmuch_database_get_directory (notmuch_database_t *notmuch,
1341 notmuch_directory_t **directory)
1343 notmuch_status_t status;
1345 if (directory == NULL)
1346 return NOTMUCH_STATUS_NULL_POINTER;
1350 *directory = _notmuch_directory_create (notmuch, path,
1351 NOTMUCH_FIND_LOOKUP, &status);
1352 } catch (const Xapian::Error &error) {
1353 fprintf (stderr, "A Xapian exception occurred getting directory: %s.\n",
1354 error.get_msg().c_str());
1355 notmuch->exception_reported = TRUE;
1356 status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
1361 /* Allocate a document ID that satisfies the following criteria:
1363 * 1. The ID does not exist for any document in the Xapian database
1365 * 2. The ID was not previously returned from this function
1367 * 3. The ID is the smallest integer satisfying (1) and (2)
1369 * This function will trigger an internal error if these constraints
1370 * cannot all be satisfied, (that is, the pool of available document
1371 * IDs has been exhausted).
1374 _notmuch_database_generate_doc_id (notmuch_database_t *notmuch)
1376 assert (notmuch->last_doc_id >= notmuch->xapian_db->get_lastdocid ());
1378 notmuch->last_doc_id++;
1380 if (notmuch->last_doc_id == 0)
1381 INTERNAL_ERROR ("Xapian document IDs are exhausted.\n");
1383 return notmuch->last_doc_id;
1387 _notmuch_database_generate_thread_id (notmuch_database_t *notmuch)
1389 /* 16 bytes (+ terminator) for hexadecimal representation of
1390 * a 64-bit integer. */
1391 static char thread_id[17];
1392 Xapian::WritableDatabase *db;
1394 db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
1396 notmuch->last_thread_id++;
1398 sprintf (thread_id, "%016" PRIx64, notmuch->last_thread_id);
1400 db->set_metadata ("last_thread_id", thread_id);
1406 _get_metadata_thread_id_key (void *ctx, const char *message_id)
1408 if (strlen (message_id) > NOTMUCH_MESSAGE_ID_MAX)
1409 message_id = _message_id_compressed (ctx, message_id);
1411 return talloc_asprintf (ctx, NOTMUCH_METADATA_THREAD_ID_PREFIX "%s",
1415 /* Find the thread ID to which the message with 'message_id' belongs.
1417 * Note: 'thread_id_ret' must not be NULL!
1418 * On success '*thread_id_ret' is set to a newly talloced string belonging to
1421 * Note: If there is no message in the database with the given
1422 * 'message_id' then a new thread_id will be allocated for this
1423 * message and stored in the database metadata, (where this same
1424 * thread ID can be looked up if the message is added to the database
1427 static notmuch_status_t
1428 _resolve_message_id_to_thread_id (notmuch_database_t *notmuch,
1430 const char *message_id,
1431 const char **thread_id_ret)
1433 notmuch_status_t status;
1434 notmuch_message_t *message;
1435 string thread_id_string;
1437 Xapian::WritableDatabase *db;
1439 status = notmuch_database_find_message (notmuch, message_id, &message);
1445 *thread_id_ret = talloc_steal (ctx,
1446 notmuch_message_get_thread_id (message));
1448 notmuch_message_destroy (message);
1450 return NOTMUCH_STATUS_SUCCESS;
1453 /* Message has not been seen yet.
1455 * We may have seen a reference to it already, in which case, we
1456 * can return the thread ID stored in the metadata. Otherwise, we
1457 * generate a new thread ID and store it there.
1459 db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
1460 metadata_key = _get_metadata_thread_id_key (ctx, message_id);
1461 thread_id_string = notmuch->xapian_db->get_metadata (metadata_key);
1463 if (thread_id_string.empty()) {
1464 *thread_id_ret = talloc_strdup (ctx,
1465 _notmuch_database_generate_thread_id (notmuch));
1466 db->set_metadata (metadata_key, *thread_id_ret);
1468 *thread_id_ret = talloc_strdup (ctx, thread_id_string.c_str());
1471 talloc_free (metadata_key);
1473 return NOTMUCH_STATUS_SUCCESS;
1476 static notmuch_status_t
1477 _merge_threads (notmuch_database_t *notmuch,
1478 const char *winner_thread_id,
1479 const char *loser_thread_id)
1481 Xapian::PostingIterator loser, loser_end;
1482 notmuch_message_t *message = NULL;
1483 notmuch_private_status_t private_status;
1484 notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
1486 find_doc_ids (notmuch, "thread", loser_thread_id, &loser, &loser_end);
1488 for ( ; loser != loser_end; loser++) {
1489 message = _notmuch_message_create (notmuch, notmuch,
1490 *loser, &private_status);
1491 if (message == NULL) {
1492 ret = COERCE_STATUS (private_status,
1493 "Cannot find document for doc_id from query");
1497 _notmuch_message_remove_term (message, "thread", loser_thread_id);
1498 _notmuch_message_add_term (message, "thread", winner_thread_id);
1499 _notmuch_message_sync (message);
1501 notmuch_message_destroy (message);
1507 notmuch_message_destroy (message);
1513 _my_talloc_free_for_g_hash (void *ptr)
1518 static notmuch_status_t
1519 _notmuch_database_link_message_to_parents (notmuch_database_t *notmuch,
1520 notmuch_message_t *message,
1521 notmuch_message_file_t *message_file,
1522 const char **thread_id)
1524 GHashTable *parents = NULL;
1525 const char *refs, *in_reply_to, *in_reply_to_message_id;
1526 const char *last_ref_message_id, *this_message_id;
1527 GList *l, *keys = NULL;
1528 notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
1530 parents = g_hash_table_new_full (g_str_hash, g_str_equal,
1531 _my_talloc_free_for_g_hash, NULL);
1532 this_message_id = notmuch_message_get_message_id (message);
1534 refs = notmuch_message_file_get_header (message_file, "references");
1535 last_ref_message_id = parse_references (message,
1539 in_reply_to = notmuch_message_file_get_header (message_file, "in-reply-to");
1540 in_reply_to_message_id = parse_references (message,
1542 parents, in_reply_to);
1544 /* For the parent of this message, use the last message ID of the
1545 * References header, if available. If not, fall back to the
1546 * first message ID in the In-Reply-To header. */
1547 if (last_ref_message_id) {
1548 _notmuch_message_add_term (message, "replyto",
1549 last_ref_message_id);
1550 } else if (in_reply_to_message_id) {
1551 _notmuch_message_add_term (message, "replyto",
1552 in_reply_to_message_id);
1555 keys = g_hash_table_get_keys (parents);
1556 for (l = keys; l; l = l->next) {
1557 char *parent_message_id;
1558 const char *parent_thread_id = NULL;
1560 parent_message_id = (char *) l->data;
1562 _notmuch_message_add_term (message, "reference",
1565 ret = _resolve_message_id_to_thread_id (notmuch,
1572 if (*thread_id == NULL) {
1573 *thread_id = talloc_strdup (message, parent_thread_id);
1574 _notmuch_message_add_term (message, "thread", *thread_id);
1575 } else if (strcmp (*thread_id, parent_thread_id)) {
1576 ret = _merge_threads (notmuch, *thread_id, parent_thread_id);
1586 g_hash_table_unref (parents);
1591 static notmuch_status_t
1592 _notmuch_database_link_message_to_children (notmuch_database_t *notmuch,
1593 notmuch_message_t *message,
1594 const char **thread_id)
1596 const char *message_id = notmuch_message_get_message_id (message);
1597 Xapian::PostingIterator child, children_end;
1598 notmuch_message_t *child_message = NULL;
1599 const char *child_thread_id;
1600 notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
1601 notmuch_private_status_t private_status;
1603 find_doc_ids (notmuch, "reference", message_id, &child, &children_end);
1605 for ( ; child != children_end; child++) {
1607 child_message = _notmuch_message_create (message, notmuch,
1608 *child, &private_status);
1609 if (child_message == NULL) {
1610 ret = COERCE_STATUS (private_status,
1611 "Cannot find document for doc_id from query");
1615 child_thread_id = notmuch_message_get_thread_id (child_message);
1616 if (*thread_id == NULL) {
1617 *thread_id = talloc_strdup (message, child_thread_id);
1618 _notmuch_message_add_term (message, "thread", *thread_id);
1619 } else if (strcmp (*thread_id, child_thread_id)) {
1620 _notmuch_message_remove_term (child_message, "reference",
1622 _notmuch_message_sync (child_message);
1623 ret = _merge_threads (notmuch, *thread_id, child_thread_id);
1628 notmuch_message_destroy (child_message);
1629 child_message = NULL;
1634 notmuch_message_destroy (child_message);
1639 /* Given a (mostly empty) 'message' and its corresponding
1640 * 'message_file' link it to existing threads in the database.
1642 * The first check is in the metadata of the database to see if we
1643 * have pre-allocated a thread_id in advance for this message, (which
1644 * would have happened if a message was previously added that
1645 * referenced this one).
1647 * Second, we look at 'message_file' and its link-relevant headers
1648 * (References and In-Reply-To) for message IDs.
1650 * Finally, we look in the database for existing message that
1651 * reference 'message'.
1653 * In all cases, we assign to the current message the first thread_id
1654 * found (through either parent or child). We will also merge any
1655 * existing, distinct threads where this message belongs to both,
1656 * (which is not uncommon when messages are processed out of order).
1658 * Finally, if no thread ID has been found through parent or child, we
1659 * call _notmuch_message_generate_thread_id to generate a new thread
1660 * ID. This should only happen for new, top-level messages, (no
1661 * References or In-Reply-To header in this message, and no previously
1662 * added message refers to this message).
1664 static notmuch_status_t
1665 _notmuch_database_link_message (notmuch_database_t *notmuch,
1666 notmuch_message_t *message,
1667 notmuch_message_file_t *message_file)
1669 notmuch_status_t status;
1670 const char *message_id, *thread_id = NULL;
1674 message_id = notmuch_message_get_message_id (message);
1675 metadata_key = _get_metadata_thread_id_key (message, message_id);
1677 /* Check if we have already seen related messages to this one.
1678 * If we have then use the thread_id that we stored at that time.
1680 stored_id = notmuch->xapian_db->get_metadata (metadata_key);
1681 if (! stored_id.empty()) {
1682 Xapian::WritableDatabase *db;
1684 db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
1686 /* Clear the metadata for this message ID. We don't need it
1688 db->set_metadata (metadata_key, "");
1689 thread_id = stored_id.c_str();
1691 _notmuch_message_add_term (message, "thread", thread_id);
1693 talloc_free (metadata_key);
1695 status = _notmuch_database_link_message_to_parents (notmuch, message,
1701 status = _notmuch_database_link_message_to_children (notmuch, message,
1706 /* If not part of any existing thread, generate a new thread ID. */
1707 if (thread_id == NULL) {
1708 thread_id = _notmuch_database_generate_thread_id (notmuch);
1710 _notmuch_message_add_term (message, "thread", thread_id);
1713 return NOTMUCH_STATUS_SUCCESS;
1717 notmuch_database_add_message (notmuch_database_t *notmuch,
1718 const char *filename,
1719 notmuch_message_t **message_ret)
1721 notmuch_message_file_t *message_file;
1722 notmuch_message_t *message = NULL;
1723 notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS, ret2;
1724 notmuch_private_status_t private_status;
1726 const char *date, *header;
1727 const char *from, *to, *subject;
1728 char *message_id = NULL;
1731 *message_ret = NULL;
1733 ret = _notmuch_database_ensure_writable (notmuch);
1737 message_file = notmuch_message_file_open (filename);
1738 if (message_file == NULL)
1739 return NOTMUCH_STATUS_FILE_ERROR;
1741 /* Adding a message may change many documents. Do this all
1743 ret = notmuch_database_begin_atomic (notmuch);
1747 notmuch_message_file_restrict_headers (message_file,
1758 /* Before we do any real work, (especially before doing a
1759 * potential SHA-1 computation on the entire file's contents),
1760 * let's make sure that what we're looking at looks like an
1761 * actual email message.
1763 from = notmuch_message_file_get_header (message_file, "from");
1764 subject = notmuch_message_file_get_header (message_file, "subject");
1765 to = notmuch_message_file_get_header (message_file, "to");
1767 if ((from == NULL || *from == '\0') &&
1768 (subject == NULL || *subject == '\0') &&
1769 (to == NULL || *to == '\0'))
1771 ret = NOTMUCH_STATUS_FILE_NOT_EMAIL;
1775 /* Now that we're sure it's mail, the first order of business
1776 * is to find a message ID (or else create one ourselves). */
1778 header = notmuch_message_file_get_header (message_file, "message-id");
1779 if (header && *header != '\0') {
1780 message_id = _parse_message_id (message_file, header, NULL);
1782 /* So the header value isn't RFC-compliant, but it's
1783 * better than no message-id at all. */
1784 if (message_id == NULL)
1785 message_id = talloc_strdup (message_file, header);
1787 /* If a message ID is too long, substitute its sha1 instead. */
1788 if (message_id && strlen (message_id) > NOTMUCH_MESSAGE_ID_MAX) {
1789 char *compressed = _message_id_compressed (message_file,
1791 talloc_free (message_id);
1792 message_id = compressed;
1796 if (message_id == NULL ) {
1797 /* No message-id at all, let's generate one by taking a
1798 * hash over the file's contents. */
1799 char *sha1 = notmuch_sha1_of_file (filename);
1801 /* If that failed too, something is really wrong. Give up. */
1803 ret = NOTMUCH_STATUS_FILE_ERROR;
1807 message_id = talloc_asprintf (message_file,
1808 "notmuch-sha1-%s", sha1);
1812 /* Now that we have a message ID, we get a message object,
1813 * (which may or may not reference an existing document in the
1816 message = _notmuch_message_create_for_message_id (notmuch,
1820 talloc_free (message_id);
1822 if (message == NULL) {
1823 ret = COERCE_STATUS (private_status,
1824 "Unexpected status value from _notmuch_message_create_for_message_id");
1828 _notmuch_message_add_filename (message, filename);
1830 /* Is this a newly created message object? */
1831 if (private_status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND) {
1832 _notmuch_message_add_term (message, "type", "mail");
1834 ret = _notmuch_database_link_message (notmuch, message,
1839 date = notmuch_message_file_get_header (message_file, "date");
1840 _notmuch_message_set_header_values (message, date, from, subject);
1842 ret = _notmuch_message_index_file (message, filename);
1846 ret = NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID;
1849 _notmuch_message_sync (message);
1850 } catch (const Xapian::Error &error) {
1851 fprintf (stderr, "A Xapian exception occurred adding message: %s.\n",
1852 error.get_msg().c_str());
1853 notmuch->exception_reported = TRUE;
1854 ret = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
1860 if ((ret == NOTMUCH_STATUS_SUCCESS ||
1861 ret == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) && message_ret)
1862 *message_ret = message;
1864 notmuch_message_destroy (message);
1868 notmuch_message_file_close (message_file);
1870 ret2 = notmuch_database_end_atomic (notmuch);
1871 if ((ret == NOTMUCH_STATUS_SUCCESS ||
1872 ret == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) &&
1873 ret2 != NOTMUCH_STATUS_SUCCESS)
1880 notmuch_database_remove_message (notmuch_database_t *notmuch,
1881 const char *filename)
1883 notmuch_status_t status;
1884 notmuch_message_t *message;
1886 status = notmuch_database_find_message_by_filename (notmuch, filename,
1889 if (status == NOTMUCH_STATUS_SUCCESS && message) {
1890 status = _notmuch_message_remove_filename (message, filename);
1891 if (status == NOTMUCH_STATUS_SUCCESS)
1892 _notmuch_message_delete (message);
1893 else if (status == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID)
1894 _notmuch_message_sync (message);
1896 notmuch_message_destroy (message);
1903 notmuch_database_find_message_by_filename (notmuch_database_t *notmuch,
1904 const char *filename,
1905 notmuch_message_t **message_ret)
1908 const char *prefix = _find_prefix ("file-direntry");
1909 char *direntry, *term;
1910 Xapian::PostingIterator i, end;
1911 notmuch_status_t status;
1913 if (message_ret == NULL)
1914 return NOTMUCH_STATUS_NULL_POINTER;
1916 /* return NULL on any failure */
1917 *message_ret = NULL;
1919 local = talloc_new (notmuch);
1922 status = _notmuch_database_filename_to_direntry (
1923 local, notmuch, filename, NOTMUCH_FIND_LOOKUP, &direntry);
1924 if (status || !direntry)
1927 term = talloc_asprintf (local, "%s%s", prefix, direntry);
1929 find_doc_ids_for_term (notmuch, term, &i, &end);
1932 notmuch_private_status_t private_status;
1934 *message_ret = _notmuch_message_create (notmuch, notmuch, *i,
1936 if (*message_ret == NULL)
1937 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
1939 } catch (const Xapian::Error &error) {
1940 fprintf (stderr, "Error: A Xapian exception occurred finding message by filename: %s\n",
1941 error.get_msg().c_str());
1942 notmuch->exception_reported = TRUE;
1943 status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
1947 talloc_free (local);
1949 if (status && *message_ret) {
1950 notmuch_message_destroy (*message_ret);
1951 *message_ret = NULL;
1956 notmuch_string_list_t *
1957 _notmuch_database_get_terms_with_prefix (void *ctx, Xapian::TermIterator &i,
1958 Xapian::TermIterator &end,
1961 int prefix_len = strlen (prefix);
1962 notmuch_string_list_t *list;
1964 list = _notmuch_string_list_create (ctx);
1965 if (unlikely (list == NULL))
1968 for (i.skip_to (prefix); i != end; i++) {
1969 /* Terminate loop at first term without desired prefix. */
1970 if (strncmp ((*i).c_str (), prefix, prefix_len))
1973 _notmuch_string_list_append (list, (*i).c_str () + prefix_len);
1980 notmuch_database_get_all_tags (notmuch_database_t *db)
1982 Xapian::TermIterator i, end;
1983 notmuch_string_list_t *tags;
1986 i = db->xapian_db->allterms_begin();
1987 end = db->xapian_db->allterms_end();
1988 tags = _notmuch_database_get_terms_with_prefix (db, i, end,
1989 _find_prefix ("tag"));
1990 _notmuch_string_list_sort (tags);
1991 return _notmuch_tags_create (db, tags);
1992 } catch (const Xapian::Error &error) {
1993 fprintf (stderr, "A Xapian exception occurred getting tags: %s.\n",
1994 error.get_msg().c_str());
1995 db->exception_reported = TRUE;