- /* Add the message to the index.
- * Even if adding the message to the notmuch database fails,
- * the message is on disk and we consider the delivery completed. */
- add_file_to_database (notmuch, newpath, tag_ops,
- synchronize_flags);
+ status = notmuch_database_open (notmuch_config_get_database_path (config),
+ NOTMUCH_DATABASE_MODE_READ_WRITE, ¬much);
+ if (status)
+ return keep ? NOTMUCH_STATUS_SUCCESS : status_to_exit (status);
+
+ notmuch_exit_if_unmatched_db_uuid (notmuch);
+
+
+ /* Index the message. */
+ status = add_file (notmuch, newpath, tag_ops, synchronize_flags, keep);
+
+ /* Commit changes. */
+ close_status = notmuch_database_destroy (notmuch);
+ if (close_status) {
+ /* Hold on to the first error, if any. */
+ if (! status)
+ status = close_status;
+ fprintf (stderr, "%s: failed to commit database changes: %s\n",
+ keep ? "Warning" : "Error",
+ notmuch_status_to_string (close_status));
+ }
+
+ if (status) {
+ if (keep) {
+ status = NOTMUCH_STATUS_SUCCESS;
+ } else {
+ /* If maildir flag sync failed, this might fail. */
+ if (unlink (newpath)) {
+ fprintf (stderr, "Warning: failed to remove '%s' from maildir "
+ "after errors: %s. Please run 'notmuch new' to fix.\n",
+ newpath, strerror (errno));
+ }
+ }
+ }
+
+ if (! no_hooks && status == NOTMUCH_STATUS_SUCCESS) {
+ /* Ignore hook failures. */
+ notmuch_run_hook (db_path, "post-insert");
+ }