From: Michael Tremer Date: Fri, 19 Mar 2021 18:57:57 +0000 (+0000) Subject: libpakfire: pakfire_create: Use errno to report errors X-Git-Tag: 0.9.28~1285^2~503 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=f4e91beb901df324ce1900a99019d5ec5476124c;p=pakfire.git libpakfire: pakfire_create: Use errno to report errors Signed-off-by: Michael Tremer --- diff --git a/src/_pakfire/pakfire.c b/src/_pakfire/pakfire.c index dba2fecee..34f8f8593 100644 --- a/src/_pakfire/pakfire.c +++ b/src/_pakfire/pakfire.c @@ -59,21 +59,21 @@ static int Pakfire_init(PakfireObject* self, PyObject* args, PyObject* kwds) { // Create a new Pakfire instance int r = pakfire_create(&self->pakfire, path, arch); if (r) { - switch (r) { - // Invalid architecture - case -EINVAL: + switch (errno) { + // Invalid architecture or path + case EINVAL: PyErr_SetString(PyExc_ValueError, "Invalid architecture or path"); break; // path does not exist - case -ENOENT: + case ENOENT: PyErr_Format(PyExc_FileNotFoundError, "%s does not exist or is not a directory", path); break; // Anything else default: - PyErr_SetNone(PyExc_OSError); + PyErr_SetFromErrno(PyExc_OSError); } return -1; diff --git a/src/libpakfire/pakfire.c b/src/libpakfire/pakfire.c index d8e0cdebc..ff6dc69dc 100644 --- a/src/libpakfire/pakfire.c +++ b/src/libpakfire/pakfire.c @@ -156,28 +156,32 @@ PAKFIRE_EXPORT int pakfire_create(Pakfire* pakfire, const char* path, const char // Check if the architecture is supported if (!pakfire_arch_supported(arch)) { - return -EINVAL; + errno = EINVAL; + return 1; } // Path must be absolute if (!pakfire_string_startswith(path, "/")) { - return -EINVAL; + errno = EINVAL; + return 1; } // Check if path exists if (!pakfire_path_isdir(path)) { - return -ENOENT; + errno = ENOENT; + return 1; } // Check if we are running as root uid_t uid = getuid(); if (uid != 0) { - return -EPERM; + errno = EPERM; + return 1; } Pakfire p = calloc(1, sizeof(*p)); if (!p) - return -ENOMEM; + return 1; p->nrefs = 1; diff --git a/tests/testsuite.c b/tests/testsuite.c index fc01ed45e..cca419763 100644 --- a/tests/testsuite.c +++ b/tests/testsuite.c @@ -44,7 +44,7 @@ static int test_run(int i, struct test* t) { // Create a pakfire instance r = pakfire_create(&t->pakfire, root, NULL); if (r) { - LOG("ERROR: Could not initialize Pakfire: %s\n", strerror(-r)); + LOG("ERROR: Could not initialize Pakfire: %s\n", strerror(errno)); exit(1); }