]> git.notmuchmail.org Git - notmuch/blobdiff - lib/database.cc
Merge branch 'release'
[notmuch] / lib / database.cc
index 88be939138c7e6db51fc2e1fc70313bb170d2a1e..8f8df1a1434ed604ca2fe1a7bdf5f87b7084a28d 100644 (file)
@@ -26,6 +26,9 @@
 #include <signal.h>
 
 #include <glib.h> /* g_free, GPtrArray, GHashTable */
+#include <glib-object.h> /* g_type_init */
+
+#include <gmime/gmime.h> /* g_mime_init */
 
 using namespace std;
 
@@ -80,13 +83,17 @@ typedef struct {
  *                     STRING is the name of a file within that
  *                     directory for this mail message.
  *
- *    A mail document also has two values:
+ *    A mail document also has four values:
  *
  *     TIMESTAMP:      The time_t value corresponding to the message's
  *                     Date header.
  *
  *     MESSAGE_ID:     The unique ID of the mail mess (see "id" above)
  *
+ *     FROM:           The value of the "From" header
+ *
+ *     SUBJECT:        The value of the "Subject" header
+ *
  * In addition, terms from the content of the message are added with
  * "from", "to", "attachment", and "subject" prefixes for use by the
  * user in searching. Similarly, terms from the path of the mail
@@ -575,14 +582,15 @@ notmuch_database_t *
 notmuch_database_open (const char *path,
                       notmuch_database_mode_t mode)
 {
+    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 (! (notmuch_path = talloc_asprintf (local, "%s/%s", path, ".notmuch"))) {
        fprintf (stderr, "Out of memory\n");
        goto DONE;
     }
@@ -594,13 +602,21 @@ notmuch_database_open (const char *path,
        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");
        goto DONE;
     }
 
-    notmuch = talloc (NULL, notmuch_database_t);
+    /* Initialize the GLib type system and threads */
+    g_type_init ();
+
+    /* Initialize gmime */
+    if (! initialized) {
+       g_mime_init (0);
+       initialized = 1;
+    }
+
+    notmuch = talloc_zero (NULL, notmuch_database_t);
     notmuch->exception_reported = FALSE;
     notmuch->path = talloc_strdup (notmuch, path);
 
@@ -686,14 +702,12 @@ 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_close (notmuch);
        notmuch = NULL;
     }
 
   DONE:
-    if (notmuch_path)
-       free (notmuch_path);
-    if (xapian_path)
-       free (xapian_path);
+    talloc_free (local);
 
     return notmuch;
 }
@@ -702,7 +716,8 @@ 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) {
@@ -711,6 +726,17 @@ 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;
     delete notmuch->query_parser;
     delete notmuch->xapian_db;
@@ -1015,7 +1041,7 @@ notmuch_database_end_atomic (notmuch_database_t *notmuch)
         * However, we rely on flushing to test atomicity. */
        const char *thresh = getenv ("XAPIAN_FLUSH_THRESHOLD");
        if (thresh && atoi (thresh) == 1)
-           db->commit ();
+           db->flush ();
     } catch (const Xapian::Error &error) {
        fprintf (stderr, "A Xapian exception occurred committing transaction: %s.\n",
                 error.get_msg().c_str());
@@ -1443,7 +1469,7 @@ _notmuch_database_link_message_to_parents (notmuch_database_t *notmuch,
     keys = g_hash_table_get_keys (parents);
     for (l = keys; l; l = l->next) {
        char *parent_message_id;
-       const char *parent_thread_id;
+       const char *parent_thread_id = NULL;
 
        parent_message_id = (char *) l->data;
 
@@ -1725,7 +1751,7 @@ notmuch_database_add_message (notmuch_database_t *notmuch,
                goto DONE;
 
            date = notmuch_message_file_get_header (message_file, "date");
-           _notmuch_message_set_date (message, date);
+           _notmuch_message_set_header_values (message, date, from, subject);
 
            _notmuch_message_index_file (message, filename);
        } else {