From 3c5daf78b3e0987e8e2ace675c5d958e08dcffe6 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Tue, 25 Jul 2023 11:48:18 +0000 Subject: [PATCH] libpakfire: archive: Return better return codes Signed-off-by: Michael Tremer --- src/_pakfire/archive.c | 8 +++--- src/libpakfire/archive.c | 58 +++++++++++++++------------------------- 2 files changed, 26 insertions(+), 40 deletions(-) diff --git a/src/_pakfire/archive.c b/src/_pakfire/archive.c index 73cdd080b..236c05f6e 100644 --- a/src/_pakfire/archive.c +++ b/src/_pakfire/archive.c @@ -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; } diff --git a/src/libpakfire/archive.c b/src/libpakfire/archive.c index a5a91ac68..089f34d1b 100644 --- a/src/libpakfire/archive.c +++ b/src/libpakfire/archive.c @@ -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; -- 2.39.5