]> git.ipfire.org Git - pakfire.git/commitdiff
libpakfire: pakfire_create: Use errno to report errors
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 19 Mar 2021 18:57:57 +0000 (18:57 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 19 Mar 2021 18:57:57 +0000 (18:57 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/_pakfire/pakfire.c
src/libpakfire/pakfire.c
tests/testsuite.c

index dba2feceef058c066f2c1790de05ec35f3ee43f4..34f8f8593d0faaeee6da89490b4532b78f2bf8e5 100644 (file)
@@ -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;
index d8e0cdebc267a6a2493c19aabddce585a0259764..ff6dc69dcacc88b17ae66522518a84e45da89878 100644 (file)
@@ -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;
 
index fc01ed45e54d0c76705d7952688505982d9c1f63..cca4197635318c2f19f3e2da6ebb94bbf4d5ff84 100644 (file)
@@ -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);
        }