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"
31 #include <glib.h> /* g_free, GPtrArray, GHashTable */
32 #include <glib-object.h> /* g_type_init */
34 #include <gmime/gmime.h> /* g_mime_init */
38 #define ARRAY_SIZE(arr) (sizeof (arr) / sizeof (arr[0]))
45 #define NOTMUCH_DATABASE_VERSION 2
47 #define STRINGIFY(s) _SUB_STRINGIFY(s)
48 #define _SUB_STRINGIFY(s) #s
50 /* Here's the current schema for our database (for NOTMUCH_DATABASE_VERSION):
52 * We currently have two different types of documents (mail and
53 * directory) and also some metadata.
57 * A mail document is associated with a particular email message. It
58 * is stored in one or more files on disk (though only one has its
59 * content indexed) and is uniquely identified by its "id" field
60 * (which is generally the message ID). It is indexed with the
61 * following prefixed terms which the database uses to construct
64 * Single terms of given prefix:
68 * id: Unique ID of mail. This is from the Message-ID header
69 * if present and not too long (see NOTMUCH_MESSAGE_ID_MAX).
70 * If it's present and too long, then we use
71 * "notmuch-sha1-<sha1_sum_of_message_id>".
72 * If this header is not present, we use
73 * "notmuch-sha1-<sha1_sum_of_entire_file>".
75 * thread: The ID of the thread to which the mail belongs
77 * replyto: The ID from the In-Reply-To header of the mail (if any).
79 * Multiple terms of given prefix:
81 * reference: All message IDs from In-Reply-To and References
82 * headers in the message.
84 * tag: Any tags associated with this message by the user.
86 * file-direntry: A colon-separated pair of values
87 * (INTEGER:STRING), where INTEGER is the
88 * document ID of a directory document, and
89 * STRING is the name of a file within that
90 * directory for this mail message.
92 * A mail document also has four values:
94 * TIMESTAMP: The time_t value corresponding to the message's
97 * MESSAGE_ID: The unique ID of the mail mess (see "id" above)
99 * FROM: The value of the "From" header
101 * SUBJECT: The value of the "Subject" header
103 * In addition, terms from the content of the message are added with
104 * "from", "to", "attachment", and "subject" prefixes for use by the
105 * user in searching. Similarly, terms from the path of the mail
106 * message are added with "folder" and "path" prefixes. But the
107 * database doesn't really care itself about any of these.
109 * The data portion of a mail document is empty.
113 * A directory document is used by a client of the notmuch library to
114 * maintain data necessary to allow for efficient polling of mail
117 * All directory documents contain one term:
119 * directory: The directory path (relative to the database path)
120 * Or the SHA1 sum of the directory path (if the
121 * path itself is too long to fit in a Xapian
124 * And all directory documents for directories other than top-level
125 * directories also contain the following term:
127 * directory-direntry: A colon-separated pair of values
128 * (INTEGER:STRING), where INTEGER is the
129 * document ID of the parent directory
130 * document, and STRING is the name of this
131 * directory within that parent.
133 * All directory documents have a single value:
135 * TIMESTAMP: The mtime of the directory (at last scan)
137 * The data portion of a directory document contains the path of the
138 * directory (relative to the database path).
142 * Xapian allows us to store arbitrary name-value pairs as
143 * "metadata". We currently use the following metadata names with the
146 * version The database schema version, (which is distinct
147 * from both the notmuch package version (see
148 * notmuch --version) and the libnotmuch library
149 * version. The version is stored as an base-10
150 * ASCII integer. The initial database version
151 * was 1, (though a schema existed before that
152 * were no "version" database value existed at
153 * all). Successive versions are allocated as
154 * changes are made to the database (such as by
155 * indexing new fields).
157 * last_thread_id The last thread ID generated. This is stored
158 * as a 16-byte hexadecimal ASCII representation
159 * of a 64-bit unsigned integer. The first ID
160 * generated is 1 and the value will be
161 * incremented for each thread ID.
163 * thread_id_* A pre-allocated thread ID for a particular
164 * message. This is actually an arbitrarily large
165 * family of metadata name. Any particular name is
166 * formed by concatenating "thread_id_" with a message
167 * ID (or the SHA1 sum of a message ID if it is very
168 * long---see description of 'id' in the mail
169 * document). The value stored is a thread ID.
171 * These thread ID metadata values are stored
172 * whenever a message references a parent message
173 * that does not yet exist in the database. A
174 * thread ID will be allocated and stored, and if
175 * the message is later added, the stored thread
176 * ID will be used (and the metadata value will
179 * Even before a message is added, it's
180 * pre-allocated thread ID is useful so that all
181 * descendant messages that reference this common
182 * parent can be recognized as belonging to the
186 /* With these prefix values we follow the conventions published here:
188 * http://xapian.org/docs/omega/termprefixes.html
190 * as much as makes sense. Note that I took some liberty in matching
191 * the reserved prefix values to notmuch concepts, (for example, 'G'
192 * is documented as "newsGroup (or similar entity - e.g. a web forum
193 * name)", for which I think the thread is the closest analogue in
194 * notmuch. This in spite of the fact that we will eventually be
195 * storing mailing-list messages where 'G' for "mailing list name"
196 * might be even a closer analogue. I'm treating the single-character
197 * prefixes preferentially for core notmuch concepts (which will be
198 * nearly universal to all mail messages).
201 static prefix_t BOOLEAN_PREFIX_INTERNAL[] = {
203 { "reference", "XREFERENCE" },
204 { "replyto", "XREPLYTO" },
205 { "directory", "XDIRECTORY" },
206 { "file-direntry", "XFDIRENTRY" },
207 { "directory-direntry", "XDDIRENTRY" },
210 static prefix_t BOOLEAN_PREFIX_EXTERNAL[] = {
217 * Without the ":", since this is a multi-letter prefix, Xapian
218 * will add a colon itself if the first letter of the path is
219 * upper-case ASCII. Including the ":" forces there to always be a
220 * colon, which keeps our own logic simpler.
222 { "folder", "XFOLDER:" },
225 static prefix_t PROBABILISTIC_PREFIX[]= {
228 { "attachment", "XATTACHMENT" },
229 { "subject", "XSUBJECT"},
233 _find_prefix (const char *name)
237 for (i = 0; i < ARRAY_SIZE (BOOLEAN_PREFIX_INTERNAL); i++) {
238 if (strcmp (name, BOOLEAN_PREFIX_INTERNAL[i].name) == 0)
239 return BOOLEAN_PREFIX_INTERNAL[i].prefix;
242 for (i = 0; i < ARRAY_SIZE (BOOLEAN_PREFIX_EXTERNAL); i++) {
243 if (strcmp (name, BOOLEAN_PREFIX_EXTERNAL[i].name) == 0)
244 return BOOLEAN_PREFIX_EXTERNAL[i].prefix;
247 for (i = 0; i < ARRAY_SIZE (PROBABILISTIC_PREFIX); i++) {
248 if (strcmp (name, PROBABILISTIC_PREFIX[i].name) == 0)
249 return PROBABILISTIC_PREFIX[i].prefix;
252 INTERNAL_ERROR ("No prefix exists for '%s'\n", name);
258 notmuch_status_to_string (notmuch_status_t status)
261 case NOTMUCH_STATUS_SUCCESS:
262 return "No error occurred";
263 case NOTMUCH_STATUS_OUT_OF_MEMORY:
264 return "Out of memory";
265 case NOTMUCH_STATUS_READ_ONLY_DATABASE:
266 return "Attempt to write to a read-only database";
267 case NOTMUCH_STATUS_XAPIAN_EXCEPTION:
268 return "A Xapian exception occurred";
269 case NOTMUCH_STATUS_FILE_ERROR:
270 return "Something went wrong trying to read or write a file";
271 case NOTMUCH_STATUS_FILE_NOT_EMAIL:
272 return "File is not an email";
273 case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:
274 return "Message ID is identical to a message in database";
275 case NOTMUCH_STATUS_NULL_POINTER:
276 return "Erroneous NULL pointer";
277 case NOTMUCH_STATUS_TAG_TOO_LONG:
278 return "Tag value is too long (exceeds NOTMUCH_TAG_MAX)";
279 case NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW:
280 return "Unbalanced number of calls to notmuch_message_freeze/thaw";
281 case NOTMUCH_STATUS_UNBALANCED_ATOMIC:
282 return "Unbalanced number of calls to notmuch_database_begin_atomic/end_atomic";
283 case NOTMUCH_STATUS_UNSUPPORTED_OPERATION:
284 return "Unsupported operation";
286 case NOTMUCH_STATUS_LAST_STATUS:
287 return "Unknown error status value";
292 find_doc_ids_for_term (notmuch_database_t *notmuch,
294 Xapian::PostingIterator *begin,
295 Xapian::PostingIterator *end)
297 *begin = notmuch->xapian_db->postlist_begin (term);
299 *end = notmuch->xapian_db->postlist_end (term);
303 find_doc_ids (notmuch_database_t *notmuch,
304 const char *prefix_name,
306 Xapian::PostingIterator *begin,
307 Xapian::PostingIterator *end)
311 term = talloc_asprintf (notmuch, "%s%s",
312 _find_prefix (prefix_name), value);
314 find_doc_ids_for_term (notmuch, term, begin, end);
319 notmuch_private_status_t
320 _notmuch_database_find_unique_doc_id (notmuch_database_t *notmuch,
321 const char *prefix_name,
323 unsigned int *doc_id)
325 Xapian::PostingIterator i, end;
327 find_doc_ids (notmuch, prefix_name, value, &i, &end);
331 return NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
336 #if DEBUG_DATABASE_SANITY
340 INTERNAL_ERROR ("Term %s:%s is not unique as expected.\n",
344 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
347 static Xapian::Document
348 find_document_for_doc_id (notmuch_database_t *notmuch, unsigned doc_id)
350 return notmuch->xapian_db->get_document (doc_id);
353 /* Generate a compressed version of 'message_id' of the form:
355 * notmuch-sha1-<sha1_sum_of_message_id>
358 _message_id_compressed (void *ctx, const char *message_id)
360 char *sha1, *compressed;
362 sha1 = _notmuch_sha1_of_string (message_id);
364 compressed = talloc_asprintf (ctx, "notmuch-sha1-%s", sha1);
371 notmuch_database_find_message (notmuch_database_t *notmuch,
372 const char *message_id,
373 notmuch_message_t **message_ret)
375 notmuch_private_status_t status;
378 if (message_ret == NULL)
379 return NOTMUCH_STATUS_NULL_POINTER;
381 if (strlen (message_id) > NOTMUCH_MESSAGE_ID_MAX)
382 message_id = _message_id_compressed (notmuch, message_id);
385 status = _notmuch_database_find_unique_doc_id (notmuch, "id",
386 message_id, &doc_id);
388 if (status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND)
391 *message_ret = _notmuch_message_create (notmuch, notmuch, doc_id,
393 if (*message_ret == NULL)
394 return NOTMUCH_STATUS_OUT_OF_MEMORY;
397 return NOTMUCH_STATUS_SUCCESS;
398 } catch (const Xapian::Error &error) {
399 fprintf (stderr, "A Xapian exception occurred finding message: %s.\n",
400 error.get_msg().c_str());
401 notmuch->exception_reported = TRUE;
403 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
407 /* Advance 'str' past any whitespace or RFC 822 comments. A comment is
408 * a (potentially nested) parenthesized sequence with '\' used to
409 * escape any character (including parentheses).
411 * If the sequence to be skipped continues to the end of the string,
412 * then 'str' will be left pointing at the final terminating '\0'
416 skip_space_and_comments (const char **str)
421 while (*s && (isspace (*s) || *s == '(')) {
422 while (*s && isspace (*s))
427 while (*s && nesting) {
430 } else if (*s == ')') {
432 } else if (*s == '\\') {
444 /* Parse an RFC 822 message-id, discarding whitespace, any RFC 822
445 * comments, and the '<' and '>' delimiters.
447 * If not NULL, then *next will be made to point to the first character
448 * not parsed, (possibly pointing to the final '\0' terminator.
450 * Returns a newly talloc'ed string belonging to 'ctx'.
452 * Returns NULL if there is any error parsing the message-id. */
454 _parse_message_id (void *ctx, const char *message_id, const char **next)
459 if (message_id == NULL || *message_id == '\0')
464 skip_space_and_comments (&s);
466 /* Skip any unstructured text as well. */
467 while (*s && *s != '<')
478 skip_space_and_comments (&s);
481 while (*end && *end != '>')
490 if (end > s && *end == '>')
495 result = talloc_strndup (ctx, s, end - s + 1);
497 /* Finally, collapse any whitespace that is within the message-id
503 for (r = result, len = strlen (r); *r; r++, len--)
504 if (*r == ' ' || *r == '\t')
505 memmove (r, r+1, len);
511 /* Parse a References header value, putting a (talloc'ed under 'ctx')
512 * copy of each referenced message-id into 'hash'.
514 * We explicitly avoid including any reference identical to
515 * 'message_id' in the result (to avoid mass confusion when a single
516 * message references itself cyclically---and yes, mail messages are
517 * not infrequent in the wild that do this---don't ask me why).
519 * Return the last reference parsed, if it is not equal to message_id.
522 parse_references (void *ctx,
523 const char *message_id,
527 char *ref, *last_ref = NULL;
529 if (refs == NULL || *refs == '\0')
533 ref = _parse_message_id (ctx, refs, &refs);
535 if (ref && strcmp (ref, message_id)) {
536 g_hash_table_insert (hash, ref, NULL);
541 /* The return value of this function is used to add a parent
542 * reference to the database. We should avoid making a message
543 * its own parent, thus the above check.
549 notmuch_database_create (const char *path, notmuch_database_t **database)
551 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
552 notmuch_database_t *notmuch = NULL;
553 char *notmuch_path = NULL;
558 fprintf (stderr, "Error: Cannot create a database for a NULL path.\n");
559 status = NOTMUCH_STATUS_NULL_POINTER;
563 err = stat (path, &st);
565 fprintf (stderr, "Error: Cannot create database at %s: %s.\n",
566 path, strerror (errno));
567 status = NOTMUCH_STATUS_FILE_ERROR;
571 if (! S_ISDIR (st.st_mode)) {
572 fprintf (stderr, "Error: Cannot create database at %s: Not a directory.\n",
574 status = NOTMUCH_STATUS_FILE_ERROR;
578 notmuch_path = talloc_asprintf (NULL, "%s/%s", path, ".notmuch");
580 err = mkdir (notmuch_path, 0755);
583 fprintf (stderr, "Error: Cannot create directory %s: %s.\n",
584 notmuch_path, strerror (errno));
585 status = NOTMUCH_STATUS_FILE_ERROR;
589 status = notmuch_database_open (path,
590 NOTMUCH_DATABASE_MODE_READ_WRITE,
594 status = notmuch_database_upgrade (notmuch, NULL, NULL);
596 notmuch_database_close(notmuch);
602 talloc_free (notmuch_path);
607 talloc_free (notmuch);
612 _notmuch_database_ensure_writable (notmuch_database_t *notmuch)
614 if (notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY) {
615 fprintf (stderr, "Cannot write to a read-only database.\n");
616 return NOTMUCH_STATUS_READ_ONLY_DATABASE;
619 return NOTMUCH_STATUS_SUCCESS;
623 notmuch_database_open (const char *path,
624 notmuch_database_mode_t mode,
625 notmuch_database_t **database)
627 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
628 void *local = talloc_new (NULL);
629 notmuch_database_t *notmuch = NULL;
630 char *notmuch_path, *xapian_path;
633 unsigned int i, version;
634 static int initialized = 0;
637 fprintf (stderr, "Error: Cannot open a database for a NULL path.\n");
638 status = NOTMUCH_STATUS_NULL_POINTER;
642 if (! (notmuch_path = talloc_asprintf (local, "%s/%s", path, ".notmuch"))) {
643 fprintf (stderr, "Out of memory\n");
644 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
648 err = stat (notmuch_path, &st);
650 fprintf (stderr, "Error opening database at %s: %s\n",
651 notmuch_path, strerror (errno));
652 status = NOTMUCH_STATUS_FILE_ERROR;
656 if (! (xapian_path = talloc_asprintf (local, "%s/%s", notmuch_path, "xapian"))) {
657 fprintf (stderr, "Out of memory\n");
658 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
662 /* Initialize the GLib type system and threads */
663 #if !GLIB_CHECK_VERSION(2, 35, 1)
667 /* Initialize gmime */
669 g_mime_init (GMIME_ENABLE_RFC2047_WORKAROUNDS);
673 notmuch = talloc_zero (NULL, notmuch_database_t);
674 notmuch->exception_reported = FALSE;
675 notmuch->path = talloc_strdup (notmuch, path);
677 if (notmuch->path[strlen (notmuch->path) - 1] == '/')
678 notmuch->path[strlen (notmuch->path) - 1] = '\0';
680 notmuch->needs_upgrade = FALSE;
681 notmuch->mode = mode;
682 notmuch->atomic_nesting = 0;
684 string last_thread_id;
686 if (mode == NOTMUCH_DATABASE_MODE_READ_WRITE) {
687 notmuch->xapian_db = new Xapian::WritableDatabase (xapian_path,
688 Xapian::DB_CREATE_OR_OPEN);
689 version = notmuch_database_get_version (notmuch);
691 if (version > NOTMUCH_DATABASE_VERSION) {
693 "Error: Notmuch database at %s\n"
694 " has a newer database format version (%u) than supported by this\n"
695 " version of notmuch (%u). Refusing to open this database in\n"
696 " read-write mode.\n",
697 notmuch_path, version, NOTMUCH_DATABASE_VERSION);
698 notmuch->mode = NOTMUCH_DATABASE_MODE_READ_ONLY;
699 notmuch_database_destroy (notmuch);
701 status = NOTMUCH_STATUS_FILE_ERROR;
705 if (version < NOTMUCH_DATABASE_VERSION)
706 notmuch->needs_upgrade = TRUE;
708 notmuch->xapian_db = new Xapian::Database (xapian_path);
709 version = notmuch_database_get_version (notmuch);
710 if (version > NOTMUCH_DATABASE_VERSION)
713 "Warning: Notmuch database at %s\n"
714 " has a newer database format version (%u) than supported by this\n"
715 " version of notmuch (%u). Some operations may behave incorrectly,\n"
716 " (but the database will not be harmed since it is being opened\n"
717 " in read-only mode).\n",
718 notmuch_path, version, NOTMUCH_DATABASE_VERSION);
722 notmuch->last_doc_id = notmuch->xapian_db->get_lastdocid ();
723 last_thread_id = notmuch->xapian_db->get_metadata ("last_thread_id");
724 if (last_thread_id.empty ()) {
725 notmuch->last_thread_id = 0;
730 str = last_thread_id.c_str ();
731 notmuch->last_thread_id = strtoull (str, &end, 16);
733 INTERNAL_ERROR ("Malformed database last_thread_id: %s", str);
736 notmuch->query_parser = new Xapian::QueryParser;
737 notmuch->term_gen = new Xapian::TermGenerator;
738 notmuch->term_gen->set_stemmer (Xapian::Stem ("english"));
739 notmuch->value_range_processor = new Xapian::NumberValueRangeProcessor (NOTMUCH_VALUE_TIMESTAMP);
740 notmuch->date_range_processor = new ParseTimeValueRangeProcessor (NOTMUCH_VALUE_TIMESTAMP);
742 notmuch->query_parser->set_default_op (Xapian::Query::OP_AND);
743 notmuch->query_parser->set_database (*notmuch->xapian_db);
744 notmuch->query_parser->set_stemmer (Xapian::Stem ("english"));
745 notmuch->query_parser->set_stemming_strategy (Xapian::QueryParser::STEM_SOME);
746 notmuch->query_parser->add_valuerangeprocessor (notmuch->value_range_processor);
747 notmuch->query_parser->add_valuerangeprocessor (notmuch->date_range_processor);
749 for (i = 0; i < ARRAY_SIZE (BOOLEAN_PREFIX_EXTERNAL); i++) {
750 prefix_t *prefix = &BOOLEAN_PREFIX_EXTERNAL[i];
751 notmuch->query_parser->add_boolean_prefix (prefix->name,
755 for (i = 0; i < ARRAY_SIZE (PROBABILISTIC_PREFIX); i++) {
756 prefix_t *prefix = &PROBABILISTIC_PREFIX[i];
757 notmuch->query_parser->add_prefix (prefix->name, prefix->prefix);
759 } catch (const Xapian::Error &error) {
760 fprintf (stderr, "A Xapian exception occurred opening database: %s\n",
761 error.get_msg().c_str());
762 notmuch_database_destroy (notmuch);
764 status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
773 talloc_free (notmuch);
778 notmuch_database_close (notmuch_database_t *notmuch)
780 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
783 if (notmuch->xapian_db != NULL &&
784 notmuch->mode == NOTMUCH_DATABASE_MODE_READ_WRITE)
785 (static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db))->flush ();
786 } catch (const Xapian::Error &error) {
787 status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
788 if (! notmuch->exception_reported) {
789 fprintf (stderr, "Error: A Xapian exception occurred flushing database: %s\n",
790 error.get_msg().c_str());
794 /* Many Xapian objects (and thus notmuch objects) hold references to
795 * the database, so merely deleting the database may not suffice to
796 * close it. Thus, we explicitly close it here. */
797 if (notmuch->xapian_db != NULL) {
799 notmuch->xapian_db->close();
800 } catch (const Xapian::Error &error) {
801 /* don't clobber previous error status */
802 if (status == NOTMUCH_STATUS_SUCCESS)
803 status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
807 delete notmuch->term_gen;
808 notmuch->term_gen = NULL;
809 delete notmuch->query_parser;
810 notmuch->query_parser = NULL;
811 delete notmuch->xapian_db;
812 notmuch->xapian_db = NULL;
813 delete notmuch->value_range_processor;
814 notmuch->value_range_processor = NULL;
815 delete notmuch->date_range_processor;
816 notmuch->date_range_processor = NULL;
821 #if HAVE_XAPIAN_COMPACT
823 unlink_cb (const char *path,
824 unused (const struct stat *sb),
826 unused (struct FTW *ftw))
828 return remove (path);
832 rmtree (const char *path)
834 return nftw (path, unlink_cb, 64, FTW_DEPTH | FTW_PHYS);
837 class NotmuchCompactor : public Xapian::Compactor
839 notmuch_compact_status_cb_t status_cb;
840 void *status_closure;
843 NotmuchCompactor(notmuch_compact_status_cb_t cb, void *closure) :
844 status_cb (cb), status_closure (closure) { }
847 set_status (const std::string &table, const std::string &status)
851 if (status_cb == NULL)
854 if (status.length () == 0)
855 msg = talloc_asprintf (NULL, "compacting table %s", table.c_str());
857 msg = talloc_asprintf (NULL, " %s", status.c_str());
863 status_cb (msg, status_closure);
868 /* Compacts the given database, optionally saving the original database
869 * in backup_path. Additionally, a callback function can be provided to
870 * give the user feedback on the progress of the (likely long-lived)
871 * compaction process.
873 * The backup path must point to a directory on the same volume as the
874 * original database. Passing a NULL backup_path will result in the
875 * uncompacted database being deleted after compaction has finished.
876 * Note that the database write lock will be held during the
877 * compaction process to protect data integrity.
880 notmuch_database_compact (const char *path,
881 const char *backup_path,
882 notmuch_compact_status_cb_t status_cb,
886 char *notmuch_path, *xapian_path, *compact_xapian_path;
887 notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
888 notmuch_database_t *notmuch = NULL;
890 notmuch_bool_t keep_backup;
892 local = talloc_new (NULL);
894 return NOTMUCH_STATUS_OUT_OF_MEMORY;
896 ret = notmuch_database_open (path, NOTMUCH_DATABASE_MODE_READ_WRITE, ¬much);
901 if (! (notmuch_path = talloc_asprintf (local, "%s/%s", path, ".notmuch"))) {
902 ret = NOTMUCH_STATUS_OUT_OF_MEMORY;
906 if (! (xapian_path = talloc_asprintf (local, "%s/%s", notmuch_path, "xapian"))) {
907 ret = NOTMUCH_STATUS_OUT_OF_MEMORY;
911 if (! (compact_xapian_path = talloc_asprintf (local, "%s.compact", xapian_path))) {
912 ret = NOTMUCH_STATUS_OUT_OF_MEMORY;
916 if (backup_path == NULL) {
917 if (! (backup_path = talloc_asprintf (local, "%s.old", xapian_path))) {
918 ret = NOTMUCH_STATUS_OUT_OF_MEMORY;
927 if (stat (backup_path, &statbuf) != -1) {
928 fprintf (stderr, "Path already exists: %s\n", backup_path);
929 ret = NOTMUCH_STATUS_FILE_ERROR;
932 if (errno != ENOENT) {
933 fprintf (stderr, "Unknown error while stat()ing path: %s\n",
935 ret = NOTMUCH_STATUS_FILE_ERROR;
939 /* Unconditionally attempt to remove old work-in-progress database (if
940 * any). This is "protected" by database lock. If this fails due to write
941 * errors (etc), the following code will fail and provide error message.
943 (void) rmtree (compact_xapian_path);
946 NotmuchCompactor compactor (status_cb, closure);
948 compactor.set_renumber (false);
949 compactor.add_source (xapian_path);
950 compactor.set_destdir (compact_xapian_path);
951 compactor.compact ();
952 } catch (const Xapian::Error &error) {
953 fprintf (stderr, "Error while compacting: %s\n", error.get_msg().c_str());
954 ret = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
958 if (rename (xapian_path, backup_path)) {
959 fprintf (stderr, "Error moving %s to %s: %s\n",
960 xapian_path, backup_path, strerror (errno));
961 ret = NOTMUCH_STATUS_FILE_ERROR;
965 if (rename (compact_xapian_path, xapian_path)) {
966 fprintf (stderr, "Error moving %s to %s: %s\n",
967 compact_xapian_path, xapian_path, strerror (errno));
968 ret = NOTMUCH_STATUS_FILE_ERROR;
973 if (rmtree (backup_path)) {
974 fprintf (stderr, "Error removing old database %s: %s\n",
975 backup_path, strerror (errno));
976 ret = NOTMUCH_STATUS_FILE_ERROR;
983 notmuch_status_t ret2;
985 ret2 = notmuch_database_destroy (notmuch);
987 /* don't clobber previous error status */
988 if (ret == NOTMUCH_STATUS_SUCCESS && ret2 != NOTMUCH_STATUS_SUCCESS)
998 notmuch_database_compact (unused (const char *path),
999 unused (const char *backup_path),
1000 unused (notmuch_compact_status_cb_t status_cb),
1001 unused (void *closure))
1003 fprintf (stderr, "notmuch was compiled against a xapian version lacking compaction support.\n");
1004 return NOTMUCH_STATUS_UNSUPPORTED_OPERATION;
1009 notmuch_database_destroy (notmuch_database_t *notmuch)
1011 notmuch_status_t status;
1013 status = notmuch_database_close (notmuch);
1014 talloc_free (notmuch);
1020 notmuch_database_get_path (notmuch_database_t *notmuch)
1022 return notmuch->path;
1026 notmuch_database_get_version (notmuch_database_t *notmuch)
1028 unsigned int version;
1029 string version_string;
1033 version_string = notmuch->xapian_db->get_metadata ("version");
1034 if (version_string.empty ())
1037 str = version_string.c_str ();
1038 if (str == NULL || *str == '\0')
1041 version = strtoul (str, &end, 10);
1043 INTERNAL_ERROR ("Malformed database version: %s", str);
1049 notmuch_database_needs_upgrade (notmuch_database_t *notmuch)
1051 return notmuch->needs_upgrade;
1054 static volatile sig_atomic_t do_progress_notify = 0;
1057 handle_sigalrm (unused (int signal))
1059 do_progress_notify = 1;
1062 /* Upgrade the current database.
1064 * After opening a database in read-write mode, the client should
1065 * check if an upgrade is needed (notmuch_database_needs_upgrade) and
1066 * if so, upgrade with this function before making any modifications.
1068 * The optional progress_notify callback can be used by the caller to
1069 * provide progress indication to the user. If non-NULL it will be
1070 * called periodically with 'count' as the number of messages upgraded
1071 * so far and 'total' the overall number of messages that will be
1075 notmuch_database_upgrade (notmuch_database_t *notmuch,
1076 void (*progress_notify) (void *closure,
1080 Xapian::WritableDatabase *db;
1081 struct sigaction action;
1082 struct itimerval timerval;
1083 notmuch_bool_t timer_is_active = FALSE;
1084 unsigned int version;
1085 notmuch_status_t status;
1086 unsigned int count = 0, total = 0;
1088 status = _notmuch_database_ensure_writable (notmuch);
1092 db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
1094 version = notmuch_database_get_version (notmuch);
1096 if (version >= NOTMUCH_DATABASE_VERSION)
1097 return NOTMUCH_STATUS_SUCCESS;
1099 if (progress_notify) {
1100 /* Setup our handler for SIGALRM */
1101 memset (&action, 0, sizeof (struct sigaction));
1102 action.sa_handler = handle_sigalrm;
1103 sigemptyset (&action.sa_mask);
1104 action.sa_flags = SA_RESTART;
1105 sigaction (SIGALRM, &action, NULL);
1107 /* Then start a timer to send SIGALRM once per second. */
1108 timerval.it_interval.tv_sec = 1;
1109 timerval.it_interval.tv_usec = 0;
1110 timerval.it_value.tv_sec = 1;
1111 timerval.it_value.tv_usec = 0;
1112 setitimer (ITIMER_REAL, &timerval, NULL);
1114 timer_is_active = TRUE;
1117 /* Before version 1, each message document had its filename in the
1118 * data field. Copy that into the new format by calling
1119 * notmuch_message_add_filename.
1122 notmuch_query_t *query = notmuch_query_create (notmuch, "");
1123 notmuch_messages_t *messages;
1124 notmuch_message_t *message;
1126 Xapian::TermIterator t, t_end;
1128 total = notmuch_query_count_messages (query);
1130 for (messages = notmuch_query_search_messages (query);
1131 notmuch_messages_valid (messages);
1132 notmuch_messages_move_to_next (messages))
1134 if (do_progress_notify) {
1135 progress_notify (closure, (double) count / total);
1136 do_progress_notify = 0;
1139 message = notmuch_messages_get (messages);
1141 filename = _notmuch_message_talloc_copy_data (message);
1142 if (filename && *filename != '\0') {
1143 _notmuch_message_add_filename (message, filename);
1144 _notmuch_message_sync (message);
1146 talloc_free (filename);
1148 notmuch_message_destroy (message);
1153 notmuch_query_destroy (query);
1155 /* Also, before version 1 we stored directory timestamps in
1156 * XTIMESTAMP documents instead of the current XDIRECTORY
1157 * documents. So copy those as well. */
1159 t_end = notmuch->xapian_db->allterms_end ("XTIMESTAMP");
1161 for (t = notmuch->xapian_db->allterms_begin ("XTIMESTAMP");
1165 Xapian::PostingIterator p, p_end;
1166 std::string term = *t;
1168 p_end = notmuch->xapian_db->postlist_end (term);
1170 for (p = notmuch->xapian_db->postlist_begin (term);
1174 Xapian::Document document;
1176 notmuch_directory_t *directory;
1178 if (do_progress_notify) {
1179 progress_notify (closure, (double) count / total);
1180 do_progress_notify = 0;
1183 document = find_document_for_doc_id (notmuch, *p);
1184 mtime = Xapian::sortable_unserialise (
1185 document.get_value (NOTMUCH_VALUE_TIMESTAMP));
1187 directory = _notmuch_directory_create (notmuch, term.c_str() + 10,
1188 NOTMUCH_FIND_CREATE, &status);
1189 notmuch_directory_set_mtime (directory, mtime);
1190 notmuch_directory_destroy (directory);
1196 * Prior to version 2, the "folder:" prefix was probabilistic and
1197 * stemmed. Change it to the current boolean prefix. Add "path:"
1198 * prefixes while at it.
1201 notmuch_query_t *query = notmuch_query_create (notmuch, "");
1202 notmuch_messages_t *messages;
1203 notmuch_message_t *message;
1206 total = notmuch_query_count_messages (query);
1208 for (messages = notmuch_query_search_messages (query);
1209 notmuch_messages_valid (messages);
1210 notmuch_messages_move_to_next (messages)) {
1211 if (do_progress_notify) {
1212 progress_notify (closure, (double) count / total);
1213 do_progress_notify = 0;
1216 message = notmuch_messages_get (messages);
1218 _notmuch_message_upgrade_folder (message);
1219 _notmuch_message_sync (message);
1221 notmuch_message_destroy (message);
1226 notmuch_query_destroy (query);
1229 db->set_metadata ("version", STRINGIFY (NOTMUCH_DATABASE_VERSION));
1232 /* Now that the upgrade is complete we can remove the old data
1233 * and documents that are no longer needed. */
1235 notmuch_query_t *query = notmuch_query_create (notmuch, "");
1236 notmuch_messages_t *messages;
1237 notmuch_message_t *message;
1240 for (messages = notmuch_query_search_messages (query);
1241 notmuch_messages_valid (messages);
1242 notmuch_messages_move_to_next (messages))
1244 if (do_progress_notify) {
1245 progress_notify (closure, (double) count / total);
1246 do_progress_notify = 0;
1249 message = notmuch_messages_get (messages);
1251 filename = _notmuch_message_talloc_copy_data (message);
1252 if (filename && *filename != '\0') {
1253 _notmuch_message_clear_data (message);
1254 _notmuch_message_sync (message);
1256 talloc_free (filename);
1258 notmuch_message_destroy (message);
1261 notmuch_query_destroy (query);
1265 Xapian::TermIterator t, t_end;
1267 t_end = notmuch->xapian_db->allterms_end ("XTIMESTAMP");
1269 for (t = notmuch->xapian_db->allterms_begin ("XTIMESTAMP");
1273 Xapian::PostingIterator p, p_end;
1274 std::string term = *t;
1276 p_end = notmuch->xapian_db->postlist_end (term);
1278 for (p = notmuch->xapian_db->postlist_begin (term);
1282 if (do_progress_notify) {
1283 progress_notify (closure, (double) count / total);
1284 do_progress_notify = 0;
1287 db->delete_document (*p);
1292 if (timer_is_active) {
1293 /* Now stop the timer. */
1294 timerval.it_interval.tv_sec = 0;
1295 timerval.it_interval.tv_usec = 0;
1296 timerval.it_value.tv_sec = 0;
1297 timerval.it_value.tv_usec = 0;
1298 setitimer (ITIMER_REAL, &timerval, NULL);
1300 /* And disable the signal handler. */
1301 action.sa_handler = SIG_IGN;
1302 sigaction (SIGALRM, &action, NULL);
1305 return NOTMUCH_STATUS_SUCCESS;
1309 notmuch_database_begin_atomic (notmuch_database_t *notmuch)
1311 if (notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY ||
1312 notmuch->atomic_nesting > 0)
1316 (static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db))->begin_transaction (false);
1317 } catch (const Xapian::Error &error) {
1318 fprintf (stderr, "A Xapian exception occurred beginning transaction: %s.\n",
1319 error.get_msg().c_str());
1320 notmuch->exception_reported = TRUE;
1321 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
1325 notmuch->atomic_nesting++;
1326 return NOTMUCH_STATUS_SUCCESS;
1330 notmuch_database_end_atomic (notmuch_database_t *notmuch)
1332 Xapian::WritableDatabase *db;
1334 if (notmuch->atomic_nesting == 0)
1335 return NOTMUCH_STATUS_UNBALANCED_ATOMIC;
1337 if (notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY ||
1338 notmuch->atomic_nesting > 1)
1341 db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
1343 db->commit_transaction ();
1345 /* This is a hack for testing. Xapian never flushes on a
1346 * non-flushed commit, even if the flush threshold is 1.
1347 * However, we rely on flushing to test atomicity. */
1348 const char *thresh = getenv ("XAPIAN_FLUSH_THRESHOLD");
1349 if (thresh && atoi (thresh) == 1)
1351 } catch (const Xapian::Error &error) {
1352 fprintf (stderr, "A Xapian exception occurred committing transaction: %s.\n",
1353 error.get_msg().c_str());
1354 notmuch->exception_reported = TRUE;
1355 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
1359 notmuch->atomic_nesting--;
1360 return NOTMUCH_STATUS_SUCCESS;
1363 /* We allow the user to use arbitrarily long paths for directories. But
1364 * we have a term-length limit. So if we exceed that, we'll use the
1365 * SHA-1 of the path for the database term.
1367 * Note: This function may return the original value of 'path'. If it
1368 * does not, then the caller is responsible to free() the returned
1372 _notmuch_database_get_directory_db_path (const char *path)
1374 int term_len = strlen (_find_prefix ("directory")) + strlen (path);
1376 if (term_len > NOTMUCH_TERM_MAX)
1377 return _notmuch_sha1_of_string (path);
1382 /* Given a path, split it into two parts: the directory part is all
1383 * components except for the last, and the basename is that last
1384 * component. Getting the return-value for either part is optional
1385 * (the caller can pass NULL).
1387 * The original 'path' can represent either a regular file or a
1388 * directory---the splitting will be carried out in the same way in
1389 * either case. Trailing slashes on 'path' will be ignored, and any
1390 * cases of multiple '/' characters appearing in series will be
1391 * treated as a single '/'.
1393 * Allocation (if any) will have 'ctx' as the talloc owner. But
1394 * pointers will be returned within the original path string whenever
1397 * Note: If 'path' is non-empty and contains no non-trailing slash,
1398 * (that is, consists of a filename with no parent directory), then
1399 * the directory returned will be an empty string. However, if 'path'
1400 * is an empty string, then both directory and basename will be
1404 _notmuch_database_split_path (void *ctx,
1406 const char **directory,
1407 const char **basename)
1411 if (path == NULL || *path == '\0') {
1416 return NOTMUCH_STATUS_SUCCESS;
1419 /* Find the last slash (not counting a trailing slash), if any. */
1421 slash = path + strlen (path) - 1;
1423 /* First, skip trailing slashes. */
1424 while (slash != path) {
1431 /* Then, find a slash. */
1432 while (slash != path) {
1442 /* Finally, skip multiple slashes. */
1443 while (slash != path) {
1450 if (slash == path) {
1452 *directory = talloc_strdup (ctx, "");
1457 *directory = talloc_strndup (ctx, path, slash - path + 1);
1460 return NOTMUCH_STATUS_SUCCESS;
1463 /* Find the document ID of the specified directory.
1465 * If (flags & NOTMUCH_FIND_CREATE), a new directory document will be
1466 * created if one does not exist for 'path'. Otherwise, if the
1467 * directory document does not exist, this sets *directory_id to
1468 * ((unsigned int)-1) and returns NOTMUCH_STATUS_SUCCESS.
1471 _notmuch_database_find_directory_id (notmuch_database_t *notmuch,
1473 notmuch_find_flags_t flags,
1474 unsigned int *directory_id)
1476 notmuch_directory_t *directory;
1477 notmuch_status_t status;
1481 return NOTMUCH_STATUS_SUCCESS;
1484 directory = _notmuch_directory_create (notmuch, path, flags, &status);
1485 if (status || !directory) {
1490 *directory_id = _notmuch_directory_get_document_id (directory);
1492 notmuch_directory_destroy (directory);
1494 return NOTMUCH_STATUS_SUCCESS;
1498 _notmuch_database_get_directory_path (void *ctx,
1499 notmuch_database_t *notmuch,
1500 unsigned int doc_id)
1502 Xapian::Document document;
1504 document = find_document_for_doc_id (notmuch, doc_id);
1506 return talloc_strdup (ctx, document.get_data ().c_str ());
1509 /* Given a legal 'filename' for the database, (either relative to
1510 * database path or absolute with initial components identical to
1511 * database path), return a new string (with 'ctx' as the talloc
1512 * owner) suitable for use as a direntry term value.
1514 * If (flags & NOTMUCH_FIND_CREATE), the necessary directory documents
1515 * will be created in the database as needed. Otherwise, if the
1516 * necessary directory documents do not exist, this sets
1517 * *direntry to NULL and returns NOTMUCH_STATUS_SUCCESS.
1520 _notmuch_database_filename_to_direntry (void *ctx,
1521 notmuch_database_t *notmuch,
1522 const char *filename,
1523 notmuch_find_flags_t flags,
1526 const char *relative, *directory, *basename;
1527 Xapian::docid directory_id;
1528 notmuch_status_t status;
1530 relative = _notmuch_database_relative_path (notmuch, filename);
1532 status = _notmuch_database_split_path (ctx, relative,
1533 &directory, &basename);
1537 status = _notmuch_database_find_directory_id (notmuch, directory, flags,
1539 if (status || directory_id == (unsigned int)-1) {
1544 *direntry = talloc_asprintf (ctx, "%u:%s", directory_id, basename);
1546 return NOTMUCH_STATUS_SUCCESS;
1549 /* Given a legal 'path' for the database, return the relative path.
1551 * The return value will be a pointer to the original path contents,
1552 * and will be either the original string (if 'path' was relative) or
1553 * a portion of the string (if path was absolute and begins with the
1557 _notmuch_database_relative_path (notmuch_database_t *notmuch,
1560 const char *db_path, *relative;
1561 unsigned int db_path_len;
1563 db_path = notmuch_database_get_path (notmuch);
1564 db_path_len = strlen (db_path);
1568 if (*relative == '/') {
1569 while (*relative == '/' && *(relative+1) == '/')
1572 if (strncmp (relative, db_path, db_path_len) == 0)
1574 relative += db_path_len;
1575 while (*relative == '/')
1584 notmuch_database_get_directory (notmuch_database_t *notmuch,
1586 notmuch_directory_t **directory)
1588 notmuch_status_t status;
1590 if (directory == NULL)
1591 return NOTMUCH_STATUS_NULL_POINTER;
1595 *directory = _notmuch_directory_create (notmuch, path,
1596 NOTMUCH_FIND_LOOKUP, &status);
1597 } catch (const Xapian::Error &error) {
1598 fprintf (stderr, "A Xapian exception occurred getting directory: %s.\n",
1599 error.get_msg().c_str());
1600 notmuch->exception_reported = TRUE;
1601 status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
1606 /* Allocate a document ID that satisfies the following criteria:
1608 * 1. The ID does not exist for any document in the Xapian database
1610 * 2. The ID was not previously returned from this function
1612 * 3. The ID is the smallest integer satisfying (1) and (2)
1614 * This function will trigger an internal error if these constraints
1615 * cannot all be satisfied, (that is, the pool of available document
1616 * IDs has been exhausted).
1619 _notmuch_database_generate_doc_id (notmuch_database_t *notmuch)
1621 assert (notmuch->last_doc_id >= notmuch->xapian_db->get_lastdocid ());
1623 notmuch->last_doc_id++;
1625 if (notmuch->last_doc_id == 0)
1626 INTERNAL_ERROR ("Xapian document IDs are exhausted.\n");
1628 return notmuch->last_doc_id;
1632 _notmuch_database_generate_thread_id (notmuch_database_t *notmuch)
1634 /* 16 bytes (+ terminator) for hexadecimal representation of
1635 * a 64-bit integer. */
1636 static char thread_id[17];
1637 Xapian::WritableDatabase *db;
1639 db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
1641 notmuch->last_thread_id++;
1643 sprintf (thread_id, "%016" PRIx64, notmuch->last_thread_id);
1645 db->set_metadata ("last_thread_id", thread_id);
1651 _get_metadata_thread_id_key (void *ctx, const char *message_id)
1653 if (strlen (message_id) > NOTMUCH_MESSAGE_ID_MAX)
1654 message_id = _message_id_compressed (ctx, message_id);
1656 return talloc_asprintf (ctx, NOTMUCH_METADATA_THREAD_ID_PREFIX "%s",
1660 /* Find the thread ID to which the message with 'message_id' belongs.
1662 * Note: 'thread_id_ret' must not be NULL!
1663 * On success '*thread_id_ret' is set to a newly talloced string belonging to
1666 * Note: If there is no message in the database with the given
1667 * 'message_id' then a new thread_id will be allocated for this
1668 * message and stored in the database metadata, (where this same
1669 * thread ID can be looked up if the message is added to the database
1672 static notmuch_status_t
1673 _resolve_message_id_to_thread_id (notmuch_database_t *notmuch,
1675 const char *message_id,
1676 const char **thread_id_ret)
1678 notmuch_status_t status;
1679 notmuch_message_t *message;
1680 string thread_id_string;
1682 Xapian::WritableDatabase *db;
1684 status = notmuch_database_find_message (notmuch, message_id, &message);
1690 *thread_id_ret = talloc_steal (ctx,
1691 notmuch_message_get_thread_id (message));
1693 notmuch_message_destroy (message);
1695 return NOTMUCH_STATUS_SUCCESS;
1698 /* Message has not been seen yet.
1700 * We may have seen a reference to it already, in which case, we
1701 * can return the thread ID stored in the metadata. Otherwise, we
1702 * generate a new thread ID and store it there.
1704 db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
1705 metadata_key = _get_metadata_thread_id_key (ctx, message_id);
1706 thread_id_string = notmuch->xapian_db->get_metadata (metadata_key);
1708 if (thread_id_string.empty()) {
1709 *thread_id_ret = talloc_strdup (ctx,
1710 _notmuch_database_generate_thread_id (notmuch));
1711 db->set_metadata (metadata_key, *thread_id_ret);
1713 *thread_id_ret = talloc_strdup (ctx, thread_id_string.c_str());
1716 talloc_free (metadata_key);
1718 return NOTMUCH_STATUS_SUCCESS;
1721 static notmuch_status_t
1722 _merge_threads (notmuch_database_t *notmuch,
1723 const char *winner_thread_id,
1724 const char *loser_thread_id)
1726 Xapian::PostingIterator loser, loser_end;
1727 notmuch_message_t *message = NULL;
1728 notmuch_private_status_t private_status;
1729 notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
1731 find_doc_ids (notmuch, "thread", loser_thread_id, &loser, &loser_end);
1733 for ( ; loser != loser_end; loser++) {
1734 message = _notmuch_message_create (notmuch, notmuch,
1735 *loser, &private_status);
1736 if (message == NULL) {
1737 ret = COERCE_STATUS (private_status,
1738 "Cannot find document for doc_id from query");
1742 _notmuch_message_remove_term (message, "thread", loser_thread_id);
1743 _notmuch_message_add_term (message, "thread", winner_thread_id);
1744 _notmuch_message_sync (message);
1746 notmuch_message_destroy (message);
1752 notmuch_message_destroy (message);
1758 _my_talloc_free_for_g_hash (void *ptr)
1763 static notmuch_status_t
1764 _notmuch_database_link_message_to_parents (notmuch_database_t *notmuch,
1765 notmuch_message_t *message,
1766 notmuch_message_file_t *message_file,
1767 const char **thread_id)
1769 GHashTable *parents = NULL;
1770 const char *refs, *in_reply_to, *in_reply_to_message_id;
1771 const char *last_ref_message_id, *this_message_id;
1772 GList *l, *keys = NULL;
1773 notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
1775 parents = g_hash_table_new_full (g_str_hash, g_str_equal,
1776 _my_talloc_free_for_g_hash, NULL);
1777 this_message_id = notmuch_message_get_message_id (message);
1779 refs = _notmuch_message_file_get_header (message_file, "references");
1780 last_ref_message_id = parse_references (message,
1784 in_reply_to = _notmuch_message_file_get_header (message_file, "in-reply-to");
1785 in_reply_to_message_id = parse_references (message,
1787 parents, in_reply_to);
1789 /* For the parent of this message, use the last message ID of the
1790 * References header, if available. If not, fall back to the
1791 * first message ID in the In-Reply-To header. */
1792 if (last_ref_message_id) {
1793 _notmuch_message_add_term (message, "replyto",
1794 last_ref_message_id);
1795 } else if (in_reply_to_message_id) {
1796 _notmuch_message_add_term (message, "replyto",
1797 in_reply_to_message_id);
1800 keys = g_hash_table_get_keys (parents);
1801 for (l = keys; l; l = l->next) {
1802 char *parent_message_id;
1803 const char *parent_thread_id = NULL;
1805 parent_message_id = (char *) l->data;
1807 _notmuch_message_add_term (message, "reference",
1810 ret = _resolve_message_id_to_thread_id (notmuch,
1817 if (*thread_id == NULL) {
1818 *thread_id = talloc_strdup (message, parent_thread_id);
1819 _notmuch_message_add_term (message, "thread", *thread_id);
1820 } else if (strcmp (*thread_id, parent_thread_id)) {
1821 ret = _merge_threads (notmuch, *thread_id, parent_thread_id);
1831 g_hash_table_unref (parents);
1836 static notmuch_status_t
1837 _notmuch_database_link_message_to_children (notmuch_database_t *notmuch,
1838 notmuch_message_t *message,
1839 const char **thread_id)
1841 const char *message_id = notmuch_message_get_message_id (message);
1842 Xapian::PostingIterator child, children_end;
1843 notmuch_message_t *child_message = NULL;
1844 const char *child_thread_id;
1845 notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
1846 notmuch_private_status_t private_status;
1848 find_doc_ids (notmuch, "reference", message_id, &child, &children_end);
1850 for ( ; child != children_end; child++) {
1852 child_message = _notmuch_message_create (message, notmuch,
1853 *child, &private_status);
1854 if (child_message == NULL) {
1855 ret = COERCE_STATUS (private_status,
1856 "Cannot find document for doc_id from query");
1860 child_thread_id = notmuch_message_get_thread_id (child_message);
1861 if (*thread_id == NULL) {
1862 *thread_id = talloc_strdup (message, child_thread_id);
1863 _notmuch_message_add_term (message, "thread", *thread_id);
1864 } else if (strcmp (*thread_id, child_thread_id)) {
1865 _notmuch_message_remove_term (child_message, "reference",
1867 _notmuch_message_sync (child_message);
1868 ret = _merge_threads (notmuch, *thread_id, child_thread_id);
1873 notmuch_message_destroy (child_message);
1874 child_message = NULL;
1879 notmuch_message_destroy (child_message);
1884 /* Given a (mostly empty) 'message' and its corresponding
1885 * 'message_file' link it to existing threads in the database.
1887 * The first check is in the metadata of the database to see if we
1888 * have pre-allocated a thread_id in advance for this message, (which
1889 * would have happened if a message was previously added that
1890 * referenced this one).
1892 * Second, we look at 'message_file' and its link-relevant headers
1893 * (References and In-Reply-To) for message IDs.
1895 * Finally, we look in the database for existing message that
1896 * reference 'message'.
1898 * In all cases, we assign to the current message the first thread_id
1899 * found (through either parent or child). We will also merge any
1900 * existing, distinct threads where this message belongs to both,
1901 * (which is not uncommon when messages are processed out of order).
1903 * Finally, if no thread ID has been found through parent or child, we
1904 * call _notmuch_message_generate_thread_id to generate a new thread
1905 * ID. This should only happen for new, top-level messages, (no
1906 * References or In-Reply-To header in this message, and no previously
1907 * added message refers to this message).
1909 static notmuch_status_t
1910 _notmuch_database_link_message (notmuch_database_t *notmuch,
1911 notmuch_message_t *message,
1912 notmuch_message_file_t *message_file)
1914 notmuch_status_t status;
1915 const char *message_id, *thread_id = NULL;
1919 message_id = notmuch_message_get_message_id (message);
1920 metadata_key = _get_metadata_thread_id_key (message, message_id);
1922 /* Check if we have already seen related messages to this one.
1923 * If we have then use the thread_id that we stored at that time.
1925 stored_id = notmuch->xapian_db->get_metadata (metadata_key);
1926 if (! stored_id.empty()) {
1927 Xapian::WritableDatabase *db;
1929 db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
1931 /* Clear the metadata for this message ID. We don't need it
1933 db->set_metadata (metadata_key, "");
1934 thread_id = stored_id.c_str();
1936 _notmuch_message_add_term (message, "thread", thread_id);
1938 talloc_free (metadata_key);
1940 status = _notmuch_database_link_message_to_parents (notmuch, message,
1946 status = _notmuch_database_link_message_to_children (notmuch, message,
1951 /* If not part of any existing thread, generate a new thread ID. */
1952 if (thread_id == NULL) {
1953 thread_id = _notmuch_database_generate_thread_id (notmuch);
1955 _notmuch_message_add_term (message, "thread", thread_id);
1958 return NOTMUCH_STATUS_SUCCESS;
1962 notmuch_database_add_message (notmuch_database_t *notmuch,
1963 const char *filename,
1964 notmuch_message_t **message_ret)
1966 notmuch_message_file_t *message_file;
1967 notmuch_message_t *message = NULL;
1968 notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS, ret2;
1969 notmuch_private_status_t private_status;
1971 const char *date, *header;
1972 const char *from, *to, *subject;
1973 char *message_id = NULL;
1976 *message_ret = NULL;
1978 ret = _notmuch_database_ensure_writable (notmuch);
1982 message_file = _notmuch_message_file_open (filename);
1983 if (message_file == NULL)
1984 return NOTMUCH_STATUS_FILE_ERROR;
1986 /* Adding a message may change many documents. Do this all
1988 ret = notmuch_database_begin_atomic (notmuch);
1992 /* Parse message up front to get better error status. */
1993 ret = _notmuch_message_file_parse (message_file);
1998 /* Before we do any real work, (especially before doing a
1999 * potential SHA-1 computation on the entire file's contents),
2000 * let's make sure that what we're looking at looks like an
2001 * actual email message.
2003 from = _notmuch_message_file_get_header (message_file, "from");
2004 subject = _notmuch_message_file_get_header (message_file, "subject");
2005 to = _notmuch_message_file_get_header (message_file, "to");
2007 if ((from == NULL || *from == '\0') &&
2008 (subject == NULL || *subject == '\0') &&
2009 (to == NULL || *to == '\0'))
2011 ret = NOTMUCH_STATUS_FILE_NOT_EMAIL;
2015 /* Now that we're sure it's mail, the first order of business
2016 * is to find a message ID (or else create one ourselves). */
2018 header = _notmuch_message_file_get_header (message_file, "message-id");
2019 if (header && *header != '\0') {
2020 message_id = _parse_message_id (message_file, header, NULL);
2022 /* So the header value isn't RFC-compliant, but it's
2023 * better than no message-id at all. */
2024 if (message_id == NULL)
2025 message_id = talloc_strdup (message_file, header);
2027 /* If a message ID is too long, substitute its sha1 instead. */
2028 if (message_id && strlen (message_id) > NOTMUCH_MESSAGE_ID_MAX) {
2029 char *compressed = _message_id_compressed (message_file,
2031 talloc_free (message_id);
2032 message_id = compressed;
2036 if (message_id == NULL ) {
2037 /* No message-id at all, let's generate one by taking a
2038 * hash over the file's contents. */
2039 char *sha1 = _notmuch_sha1_of_file (filename);
2041 /* If that failed too, something is really wrong. Give up. */
2043 ret = NOTMUCH_STATUS_FILE_ERROR;
2047 message_id = talloc_asprintf (message_file,
2048 "notmuch-sha1-%s", sha1);
2052 /* Now that we have a message ID, we get a message object,
2053 * (which may or may not reference an existing document in the
2056 message = _notmuch_message_create_for_message_id (notmuch,
2060 talloc_free (message_id);
2062 if (message == NULL) {
2063 ret = COERCE_STATUS (private_status,
2064 "Unexpected status value from _notmuch_message_create_for_message_id");
2068 _notmuch_message_add_filename (message, filename);
2070 /* Is this a newly created message object? */
2071 if (private_status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND) {
2072 _notmuch_message_add_term (message, "type", "mail");
2074 ret = _notmuch_database_link_message (notmuch, message,
2079 date = _notmuch_message_file_get_header (message_file, "date");
2080 _notmuch_message_set_header_values (message, date, from, subject);
2082 ret = _notmuch_message_index_file (message, message_file);
2086 ret = NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID;
2089 _notmuch_message_sync (message);
2090 } catch (const Xapian::Error &error) {
2091 fprintf (stderr, "A Xapian exception occurred adding message: %s.\n",
2092 error.get_msg().c_str());
2093 notmuch->exception_reported = TRUE;
2094 ret = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
2100 if ((ret == NOTMUCH_STATUS_SUCCESS ||
2101 ret == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) && message_ret)
2102 *message_ret = message;
2104 notmuch_message_destroy (message);
2108 _notmuch_message_file_close (message_file);
2110 ret2 = notmuch_database_end_atomic (notmuch);
2111 if ((ret == NOTMUCH_STATUS_SUCCESS ||
2112 ret == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID) &&
2113 ret2 != NOTMUCH_STATUS_SUCCESS)
2120 notmuch_database_remove_message (notmuch_database_t *notmuch,
2121 const char *filename)
2123 notmuch_status_t status;
2124 notmuch_message_t *message;
2126 status = notmuch_database_find_message_by_filename (notmuch, filename,
2129 if (status == NOTMUCH_STATUS_SUCCESS && message) {
2130 status = _notmuch_message_remove_filename (message, filename);
2131 if (status == NOTMUCH_STATUS_SUCCESS)
2132 _notmuch_message_delete (message);
2133 else if (status == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID)
2134 _notmuch_message_sync (message);
2136 notmuch_message_destroy (message);
2143 notmuch_database_find_message_by_filename (notmuch_database_t *notmuch,
2144 const char *filename,
2145 notmuch_message_t **message_ret)
2148 const char *prefix = _find_prefix ("file-direntry");
2149 char *direntry, *term;
2150 Xapian::PostingIterator i, end;
2151 notmuch_status_t status;
2153 if (message_ret == NULL)
2154 return NOTMUCH_STATUS_NULL_POINTER;
2156 /* return NULL on any failure */
2157 *message_ret = NULL;
2159 local = talloc_new (notmuch);
2162 status = _notmuch_database_filename_to_direntry (
2163 local, notmuch, filename, NOTMUCH_FIND_LOOKUP, &direntry);
2164 if (status || !direntry)
2167 term = talloc_asprintf (local, "%s%s", prefix, direntry);
2169 find_doc_ids_for_term (notmuch, term, &i, &end);
2172 notmuch_private_status_t private_status;
2174 *message_ret = _notmuch_message_create (notmuch, notmuch, *i,
2176 if (*message_ret == NULL)
2177 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
2179 } catch (const Xapian::Error &error) {
2180 fprintf (stderr, "Error: A Xapian exception occurred finding message by filename: %s\n",
2181 error.get_msg().c_str());
2182 notmuch->exception_reported = TRUE;
2183 status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
2187 talloc_free (local);
2189 if (status && *message_ret) {
2190 notmuch_message_destroy (*message_ret);
2191 *message_ret = NULL;
2196 notmuch_string_list_t *
2197 _notmuch_database_get_terms_with_prefix (void *ctx, Xapian::TermIterator &i,
2198 Xapian::TermIterator &end,
2201 int prefix_len = strlen (prefix);
2202 notmuch_string_list_t *list;
2204 list = _notmuch_string_list_create (ctx);
2205 if (unlikely (list == NULL))
2208 for (i.skip_to (prefix); i != end; i++) {
2209 /* Terminate loop at first term without desired prefix. */
2210 if (strncmp ((*i).c_str (), prefix, prefix_len))
2213 _notmuch_string_list_append (list, (*i).c_str () + prefix_len);
2220 notmuch_database_get_all_tags (notmuch_database_t *db)
2222 Xapian::TermIterator i, end;
2223 notmuch_string_list_t *tags;
2226 i = db->xapian_db->allterms_begin();
2227 end = db->xapian_db->allterms_end();
2228 tags = _notmuch_database_get_terms_with_prefix (db, i, end,
2229 _find_prefix ("tag"));
2230 _notmuch_string_list_sort (tags);
2231 return _notmuch_tags_create (db, tags);
2232 } catch (const Xapian::Error &error) {
2233 fprintf (stderr, "A Xapian exception occurred getting tags: %s.\n",
2234 error.get_msg().c_str());
2235 db->exception_reported = TRUE;