void pakfire_log(struct pakfire* pakfire, int level, const char* file, int line,
const char* fn, const char* format, ...) __attribute__((format(printf, 6, 7)));
-// Locking
-int pakfire_acquire_lock(struct pakfire* pakfire);
-void pakfire_release_lock(struct pakfire* pakfire);
-
struct pakfire_config* pakfire_get_config(struct pakfire* pakfire);
int pakfire_confirm(struct pakfire* pakfire, const char* message, const char* question);
#include <pakfire/transaction.h>
#include <pakfire/util.h>
-#define LOCK_PATH PAKFIRE_PRIVATE_DIR "/.lock"
-
struct pakfire {
struct pakfire_ctx* ctx;
int nrefs;
char path[PATH_MAX];
- char lock_path[PATH_MAX];
char cache_path[PATH_MAX];
struct pakfire_arches {
pakfire_repo_unref(repo);
}
- // Release lock (if not already done so)
- pakfire_release_lock(pakfire);
-
if (pakfire->destroy_on_free && *pakfire->path) {
CTX_DEBUG(pakfire->ctx, "Destroying %s\n", pakfire->path);
if (*p->distro.slogan)
CTX_DEBUG(p->ctx, " slogan = %s\n", p->distro.slogan);
- // Set lock path
- r = pakfire_path(p, p->lock_path, "%s", LOCK_PATH);
- if (r) {
- CTX_ERROR(p->ctx, "Could not set lock path: %m\n");
- goto ERROR;
- }
-
// Set cache path
r = pakfire_set_cache_path(p);
if (r) {
return pakfire->path;
}
-int pakfire_acquire_lock(struct pakfire* pakfire) {
- int r;
-
- // Check if the lock is already held
- if (pakfire->lock) {
- CTX_ERROR(pakfire->ctx, "Lock is already been acquired by this process\n");
- return -ENOLCK;
- }
-
- CTX_DEBUG(pakfire->ctx, "Acquire lock...\n");
-
- // Ensure the parent directory exists
- pakfire_mkparentdir(pakfire->lock_path, 0755);
-
- // Open the lock file
- pakfire->lock = fopen(pakfire->lock_path, "w");
- if (!pakfire->lock) {
- CTX_ERROR(pakfire->ctx, "Could not open lock file %s: %m\n", pakfire->lock_path);
- return -errno;
- }
-
- // Attempt to lock the file exclusively
- while (1) {
- r = flock(fileno(pakfire->lock), LOCK_EX|LOCK_NB);
-
- // Success!
- if (r == 0)
- goto SUCCESS;
-
- CTX_DEBUG(pakfire->ctx, "Could not acquire lock %s: %m\n", pakfire->lock_path);
-
- // Wait 500ms until the next attempt
- usleep(500000);
- }
-
-SUCCESS:
- CTX_DEBUG(pakfire->ctx, "Lock acquired\n");
-
- return 0;
-}
-
-void pakfire_release_lock(struct pakfire* pakfire) {
- if (!pakfire->lock)
- return;
-
- CTX_DEBUG(pakfire->ctx, "Releasing lock\n");
-
- fclose(pakfire->lock);
- pakfire->lock = NULL;
-
- // Attempt to unlink the lock file
- unlink(pakfire->lock_path);
-}
-
int __pakfire_path(struct pakfire* pakfire, char* path, const size_t length,
const char* format, ...) {
char buffer[PATH_MAX];