]> git.notmuchmail.org Git - notmuch/blobdiff - lib/database.cc
lib: use the compaction backup path provided by the caller
[notmuch] / lib / database.cc
index 06f1c0a18043e2551c56290265a461f742ed39f5..a021bf17253cd9ceecfa02a3b4c28d5f5583355d 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,19 @@ 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;
@@ -892,13 +897,8 @@ notmuch_database_compact (const char* path,
     }
 
     if (backup_path != NULL) {
-       if (! (old_xapian_path = talloc_asprintf (local, "%s/xapian.old", backup_path))) {
-           ret = NOTMUCH_STATUS_OUT_OF_MEMORY;
-           goto DONE;
-       }
-
-       if (stat(old_xapian_path, &statbuf) != -1) {
-           fprintf (stderr, "Backup path already exists: %s\n", old_xapian_path);
+       if (stat(backup_path, &statbuf) != -1) {
+           fprintf (stderr, "Backup path already exists: %s\n", backup_path);
            ret = NOTMUCH_STATUS_FILE_ERROR;
            goto DONE;
        }
@@ -910,6 +910,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);
@@ -920,8 +922,8 @@ notmuch_database_compact (const char* path,
        goto DONE;
     }
 
-    if (old_xapian_path != NULL) {
-       if (rename(xapian_path, old_xapian_path)) {
+    if (backup_path) {
+       if (rename(xapian_path, backup_path)) {
            fprintf (stderr, "Error moving old database out of the way\n");
            ret = NOTMUCH_STATUS_FILE_ERROR;
            goto DONE;
@@ -936,15 +938,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_close (unused (notmuch_database_t *notmuch))
+notmuch_database_compact (unused (const char* path),
+                         unused (const char* backup_path),
+                         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;