lib: add closure parameter to compact status update callback
authorJani Nikula <jani@nikula.org>
Sun, 3 Nov 2013 12:24:45 +0000 (14:24 +0200)
committerDavid Bremner <david@tethera.net>
Thu, 7 Nov 2013 10:46:42 +0000 (06:46 -0400)
This provides much more flexibility for the caller.

lib/database.cc
lib/notmuch.h
notmuch-compact.c

index eadf8a763395187afccd18cefad47e1cca8d2650..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,7 +863,8 @@ 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;
@@ -913,7 +916,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);
@@ -953,7 +956,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;
index 9dab555fdc1eb86f119028337ce4e46f960850e3..cd301a4f0c6b9bd31dc6c90d524fa7d4271a0686 100644 (file)
@@ -219,7 +219,7 @@ notmuch_database_close (notmuch_database_t *database);
 /* A callback invoked by notmuch_database_compact to notify the user
  * of the progress of the compaction process.
  */
-typedef void (*notmuch_compact_status_cb_t)(const char*);
+typedef void (*notmuch_compact_status_cb_t)(const char *message, void *closure);
 
 /* Compact a notmuch database, backing up the original database to the
  * given path.
@@ -231,7 +231,8 @@ typedef void (*notmuch_compact_status_cb_t)(const char*);
 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);
 
 /* Destroy the notmuch database, closing it if necessary and freeing
  * all associated resources.
index bfda40e3a7f3dcaf6a99855981dd37ded8e66bd5..ee7afcf6b9a0c32be4e01ff1881c572bc07bf5fe 100644 (file)
 
 #include "notmuch-client.h"
 
-void status_update_cb (const char *msg);
-
-void
-status_update_cb (const char *msg)
+static void
+status_update_cb (const char *msg, unused (void *closure))
 {
     printf("%s\n", msg);
 }
@@ -38,7 +36,7 @@ notmuch_compact_command (notmuch_config_t *config,
     notmuch_status_t ret;
 
     printf ("Compacting database...\n");
-    ret = notmuch_database_compact (path, backup_path, status_update_cb);
+    ret = notmuch_database_compact (path, backup_path, status_update_cb, NULL);
     if (ret) {
        fprintf (stderr, "Compaction failed: %s\n", notmuch_status_to_string(ret));
     } else {