aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/Makefile.local2
-rw-r--r--lib/database.cc147
-rw-r--r--lib/directory.cc41
-rw-r--r--lib/index.cc15
-rw-r--r--lib/message.cc117
-rw-r--r--lib/notmuch-private.h43
-rw-r--r--lib/notmuch.h122
-rw-r--r--lib/query.cc127
-rw-r--r--lib/thread.cc18
9 files changed, 496 insertions, 136 deletions
diff --git a/lib/Makefile.local b/lib/Makefile.local
index 54c4dea..8a9aa28 100644
--- a/lib/Makefile.local
+++ b/lib/Makefile.local
@@ -5,7 +5,7 @@
# the library interface, (such as the deletion of an API or a major
# semantic change that breaks formerly functioning code).
#
-LIBNOTMUCH_VERSION_MAJOR = 2
+LIBNOTMUCH_VERSION_MAJOR = 3
# The minor version of the library interface. This should be incremented at
# the time of release for any additions to the library interface,
diff --git a/lib/database.cc b/lib/database.cc
index 8103bd9..761dc1a 100644
--- a/lib/database.cc
+++ b/lib/database.cc
@@ -520,9 +520,10 @@ parse_references (void *ctx,
}
}
-notmuch_database_t *
-notmuch_database_create (const char *path)
+notmuch_status_t
+notmuch_database_create (const char *path, notmuch_database_t **database)
{
+ notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
notmuch_database_t *notmuch = NULL;
char *notmuch_path = NULL;
struct stat st;
@@ -530,6 +531,7 @@ notmuch_database_create (const char *path)
if (path == NULL) {
fprintf (stderr, "Error: Cannot create a database for a NULL path.\n");
+ status = NOTMUCH_STATUS_NULL_POINTER;
goto DONE;
}
@@ -537,12 +539,14 @@ notmuch_database_create (const char *path)
if (err) {
fprintf (stderr, "Error: Cannot create database at %s: %s.\n",
path, strerror (errno));
+ status = NOTMUCH_STATUS_FILE_ERROR;
goto DONE;
}
if (! S_ISDIR (st.st_mode)) {
fprintf (stderr, "Error: Cannot create database at %s: Not a directory.\n",
path);
+ status = NOTMUCH_STATUS_FILE_ERROR;
goto DONE;
}
@@ -553,18 +557,30 @@ notmuch_database_create (const char *path)
if (err) {
fprintf (stderr, "Error: Cannot create directory %s: %s.\n",
notmuch_path, strerror (errno));
+ status = NOTMUCH_STATUS_FILE_ERROR;
goto DONE;
}
- notmuch = notmuch_database_open (path,
- NOTMUCH_DATABASE_MODE_READ_WRITE);
- notmuch_database_upgrade (notmuch, NULL, NULL);
+ status = notmuch_database_open (path,
+ NOTMUCH_DATABASE_MODE_READ_WRITE,
+ &notmuch);
+ if (status)
+ goto DONE;
+ status = notmuch_database_upgrade (notmuch, NULL, NULL);
+ if (status) {
+ notmuch_database_close(notmuch);
+ notmuch = NULL;
+ }
DONE:
if (notmuch_path)
talloc_free (notmuch_path);
- return notmuch;
+ if (database)
+ *database = notmuch;
+ else
+ talloc_free (notmuch);
+ return status;
}
notmuch_status_t
@@ -578,20 +594,29 @@ _notmuch_database_ensure_writable (notmuch_database_t *notmuch)
return NOTMUCH_STATUS_SUCCESS;
}
-notmuch_database_t *
+notmuch_status_t
notmuch_database_open (const char *path,
- notmuch_database_mode_t mode)
+ notmuch_database_mode_t mode,
+ notmuch_database_t **database)
{
+ notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
+ void *local = talloc_new (NULL);
notmuch_database_t *notmuch = NULL;
- char *notmuch_path = NULL, *xapian_path = NULL;
+ char *notmuch_path, *xapian_path;
struct stat st;
int err;
unsigned int i, version;
static int initialized = 0;
- if (asprintf (&notmuch_path, "%s/%s", path, ".notmuch") == -1) {
- notmuch_path = NULL;
+ if (path == NULL) {
+ fprintf (stderr, "Error: Cannot open a database for a NULL path.\n");
+ status = NOTMUCH_STATUS_NULL_POINTER;
+ goto DONE;
+ }
+
+ if (! (notmuch_path = talloc_asprintf (local, "%s/%s", path, ".notmuch"))) {
fprintf (stderr, "Out of memory\n");
+ status = NOTMUCH_STATUS_OUT_OF_MEMORY;
goto DONE;
}
@@ -599,12 +624,13 @@ notmuch_database_open (const char *path,
if (err) {
fprintf (stderr, "Error opening database at %s: %s\n",
notmuch_path, strerror (errno));
+ status = NOTMUCH_STATUS_FILE_ERROR;
goto DONE;
}
- if (asprintf (&xapian_path, "%s/%s", notmuch_path, "xapian") == -1) {
- xapian_path = NULL;
+ if (! (xapian_path = talloc_asprintf (local, "%s/%s", notmuch_path, "xapian"))) {
fprintf (stderr, "Out of memory\n");
+ status = NOTMUCH_STATUS_OUT_OF_MEMORY;
goto DONE;
}
@@ -617,7 +643,7 @@ notmuch_database_open (const char *path,
initialized = 1;
}
- notmuch = talloc (NULL, notmuch_database_t);
+ notmuch = talloc_zero (NULL, notmuch_database_t);
notmuch->exception_reported = FALSE;
notmuch->path = talloc_strdup (notmuch, path);
@@ -643,8 +669,9 @@ notmuch_database_open (const char *path,
" read-write mode.\n",
notmuch_path, version, NOTMUCH_DATABASE_VERSION);
notmuch->mode = NOTMUCH_DATABASE_MODE_READ_ONLY;
- notmuch_database_close (notmuch);
+ notmuch_database_destroy (notmuch);
notmuch = NULL;
+ status = NOTMUCH_STATUS_FILE_ERROR;
goto DONE;
}
@@ -703,23 +730,27 @@ notmuch_database_open (const char *path,
} catch (const Xapian::Error &error) {
fprintf (stderr, "A Xapian exception occurred opening database: %s\n",
error.get_msg().c_str());
+ notmuch_database_destroy (notmuch);
notmuch = NULL;
+ status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
}
DONE:
- if (notmuch_path)
- free (notmuch_path);
- if (xapian_path)
- free (xapian_path);
+ talloc_free (local);
- return notmuch;
+ if (database)
+ *database = notmuch;
+ else
+ talloc_free (notmuch);
+ return status;
}
void
notmuch_database_close (notmuch_database_t *notmuch)
{
try {
- if (notmuch->mode == NOTMUCH_DATABASE_MODE_READ_WRITE)
+ if (notmuch->xapian_db != NULL &&
+ notmuch->mode == NOTMUCH_DATABASE_MODE_READ_WRITE)
(static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db))->flush ();
} catch (const Xapian::Error &error) {
if (! notmuch->exception_reported) {
@@ -728,10 +759,31 @@ notmuch_database_close (notmuch_database_t *notmuch)
}
}
+ /* Many Xapian objects (and thus notmuch objects) hold references to
+ * the database, so merely deleting the database may not suffice to
+ * close it. Thus, we explicitly close it here. */
+ if (notmuch->xapian_db != NULL) {
+ try {
+ notmuch->xapian_db->close();
+ } catch (const Xapian::Error &error) {
+ /* do nothing */
+ }
+ }
+
delete notmuch->term_gen;
+ notmuch->term_gen = NULL;
delete notmuch->query_parser;
+ notmuch->query_parser = NULL;
delete notmuch->xapian_db;
+ notmuch->xapian_db = NULL;
delete notmuch->value_range_processor;
+ notmuch->value_range_processor = NULL;
+}
+
+void
+notmuch_database_destroy (notmuch_database_t *notmuch)
+{
+ notmuch_database_close (notmuch);
talloc_free (notmuch);
}
@@ -903,8 +955,8 @@ notmuch_database_upgrade (notmuch_database_t *notmuch,
mtime = Xapian::sortable_unserialise (
document.get_value (NOTMUCH_VALUE_TIMESTAMP));
- directory = notmuch_database_get_directory (notmuch,
- term.c_str() + 10);
+ directory = _notmuch_directory_create (notmuch, term.c_str() + 10,
+ NOTMUCH_FIND_CREATE, &status);
notmuch_directory_set_mtime (directory, mtime);
notmuch_directory_destroy (directory);
}
@@ -1145,9 +1197,17 @@ _notmuch_database_split_path (void *ctx,
return NOTMUCH_STATUS_SUCCESS;
}
+/* Find the document ID of the specified directory.
+ *
+ * If (flags & NOTMUCH_FIND_CREATE), a new directory document will be
+ * created if one does not exist for 'path'. Otherwise, if the
+ * directory document does not exist, this sets *directory_id to
+ * ((unsigned int)-1) and returns NOTMUCH_STATUS_SUCCESS.
+ */
notmuch_status_t
_notmuch_database_find_directory_id (notmuch_database_t *notmuch,
const char *path,
+ notmuch_find_flags_t flags,
unsigned int *directory_id)
{
notmuch_directory_t *directory;
@@ -1158,8 +1218,8 @@ _notmuch_database_find_directory_id (notmuch_database_t *notmuch,
return NOTMUCH_STATUS_SUCCESS;
}
- directory = _notmuch_directory_create (notmuch, path, &status);
- if (status) {
+ directory = _notmuch_directory_create (notmuch, path, flags, &status);
+ if (status || !directory) {
*directory_id = -1;
return status;
}
@@ -1188,13 +1248,16 @@ _notmuch_database_get_directory_path (void *ctx,
* database path), return a new string (with 'ctx' as the talloc
* owner) suitable for use as a direntry term value.
*
- * The necessary directory documents will be created in the database
- * as needed.
+ * If (flags & NOTMUCH_FIND_CREATE), the necessary directory documents
+ * will be created in the database as needed. Otherwise, if the
+ * necessary directory documents do not exist, this sets
+ * *direntry to NULL and returns NOTMUCH_STATUS_SUCCESS.
*/
notmuch_status_t
_notmuch_database_filename_to_direntry (void *ctx,
notmuch_database_t *notmuch,
const char *filename,
+ notmuch_find_flags_t flags,
char **direntry)
{
const char *relative, *directory, *basename;
@@ -1208,10 +1271,12 @@ _notmuch_database_filename_to_direntry (void *ctx,
if (status)
return status;
- status = _notmuch_database_find_directory_id (notmuch, directory,
+ status = _notmuch_database_find_directory_id (notmuch, directory, flags,
&directory_id);
- if (status)
+ if (status || directory_id == (unsigned int)-1) {
+ *direntry = NULL;
return status;
+ }
*direntry = talloc_asprintf (ctx, "%u:%s", directory_id, basename);
@@ -1252,20 +1317,27 @@ _notmuch_database_relative_path (notmuch_database_t *notmuch,
return relative;
}
-notmuch_directory_t *
+notmuch_status_t
notmuch_database_get_directory (notmuch_database_t *notmuch,
- const char *path)
+ const char *path,
+ notmuch_directory_t **directory)
{
notmuch_status_t status;
+ if (directory == NULL)
+ return NOTMUCH_STATUS_NULL_POINTER;
+ *directory = NULL;
+
try {
- return _notmuch_directory_create (notmuch, path, &status);
+ *directory = _notmuch_directory_create (notmuch, path,
+ NOTMUCH_FIND_LOOKUP, &status);
} catch (const Xapian::Error &error) {
fprintf (stderr, "A Xapian exception occurred getting directory: %s.\n",
error.get_msg().c_str());
notmuch->exception_reported = TRUE;
- return NULL;
+ status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
}
+ return status;
}
/* Allocate a document ID that satisfies the following criteria:
@@ -1816,12 +1888,15 @@ notmuch_database_find_message_by_filename (notmuch_database_t *notmuch,
if (message_ret == NULL)
return NOTMUCH_STATUS_NULL_POINTER;
+ /* return NULL on any failure */
+ *message_ret = NULL;
+
local = talloc_new (notmuch);
try {
- status = _notmuch_database_filename_to_direntry (local, notmuch,
- filename, &direntry);
- if (status)
+ status = _notmuch_database_filename_to_direntry (
+ local, notmuch, filename, NOTMUCH_FIND_LOOKUP, &direntry);
+ if (status || !direntry)
goto DONE;
term = talloc_asprintf (local, "%s%s", prefix, direntry);
diff --git a/lib/directory.cc b/lib/directory.cc
index 70e1693..6a3ffed 100644
--- a/lib/directory.cc
+++ b/lib/directory.cc
@@ -82,28 +82,41 @@ find_directory_document (notmuch_database_t *notmuch,
return NOTMUCH_PRIVATE_STATUS_SUCCESS;
}
+/* Find or create a directory document.
+ *
+ * 'path' should be a path relative to the path of 'database', or else
+ * should be an absolute path with initial components that match the
+ * path of 'database'.
+ *
+ * If (flags & NOTMUCH_FIND_CREATE), then the directory document will
+ * be created if it does not exist. Otherwise, if the directory
+ * document does not exist, *status_ret is set to
+ * NOTMUCH_STATUS_SUCCESS and this returns NULL.
+ */
notmuch_directory_t *
_notmuch_directory_create (notmuch_database_t *notmuch,
const char *path,
+ notmuch_find_flags_t flags,
notmuch_status_t *status_ret)
{
Xapian::WritableDatabase *db;
notmuch_directory_t *directory;
notmuch_private_status_t private_status;
const char *db_path;
+ notmuch_bool_t create = (flags & NOTMUCH_FIND_CREATE);
*status_ret = NOTMUCH_STATUS_SUCCESS;
path = _notmuch_database_relative_path (notmuch, path);
- if (notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)
+ if (create && notmuch->mode == NOTMUCH_DATABASE_MODE_READ_ONLY)
INTERNAL_ERROR ("Failure to ensure database is writable");
- db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
-
directory = talloc (notmuch, notmuch_directory_t);
- if (unlikely (directory == NULL))
+ if (unlikely (directory == NULL)) {
+ *status_ret = NOTMUCH_STATUS_OUT_OF_MEMORY;
return NULL;
+ }
directory->notmuch = notmuch;
@@ -122,6 +135,13 @@ _notmuch_directory_create (notmuch_database_t *notmuch,
directory->document_id = directory->doc.get_docid ();
if (private_status == NOTMUCH_PRIVATE_STATUS_NO_DOCUMENT_FOUND) {
+ if (!create) {
+ notmuch_directory_destroy (directory);
+ directory = NULL;
+ *status_ret = NOTMUCH_STATUS_SUCCESS;
+ goto DONE;
+ }
+
void *local = talloc_new (directory);
const char *parent, *basename;
Xapian::docid parent_id;
@@ -133,7 +153,13 @@ _notmuch_directory_create (notmuch_database_t *notmuch,
_notmuch_database_split_path (local, path, &parent, &basename);
- _notmuch_database_find_directory_id (notmuch, parent, &parent_id);
+ *status_ret = _notmuch_database_find_directory_id (
+ notmuch, parent, NOTMUCH_FIND_CREATE, &parent_id);
+ if (*status_ret) {
+ notmuch_directory_destroy (directory);
+ directory = NULL;
+ goto DONE;
+ }
if (basename) {
term = talloc_asprintf (local, "%s%u:%s",
@@ -145,6 +171,8 @@ _notmuch_directory_create (notmuch_database_t *notmuch,
directory->doc.add_value (NOTMUCH_VALUE_TIMESTAMP,
Xapian::sortable_serialise (0));
+ db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
+
directory->document_id = _notmuch_database_generate_doc_id (notmuch);
db->replace_document (directory->document_id, directory->doc);
talloc_free (local);
@@ -158,10 +186,11 @@ _notmuch_directory_create (notmuch_database_t *notmuch,
error.get_msg().c_str());
notmuch->exception_reported = TRUE;
notmuch_directory_destroy (directory);
+ directory = NULL;
*status_ret = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
- return NULL;
}
+ DONE:
if (db_path != path)
free ((char *) db_path);
diff --git a/lib/index.cc b/lib/index.cc
index d8f8b2b..e377732 100644
--- a/lib/index.cc
+++ b/lib/index.cc
@@ -315,6 +315,7 @@ _index_mime_part (notmuch_message_t *message,
GByteArray *byte_array;
GMimeContentDisposition *disposition;
char *body;
+ const char *charset;
if (! part) {
fprintf (stderr, "Warning: Not indexing empty mime part.\n");
@@ -390,6 +391,20 @@ _index_mime_part (notmuch_message_t *message,
g_mime_stream_filter_add (GMIME_STREAM_FILTER (filter),
discard_uuencode_filter);
+ charset = g_mime_object_get_content_type_parameter (part, "charset");
+ if (charset) {
+ GMimeFilter *charset_filter;
+ charset_filter = g_mime_filter_charset_new (charset, "UTF-8");
+ /* This result can be NULL for things like "unknown-8bit".
+ * Don't set a NULL filter as that makes GMime print
+ * annoying assertion-failure messages on stderr. */
+ if (charset_filter) {
+ g_mime_stream_filter_add (GMIME_STREAM_FILTER (filter),
+ charset_filter);
+ g_object_unref (charset_filter);
+ }
+ }
+
wrapper = g_mime_part_get_content_object (GMIME_PART (part));
if (wrapper)
g_mime_data_wrapper_write_to_stream (wrapper, filter);
diff --git a/lib/message.cc b/lib/message.cc
index 0075425..978de06 100644
--- a/lib/message.cc
+++ b/lib/message.cc
@@ -495,9 +495,8 @@ _notmuch_message_add_filename (notmuch_message_t *message,
if (status)
return status;
- status = _notmuch_database_filename_to_direntry (local,
- message->notmuch,
- filename, &direntry);
+ status = _notmuch_database_filename_to_direntry (
+ local, message->notmuch, filename, NOTMUCH_FIND_CREATE, &direntry);
if (status)
return status;
@@ -541,9 +540,9 @@ _notmuch_message_remove_filename (notmuch_message_t *message,
notmuch_status_t status;
Xapian::TermIterator i, last;
- status = _notmuch_database_filename_to_direntry (local, message->notmuch,
- filename, &direntry);
- if (status)
+ status = _notmuch_database_filename_to_direntry (
+ local, message->notmuch, filename, NOTMUCH_FIND_LOOKUP, &direntry);
+ if (status || !direntry)
return status;
/* Unlink this file from its parent directory. */
@@ -1028,13 +1027,54 @@ notmuch_message_remove_tag (notmuch_message_t *message, const char *tag)
return NOTMUCH_STATUS_SUCCESS;
}
+/* Is the given filename within a maildir directory?
+ *
+ * Specifically, is the final directory component of 'filename' either
+ * "cur" or "new". If so, return a pointer to that final directory
+ * component within 'filename'. If not, return NULL.
+ *
+ * A non-NULL return value is guaranteed to be a valid string pointer
+ * pointing to the characters "new/" or "cur/", (but not
+ * NUL-terminated).
+ */
+static const char *
+_filename_is_in_maildir (const char *filename)
+{
+ const char *slash, *dir = NULL;
+
+ /* Find the last '/' separating directory from filename. */
+ slash = strrchr (filename, '/');
+ if (slash == NULL)
+ return NULL;
+
+ /* Jump back 4 characters to where the previous '/' will be if the
+ * directory is named "cur" or "new". */
+ if (slash - filename < 4)
+ return NULL;
+
+ slash -= 4;
+
+ if (*slash != '/')
+ return NULL;
+
+ dir = slash + 1;
+
+ if (STRNCMP_LITERAL (dir, "cur/") == 0 ||
+ STRNCMP_LITERAL (dir, "new/") == 0)
+ {
+ return dir;
+ }
+
+ return NULL;
+}
+
notmuch_status_t
notmuch_message_maildir_flags_to_tags (notmuch_message_t *message)
{
const char *flags;
notmuch_status_t status;
notmuch_filenames_t *filenames;
- const char *filename;
+ const char *filename, *dir;
char *combined_flags = talloc_strdup (message, "");
unsigned i;
int seen_maildir_info = 0;
@@ -1044,15 +1084,25 @@ notmuch_message_maildir_flags_to_tags (notmuch_message_t *message)
notmuch_filenames_move_to_next (filenames))
{
filename = notmuch_filenames_get (filenames);
+ dir = _filename_is_in_maildir (filename);
- flags = strstr (filename, ":2,");
- if (! flags)
+ if (! dir)
continue;
- seen_maildir_info = 1;
- flags += 3;
-
- combined_flags = talloc_strdup_append (combined_flags, flags);
+ flags = strstr (filename, ":2,");
+ if (flags) {
+ seen_maildir_info = 1;
+ flags += 3;
+ combined_flags = talloc_strdup_append (combined_flags, flags);
+ } else if (STRNCMP_LITERAL (dir, "new/") == 0) {
+ /* Messages are delivered to new/ with no "info" part, but
+ * they effectively have default maildir flags. According
+ * to the spec, we should ignore the info part for
+ * messages in new/, but some MUAs (mutt) can set maildir
+ * flags on messages in new/, so we're liberal in what we
+ * accept. */
+ seen_maildir_info = 1;
+ }
}
/* If none of the filenames have any maildir info field (not even
@@ -1084,47 +1134,6 @@ notmuch_message_maildir_flags_to_tags (notmuch_message_t *message)
return status;
}
-/* Is the given filename within a maildir directory?
- *
- * Specifically, is the final directory component of 'filename' either
- * "cur" or "new". If so, return a pointer to that final directory
- * component within 'filename'. If not, return NULL.
- *
- * A non-NULL return value is guaranteed to be a valid string pointer
- * pointing to the characters "new/" or "cur/", (but not
- * NUL-terminated).
- */
-static const char *
-_filename_is_in_maildir (const char *filename)
-{
- const char *slash, *dir = NULL;
-
- /* Find the last '/' separating directory from filename. */
- slash = strrchr (filename, '/');
- if (slash == NULL)
- return NULL;
-
- /* Jump back 4 characters to where the previous '/' will be if the
- * directory is named "cur" or "new". */
- if (slash - filename < 4)
- return NULL;
-
- slash -= 4;
-
- if (*slash != '/')
- return NULL;
-
- dir = slash + 1;
-
- if (STRNCMP_LITERAL (dir, "cur/") == 0 ||
- STRNCMP_LITERAL (dir, "new/") == 0)
- {
- return dir;
- }
-
- return NULL;
-}
-
/* From the set of tags on 'message' and the flag2tag table, compute a
* set of maildir-flag actions to be taken, (flags that should be
* either set or cleared).
diff --git a/lib/notmuch-private.h b/lib/notmuch-private.h
index 60a932f..bfb4111 100644
--- a/lib/notmuch-private.h
+++ b/lib/notmuch-private.h
@@ -146,8 +146,20 @@ typedef enum _notmuch_private_status {
: \
(notmuch_status_t) private_status)
+/* Flags shared by various lookup functions. */
+typedef enum _notmuch_find_flags {
+ /* Lookup without creating any documents. This is the default
+ * behavior. */
+ NOTMUCH_FIND_LOOKUP = 0,
+ /* If set, create the necessary document (or documents) if they
+ * are missing. Requires a read/write database. */
+ NOTMUCH_FIND_CREATE = 1<<0,
+} notmuch_find_flags_t;
+
typedef struct _notmuch_doc_id_set notmuch_doc_id_set_t;
+typedef struct _notmuch_string_list notmuch_string_list_t;
+
/* database.cc */
/* Lookup a prefix value by name.
@@ -186,6 +198,7 @@ _notmuch_database_find_unique_doc_id (notmuch_database_t *notmuch,
notmuch_status_t
_notmuch_database_find_directory_id (notmuch_database_t *database,
const char *path,
+ notmuch_find_flags_t flags,
unsigned int *directory_id);
const char *
@@ -197,6 +210,7 @@ notmuch_status_t
_notmuch_database_filename_to_direntry (void *ctx,
notmuch_database_t *notmuch,
const char *filename,
+ notmuch_find_flags_t flags,
char **direntry);
/* directory.cc */
@@ -204,6 +218,7 @@ _notmuch_database_filename_to_direntry (void *ctx,
notmuch_directory_t *
_notmuch_directory_create (notmuch_database_t *notmuch,
const char *path,
+ notmuch_find_flags_t flags,
notmuch_status_t *status_ret);
unsigned int
@@ -216,6 +231,7 @@ _notmuch_thread_create (void *ctx,
notmuch_database_t *notmuch,
unsigned int seed_doc_id,
notmuch_doc_id_set_t *match_set,
+ notmuch_string_list_t *excluded_terms,
notmuch_sort_t sort);
/* message.cc */
@@ -401,6 +417,7 @@ typedef struct _notmuch_message_list {
*/
struct visible _notmuch_messages {
notmuch_bool_t is_of_list_type;
+ notmuch_doc_id_set_t *excluded_doc_ids;
notmuch_message_node_t *iterator;
};
@@ -458,11 +475,11 @@ typedef struct _notmuch_string_node {
struct _notmuch_string_node *next;
} notmuch_string_node_t;
-typedef struct _notmuch_string_list {
+struct visible _notmuch_string_list {
int length;
notmuch_string_node_t *head;
notmuch_string_node_t **tail;
-} notmuch_string_list_t;
+};
notmuch_string_list_t *
_notmuch_string_list_create (const void *ctx);
@@ -491,8 +508,26 @@ notmuch_filenames_t *
_notmuch_filenames_create (const void *ctx,
notmuch_string_list_t *list);
-#pragma GCC visibility pop
-
NOTMUCH_END_DECLS
+#ifdef __cplusplus
+/* Implicit typecast from 'void *' to 'T *' is okay in C, but not in
+ * C++. In talloc_steal, an explicit cast is provided for type safety
+ * in some GCC versions. Otherwise, a cast is required. Provide a
+ * template function for this to maintain type safety, and redefine
+ * talloc_steal to use it.
+ */
+#if !(__GNUC__ >= 3)
+template <class T> T *
+_notmuch_talloc_steal (const void *new_ctx, const T *ptr)
+{
+ return static_cast<T *> (talloc_steal (new_ctx, ptr));
+}
+#undef talloc_steal
+#define talloc_steal _notmuch_talloc_steal
+#endif
+#endif
+
+#pragma GCC visibility pop
+
#endif
diff --git a/lib/notmuch.h b/lib/notmuch.h
index 9f23a10..3633bed 100644
--- a/lib/notmuch.h
+++ b/lib/notmuch.h
@@ -133,27 +133,38 @@ typedef struct _notmuch_filenames notmuch_filenames_t;
*
* After a successful call to notmuch_database_create, the returned
* database will be open so the caller should call
- * notmuch_database_close when finished with it.
+ * notmuch_database_destroy when finished with it.
*
* The database will not yet have any data in it
* (notmuch_database_create itself is a very cheap function). Messages
* contained within 'path' can be added to the database by calling
* notmuch_database_add_message.
*
- * In case of any failure, this function returns NULL, (after printing
- * an error message on stderr).
+ * In case of any failure, this function returns an error status and
+ * sets *database to NULL (after printing an error message on stderr).
+ *
+ * Return value:
+ *
+ * NOTMUCH_STATUS_SUCCESS: Successfully created the database.
+ *
+ * NOTMUCH_STATUS_NULL_POINTER: The given 'path' argument is NULL.
+ *
+ * NOTMUCH_STATUS_OUT_OF_MEMORY: Out of memory.
+ *
+ * NOTMUCH_STATUS_FILE_ERROR: An error occurred trying to create the
+ * database file (such as permission denied, or file not found,
+ * etc.), or the database already exists.
+ *
+ * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred.
*/
-notmuch_database_t *
-notmuch_database_create (const char *path);
+notmuch_status_t
+notmuch_database_create (const char *path, notmuch_database_t **database);
typedef enum {
NOTMUCH_DATABASE_MODE_READ_ONLY = 0,
NOTMUCH_DATABASE_MODE_READ_WRITE
} notmuch_database_mode_t;
-/* XXX: I think I'd like this to take an extra argument of
- * notmuch_status_t* for returning a status value on failure. */
-
/* Open an existing notmuch database located at 'path'.
*
* The database should have been created at some time in the past,
@@ -165,21 +176,50 @@ typedef enum {
* An existing notmuch database can be identified by the presence of a
* directory named ".notmuch" below 'path'.
*
- * The caller should call notmuch_database_close when finished with
+ * The caller should call notmuch_database_destroy when finished with
* this database.
*
- * In case of any failure, this function returns NULL, (after printing
- * an error message on stderr).
+ * In case of any failure, this function returns an error status and
+ * sets *database to NULL (after printing an error message on stderr).
+ *
+ * Return value:
+ *
+ * NOTMUCH_STATUS_SUCCESS: Successfully opened the database.
+ *
+ * NOTMUCH_STATUS_NULL_POINTER: The given 'path' argument is NULL.
+ *
+ * NOTMUCH_STATUS_OUT_OF_MEMORY: Out of memory.
+ *
+ * NOTMUCH_STATUS_FILE_ERROR: An error occurred trying to open the
+ * database file (such as permission denied, or file not found,
+ * etc.), or the database version is unknown.
+ *
+ * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred.
*/
-notmuch_database_t *
+notmuch_status_t
notmuch_database_open (const char *path,
- notmuch_database_mode_t mode);
+ notmuch_database_mode_t mode,
+ notmuch_database_t **database);
-/* Close the given notmuch database, freeing all associated
- * resources. See notmuch_database_open. */
+/* Close the given notmuch database.
+ *
+ * After notmuch_database_close has been called, calls to other
+ * functions on objects derived from this database may either behave
+ * as if the database had not been closed (e.g., if the required data
+ * has been cached) or may fail with a
+ * NOTMUCH_STATUS_XAPIAN_EXCEPTION.
+ *
+ * notmuch_database_close can be called multiple times. Later calls
+ * have no effect.
+ */
void
notmuch_database_close (notmuch_database_t *database);
+/* Destroy the notmuch database, closing it if necessary and freeing
+* all associated resources. */
+void
+notmuch_database_destroy (notmuch_database_t *database);
+
/* Return the database path of the given database.
*
* The return value is a string owned by notmuch so should not be
@@ -260,11 +300,22 @@ notmuch_database_end_atomic (notmuch_database_t *notmuch);
* (see notmuch_database_get_path), or else should be an absolute path
* with initial components that match the path of 'database'.
*
- * Can return NULL if a Xapian exception occurs.
+ * If this directory object does not exist in the database, this
+ * returns NOTMUCH_STATUS_SUCCESS and sets *directory to NULL.
+ *
+ * Return value:
+ *
+ * NOTMUCH_STATUS_SUCCESS: Successfully retrieved directory.
+ *
+ * NOTMUCH_STATUS_NULL_POINTER: The given 'directory' argument is NULL.
+ *
+ * NOTMUCH_STATUS_XAPIAN_EXCEPTION: A Xapian exception occurred;
+ * directory not retrieved.
*/
-notmuch_directory_t *
+notmuch_status_t
notmuch_database_get_directory (notmuch_database_t *database,
- const char *path);
+ const char *path,
+ notmuch_directory_t **directory);
/* Add a new message to the given notmuch database or associate an
* additional filename with an existing message.
@@ -449,6 +500,26 @@ typedef enum {
const char *
notmuch_query_get_query_string (notmuch_query_t *query);
+/* Specify whether to omit excluded results or simply flag them. By
+ * default, this is set to TRUE.
+ *
+ * If this is TRUE, notmuch_query_search_messages will omit excluded
+ * messages from the results. notmuch_query_search_threads will omit
+ * threads that match only in excluded messages, but will include all
+ * messages in threads that match in at least one non-excluded
+ * message.
+ *
+ * The performance difference when calling
+ * notmuch_query_search_messages should be relatively small (and both
+ * should be very fast). However, in some cases,
+ * notmuch_query_search_threads is very much faster when omitting
+ * excluded messages as it does not need to construct the threads that
+ * only match in excluded messages.
+ */
+
+void
+notmuch_query_set_omit_excluded (notmuch_query_t *query, notmuch_bool_t omit_excluded);
+
/* Specify the sorting desired for this query. */
void
notmuch_query_set_sort (notmuch_query_t *query, notmuch_sort_t sort);
@@ -457,6 +528,12 @@ notmuch_query_set_sort (notmuch_query_t *query, notmuch_sort_t sort);
notmuch_sort_t
notmuch_query_get_sort (notmuch_query_t *query);
+/* Add a tag that will be excluded from the query results by default.
+ * This exclusion will be overridden if this tag appears explicitly in
+ * the query. */
+void
+notmuch_query_add_tag_exclude (notmuch_query_t *query, const char *tag);
+
/* Execute a query for threads, returning a notmuch_threads_t object
* which can be used to iterate over the results. The returned threads
* object is owned by the query and as such, will only be valid until
@@ -659,8 +736,10 @@ notmuch_thread_get_toplevel_messages (notmuch_thread_t *thread);
/* Get the number of messages in 'thread' that matched the search.
*
* This count includes only the messages in this thread that were
- * matched by the search from which the thread was created. Contrast
- * with notmuch_thread_get_total_messages() .
+ * matched by the search from which the thread was created and were
+ * not excluded by any exclude tags passed in with the query (see
+ * notmuch_query_add_tag_exclude). Contrast with
+ * notmuch_thread_get_total_messages() .
*/
int
notmuch_thread_get_matched_messages (notmuch_thread_t *thread);
@@ -889,7 +968,8 @@ notmuch_message_get_filenames (notmuch_message_t *message);
/* Message flags */
typedef enum _notmuch_message_flag {
- NOTMUCH_MESSAGE_FLAG_MATCH
+ NOTMUCH_MESSAGE_FLAG_MATCH,
+ NOTMUCH_MESSAGE_FLAG_EXCLUDED
} notmuch_message_flag_t;
/* Get a value of a flag for the email corresponding to 'message'. */
diff --git a/lib/query.cc b/lib/query.cc
index b6c0f12..e9c1a2d 100644
--- a/lib/query.cc
+++ b/lib/query.cc
@@ -27,6 +27,8 @@ struct _notmuch_query {
notmuch_database_t *notmuch;
const char *query_string;
notmuch_sort_t sort;
+ notmuch_string_list_t *exclude_terms;
+ notmuch_bool_t omit_excluded;
};
typedef struct _notmuch_mset_messages {
@@ -56,15 +58,27 @@ struct visible _notmuch_threads {
notmuch_doc_id_set_t match_set;
};
+/* We need this in the message functions so forward declare. */
+static notmuch_bool_t
+_notmuch_doc_id_set_init (void *ctx,
+ notmuch_doc_id_set_t *doc_ids,
+ GArray *arr);
+
+static notmuch_bool_t
+_debug_query (void)
+{
+ char *env = getenv ("NOTMUCH_DEBUG_QUERY");
+ return (env && strcmp (env, "") != 0);
+}
+
notmuch_query_t *
notmuch_query_create (notmuch_database_t *notmuch,
const char *query_string)
{
notmuch_query_t *query;
-#ifdef DEBUG_QUERY
- fprintf (stderr, "Query string is:\n%s\n", query_string);
-#endif
+ if (_debug_query ())
+ fprintf (stderr, "Query string is:\n%s\n", query_string);
query = talloc (NULL, notmuch_query_t);
if (unlikely (query == NULL))
@@ -76,6 +90,10 @@ notmuch_query_create (notmuch_database_t *notmuch,
query->sort = NOTMUCH_SORT_NEWEST_FIRST;
+ query->exclude_terms = _notmuch_string_list_create (query);
+
+ query->omit_excluded = TRUE;
+
return query;
}
@@ -86,6 +104,12 @@ notmuch_query_get_query_string (notmuch_query_t *query)
}
void
+notmuch_query_set_omit_excluded (notmuch_query_t *query, notmuch_bool_t omit_excluded)
+{
+ query->omit_excluded = omit_excluded;
+}
+
+void
notmuch_query_set_sort (notmuch_query_t *query, notmuch_sort_t sort)
{
query->sort = sort;
@@ -97,6 +121,13 @@ notmuch_query_get_sort (notmuch_query_t *query)
return query->sort;
}
+void
+notmuch_query_add_tag_exclude (notmuch_query_t *query, const char *tag)
+{
+ char *term = talloc_asprintf (query, "%s%s", _find_prefix ("tag"), tag);
+ _notmuch_string_list_append (query->exclude_terms, term);
+}
+
/* We end up having to call the destructors explicitly because we had
* to use "placement new" in order to initialize C++ objects within a
* block that we allocated with talloc. So C++ is making talloc
@@ -112,6 +143,33 @@ _notmuch_messages_destructor (notmuch_mset_messages_t *messages)
return 0;
}
+/* Return a query that matches messages with the excluded tags
+ * registered with query. Any tags that explicitly appear in xquery
+ * will not be excluded, and will be removed from the list of exclude
+ * tags. The caller of this function has to combine the returned
+ * query appropriately.*/
+static Xapian::Query
+_notmuch_exclude_tags (notmuch_query_t *query, Xapian::Query xquery)
+{
+ Xapian::Query exclude_query = Xapian::Query::MatchNothing;
+
+ for (notmuch_string_node_t *term = query->exclude_terms->head; term;
+ term = term->next) {
+ Xapian::TermIterator it = xquery.get_terms_begin ();
+ Xapian::TermIterator end = xquery.get_terms_end ();
+ for (; it != end; it++) {
+ if ((*it).compare (term->string) == 0)
+ break;
+ }
+ if (it == end)
+ exclude_query = Xapian::Query (Xapian::Query::OP_OR,
+ exclude_query, Xapian::Query (term->string));
+ else
+ term->string = talloc_strdup (query, "");
+ }
+ return exclude_query;
+}
+
notmuch_messages_t *
notmuch_query_search_messages (notmuch_query_t *query)
{
@@ -137,8 +195,9 @@ notmuch_query_search_messages (notmuch_query_t *query)
Xapian::Query mail_query (talloc_asprintf (query, "%s%s",
_find_prefix ("type"),
"mail"));
- Xapian::Query string_query, final_query;
+ Xapian::Query string_query, final_query, exclude_query;
Xapian::MSet mset;
+ Xapian::MSetIterator iterator;
unsigned int flags = (Xapian::QueryParser::FLAG_BOOLEAN |
Xapian::QueryParser::FLAG_PHRASE |
Xapian::QueryParser::FLAG_LOVEHATE |
@@ -156,6 +215,36 @@ notmuch_query_search_messages (notmuch_query_t *query)
final_query = Xapian::Query (Xapian::Query::OP_AND,
mail_query, string_query);
}
+ messages->base.excluded_doc_ids = NULL;
+
+ if (query->exclude_terms) {
+ exclude_query = _notmuch_exclude_tags (query, final_query);
+
+ if (query->omit_excluded)
+ final_query = Xapian::Query (Xapian::Query::OP_AND_NOT,
+ final_query, exclude_query);
+ else {
+ exclude_query = Xapian::Query (Xapian::Query::OP_AND,
+ exclude_query, final_query);
+
+ enquire.set_weighting_scheme (Xapian::BoolWeight());
+ enquire.set_query (exclude_query);
+
+ mset = enquire.get_mset (0, notmuch->xapian_db->get_doccount ());
+
+ GArray *excluded_doc_ids = g_array_new (FALSE, FALSE, sizeof (unsigned int));
+
+ for (iterator = mset.begin (); iterator != mset.end (); iterator++) {
+ unsigned int doc_id = *iterator;
+ g_array_append_val (excluded_doc_ids, doc_id);
+ }
+ messages->base.excluded_doc_ids = talloc (messages, _notmuch_doc_id_set);
+ _notmuch_doc_id_set_init (query, messages->base.excluded_doc_ids,
+ excluded_doc_ids);
+ g_array_unref (excluded_doc_ids);
+ }
+ }
+
enquire.set_weighting_scheme (Xapian::BoolWeight());
@@ -173,9 +262,12 @@ notmuch_query_search_messages (notmuch_query_t *query)
break;
}
-#if DEBUG_QUERY
- fprintf (stderr, "Final query is:\n%s\n", final_query.get_description().c_str());
-#endif
+ if (_debug_query ()) {
+ fprintf (stderr, "Exclude query is:\n%s\n",
+ exclude_query.get_description ().c_str ());
+ fprintf (stderr, "Final query is:\n%s\n",
+ final_query.get_description ().c_str ());
+ }
enquire.set_query (final_query);
@@ -244,6 +336,10 @@ _notmuch_mset_messages_get (notmuch_messages_t *messages)
INTERNAL_ERROR ("a messages iterator contains a non-existent document ID.\n");
}
+ if (messages->excluded_doc_ids &&
+ _notmuch_doc_id_set_contains (messages->excluded_doc_ids, doc_id))
+ notmuch_message_set_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED, TRUE);
+
return message;
}
@@ -389,6 +485,7 @@ notmuch_threads_get (notmuch_threads_t *threads)
threads->query->notmuch,
doc_id,
&threads->match_set,
+ threads->query->exclude_terms,
threads->query->sort);
}
@@ -416,7 +513,7 @@ notmuch_query_count_messages (notmuch_query_t *query)
Xapian::Query mail_query (talloc_asprintf (query, "%s%s",
_find_prefix ("type"),
"mail"));
- Xapian::Query string_query, final_query;
+ Xapian::Query string_query, final_query, exclude_query;
Xapian::MSet mset;
unsigned int flags = (Xapian::QueryParser::FLAG_BOOLEAN |
Xapian::QueryParser::FLAG_PHRASE |
@@ -436,12 +533,20 @@ notmuch_query_count_messages (notmuch_query_t *query)
mail_query, string_query);
}
+ exclude_query = _notmuch_exclude_tags (query, final_query);
+
+ final_query = Xapian::Query (Xapian::Query::OP_AND_NOT,
+ final_query, exclude_query);
+
enquire.set_weighting_scheme(Xapian::BoolWeight());
enquire.set_docid_order(Xapian::Enquire::ASCENDING);
-#if DEBUG_QUERY
- fprintf (stderr, "Final query is:\n%s\n", final_query.get_description().c_str());
-#endif
+ if (_debug_query ()) {
+ fprintf (stderr, "Exclude query is:\n%s\n",
+ exclude_query.get_description ().c_str ());
+ fprintf (stderr, "Final query is:\n%s\n",
+ final_query.get_description ().c_str ());
+ }
enquire.set_query (final_query);
diff --git a/lib/thread.cc b/lib/thread.cc
index 0435ee6..e976d64 100644
--- a/lib/thread.cc
+++ b/lib/thread.cc
@@ -214,7 +214,8 @@ _thread_cleanup_author (notmuch_thread_t *thread,
*/
static void
_thread_add_message (notmuch_thread_t *thread,
- notmuch_message_t *message)
+ notmuch_message_t *message,
+ notmuch_string_list_t *exclude_terms)
{
notmuch_tags_t *tags;
const char *tag;
@@ -262,6 +263,15 @@ _thread_add_message (notmuch_thread_t *thread,
notmuch_tags_move_to_next (tags))
{
tag = notmuch_tags_get (tags);
+ /* Mark excluded messages. */
+ for (notmuch_string_node_t *term = exclude_terms->head; term;
+ term = term->next) {
+ /* We ignore initial 'K'. */
+ if (strcmp(tag, (term->string + 1)) == 0) {
+ notmuch_message_set_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED, TRUE);
+ break;
+ }
+ }
g_hash_table_insert (thread->tags, xstrdup (tag), NULL);
}
}
@@ -321,7 +331,8 @@ _thread_add_matched_message (notmuch_thread_t *thread,
_thread_set_subject_from_message (thread, message);
}
- thread->matched_messages++;
+ if (!notmuch_message_get_flag (message, NOTMUCH_MESSAGE_FLAG_EXCLUDED))
+ thread->matched_messages++;
if (g_hash_table_lookup_extended (thread->message_hash,
notmuch_message_get_message_id (message), NULL,
@@ -392,6 +403,7 @@ _notmuch_thread_create (void *ctx,
notmuch_database_t *notmuch,
unsigned int seed_doc_id,
notmuch_doc_id_set_t *match_set,
+ notmuch_string_list_t *exclude_terms,
notmuch_sort_t sort)
{
notmuch_thread_t *thread;
@@ -467,7 +479,7 @@ _notmuch_thread_create (void *ctx,
if (doc_id == seed_doc_id)
message = seed_message;
- _thread_add_message (thread, message);
+ _thread_add_message (thread, message, exclude_terms);
if ( _notmuch_doc_id_set_contains (match_set, doc_id)) {
_notmuch_doc_id_set_remove (match_set, doc_id);