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 https://www.gnu.org/licenses/ .
18 * Author: Carl Worth <cworth@cworth.org>
21 #include "database-private.h"
22 #include "string-util.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 */
41 notmuch_field_flag_t flags;
44 #define NOTMUCH_DATABASE_VERSION 3
46 #define STRINGIFY(s) _SUB_STRINGIFY (s)
47 #define _SUB_STRINGIFY(s) #s
49 #define LOG_XAPIAN_EXCEPTION(message, error) _log_xapian_exception (__location__, message, error)
52 _log_xapian_exception (const char *where, notmuch_database_t *notmuch, const Xapian::Error error) {
53 _notmuch_database_log (notmuch,
54 "A Xapian exception occurred at %s: %s\n",
56 error.get_msg ().c_str ());
57 notmuch->exception_reported = true;
60 notmuch_database_mode_t
61 _notmuch_database_mode (notmuch_database_t *notmuch)
63 if (notmuch->writable_xapian_db)
64 return NOTMUCH_DATABASE_MODE_READ_WRITE;
66 return NOTMUCH_DATABASE_MODE_READ_ONLY;
69 /* Here's the current schema for our database (for NOTMUCH_DATABASE_VERSION):
71 * We currently have three different types of documents (mail, ghost,
72 * and directory) and also some metadata.
74 * There are two kinds of prefixes used in notmuch. There are the
75 * human friendly 'prefix names' like "thread:", which are also used
76 * in the query parser, and the actual prefix terms in the database
77 * (e.g. "G"). The correspondence is maintained in the file scope data
78 * structure 'prefix_table'.
82 * A mail document is associated with a particular email message. It
83 * is stored in one or more files on disk and is uniquely identified
84 * by its "id" field (which is generally the message ID). It is
85 * indexed with the following prefixed terms which the database uses
86 * to construct threads, etc.:
88 * Single terms of given prefix:
92 * id: Unique ID of mail. This is from the Message-ID header
93 * if present and not too long (see NOTMUCH_MESSAGE_ID_MAX).
94 * If it's present and too long, then we use
95 * "notmuch-sha1-<sha1_sum_of_message_id>".
96 * If this header is not present, we use
97 * "notmuch-sha1-<sha1_sum_of_entire_file>".
99 * thread: The ID of the thread to which the mail belongs
101 * replyto: The ID from the In-Reply-To header of the mail (if any).
103 * Multiple terms of given prefix:
105 * reference: All message IDs from In-Reply-To and References
106 * headers in the message.
108 * tag: Any tags associated with this message by the user.
110 * file-direntry: A colon-separated pair of values
111 * (INTEGER:STRING), where INTEGER is the
112 * document ID of a directory document, and
113 * STRING is the name of a file within that
114 * directory for this mail message.
116 * property: Has a property with key=value
117 * FIXME: if no = is present, should match on any value
119 * A mail document also has four values:
121 * TIMESTAMP: The time_t value corresponding to the message's
124 * MESSAGE_ID: The unique ID of the mail mess (see "id" above)
126 * FROM: The value of the "From" header
128 * SUBJECT: The value of the "Subject" header
130 * LAST_MOD: The revision number as of the last tag or
133 * The prefixed terms described above are also searchable without an
134 * explicit field name, but as of notmuch 0.29 this is due to
135 * query-parser setup, not extra terms in the database. In addition,
136 * terms from the content of the message are added without a prefix
137 * for use by the user in searching. Note that the prefix name "body"
138 * is used to refer to the empty prefix string in the database.
140 * The path of the containing folder is added with the "folder" prefix
141 * (see _notmuch_message_add_folder_terms). Sub-paths of the the path
142 * of the mail message are added with the "path" prefix.
144 * The data portion of a mail document is empty.
146 * Ghost mail document [if NOTMUCH_FEATURE_GHOSTS]
147 * -----------------------------------------------
148 * A ghost mail document is like a mail document, but where we don't
149 * have the message content. These are used to track thread reference
150 * information for messages we haven't received.
152 * A ghost mail document has type: ghost; id and thread fields that
153 * are identical to the mail document fields; and a MESSAGE_ID value.
157 * A directory document is used by a client of the notmuch library to
158 * maintain data necessary to allow for efficient polling of mail
161 * All directory documents contain one term:
163 * directory: The directory path (relative to the database path)
164 * Or the SHA1 sum of the directory path (if the
165 * path itself is too long to fit in a Xapian
168 * And all directory documents for directories other than top-level
169 * directories also contain the following term:
171 * directory-direntry: A colon-separated pair of values
172 * (INTEGER:STRING), where INTEGER is the
173 * document ID of the parent directory
174 * document, and STRING is the name of this
175 * directory within that parent.
177 * All directory documents have a single value:
179 * TIMESTAMP: The mtime of the directory (at last scan)
181 * The data portion of a directory document contains the path of the
182 * directory (relative to the database path).
186 * Xapian allows us to store arbitrary name-value pairs as
187 * "metadata". We currently use the following metadata names with the
190 * version The database schema version, (which is distinct
191 * from both the notmuch package version (see
192 * notmuch --version) and the libnotmuch library
193 * version. The version is stored as an base-10
194 * ASCII integer. The initial database version
195 * was 1, (though a schema existed before that
196 * were no "version" database value existed at
197 * all). Successive versions are allocated as
198 * changes are made to the database (such as by
199 * indexing new fields).
201 * features The set of features supported by this
202 * database. This consists of a set of
203 * '\n'-separated lines, where each is a feature
204 * name, a '\t', and compatibility flags. If the
205 * compatibility flags contain 'w', then the
206 * opener must support this feature to safely
207 * write this database. If the compatibility
208 * flags contain 'r', then the opener must
209 * support this feature to read this database.
210 * Introduced in database version 3.
212 * last_thread_id The last thread ID generated. This is stored
213 * as a 16-byte hexadecimal ASCII representation
214 * of a 64-bit unsigned integer. The first ID
215 * generated is 1 and the value will be
216 * incremented for each thread ID.
218 * C* metadata keys starting with C indicate
219 * configuration data. It can be managed with the
220 * n_database_*config* API. There is a convention
221 * of hierarchical keys separated by '.' (e.g.
222 * query.notmuch stores the value for the named
223 * query 'notmuch'), but it is not enforced by the
229 * If ! NOTMUCH_FEATURE_GHOSTS, there are no ghost mail documents.
230 * Instead, the database has the following additional database
233 * thread_id_* A pre-allocated thread ID for a particular
234 * message. This is actually an arbitrarily large
235 * family of metadata name. Any particular name is
236 * formed by concatenating "thread_id_" with a message
237 * ID (or the SHA1 sum of a message ID if it is very
238 * long---see description of 'id' in the mail
239 * document). The value stored is a thread ID.
241 * These thread ID metadata values are stored
242 * whenever a message references a parent message
243 * that does not yet exist in the database. A
244 * thread ID will be allocated and stored, and if
245 * the message is later added, the stored thread
246 * ID will be used (and the metadata value will
249 * Even before a message is added, it's
250 * pre-allocated thread ID is useful so that all
251 * descendant messages that reference this common
252 * parent can be recognized as belonging to the
257 notmuch_string_map_iterator_t *
258 _notmuch_database_user_headers (notmuch_database_t *notmuch)
260 return _notmuch_string_map_iterator_create (notmuch->user_header, "", false);
264 notmuch_status_to_string (notmuch_status_t status)
267 case NOTMUCH_STATUS_SUCCESS:
268 return "No error occurred";
269 case NOTMUCH_STATUS_OUT_OF_MEMORY:
270 return "Out of memory";
271 case NOTMUCH_STATUS_READ_ONLY_DATABASE:
272 return "Attempt to write to a read-only database";
273 case NOTMUCH_STATUS_XAPIAN_EXCEPTION:
274 return "A Xapian exception occurred";
275 case NOTMUCH_STATUS_FILE_ERROR:
276 return "Something went wrong trying to read or write a file";
277 case NOTMUCH_STATUS_FILE_NOT_EMAIL:
278 return "File is not an email";
279 case NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID:
280 return "Message ID is identical to a message in database";
281 case NOTMUCH_STATUS_NULL_POINTER:
282 return "Erroneous NULL pointer";
283 case NOTMUCH_STATUS_TAG_TOO_LONG:
284 return "Tag value is too long (exceeds NOTMUCH_TAG_MAX)";
285 case NOTMUCH_STATUS_UNBALANCED_FREEZE_THAW:
286 return "Unbalanced number of calls to notmuch_message_freeze/thaw";
287 case NOTMUCH_STATUS_UNBALANCED_ATOMIC:
288 return "Unbalanced number of calls to notmuch_database_begin_atomic/end_atomic";
289 case NOTMUCH_STATUS_UNSUPPORTED_OPERATION:
290 return "Unsupported operation";
291 case NOTMUCH_STATUS_UPGRADE_REQUIRED:
292 return "Operation requires a database upgrade";
293 case NOTMUCH_STATUS_PATH_ERROR:
294 return "Path supplied is illegal for this function";
295 case NOTMUCH_STATUS_MALFORMED_CRYPTO_PROTOCOL:
296 return "Crypto protocol missing, malformed, or unintelligible";
297 case NOTMUCH_STATUS_FAILED_CRYPTO_CONTEXT_CREATION:
298 return "Crypto engine initialization failure";
299 case NOTMUCH_STATUS_UNKNOWN_CRYPTO_PROTOCOL:
300 return "Unknown crypto protocol";
302 case NOTMUCH_STATUS_LAST_STATUS:
303 return "Unknown error status value";
308 _notmuch_database_log (notmuch_database_t *notmuch,
314 va_start (va_args, format);
316 if (notmuch->status_string)
317 talloc_free (notmuch->status_string);
319 notmuch->status_string = talloc_vasprintf (notmuch, format, va_args);
324 _notmuch_database_log_append (notmuch_database_t *notmuch,
330 va_start (va_args, format);
332 if (notmuch->status_string)
333 notmuch->status_string = talloc_vasprintf_append (notmuch->status_string, format, va_args);
335 notmuch->status_string = talloc_vasprintf (notmuch, format, va_args);
341 find_doc_ids_for_term (notmuch_database_t *notmuch,
343 Xapian::PostingIterator *begin,
344 Xapian::PostingIterator *end)
346 *begin = notmuch->xapian_db->postlist_begin (term);
348 *end = notmuch->xapian_db->postlist_end (term);
352 _notmuch_database_find_doc_ids (notmuch_database_t *notmuch,
353 const char *prefix_name,
355 Xapian::PostingIterator *begin,
356 Xapian::PostingIterator *end)
360 term = talloc_asprintf (notmuch, "%s%s",
361 _find_prefix (prefix_name), value);
363 find_doc_ids_for_term (notmuch, term, begin, end);
368 notmuch_private_status_t
369 _notmuch_database_find_unique_doc_id (notmuch_database_t *notmuch,
370 const char *prefix_name,
372 unsigned int *doc_id)
374 Xapian::PostingIterator i, end;
376 _notmuch_database_find_doc_ids (notmuch, prefix_name, value, &i, &end);
380 return NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND;
385 #if DEBUG_DATABASE_SANITY
389 INTERNAL_ERROR ("Term %s:%s is not unique as expected.\n",
393 return NOTMUCH_PRIVATE_STATUS_SUCCESS;
396 static Xapian::Document
397 find_document_for_doc_id (notmuch_database_t *notmuch, unsigned doc_id)
399 return notmuch->xapian_db->get_document (doc_id);
402 /* Generate a compressed version of 'message_id' of the form:
404 * notmuch-sha1-<sha1_sum_of_message_id>
407 _notmuch_message_id_compressed (void *ctx, const char *message_id)
409 char *sha1, *compressed;
411 sha1 = _notmuch_sha1_of_string (message_id);
413 compressed = talloc_asprintf (ctx, "notmuch-sha1-%s", sha1);
420 notmuch_database_find_message (notmuch_database_t *notmuch,
421 const char *message_id,
422 notmuch_message_t **message_ret)
424 notmuch_private_status_t status;
427 if (message_ret == NULL)
428 return NOTMUCH_STATUS_NULL_POINTER;
430 if (strlen (message_id) > NOTMUCH_MESSAGE_ID_MAX)
431 message_id = _notmuch_message_id_compressed (notmuch, message_id);
434 status = _notmuch_database_find_unique_doc_id (notmuch, "id",
435 message_id, &doc_id);
437 if (status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND)
440 *message_ret = _notmuch_message_create (notmuch, notmuch, doc_id,
442 if (*message_ret == NULL)
443 return NOTMUCH_STATUS_OUT_OF_MEMORY;
446 return NOTMUCH_STATUS_SUCCESS;
447 } catch (const Xapian::Error &error) {
448 _notmuch_database_log (notmuch, "A Xapian exception occurred finding message: %s.\n",
449 error.get_msg ().c_str ());
450 notmuch->exception_reported = true;
452 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
457 notmuch_database_create (const char *path, notmuch_database_t **database)
459 char *status_string = NULL;
460 notmuch_status_t status;
462 status = notmuch_database_create_verbose (path, database,
466 fputs (status_string, stderr);
467 free (status_string);
474 notmuch_database_create_verbose (const char *path,
475 notmuch_database_t **database,
476 char **status_string)
478 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
479 notmuch_database_t *notmuch = NULL;
480 char *notmuch_path = NULL;
481 char *message = NULL;
486 message = strdup ("Error: Cannot create a database for a NULL path.\n");
487 status = NOTMUCH_STATUS_NULL_POINTER;
491 if (path[0] != '/') {
492 message = strdup ("Error: Database path must be absolute.\n");
493 status = NOTMUCH_STATUS_PATH_ERROR;
497 err = stat (path, &st);
499 IGNORE_RESULT (asprintf (&message, "Error: Cannot create database at %s: %s.\n",
500 path, strerror (errno)));
501 status = NOTMUCH_STATUS_FILE_ERROR;
505 if (! S_ISDIR (st.st_mode)) {
506 IGNORE_RESULT (asprintf (&message, "Error: Cannot create database at %s: "
507 "Not a directory.\n",
509 status = NOTMUCH_STATUS_FILE_ERROR;
513 notmuch_path = talloc_asprintf (NULL, "%s/%s", path, ".notmuch");
515 err = mkdir (notmuch_path, 0755);
518 IGNORE_RESULT (asprintf (&message, "Error: Cannot create directory %s: %s.\n",
519 notmuch_path, strerror (errno)));
520 status = NOTMUCH_STATUS_FILE_ERROR;
524 status = notmuch_database_open_verbose (path,
525 NOTMUCH_DATABASE_MODE_READ_WRITE,
530 /* Upgrade doesn't add these feature to existing databases, but
531 * new databases have them. */
532 notmuch->features |= NOTMUCH_FEATURE_FROM_SUBJECT_ID_VALUES;
533 notmuch->features |= NOTMUCH_FEATURE_INDEXED_MIMETYPES;
534 notmuch->features |= NOTMUCH_FEATURE_UNPREFIX_BODY_ONLY;
536 status = notmuch_database_upgrade (notmuch, NULL, NULL);
538 notmuch_database_close (notmuch);
544 talloc_free (notmuch_path);
548 *status_string = message;
555 talloc_free (notmuch);
560 _notmuch_database_ensure_writable (notmuch_database_t *notmuch)
562 if (_notmuch_database_mode (notmuch) == NOTMUCH_DATABASE_MODE_READ_ONLY) {
563 _notmuch_database_log (notmuch, "Cannot write to a read-only database.\n");
564 return NOTMUCH_STATUS_READ_ONLY_DATABASE;
567 return NOTMUCH_STATUS_SUCCESS;
570 /* Allocate a revision number for the next change. */
572 _notmuch_database_new_revision (notmuch_database_t *notmuch)
574 unsigned long new_revision = notmuch->revision + 1;
576 /* If we're in an atomic section, hold off on updating the
577 * committed revision number until we commit the atomic section.
579 if (notmuch->atomic_nesting)
580 notmuch->atomic_dirty = true;
582 notmuch->revision = new_revision;
588 notmuch_database_close (notmuch_database_t *notmuch)
590 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
592 /* Many Xapian objects (and thus notmuch objects) hold references to
593 * the database, so merely deleting the database may not suffice to
594 * close it. Thus, we explicitly close it here. */
597 /* If there's an outstanding transaction, it's unclear if
598 * closing the Xapian database commits everything up to
599 * that transaction, or may discard committed (but
600 * unflushed) transactions. To be certain, explicitly
601 * cancel any outstanding transaction before closing. */
602 if (_notmuch_database_mode (notmuch) == NOTMUCH_DATABASE_MODE_READ_WRITE &&
603 notmuch->atomic_nesting)
604 notmuch->writable_xapian_db->cancel_transaction ();
606 /* Close the database. This implicitly flushes
607 * outstanding changes. */
608 notmuch->xapian_db->close ();
609 } catch (const Xapian::Error &error) {
610 status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
611 if (! notmuch->exception_reported) {
612 _notmuch_database_log (notmuch, "Error: A Xapian exception occurred closing database: %s\n",
613 error.get_msg ().c_str ());
617 notmuch->open = false;
622 _notmuch_database_reopen (notmuch_database_t *notmuch)
624 if (_notmuch_database_mode (notmuch) != NOTMUCH_DATABASE_MODE_READ_ONLY)
625 return NOTMUCH_STATUS_UNSUPPORTED_OPERATION;
628 notmuch->xapian_db->reopen ();
629 } catch (const Xapian::Error &error) {
630 if (! notmuch->exception_reported) {
631 _notmuch_database_log (notmuch, "Error: A Xapian exception reopening database: %s\n",
632 error.get_msg ().c_str ());
633 notmuch->exception_reported = true;
635 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
640 return NOTMUCH_STATUS_SUCCESS;
644 unlink_cb (const char *path,
645 unused (const struct stat *sb),
647 unused (struct FTW *ftw))
649 return remove (path);
653 rmtree (const char *path)
655 return nftw (path, unlink_cb, 64, FTW_DEPTH | FTW_PHYS);
658 class NotmuchCompactor : public Xapian::Compactor
660 notmuch_compact_status_cb_t status_cb;
661 void *status_closure;
664 NotmuchCompactor(notmuch_compact_status_cb_t cb, void *closure) :
665 status_cb (cb), status_closure (closure)
670 set_status (const std::string &table, const std::string &status)
674 if (status_cb == NULL)
677 if (status.length () == 0)
678 msg = talloc_asprintf (NULL, "compacting table %s", table.c_str ());
680 msg = talloc_asprintf (NULL, " %s", status.c_str ());
686 status_cb (msg, status_closure);
691 /* Compacts the given database, optionally saving the original database
692 * in backup_path. Additionally, a callback function can be provided to
693 * give the user feedback on the progress of the (likely long-lived)
694 * compaction process.
696 * The backup path must point to a directory on the same volume as the
697 * original database. Passing a NULL backup_path will result in the
698 * uncompacted database being deleted after compaction has finished.
699 * Note that the database write lock will be held during the
700 * compaction process to protect data integrity.
703 notmuch_database_compact (const char *path,
704 const char *backup_path,
705 notmuch_compact_status_cb_t status_cb,
709 char *notmuch_path, *xapian_path, *compact_xapian_path;
710 notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
711 notmuch_database_t *notmuch = NULL;
714 char *message = NULL;
716 local = talloc_new (NULL);
718 return NOTMUCH_STATUS_OUT_OF_MEMORY;
720 ret = notmuch_database_open_verbose (path,
721 NOTMUCH_DATABASE_MODE_READ_WRITE,
725 if (status_cb) status_cb (message, closure);
729 if (! (notmuch_path = talloc_asprintf (local, "%s/%s", path, ".notmuch"))) {
730 ret = NOTMUCH_STATUS_OUT_OF_MEMORY;
734 if (! (xapian_path = talloc_asprintf (local, "%s/%s", notmuch_path, "xapian"))) {
735 ret = NOTMUCH_STATUS_OUT_OF_MEMORY;
739 if (! (compact_xapian_path = talloc_asprintf (local, "%s.compact", xapian_path))) {
740 ret = NOTMUCH_STATUS_OUT_OF_MEMORY;
744 if (backup_path == NULL) {
745 if (! (backup_path = talloc_asprintf (local, "%s.old", xapian_path))) {
746 ret = NOTMUCH_STATUS_OUT_OF_MEMORY;
754 if (stat (backup_path, &statbuf) != -1) {
755 _notmuch_database_log (notmuch, "Path already exists: %s\n", backup_path);
756 ret = NOTMUCH_STATUS_FILE_ERROR;
759 if (errno != ENOENT) {
760 _notmuch_database_log (notmuch, "Unknown error while stat()ing path: %s\n",
762 ret = NOTMUCH_STATUS_FILE_ERROR;
766 /* Unconditionally attempt to remove old work-in-progress database (if
767 * any). This is "protected" by database lock. If this fails due to write
768 * errors (etc), the following code will fail and provide error message.
770 (void) rmtree (compact_xapian_path);
773 NotmuchCompactor compactor (status_cb, closure);
774 notmuch->xapian_db->compact (compact_xapian_path, Xapian::DBCOMPACT_NO_RENUMBER, 0, compactor);
775 } catch (const Xapian::Error &error) {
776 _notmuch_database_log (notmuch, "Error while compacting: %s\n", error.get_msg ().c_str ());
777 ret = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
781 if (rename (xapian_path, backup_path)) {
782 _notmuch_database_log (notmuch, "Error moving %s to %s: %s\n",
783 xapian_path, backup_path, strerror (errno));
784 ret = NOTMUCH_STATUS_FILE_ERROR;
788 if (rename (compact_xapian_path, xapian_path)) {
789 _notmuch_database_log (notmuch, "Error moving %s to %s: %s\n",
790 compact_xapian_path, xapian_path, strerror (errno));
791 ret = NOTMUCH_STATUS_FILE_ERROR;
796 if (rmtree (backup_path)) {
797 _notmuch_database_log (notmuch, "Error removing old database %s: %s\n",
798 backup_path, strerror (errno));
799 ret = NOTMUCH_STATUS_FILE_ERROR;
806 notmuch_status_t ret2;
808 const char *str = notmuch_database_status_string (notmuch);
809 if (status_cb && str)
810 status_cb (str, closure);
812 ret2 = notmuch_database_destroy (notmuch);
814 /* don't clobber previous error status */
815 if (ret == NOTMUCH_STATUS_SUCCESS && ret2 != NOTMUCH_STATUS_SUCCESS)
825 notmuch_database_destroy (notmuch_database_t *notmuch)
827 notmuch_status_t status;
829 status = notmuch_database_close (notmuch);
831 delete notmuch->term_gen;
832 notmuch->term_gen = NULL;
833 delete notmuch->query_parser;
834 notmuch->query_parser = NULL;
835 delete notmuch->xapian_db;
836 notmuch->xapian_db = NULL;
837 delete notmuch->value_range_processor;
838 notmuch->value_range_processor = NULL;
839 delete notmuch->date_range_processor;
840 notmuch->date_range_processor = NULL;
841 delete notmuch->last_mod_range_processor;
842 notmuch->last_mod_range_processor = NULL;
844 talloc_free (notmuch);
850 notmuch_database_get_path (notmuch_database_t *notmuch)
852 return notmuch->path;
856 notmuch_database_get_version (notmuch_database_t *notmuch)
858 unsigned int version;
859 string version_string;
864 version_string = notmuch->xapian_db->get_metadata ("version");
865 } catch (const Xapian::Error &error) {
866 LOG_XAPIAN_EXCEPTION (notmuch, error);
870 if (version_string.empty ())
873 str = version_string.c_str ();
874 if (str == NULL || *str == '\0')
877 version = strtoul (str, &end, 10);
879 INTERNAL_ERROR ("Malformed database version: %s", str);
885 notmuch_database_needs_upgrade (notmuch_database_t *notmuch)
887 unsigned int version;
889 if (_notmuch_database_mode (notmuch) != NOTMUCH_DATABASE_MODE_READ_WRITE)
892 if (NOTMUCH_FEATURES_CURRENT & ~notmuch->features)
895 version = notmuch_database_get_version (notmuch);
897 return (version > 0 && version < NOTMUCH_DATABASE_VERSION);
900 static volatile sig_atomic_t do_progress_notify = 0;
903 handle_sigalrm (unused (int signal))
905 do_progress_notify = 1;
908 /* Upgrade the current database.
910 * After opening a database in read-write mode, the client should
911 * check if an upgrade is needed (notmuch_database_needs_upgrade) and
912 * if so, upgrade with this function before making any modifications.
914 * The optional progress_notify callback can be used by the caller to
915 * provide progress indication to the user. If non-NULL it will be
916 * called periodically with 'count' as the number of messages upgraded
917 * so far and 'total' the overall number of messages that will be
921 notmuch_database_upgrade (notmuch_database_t *notmuch,
922 void (*progress_notify) (void *closure,
926 void *local = talloc_new (NULL);
927 Xapian::TermIterator t, t_end;
928 Xapian::WritableDatabase *db;
929 struct sigaction action;
930 struct itimerval timerval;
931 bool timer_is_active = false;
932 enum _notmuch_features target_features, new_features;
933 notmuch_status_t status;
934 notmuch_private_status_t private_status;
935 notmuch_query_t *query = NULL;
936 unsigned int count = 0, total = 0;
938 status = _notmuch_database_ensure_writable (notmuch);
942 db = notmuch->writable_xapian_db;
944 target_features = notmuch->features | NOTMUCH_FEATURES_CURRENT;
945 new_features = NOTMUCH_FEATURES_CURRENT & ~notmuch->features;
947 if (! notmuch_database_needs_upgrade (notmuch))
948 return NOTMUCH_STATUS_SUCCESS;
950 if (progress_notify) {
951 /* Set up our handler for SIGALRM */
952 memset (&action, 0, sizeof (struct sigaction));
953 action.sa_handler = handle_sigalrm;
954 sigemptyset (&action.sa_mask);
955 action.sa_flags = SA_RESTART;
956 sigaction (SIGALRM, &action, NULL);
958 /* Then start a timer to send SIGALRM once per second. */
959 timerval.it_interval.tv_sec = 1;
960 timerval.it_interval.tv_usec = 0;
961 timerval.it_value.tv_sec = 1;
962 timerval.it_value.tv_usec = 0;
963 setitimer (ITIMER_REAL, &timerval, NULL);
965 timer_is_active = true;
968 /* Figure out how much total work we need to do. */
970 (NOTMUCH_FEATURE_FILE_TERMS | NOTMUCH_FEATURE_BOOL_FOLDER |
971 NOTMUCH_FEATURE_LAST_MOD)) {
972 query = notmuch_query_create (notmuch, "");
975 status = notmuch_query_count_messages (query, &msg_count);
980 notmuch_query_destroy (query);
983 if (new_features & NOTMUCH_FEATURE_DIRECTORY_DOCS) {
984 t_end = db->allterms_end ("XTIMESTAMP");
985 for (t = db->allterms_begin ("XTIMESTAMP"); t != t_end; t++)
988 if (new_features & NOTMUCH_FEATURE_GHOSTS) {
989 /* The ghost message upgrade converts all thread_id_*
990 * metadata values into ghost message documents. */
991 t_end = db->metadata_keys_end ("thread_id_");
992 for (t = db->metadata_keys_begin ("thread_id_"); t != t_end; ++t)
996 /* Perform the upgrade in a transaction. */
997 db->begin_transaction (true);
999 /* Set the target features so we write out changes in the desired
1001 notmuch->features = target_features;
1003 /* Perform per-message upgrades. */
1005 (NOTMUCH_FEATURE_FILE_TERMS | NOTMUCH_FEATURE_BOOL_FOLDER |
1006 NOTMUCH_FEATURE_LAST_MOD)) {
1007 notmuch_messages_t *messages;
1008 notmuch_message_t *message;
1011 query = notmuch_query_create (notmuch, "");
1013 status = notmuch_query_search_messages (query, &messages);
1017 notmuch_messages_valid (messages);
1018 notmuch_messages_move_to_next (messages)) {
1019 if (do_progress_notify) {
1020 progress_notify (closure, (double) count / total);
1021 do_progress_notify = 0;
1024 message = notmuch_messages_get (messages);
1026 /* Before version 1, each message document had its
1027 * filename in the data field. Copy that into the new
1028 * format by calling notmuch_message_add_filename.
1030 if (new_features & NOTMUCH_FEATURE_FILE_TERMS) {
1031 filename = _notmuch_message_talloc_copy_data (message);
1032 if (filename && *filename != '\0') {
1033 _notmuch_message_add_filename (message, filename);
1034 _notmuch_message_clear_data (message);
1036 talloc_free (filename);
1039 /* Prior to version 2, the "folder:" prefix was
1040 * probabilistic and stemmed. Change it to the current
1041 * boolean prefix. Add "path:" prefixes while at it.
1043 if (new_features & NOTMUCH_FEATURE_BOOL_FOLDER)
1044 _notmuch_message_upgrade_folder (message);
1046 /* Prior to NOTMUCH_FEATURE_LAST_MOD, messages did not
1047 * track modification revisions. Give all messages the
1048 * next available revision; since we just started tracking
1049 * revisions for this database, that will be 1.
1051 if (new_features & NOTMUCH_FEATURE_LAST_MOD)
1052 _notmuch_message_upgrade_last_mod (message);
1054 _notmuch_message_sync (message);
1056 notmuch_message_destroy (message);
1061 notmuch_query_destroy (query);
1065 /* Perform per-directory upgrades. */
1067 /* Before version 1 we stored directory timestamps in
1068 * XTIMESTAMP documents instead of the current XDIRECTORY
1069 * documents. So copy those as well. */
1070 if (new_features & NOTMUCH_FEATURE_DIRECTORY_DOCS) {
1071 t_end = notmuch->xapian_db->allterms_end ("XTIMESTAMP");
1073 for (t = notmuch->xapian_db->allterms_begin ("XTIMESTAMP");
1076 Xapian::PostingIterator p, p_end;
1077 std::string term = *t;
1079 p_end = notmuch->xapian_db->postlist_end (term);
1081 for (p = notmuch->xapian_db->postlist_begin (term);
1084 Xapian::Document document;
1086 notmuch_directory_t *directory;
1088 if (do_progress_notify) {
1089 progress_notify (closure, (double) count / total);
1090 do_progress_notify = 0;
1093 document = find_document_for_doc_id (notmuch, *p);
1094 mtime = Xapian::sortable_unserialise (
1095 document.get_value (NOTMUCH_VALUE_TIMESTAMP));
1097 directory = _notmuch_directory_find_or_create (notmuch, term.c_str () + 10,
1098 NOTMUCH_FIND_CREATE, &status);
1099 notmuch_directory_set_mtime (directory, mtime);
1100 notmuch_directory_destroy (directory);
1102 db->delete_document (*p);
1109 /* Perform metadata upgrades. */
1111 /* Prior to NOTMUCH_FEATURE_GHOSTS, thread IDs for missing
1112 * messages were stored as database metadata. Change these to
1115 if (new_features & NOTMUCH_FEATURE_GHOSTS) {
1116 notmuch_message_t *message;
1117 std::string message_id, thread_id;
1119 t_end = db->metadata_keys_end (NOTMUCH_METADATA_THREAD_ID_PREFIX);
1120 for (t = db->metadata_keys_begin (NOTMUCH_METADATA_THREAD_ID_PREFIX);
1122 if (do_progress_notify) {
1123 progress_notify (closure, (double) count / total);
1124 do_progress_notify = 0;
1127 message_id = (*t).substr (
1128 strlen (NOTMUCH_METADATA_THREAD_ID_PREFIX));
1129 thread_id = db->get_metadata (*t);
1131 /* Create ghost message */
1132 message = _notmuch_message_create_for_message_id (
1133 notmuch, message_id.c_str (), &private_status);
1134 if (private_status == NOTMUCH_PRIVATE_STATUS_SUCCESS) {
1135 /* Document already exists; ignore the stored thread ID */
1136 } else if (private_status ==
1137 NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND) {
1138 private_status = _notmuch_message_initialize_ghost (
1139 message, thread_id.c_str ());
1140 if (! private_status)
1141 _notmuch_message_sync (message);
1144 if (private_status) {
1145 _notmuch_database_log (notmuch,
1146 "Upgrade failed while creating ghost messages.\n");
1147 status = COERCE_STATUS (private_status, "Unexpected status from _notmuch_message_initialize_ghost");
1151 /* Clear saved metadata thread ID */
1152 db->set_metadata (*t, "");
1158 status = NOTMUCH_STATUS_SUCCESS;
1159 db->set_metadata ("features", _notmuch_database_print_features (local, notmuch->features));
1160 db->set_metadata ("version", STRINGIFY (NOTMUCH_DATABASE_VERSION));
1163 if (status == NOTMUCH_STATUS_SUCCESS)
1164 db->commit_transaction ();
1166 db->cancel_transaction ();
1168 if (timer_is_active) {
1169 /* Now stop the timer. */
1170 timerval.it_interval.tv_sec = 0;
1171 timerval.it_interval.tv_usec = 0;
1172 timerval.it_value.tv_sec = 0;
1173 timerval.it_value.tv_usec = 0;
1174 setitimer (ITIMER_REAL, &timerval, NULL);
1176 /* And disable the signal handler. */
1177 action.sa_handler = SIG_IGN;
1178 sigaction (SIGALRM, &action, NULL);
1182 notmuch_query_destroy (query);
1184 talloc_free (local);
1189 notmuch_database_begin_atomic (notmuch_database_t *notmuch)
1191 if (_notmuch_database_mode (notmuch) == NOTMUCH_DATABASE_MODE_READ_ONLY ||
1192 notmuch->atomic_nesting > 0)
1195 if (notmuch_database_needs_upgrade (notmuch))
1196 return NOTMUCH_STATUS_UPGRADE_REQUIRED;
1199 notmuch->writable_xapian_db->begin_transaction (false);
1200 } catch (const Xapian::Error &error) {
1201 _notmuch_database_log (notmuch, "A Xapian exception occurred beginning transaction: %s.\n",
1202 error.get_msg ().c_str ());
1203 notmuch->exception_reported = true;
1204 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
1208 notmuch->atomic_nesting++;
1209 return NOTMUCH_STATUS_SUCCESS;
1213 notmuch_database_end_atomic (notmuch_database_t *notmuch)
1215 Xapian::WritableDatabase *db;
1217 if (notmuch->atomic_nesting == 0)
1218 return NOTMUCH_STATUS_UNBALANCED_ATOMIC;
1220 if (_notmuch_database_mode (notmuch) == NOTMUCH_DATABASE_MODE_READ_ONLY ||
1221 notmuch->atomic_nesting > 1)
1224 db = notmuch->writable_xapian_db;
1226 db->commit_transaction ();
1228 /* This is a hack for testing. Xapian never flushes on a
1229 * non-flushed commit, even if the flush threshold is 1.
1230 * However, we rely on flushing to test atomicity. */
1231 const char *thresh = getenv ("XAPIAN_FLUSH_THRESHOLD");
1232 if (thresh && atoi (thresh) == 1)
1234 } catch (const Xapian::Error &error) {
1235 _notmuch_database_log (notmuch, "A Xapian exception occurred committing transaction: %s.\n",
1236 error.get_msg ().c_str ());
1237 notmuch->exception_reported = true;
1238 return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
1241 if (notmuch->atomic_dirty) {
1242 ++notmuch->revision;
1243 notmuch->atomic_dirty = false;
1247 notmuch->atomic_nesting--;
1248 return NOTMUCH_STATUS_SUCCESS;
1252 notmuch_database_get_revision (notmuch_database_t *notmuch,
1256 *uuid = notmuch->uuid;
1257 return notmuch->revision;
1260 /* We allow the user to use arbitrarily long paths for directories. But
1261 * we have a term-length limit. So if we exceed that, we'll use the
1262 * SHA-1 of the path for the database term.
1264 * Note: This function may return the original value of 'path'. If it
1265 * does not, then the caller is responsible to free() the returned
1269 _notmuch_database_get_directory_db_path (const char *path)
1271 int term_len = strlen (_find_prefix ("directory")) + strlen (path);
1273 if (term_len > NOTMUCH_TERM_MAX)
1274 return _notmuch_sha1_of_string (path);
1279 /* Given a path, split it into two parts: the directory part is all
1280 * components except for the last, and the basename is that last
1281 * component. Getting the return-value for either part is optional
1282 * (the caller can pass NULL).
1284 * The original 'path' can represent either a regular file or a
1285 * directory---the splitting will be carried out in the same way in
1286 * either case. Trailing slashes on 'path' will be ignored, and any
1287 * cases of multiple '/' characters appearing in series will be
1288 * treated as a single '/'.
1290 * Allocation (if any) will have 'ctx' as the talloc owner. But
1291 * pointers will be returned within the original path string whenever
1294 * Note: If 'path' is non-empty and contains no non-trailing slash,
1295 * (that is, consists of a filename with no parent directory), then
1296 * the directory returned will be an empty string. However, if 'path'
1297 * is an empty string, then both directory and basename will be
1301 _notmuch_database_split_path (void *ctx,
1303 const char **directory,
1304 const char **basename)
1308 if (path == NULL || *path == '\0') {
1313 return NOTMUCH_STATUS_SUCCESS;
1316 /* Find the last slash (not counting a trailing slash), if any. */
1318 slash = path + strlen (path) - 1;
1320 /* First, skip trailing slashes. */
1321 while (slash != path && *slash == '/')
1324 /* Then, find a slash. */
1325 while (slash != path && *slash != '/') {
1332 /* Finally, skip multiple slashes. */
1333 while (slash != path && *(slash - 1) == '/')
1336 if (slash == path) {
1338 *directory = talloc_strdup (ctx, "");
1343 *directory = talloc_strndup (ctx, path, slash - path);
1346 return NOTMUCH_STATUS_SUCCESS;
1349 /* Find the document ID of the specified directory.
1351 * If (flags & NOTMUCH_FIND_CREATE), a new directory document will be
1352 * created if one does not exist for 'path'. Otherwise, if the
1353 * directory document does not exist, this sets *directory_id to
1354 * ((unsigned int)-1) and returns NOTMUCH_STATUS_SUCCESS.
1357 _notmuch_database_find_directory_id (notmuch_database_t *notmuch,
1359 notmuch_find_flags_t flags,
1360 unsigned int *directory_id)
1362 notmuch_directory_t *directory;
1363 notmuch_status_t status;
1367 return NOTMUCH_STATUS_SUCCESS;
1370 directory = _notmuch_directory_find_or_create (notmuch, path, flags, &status);
1371 if (status || ! directory) {
1376 *directory_id = _notmuch_directory_get_document_id (directory);
1378 notmuch_directory_destroy (directory);
1380 return NOTMUCH_STATUS_SUCCESS;
1384 _notmuch_database_get_directory_path (void *ctx,
1385 notmuch_database_t *notmuch,
1386 unsigned int doc_id)
1388 Xapian::Document document;
1390 document = find_document_for_doc_id (notmuch, doc_id);
1392 return talloc_strdup (ctx, document.get_data ().c_str ());
1395 /* Given a legal 'filename' for the database, (either relative to
1396 * database path or absolute with initial components identical to
1397 * database path), return a new string (with 'ctx' as the talloc
1398 * owner) suitable for use as a direntry term value.
1400 * If (flags & NOTMUCH_FIND_CREATE), the necessary directory documents
1401 * will be created in the database as needed. Otherwise, if the
1402 * necessary directory documents do not exist, this sets
1403 * *direntry to NULL and returns NOTMUCH_STATUS_SUCCESS.
1406 _notmuch_database_filename_to_direntry (void *ctx,
1407 notmuch_database_t *notmuch,
1408 const char *filename,
1409 notmuch_find_flags_t flags,
1412 const char *relative, *directory, *basename;
1413 Xapian::docid directory_id;
1414 notmuch_status_t status;
1416 relative = _notmuch_database_relative_path (notmuch, filename);
1418 status = _notmuch_database_split_path (ctx, relative,
1419 &directory, &basename);
1423 status = _notmuch_database_find_directory_id (notmuch, directory, flags,
1425 if (status || directory_id == (unsigned int) -1) {
1430 *direntry = talloc_asprintf (ctx, "%u:%s", directory_id, basename);
1432 return NOTMUCH_STATUS_SUCCESS;
1435 /* Given a legal 'path' for the database, return the relative path.
1437 * The return value will be a pointer to the original path contents,
1438 * and will be either the original string (if 'path' was relative) or
1439 * a portion of the string (if path was absolute and begins with the
1443 _notmuch_database_relative_path (notmuch_database_t *notmuch,
1446 const char *db_path, *relative;
1447 unsigned int db_path_len;
1449 db_path = notmuch_database_get_path (notmuch);
1450 db_path_len = strlen (db_path);
1454 if (*relative == '/') {
1455 while (*relative == '/' && *(relative + 1) == '/')
1458 if (strncmp (relative, db_path, db_path_len) == 0) {
1459 relative += db_path_len;
1460 while (*relative == '/')
1469 notmuch_database_get_directory (notmuch_database_t *notmuch,
1471 notmuch_directory_t **directory)
1473 notmuch_status_t status;
1475 if (directory == NULL)
1476 return NOTMUCH_STATUS_NULL_POINTER;
1480 *directory = _notmuch_directory_find_or_create (notmuch, path,
1481 NOTMUCH_FIND_LOOKUP, &status);
1482 } catch (const Xapian::Error &error) {
1483 _notmuch_database_log (notmuch, "A Xapian exception occurred getting directory: %s.\n",
1484 error.get_msg ().c_str ());
1485 notmuch->exception_reported = true;
1486 status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
1491 /* Allocate a document ID that satisfies the following criteria:
1493 * 1. The ID does not exist for any document in the Xapian database
1495 * 2. The ID was not previously returned from this function
1497 * 3. The ID is the smallest integer satisfying (1) and (2)
1499 * This function will trigger an internal error if these constraints
1500 * cannot all be satisfied, (that is, the pool of available document
1501 * IDs has been exhausted).
1504 _notmuch_database_generate_doc_id (notmuch_database_t *notmuch)
1506 assert (notmuch->last_doc_id >= notmuch->xapian_db->get_lastdocid ());
1508 notmuch->last_doc_id++;
1510 if (notmuch->last_doc_id == 0)
1511 INTERNAL_ERROR ("Xapian document IDs are exhausted.\n");
1513 return notmuch->last_doc_id;
1517 notmuch_database_remove_message (notmuch_database_t *notmuch,
1518 const char *filename)
1520 notmuch_status_t status;
1521 notmuch_message_t *message;
1523 status = notmuch_database_find_message_by_filename (notmuch, filename,
1526 if (status == NOTMUCH_STATUS_SUCCESS && message) {
1527 status = _notmuch_message_remove_filename (message, filename);
1528 if (status == NOTMUCH_STATUS_SUCCESS)
1529 _notmuch_message_delete (message);
1530 else if (status == NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID)
1531 _notmuch_message_sync (message);
1533 notmuch_message_destroy (message);
1540 notmuch_database_find_message_by_filename (notmuch_database_t *notmuch,
1541 const char *filename,
1542 notmuch_message_t **message_ret)
1545 const char *prefix = _find_prefix ("file-direntry");
1546 char *direntry, *term;
1547 Xapian::PostingIterator i, end;
1548 notmuch_status_t status;
1550 if (message_ret == NULL)
1551 return NOTMUCH_STATUS_NULL_POINTER;
1553 if (! (notmuch->features & NOTMUCH_FEATURE_FILE_TERMS))
1554 return NOTMUCH_STATUS_UPGRADE_REQUIRED;
1556 /* return NULL on any failure */
1557 *message_ret = NULL;
1559 local = talloc_new (notmuch);
1562 status = _notmuch_database_filename_to_direntry (
1563 local, notmuch, filename, NOTMUCH_FIND_LOOKUP, &direntry);
1564 if (status || ! direntry)
1567 term = talloc_asprintf (local, "%s%s", prefix, direntry);
1569 find_doc_ids_for_term (notmuch, term, &i, &end);
1572 notmuch_private_status_t private_status;
1574 *message_ret = _notmuch_message_create (notmuch, notmuch, *i,
1576 if (*message_ret == NULL)
1577 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
1579 } catch (const Xapian::Error &error) {
1580 _notmuch_database_log (notmuch, "Error: A Xapian exception occurred finding message by filename: %s\n",
1581 error.get_msg ().c_str ());
1582 notmuch->exception_reported = true;
1583 status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
1587 talloc_free (local);
1589 if (status && *message_ret) {
1590 notmuch_message_destroy (*message_ret);
1591 *message_ret = NULL;
1596 notmuch_string_list_t *
1597 _notmuch_database_get_terms_with_prefix (void *ctx, Xapian::TermIterator &i,
1598 Xapian::TermIterator &end,
1601 int prefix_len = strlen (prefix);
1602 notmuch_string_list_t *list;
1604 list = _notmuch_string_list_create (ctx);
1605 if (unlikely (list == NULL))
1608 for (i.skip_to (prefix); i != end; i++) {
1609 /* Terminate loop at first term without desired prefix. */
1610 if (strncmp ((*i).c_str (), prefix, prefix_len))
1613 _notmuch_string_list_append (list, (*i).c_str () + prefix_len);
1620 notmuch_database_get_all_tags (notmuch_database_t *db)
1622 Xapian::TermIterator i, end;
1623 notmuch_string_list_t *tags;
1626 i = db->xapian_db->allterms_begin ();
1627 end = db->xapian_db->allterms_end ();
1628 tags = _notmuch_database_get_terms_with_prefix (db, i, end,
1629 _find_prefix ("tag"));
1630 _notmuch_string_list_sort (tags);
1631 return _notmuch_tags_create (db, tags);
1632 } catch (const Xapian::Error &error) {
1633 _notmuch_database_log (db, "A Xapian exception occurred getting tags: %s.\n",
1634 error.get_msg ().c_str ());
1635 db->exception_reported = true;
1641 notmuch_database_status_string (const notmuch_database_t *notmuch)
1643 return notmuch->status_string;