X-Git-Url: https://git.notmuchmail.org/git?p=notmuch;a=blobdiff_plain;f=lib%2Fdatabase.cc;h=a021bf17253cd9ceecfa02a3b4c28d5f5583355d;hp=3dfea0f4f9e27b48de199ed539f0d58e8614b684;hb=00d2ac2b41a6dba060837244ee87edce2fd1e465;hpb=8e4e537ceefec51fc811d0be3027572a904ba38c diff --git a/lib/database.cc b/lib/database.cc index 3dfea0f4..a021bf17 100644 --- a/lib/database.cc +++ b/lib/database.cc @@ -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,15 +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); + 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, ¬much); if (ret) { goto DONE; @@ -891,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; } @@ -909,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); @@ -921,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; @@ -937,17 +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 (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;