return 0;
}
-static int pakfire_ctx_setup_logging(struct pakfire_ctx* ctx) {
+static int pakfire_ctx_setup_logging(struct pakfire_ctx* self) {
const char* env = NULL;
// Set the default level to INFO
level = parse_log_level(env);
// Store the log level
- pakfire_ctx_set_log_level(ctx, level);
+ pakfire_ctx_set_log_level(self, level);
// Log to syslog by default
- pakfire_ctx_set_log_callback(ctx, pakfire_log_syslog, NULL);
+ pakfire_ctx_set_log_callback(self, pakfire_log_syslog, NULL);
return 0;
}
-static int pakfire_ctx_default_confirm_callback(struct pakfire_ctx* ctx,
+static int pakfire_ctx_default_confirm_callback(struct pakfire_ctx* self,
struct pakfire* pakfire, void* data, const char* message, const char* question) {
// Just log the message
- INFO(ctx, "%s\n", message);
+ INFO(self, "%s\n", message);
return 0;
}
-static int pakfire_ctx_load_config(struct pakfire_ctx* ctx, const char* path) {
+static int pakfire_ctx_load_config(struct pakfire_ctx* self, const char* path) {
FILE* f = NULL;
int r;
}
// Read the configuration file
- r = pakfire_config_read(ctx->config, f);
+ r = pakfire_config_read(self->config, f);
// Cleanup
fclose(f);
return r;
}
-static void pakfire_ctx_free(struct pakfire_ctx* ctx) {
+static void pakfire_ctx_free(struct pakfire_ctx* self) {
// Release the events
- if (ctx->events.loop_started)
- sd_event_source_unref(ctx->events.loop_started);
- if (ctx->events.loop_exited)
- sd_event_source_unref(ctx->events.loop_exited);
+ if (self->events.loop_started)
+ sd_event_source_unref(self->events.loop_started);
+ if (self->events.loop_exited)
+ sd_event_source_unref(self->events.loop_exited);
// Release event loop
- if (ctx->loop)
- sd_event_unref(ctx->loop);
+ if (self->loop)
+ sd_event_unref(self->loop);
// Release cURL Share Handle
- if (ctx->share)
- curl_share_cleanup(ctx->share);
+ if (self->share)
+ curl_share_cleanup(self->share);
// Release Magic Context
- if (ctx->magic)
- magic_close(ctx->magic);
+ if (self->magic)
+ magic_close(self->magic);
- if (ctx->config)
- pakfire_config_unref(ctx->config);
- free(ctx);
+ if (self->config)
+ pakfire_config_unref(self->config);
+ free(self);
}
int pakfire_ctx_create(struct pakfire_ctx** ctx, const char* path) {
- struct pakfire_ctx* c = NULL;
+ struct pakfire_ctx* self = NULL;
int r;
// Allocate the context
- c = calloc(1, sizeof(*c));
- if (!c)
+ self = calloc(1, sizeof(*self));
+ if (!self)
return -errno;
// Initialize the reference counter
- c->nrefs = 1;
+ self->nrefs = 1;
// Initialize the configuration
- r = pakfire_config_create(&c->config);
- if (r)
+ r = pakfire_config_create(&self->config);
+ if (r < 0)
goto ERROR;
// Load configuration
- r = pakfire_ctx_load_config(c, path);
- if (r)
+ r = pakfire_ctx_load_config(self, path);
+ if (r < 0)
goto ERROR;
// Setup logging
- r = pakfire_ctx_setup_logging(c);
- if (r)
+ r = pakfire_ctx_setup_logging(self);
+ if (r < 0)
goto ERROR;
// Setup the default cache path
- r = pakfire_ctx_set_cache_path(c, PAKFIRE_CACHE_DIR);
- if (r)
+ r = pakfire_ctx_set_cache_path(self, PAKFIRE_CACHE_DIR);
+ if (r < 0)
goto ERROR;
// Load the distribution information
- r = pakfire_distro(&c->distro, NULL);
- if (r)
+ r = pakfire_distro(&self->distro, NULL);
+ if (r < 0)
goto ERROR;
// Set the default confirm callback
- pakfire_ctx_set_confirm_callback(c, pakfire_ctx_default_confirm_callback, NULL);
+ pakfire_ctx_set_confirm_callback(self, pakfire_ctx_default_confirm_callback, NULL);
// Return the pointer
- *ctx = c;
-
+ *ctx = self;
return 0;
ERROR:
- if (c)
- pakfire_ctx_free(c);
+ if (self)
+ pakfire_ctx_free(self);
return r;
}
-struct pakfire_ctx* pakfire_ctx_ref(struct pakfire_ctx* ctx) {
- ctx->nrefs++;
+struct pakfire_ctx* pakfire_ctx_ref(struct pakfire_ctx* self) {
+ self->nrefs++;
- return ctx;
+ return self;
}
-struct pakfire_ctx* pakfire_ctx_unref(struct pakfire_ctx* ctx) {
- if (--ctx->nrefs > 0)
- return ctx;
+struct pakfire_ctx* pakfire_ctx_unref(struct pakfire_ctx* self) {
+ if (--self->nrefs > 0)
+ return self;
- pakfire_ctx_free(ctx);
+ pakfire_ctx_free(self);
return NULL;
}
// Flags
-int pakfire_ctx_has_flag(struct pakfire_ctx* ctx, int flag) {
- return ctx->flags & flag;
+int pakfire_ctx_has_flag(struct pakfire_ctx* self, int flag) {
+ return self->flags & flag;
}
-int pakfire_ctx_set_flag(struct pakfire_ctx* ctx, int flag) {
- ctx->flags |= flag;
+int pakfire_ctx_set_flag(struct pakfire_ctx* self, int flag) {
+ self->flags |= flag;
return 0;
}
// Config
-struct pakfire_config* pakfire_ctx_get_config(struct pakfire_ctx* ctx) {
- if (!ctx->config)
+struct pakfire_config* pakfire_ctx_get_config(struct pakfire_ctx* self) {
+ if (!self->config)
return NULL;
- return pakfire_config_ref(ctx->config);
+ return pakfire_config_ref(self->config);
}
// Distro
-const struct pakfire_distro* pakfire_ctx_get_distro(struct pakfire_ctx* ctx) {
- return &ctx->distro;
+const struct pakfire_distro* pakfire_ctx_get_distro(struct pakfire_ctx* self) {
+ return &self->distro;
}
// Logging
-int pakfire_ctx_get_log_level(struct pakfire_ctx* ctx) {
- return ctx->log.level;
+int pakfire_ctx_get_log_level(struct pakfire_ctx* self) {
+ return self->log.level;
}
-void pakfire_ctx_set_log_level(struct pakfire_ctx* ctx, int level) {
- ctx->log.level = level;
+void pakfire_ctx_set_log_level(struct pakfire_ctx* self, int level) {
+ self->log.level = level;
}
-void pakfire_ctx_set_log_callback(struct pakfire_ctx* ctx,
+void pakfire_ctx_set_log_callback(struct pakfire_ctx* self,
pakfire_log_callback callback, void* data) {
- ctx->log.callback = callback;
- ctx->log.data = data;
+ self->log.callback = callback;
+ self->log.data = data;
}
-void pakfire_ctx_log(struct pakfire_ctx* ctx, int level, const char* file, int line,
+void pakfire_ctx_log(struct pakfire_ctx* self, int level, const char* file, int line,
const char* fn, const char* format, ...) {
va_list args;
// Return if the callback hasn't been set
- if (!ctx->log.callback)
+ if (!self->log.callback)
return;
va_start(args, format);
- ctx->log.callback(ctx->log.data, level, file, line, fn, format, args);
+ self->log.callback(self->log.data, level, file, line, fn, format, args);
va_end(args);
}
// Paths
-const char* pakfire_ctx_get_cache_path(struct pakfire_ctx* ctx) {
- return ctx->paths.cache;
+const char* pakfire_ctx_get_cache_path(struct pakfire_ctx* self) {
+ return self->paths.cache;
}
-int pakfire_ctx_set_cache_path(struct pakfire_ctx* ctx, const char* path) {
- return pakfire_path_expand(ctx->paths.cache, path);
+int pakfire_ctx_set_cache_path(struct pakfire_ctx* self, const char* path) {
+ return pakfire_path_expand(self->paths.cache, path);
}
// Confirm
-void pakfire_ctx_set_confirm_callback(struct pakfire_ctx* ctx,
+void pakfire_ctx_set_confirm_callback(struct pakfire_ctx* self,
pakfire_confirm_callback callback, void* data) {
- ctx->confirm.callback = callback;
- ctx->confirm.data = data;
+ self->confirm.callback = callback;
+ self->confirm.data = data;
}
-int pakfire_ctx_confirm(struct pakfire_ctx* ctx, struct pakfire* pakfire,
+int pakfire_ctx_confirm(struct pakfire_ctx* self, struct pakfire* pakfire,
const char* message, const char* question) {
// Run callback
- if (!ctx->confirm.callback)
+ if (!self->confirm.callback)
return 0;
- return ctx->confirm.callback(ctx, pakfire, ctx->confirm.data, message, question);
+ return self->confirm.callback(self, pakfire, self->confirm.data, message, question);
}
// Progress
-void pakfire_ctx_set_progress_callback(struct pakfire_ctx* ctx,
+void pakfire_ctx_set_progress_callback(struct pakfire_ctx* self,
pakfire_progress_callback callback, void* data) {
- ctx->progress.callback = callback;
- ctx->progress.data = data;
+ self->progress.callback = callback;
+ self->progress.data = data;
}
-int pakfire_ctx_setup_progress(struct pakfire_ctx* ctx, struct pakfire_progress* progress) {
- if (!ctx->progress.callback)
+int pakfire_ctx_setup_progress(struct pakfire_ctx* self, struct pakfire_progress* progress) {
+ if (!self->progress.callback)
return 0;
- return ctx->progress.callback(ctx, ctx->progress.data, progress);
+ return self->progress.callback(self, self->progress.data, progress);
}
// Pick Solution
-void pakfire_ctx_set_pick_solution_callback(struct pakfire_ctx* ctx,
+void pakfire_ctx_set_pick_solution_callback(struct pakfire_ctx* self,
pakfire_pick_solution_callback callback, void* data) {
- ctx->pick_solution.callback = callback;
- ctx->pick_solution.data = data;
-
+ self->pick_solution.callback = callback;
+ self->pick_solution.data = data;
}
-int pakfire_ctx_pick_solution(struct pakfire_ctx* ctx, struct pakfire* pakfire,
+int pakfire_ctx_pick_solution(struct pakfire_ctx* self, struct pakfire* pakfire,
struct pakfire_transaction* transaction) {
- if (!ctx->pick_solution.callback)
+ if (!self->pick_solution.callback)
return 1;
- return ctx->pick_solution.callback(ctx, pakfire, ctx->pick_solution.data, transaction);
+ return self->pick_solution.callback(self, pakfire, self->pick_solution.data, transaction);
}
// Event Loop
&self->events.loop_started, pakfire_ctx_loop_started, self);
}
-int pakfire_ctx_loop(struct pakfire_ctx* ctx, sd_event** loop) {
+int pakfire_ctx_loop(struct pakfire_ctx* self, sd_event** loop) {
int r;
// Initialize the loop whenever we need it
- if (!ctx->loop) {
- r = sd_event_new(&ctx->loop);
+ if (!self->loop) {
+ r = sd_event_new(&self->loop);
if (r < 0)
return r;
// Register an event that gets called when the loop started
- r = sd_event_add_defer(ctx->loop,
- &ctx->events.loop_started, pakfire_ctx_loop_started, ctx);
+ r = sd_event_add_defer(self->loop,
+ &self->events.loop_started, pakfire_ctx_loop_started, self);
if (r < 0)
return r;
}
// Return the loop
- *loop = sd_event_ref(ctx->loop);
+ *loop = sd_event_ref(self->loop);
return 0;
}
// cURL
-CURLSH* pakfire_ctx_curl_share(struct pakfire_ctx* ctx) {
+CURLSH* pakfire_ctx_curl_share(struct pakfire_ctx* self) {
int r;
// Setup a new handle
- if (!ctx->share) {
- ctx->share = curl_share_init();
- if (!ctx->share) {
- ERROR(ctx, "Could not setup cURL share handle\n");
+ if (!self->share) {
+ self->share = curl_share_init();
+ if (!self->share) {
+ ERROR(self, "Could not setup cURL share handle\n");
goto ERROR;
}
// Share connections between handles
- r = curl_share_setopt(ctx->share, CURLSHOPT_SHARE, CURL_LOCK_DATA_CONNECT);
+ r = curl_share_setopt(self->share, CURLSHOPT_SHARE, CURL_LOCK_DATA_CONNECT);
if (r) {
- ERROR(ctx, "Could not configure the share handle: %s\n", curl_share_strerror(r));
+ ERROR(self, "Could not configure the share handle: %s\n", curl_share_strerror(r));
goto ERROR;
}
}
- return ctx->share;
+ return self->share;
ERROR:
- if (ctx->share) {
- curl_share_cleanup(ctx->share);
- ctx->share = NULL;
+ if (self->share) {
+ curl_share_cleanup(self->share);
+ self->share = NULL;
}
return NULL;
// Magic
-magic_t pakfire_ctx_magic(struct pakfire_ctx* ctx) {
+magic_t pakfire_ctx_magic(struct pakfire_ctx* self) {
int r;
// Initialize the context if not already done
- if (!ctx->magic) {
+ if (!self->magic) {
// Allocate a new context
- ctx->magic = magic_open(MAGIC_MIME_TYPE | MAGIC_ERROR | MAGIC_NO_CHECK_TOKENS);
- if (!ctx->magic) {
- ERROR(ctx, "Could not allocate magic context: %m\n");
+ self->magic = magic_open(MAGIC_MIME_TYPE | MAGIC_ERROR | MAGIC_NO_CHECK_TOKENS);
+ if (!self->magic) {
+ ERROR(self, "Could not allocate magic context: %m\n");
return NULL;
}
// Load the database
- r = magic_load(ctx->magic, NULL);
+ r = magic_load(self->magic, NULL);
if (r) {
- ERROR(ctx, "Could not open the magic database: %s\n", magic_error(ctx->magic));
+ ERROR(self, "Could not open the magic database: %s\n", magic_error(self->magic));
goto ERROR;
}
}
- return ctx->magic;
+ return self->magic;
ERROR:
- if (ctx->magic)
- magic_close(ctx->magic);
+ if (self->magic)
+ magic_close(self->magic);
// Reset the pointer
- ctx->magic = NULL;
+ self->magic = NULL;
return NULL;
}
int pakfire_ctx_create(struct pakfire_ctx** ctx, const char* path);
-struct pakfire_ctx* pakfire_ctx_ref(struct pakfire_ctx* ctx);
-struct pakfire_ctx* pakfire_ctx_unref(struct pakfire_ctx* ctx);
+struct pakfire_ctx* pakfire_ctx_ref(struct pakfire_ctx* self);
+struct pakfire_ctx* pakfire_ctx_unref(struct pakfire_ctx* self);
// Flags
PAKFIRE_CTX_IN_JAIL = (1 << 1),
};
-int pakfire_ctx_has_flag(struct pakfire_ctx* ctx, int flag);
-int pakfire_ctx_set_flag(struct pakfire_ctx* ctx, int flag);
+int pakfire_ctx_has_flag(struct pakfire_ctx* self, int flag);
+int pakfire_ctx_set_flag(struct pakfire_ctx* self, int flag);
// Config
-struct pakfire_config* pakfire_ctx_get_config(struct pakfire_ctx* ctx);
+struct pakfire_config* pakfire_ctx_get_config(struct pakfire_ctx* self);
// Logging
-int pakfire_ctx_get_log_level(struct pakfire_ctx* ctx);
-void pakfire_ctx_set_log_level(struct pakfire_ctx* ctx, int level);
+int pakfire_ctx_get_log_level(struct pakfire_ctx* self);
+void pakfire_ctx_set_log_level(struct pakfire_ctx* self, int level);
-void pakfire_ctx_set_log_callback(struct pakfire_ctx* ctx,
+void pakfire_ctx_set_log_callback(struct pakfire_ctx* self,
pakfire_log_callback callback, void* data);
// Paths
-const char* pakfire_ctx_get_cache_path(struct pakfire_ctx* ctx);
-int pakfire_ctx_set_cache_path(struct pakfire_ctx* ctx, const char* path);
+const char* pakfire_ctx_get_cache_path(struct pakfire_ctx* self);
+int pakfire_ctx_set_cache_path(struct pakfire_ctx* self, const char* path);
// Confirm
-typedef int (*pakfire_confirm_callback)(struct pakfire_ctx* ctx, struct pakfire* pakfire,
+typedef int (*pakfire_confirm_callback)(struct pakfire_ctx* self, struct pakfire* pakfire,
void* data, const char* message, const char* question);
-void pakfire_ctx_set_confirm_callback(struct pakfire_ctx* ctx,
+void pakfire_ctx_set_confirm_callback(struct pakfire_ctx* self,
pakfire_confirm_callback callback, void* data);
// Progress
-typedef int (*pakfire_progress_callback)(struct pakfire_ctx* ctx,
+typedef int (*pakfire_progress_callback)(struct pakfire_ctx* self,
void* data, struct pakfire_progress* progress);
-void pakfire_ctx_set_progress_callback(struct pakfire_ctx* ctx,
+void pakfire_ctx_set_progress_callback(struct pakfire_ctx* self,
pakfire_progress_callback callback, void* data);
// Pick Solution
-typedef int (*pakfire_pick_solution_callback)(struct pakfire_ctx* ctx,
+typedef int (*pakfire_pick_solution_callback)(struct pakfire_ctx* self,
struct pakfire* pakfire, void* data, struct pakfire_transaction* transaction);
-void pakfire_ctx_set_pick_solution_callback(struct pakfire_ctx* ctx,
+void pakfire_ctx_set_pick_solution_callback(struct pakfire_ctx* self,
pakfire_pick_solution_callback callback, void* data);
// Distro
-const struct pakfire_distro* pakfire_ctx_get_distro(struct pakfire_ctx* ctx);
+const struct pakfire_distro* pakfire_ctx_get_distro(struct pakfire_ctx* self);
// Logging
-void pakfire_ctx_log(struct pakfire_ctx* ctx, int level, const char* file, int line,
+void pakfire_ctx_log(struct pakfire_ctx* self, int level, const char* file, int line,
const char* fn, const char* format, ...) __attribute__((format(printf, 6, 7)));
// Confirm
-int pakfire_ctx_confirm(struct pakfire_ctx* ctx, struct pakfire* pakfire,
+int pakfire_ctx_confirm(struct pakfire_ctx* self, struct pakfire* pakfire,
const char* message, const char* question);
// Progress
-int pakfire_ctx_setup_progress(struct pakfire_ctx* ctx, struct pakfire_progress* progress);
+int pakfire_ctx_setup_progress(struct pakfire_ctx* self, struct pakfire_progress* progress);
// Pick Solution
-int pakfire_ctx_pick_solution(struct pakfire_ctx* ctx, struct pakfire* pakfire,
+int pakfire_ctx_pick_solution(struct pakfire_ctx* self, struct pakfire* pakfire,
struct pakfire_transaction* transaction);
// Event Loop
#include <systemd/sd-event.h>
-int pakfire_ctx_loop(struct pakfire_ctx* ctx, sd_event** loop);
+int pakfire_ctx_loop(struct pakfire_ctx* self, sd_event** loop);
int pakfire_ctx_loop_is_running(struct pakfire_ctx* self);
#include <curl/curl.h>
-CURLSH* pakfire_ctx_curl_share(struct pakfire_ctx* ctx);
+CURLSH* pakfire_ctx_curl_share(struct pakfire_ctx* self);
// Magic
#include <magic.h>
-magic_t pakfire_ctx_magic(struct pakfire_ctx* ctx);
+magic_t pakfire_ctx_magic(struct pakfire_ctx* self);
#endif /* PAKFIRE_CTX_H */