]> git.ipfire.org Git - pakfire.git/commitdiff
libpakfire: archive: Return better return codes
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 25 Jul 2023 11:48:18 +0000 (11:48 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 25 Jul 2023 12:58:49 +0000 (12:58 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/_pakfire/archive.c
src/libpakfire/archive.c

index 73cdd080bac0917fadf46ff497d75904c5d45422..236c05f6e6f2e08c01fe293334ee7e843331692d 100644 (file)
@@ -66,8 +66,8 @@ static int Archive_init(ArchiveObject* self, PyObject* args, PyObject* kwds) {
        if (!PyArg_ParseTuple(args, "O!s", &PakfireType, &pakfire, &filename))
                return -1;
 
-       int r = pakfire_archive_open(&self->archive, pakfire->pakfire, filename);
-       if (r) {
+       errno = -pakfire_archive_open(&self->archive, pakfire->pakfire, filename);
+       if (errno) {
                PyErr_SetFromErrno(PyExc_OSError);
                return -1;
        }
@@ -145,8 +145,8 @@ static PyObject* Archive_extract(ArchiveObject* self, PyObject* args, PyObject*
                return NULL;
 
        // Extract payload
-       int r = pakfire_archive_extract(self->archive, path, flags);
-       if (r) {
+       errno = -pakfire_archive_extract(self->archive, path, flags);
+       if (errno) {
                PyErr_SetFromErrno(PyExc_OSError);
                return NULL;
        }
index a5a91ac68845dbf6d891ba8a08b78baf7a7e0b9f..089f34d1b7b1b175438e85d63fc2c2558653dc28 100644 (file)
@@ -276,8 +276,7 @@ static int pakfire_archive_parse_format(struct pakfire_archive* archive,
        // Check if format has already been set
        if (archive->format) {
                ERROR(archive->pakfire, "Archive format has already been parsed\n");
-               errno = EINVAL;
-               return 1;
+               return -EINVAL;
        }
 
        // Parse the format
@@ -292,8 +291,7 @@ static int pakfire_archive_parse_format(struct pakfire_archive* archive,
                default:
                        ERROR(archive->pakfire, "This version of Pakfire does not support "
                                "archive format %d\n", archive->format);
-                       errno = EINVAL;
-                       return 1;
+                       return -EINVAL;
        }
 
        DEBUG(archive->pakfire, "Archive format is %d\n", archive->format);
@@ -310,16 +308,14 @@ static int pakfire_archive_parse_scriptlet(struct pakfire_archive* archive,
        // Check for any available space
        if (archive->num_scriptlets >= MAX_SCRIPTLETS) {
                ERROR(archive->pakfire, "Too many scriptlets\n");
-               errno = ENOBUFS;
-               return 1;
+               return -ENOBUFS;
        }
 
        // Determine type
        type = pakfire_path_relpath(".scriptlets/", path);
        if (!type) {
                ERROR(archive->pakfire, "Could not determine the scriptlet type from '%s'\n", path);
-               errno = EINVAL;
-               return 1;
+               return -EINVAL;
        }
 
        // Allocate a scriptlet
@@ -412,8 +408,7 @@ static int pakfire_archive_read_metadata(struct pakfire_archive* archive) {
        // Check if the archive file actually has any contect
        if (!archive->stat.st_size) {
                ERROR(archive->pakfire, "Trying to open an empty archive file\n");
-               errno = EINVAL;
-               return 1;
+               return -EINVAL;
        }
 
        DEBUG(archive->pakfire, "Reading archive metadata...\n");
@@ -427,15 +422,13 @@ static int pakfire_archive_read_metadata(struct pakfire_archive* archive) {
        // Check if we could successfully read something
        if (!archive->format) {
                ERROR(archive->pakfire, "Archive has an unknown format\n");
-               errno = EINVAL;
-               return 1;
+               return -EINVAL;
        }
 
        // Check if we have read some metadata
        if (!archive->metadata) {
                ERROR(archive->pakfire, "Archive has no metadata\n");
-               errno = EINVAL;
-               return 1;
+               return -EINVAL;
        }
 
        return 0;
@@ -444,10 +437,9 @@ static int pakfire_archive_read_metadata(struct pakfire_archive* archive) {
 static int pakfire_archive_try_open(struct pakfire_archive* archive, const char* path) {
        int r;
 
-       if (!path) {
-               errno = EINVAL;
-               return 1;
-       }
+       // Check inputs
+       if (!path)
+               return -EINVAL;
 
        DEBUG(archive->pakfire, "Opening archive %s\n", path);
 
@@ -721,15 +713,13 @@ ERROR:
 }
 
 int pakfire_archive_copy(struct pakfire_archive* archive, const char* path) {
-       if (!path) {
-               errno = EINVAL;
-               return 1;
-       }
+       if (!path)
+               return -EINVAL;
 
        // Determine the file size
        ssize_t size = pakfire_archive_get_size(archive);
        if (size < 0)
-               return 1;
+               return -EINVAL;
 
        DEBUG(archive->pakfire, "Copying %s to %s...\n", archive->path, path);
 
@@ -742,7 +732,7 @@ int pakfire_archive_copy(struct pakfire_archive* archive, const char* path) {
        // Open destination file
        FILE* f = fopen(path, "w");
        if (!f)
-               return 1;
+               return -errno;
 
        int r = 1;
 
@@ -771,10 +761,8 @@ static int pakfire_archive_link(struct pakfire_archive* archive, const char* pat
        int r;
 
        // Check if path is set
-       if (!path) {
-               errno = EINVAL;
-               return 1;
-       }
+       if (!path)
+               return -EINVAL;
 
        DEBUG(archive->pakfire, "Linking %s to %s...\n", archive->path, path);
 
@@ -796,15 +784,13 @@ int pakfire_archive_link_or_copy(struct pakfire_archive* archive, const char* pa
 
        // Try to create a hardlink
        r = pakfire_archive_link(archive, path);
-       if (r) {
-               switch (errno) {
-                       // Try to copy the file if we could not create a hardlink
-                       case EPERM:
-                               r = pakfire_archive_copy(archive, path);
+       switch (-r) {
+               // Try to copy the file if we could not create a hardlink
+               case EPERM:
+                       r = pakfire_archive_copy(archive, path);
 
-                       default:
-                               break;
-               }
+               default:
+                       break;
        }
 
        return r;