]> git.notmuchmail.org Git - notmuch/blobdiff - lib/database.cc
lib: use the compaction backup path provided by the caller
[notmuch] / lib / database.cc
index eadf8a763395187afccd18cefad47e1cca8d2650..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,11 +863,11 @@ 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;
     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;
@@ -895,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;
        }
@@ -913,7 +910,7 @@ notmuch_database_compact (const char* path,
     }
 
     try {
-       NotmuchCompactor compactor(status_cb);
+       NotmuchCompactor compactor(status_cb, closure);
 
        compactor.set_renumber(false);
        compactor.add_source(xapian_path);
@@ -925,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;
@@ -953,7 +950,8 @@ DONE:
 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;