From 0b8f80bbf7cf2476685e58b12694b55b82ab9ead Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Tue, 25 Jul 2023 11:33:49 +0000 Subject: [PATCH] libpakfire: pakfire_create: Return a better return code Signed-off-by: Michael Tremer --- src/_pakfire/pakfire.c | 4 +++- src/libpakfire/pakfire.c | 13 ++++++------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/_pakfire/pakfire.c b/src/_pakfire/pakfire.c index 99035637a..27fe098ed 100644 --- a/src/_pakfire/pakfire.c +++ b/src/_pakfire/pakfire.c @@ -220,7 +220,9 @@ static int Pakfire_init(PakfireObject* self, PyObject* args, PyObject* kwds) { Py_END_ALLOW_THREADS - if (r) { + if (r < 0) { + errno = -r; + switch (errno) { // Invalid architecture or path case EINVAL: diff --git a/src/libpakfire/pakfire.c b/src/libpakfire/pakfire.c index 8c3c3fd67..0544d756b 100644 --- a/src/libpakfire/pakfire.c +++ b/src/libpakfire/pakfire.c @@ -828,7 +828,7 @@ PAKFIRE_EXPORT int pakfire_create(struct pakfire** pakfire, const char* path, struct pakfire* p = calloc(1, sizeof(*p)); if (!p) - return 1; + return -errno; p->nrefs = 1; p->flags = flags; @@ -852,7 +852,7 @@ PAKFIRE_EXPORT int pakfire_create(struct pakfire** pakfire, const char* path, // Check if the architecture is supported if (!pakfire_arch_supported(arch)) { ERROR(p, "Unsupported architecture: %s\n", arch); - errno = EINVAL; + r = -EINVAL; goto ERROR; } @@ -862,7 +862,7 @@ PAKFIRE_EXPORT int pakfire_create(struct pakfire** pakfire, const char* path, // Path must be absolute if (path && !pakfire_string_startswith(path, "/")) { ERROR(p, "Invalid path: %s\n", path); - errno = EINVAL; + r = -EINVAL; goto ERROR; } @@ -870,7 +870,7 @@ PAKFIRE_EXPORT int pakfire_create(struct pakfire** pakfire, const char* path, if (!path) { path = pakfire_mkdtemp(tempdir); if (!path) { - r = 1; + r = -errno; goto ERROR; } @@ -1016,8 +1016,7 @@ int pakfire_acquire_lock(struct pakfire* pakfire) { // Check if the lock is already held if (pakfire->lock) { ERROR(pakfire, "Lock is already been acquired by this process\n"); - errno = ENOLCK; - return 1; + return -ENOLCK; } DEBUG(pakfire, "Acquire lock...\n"); @@ -1029,7 +1028,7 @@ int pakfire_acquire_lock(struct pakfire* pakfire) { pakfire->lock = fopen(pakfire->lock_path, "w"); if (!pakfire->lock) { ERROR(pakfire, "Could not open lock file %s: %m\n", pakfire->lock_path); - return 1; + return -errno; } // Attempt to lock the file exclusively -- 2.39.5