]> git.notmuchmail.org Git - notmuch/blobdiff - lib/database.cc
lib: add closure parameter to compact status update callback
[notmuch] / lib / database.cc
index 20e5ec23f9da7fcd8d7284c96cf54324c9d32643..5a017034c2af51ed824e839e92cdb20329b94f4e 100644 (file)
@@ -821,9 +821,11 @@ static int rmtree (const char *path)
 class NotmuchCompactor : public Xapian::Compactor
 {
     notmuch_compact_status_cb_t status_cb;
+    void *status_closure;
 
 public:
-    NotmuchCompactor(notmuch_compact_status_cb_t cb) : status_cb(cb) { }
+    NotmuchCompactor(notmuch_compact_status_cb_t cb, void *closure) :
+       status_cb(cb), status_closure(closure) { }
 
     virtual void
     set_status (const std::string &table, const std::string &status)
@@ -842,7 +844,7 @@ public:
            return;
        }
 
-       status_cb(msg);
+       status_cb(msg, status_closure);
        talloc_free(msg);
     }
 };
@@ -861,16 +863,20 @@ public:
 notmuch_status_t
 notmuch_database_compact (const char* path,
                          const char* backup_path,
-                         notmuch_compact_status_cb_t status_cb)
+                         notmuch_compact_status_cb_t status_cb,
+                         void *closure)
 {
-    void *local = talloc_new (NULL);
-    NotmuchCompactor compactor(status_cb);
+    void *local;
     char *notmuch_path, *xapian_path, *compact_xapian_path;
     char *old_xapian_path = NULL;
     notmuch_status_t ret = NOTMUCH_STATUS_SUCCESS;
     notmuch_database_t *notmuch = NULL;
     struct stat statbuf;
 
+    local = talloc_new (NULL);
+    if (! local)
+       return NOTMUCH_STATUS_OUT_OF_MEMORY;
+
     ret = notmuch_database_open(path, NOTMUCH_DATABASE_MODE_READ_WRITE, &notmuch);
     if (ret) {
        goto DONE;
@@ -910,6 +916,8 @@ notmuch_database_compact (const char* path,
     }
 
     try {
+       NotmuchCompactor compactor(status_cb, closure);
+
        compactor.set_renumber(false);
        compactor.add_source(xapian_path);
        compactor.set_destdir(compact_xapian_path);
@@ -936,17 +944,20 @@ notmuch_database_compact (const char* path,
        goto DONE;
     }
 
-    notmuch_database_close(notmuch);
-
 DONE:
+    if (notmuch)
+       notmuch_database_destroy (notmuch);
+
     talloc_free(local);
+
     return ret;
 }
 #else
 notmuch_status_t
 notmuch_database_compact (unused (const char* path),
                          unused (const char* backup_path),
-                         unused (notmuch_compact_status_cb_t status_cb))
+                         unused (notmuch_compact_status_cb_t status_cb),
+                         unused (void *closure))
 {
     fprintf (stderr, "notmuch was compiled against a xapian version lacking compaction support.\n");
     return NOTMUCH_STATUS_UNSUPPORTED_OPERATION;