flags |= PAKFIRE_FLAGS_DISABLE_SNAPSHOT;
}
+ // Configure callbacks
+ struct pakfire_callbacks callbacks = {
+ .data = logger,
+ .log = (logger) ? Pakfire_logging_callback : NULL,
+ };
+
// Create a new Pakfire instance
- int r = pakfire_create(&self->pakfire, path, arch, conf, flags,
- (logger) ? Pakfire_logging_callback : NULL, logger);
+ int r = pakfire_create(&self->pakfire, path, arch, conf, flags, &callbacks);
if (r) {
switch (errno) {
// Invalid architecture or path
PAKFIRE_FLAGS_DISABLE_RAMDISK = (1 << 5),
};
-typedef void (*pakfire_log_function_t)(void* data, int priority, const char* file,
+// Callbacks
+
+struct pakfire_callbacks {
+ void* data;
+
+ // Logging
+ void (*log)(void* data, int priority, const char* file,
int line, const char* fn, const char* format, va_list args);
+ // Status, Progress & Speed
+ void (*status)(void* data, const char* message);
+ void (*progress)(void* data, double progress);
+ void (*speed)(void* data, const char* speed);
+};
+
int pakfire_create(struct pakfire** pakfire, const char* path, const char* arch,
- const char* conf, int flags, pakfire_log_function_t log, void* data);
+ const char* conf, int flags, const struct pakfire_callbacks* callbacks);
struct pakfire* pakfire_ref(struct pakfire* pakfire);
struct pakfire* pakfire_unref(struct pakfire* pakfire);
struct pakfire_config* pakfire_get_config(struct pakfire* pakfire);
int pakfire_is_mountpoint(struct pakfire* pakfire, const char* path);
+void pakfire_call_status_callback(struct pakfire* pakfire, const char* message, ...);
+void pakfire_call_progress_callback(struct pakfire* pakfire, double progress);
+
gpgme_ctx_t pakfire_get_gpgctx(struct pakfire* pakfire);
const char* pakfire_get_distro_name(struct pakfire* pakfire);
};
struct pakfire {
+ int nrefs;
+
char path[PATH_MAX];
char cache_path[PATH_MAX];
char arch[ARCH_MAX];
// Pool
Pool* pool;
+ // Callbacks
+ struct pakfire_callbacks callbacks;
+
// Logging
- pakfire_log_function_t log_function;
- void* log_data;
int log_priority;
- int nrefs;
-
struct pakfire_config* config;
STAILQ_HEAD(mountpoints, mountpoint) mountpoints;
return 0;
}
-static void pakfire_log_set_function(struct pakfire* pakfire,
- pakfire_log_function_t log_function, void* data) {
- pakfire->log_function = log_function;
- pakfire->log_data = data;
-}
-
static int log_priority(const char* priority) {
char* end;
}
PAKFIRE_EXPORT int pakfire_create(struct pakfire** pakfire, const char* path,
- const char* arch, const char* conf, int flags, pakfire_log_function_t log,
- void* data) {
+ const char* arch, const char* conf, int flags,
+ const struct pakfire_callbacks* callbacks) {
char tempdir[PATH_MAX] = PAKFIRE_TMP_DIR "/XXXXXX";
int r = 1;
p->nrefs = 1;
p->flags = flags;
+ // Copy callbacks
+ if (callbacks) {
+ memcpy(&p->callbacks, callbacks, sizeof(p->callbacks));
+
+ // Set default callbacks
+ } else {
+ // Log to syslog by default
+ p->callbacks.log = pakfire_log_syslog;
+ }
+
// Set architecture
pakfire_string_set(p->arch, arch);
// Setup logging
- if (log)
- pakfire_log_set_function(p, log, data);
- else
- pakfire_log_set_function(p, pakfire_log_syslog, NULL);
-
const char* env = secure_getenv("PAKFIRE_LOG");
if (env)
pakfire_log_set_priority(p, log_priority(env));
return pakfire->path;
}
+void pakfire_call_status_callback(struct pakfire* pakfire, const char* message, ...) {
+ char* buffer = NULL;
+ va_list args;
+ int r;
+
+ // Format the message
+ va_start(args, message);
+ r = vasprintf(&buffer, message, args);
+ va_end(args);
+
+ if (r < 0)
+ return;
+
+ // Call the callback
+ pakfire->callbacks.status(pakfire->callbacks.data, buffer);
+
+ // Cleanup
+ if (buffer)
+ free(buffer);
+}
+
+void pakfire_call_progress_callback(struct pakfire* pakfire, double progress) {
+ pakfire->callbacks.progress(pakfire->callbacks.data, progress);
+}
+
const char* pakfire_get_keystore_path(struct pakfire* pakfire) {
return pakfire->keystore_path;
}
int saved_errno = errno;
va_start(args, format);
- pakfire->log_function(pakfire->log_data, priority, file, line, fn, format, args);
+ pakfire->callbacks.log(pakfire->callbacks.data, priority, file, line, fn, format, args);
va_end(args);
// Restore errno
char root[PATH_MAX];
int r;
+ struct pakfire_callbacks callbacks = {
+ .log = pakfire_log_stderr,
+ };
+
// Create test root directory
snprintf(root, PATH_MAX - 1, "%s/pakfire-test-XXXXXX", TEST_ROOTFS);
char* tmp = pakfire_mkdtemp(root);
LOG("running %s (%s)\n", t->name, root);
// Create a pakfire instance
- r = pakfire_create(&t->pakfire, root, NULL, TEST_SRC_PATH "/pakfire.conf", 0,
- pakfire_log_stderr, NULL);
+ r = pakfire_create(&t->pakfire, root, NULL, TEST_SRC_PATH "/pakfire.conf",
+ 0, &callbacks);
if (r) {
LOG("ERROR: Could not initialize pakfire: %m\n");
exit(1);