]> git.ipfire.org Git - pakfire.git/commitdiff
util: Implement rewind() that returns any errors
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 31 Dec 2024 13:56:54 +0000 (13:56 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 31 Dec 2024 13:56:54 +0000 (13:56 +0000)
The Clang Static Analyzer complains that we don't check errno after
using rewind(). This patch adds a new version of this function which
returns an error.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/archive.c
src/libpakfire/compress.c
src/libpakfire/digest.c
src/libpakfire/include/pakfire/util.h
src/libpakfire/key.c
src/libpakfire/xfer.c

index 2421e1c1bbbbeaba38153e611bd87d01ed2a88c9..28c2a92d0942380e85259153c8f1f1f2f5e8be10 100644 (file)
@@ -160,6 +160,7 @@ static int archive_file_close(struct archive* a, void* data) {
 static int archive_read_file_open(struct archive* a, FILE* f) {
        struct pakfire_archive_file* file = NULL;
        int fd = -EBADF;
+       int r;
 
        // Fetch the underlying file descriptor
        fd = fileno(f);
@@ -184,7 +185,9 @@ static int archive_read_file_open(struct archive* a, FILE* f) {
        fd = -EBADF;
 
        // Start reading at the beginning
-       rewind(f);
+       r = pakfire_rewind(f);
+       if (r < 0)
+               goto ERROR;
 
        // Allocate an object
        file = calloc(1, sizeof(*file));
@@ -265,7 +268,11 @@ static int pakfire_archive_compute_digests(struct pakfire_archive* archive) {
        int r;
 
        // Start reading at the beginning
-       rewind(archive->f);
+       r = pakfire_rewind(archive->f);
+       if (r < 0) {
+               ERROR(archive->ctx, "Could not rewind %s: %m\n", archive->path);
+               return -errno;
+       }
 
        // Calculate digest
        r = pakfire_digests_compute_from_file(archive->ctx, &archive->digests,
@@ -992,6 +999,9 @@ ERROR:
 }
 
 int pakfire_archive_copy(struct pakfire_archive* archive, const char* path) {
+       FILE* f = NULL;
+       int r;
+
        if (!path)
                return -EINVAL;
 
@@ -1003,23 +1013,29 @@ int pakfire_archive_copy(struct pakfire_archive* archive, const char* path) {
        DEBUG(archive->ctx, "Copying %s to %s...\n", archive->path, path);
 
        // Ensure we copy from the very beginning
-       rewind(archive->f);
+       r = pakfire_rewind(archive->f);
+       if (r < 0) {
+               ERROR(archive->ctx, "Could not rewind %s: %m\n", archive->path);
+               r = -errno;
+               goto ERROR;
+       }
 
        // Ensure the parent directory exists
        pakfire_mkparentdir(path, 0755);
 
        // Open destination file
-       FILE* f = fopen(path, "w");
-       if (!f)
-               return -errno;
-
-       int r = 1;
+       f = fopen(path, "w");
+       if (!f) {
+               r = -errno;
+               goto ERROR;
+       }
 
        // Copy everything
        ssize_t bytes_written = sendfile(fileno(f), fileno(archive->f), NULL, size);
        if (bytes_written < size) {
-               ERROR(archive->ctx, "Could not copy archive (%zd byte(s) written): %m\n",
-                       bytes_written);
+               ERROR(archive->ctx,
+                       "Could not copy archive (%zd byte(s) written): %m\n", bytes_written);
+               r = -errno;
                goto ERROR;
        }
 
@@ -1027,7 +1043,8 @@ int pakfire_archive_copy(struct pakfire_archive* archive, const char* path) {
        r = 0;
 
 ERROR:
-       fclose(f);
+       if (f)
+               fclose(f);
 
        // Delete the file on error
        if (r)
index 17a174fcae1cca809b9ede51a4fe8ec9a0182ea2..6d5d11e96e28f2ab1dbbe73407abc8a5e44ba4e8 100644 (file)
@@ -552,12 +552,17 @@ struct pakfire_compress {
 static int pakfire_copy_data_from_file(struct pakfire_ctx* ctx,
                struct archive* archive, FILE* f) {
        char buffer[BUFFER_SIZE];
+       int r;
 
        ssize_t bytes_read = 0;
        ssize_t bytes_written = 0;
 
        // Read file from the very beginning - also allows calling this multiple times
-       rewind(f);
+       r = pakfire_rewind(f);
+       if (r < 0) {
+               ERROR(ctx, "Could not rewind file for copying: %m\n");
+               return -errno;
+       }
 
        // Loop through the entire length of the file
        while (!feof(f)) {
index c426328448f59d09bcb1d553891ace56390c355f..8663680d5b0bce5e1dd3ae3321554f9b2d353a3d 100644 (file)
@@ -467,7 +467,11 @@ ERROR:
                EVP_MD_CTX_free(sha2_256_ctx);
 
        // Reset file offset to the beginning
-       rewind(f);
+       r = pakfire_rewind(f);
+       if (r < 0) {
+               ERROR(ctx, "Could not rewind: %m\n");
+               return -errno;
+       }
 
        return r;
 }
index 9aee571aad48dd82ad5329e656254eed773aa3d1..dba9f347a3052543d6e7303d056bbc316be82354 100644 (file)
@@ -50,6 +50,10 @@ const char* pakfire_path_relpath(const char* root, const char* path);
 
 // File stuff
 
+static inline int pakfire_rewind(FILE* f) {
+       return fseek(f, 0, SEEK_SET);
+}
+
 int pakfire_file_write(struct pakfire* pakfire, const char* path,
        uid_t owner, gid_t group, mode_t mode, const char* format, ...)
        __attribute__((format(printf, 6, 7)));
index b5423681eee7bd1896a76aff076077eae581f7bf..3bd6625ff88466eb1fd331ff4944d867e3ccfe05 100644 (file)
@@ -773,7 +773,12 @@ PAKFIRE_EXPORT char* pakfire_key_dump(struct pakfire_key* key) {
        size_t length = ftell(f);
 
        // Go back to the beginning of the file
-       rewind(f);
+       r = pakfire_rewind(f);
+       if (r < 0) {
+               ERROR(key->ctx, "Could not rewind buffer: %m\n");
+               r = -errno;
+               goto ERROR;
+       }
 
        // Allocate a buffer
        buffer = calloc(1, length + 1);
index 0514f8220c265d21e2b910c7e5a428aceb2542cc..d5d014b5b3af019c858be2b64fdc01851ec3597d 100644 (file)
@@ -872,7 +872,11 @@ static int pakfire_xfer_fail(struct pakfire_xfer* xfer, int code) {
                }
 
                // Rewind
-               rewind(xfer->fin);
+               r = pakfire_rewind(xfer->fin);
+               if (r < 0) {
+                       ERROR(xfer->ctx, "Could not rewind output file: %m\n");
+                       return -errno;
+               }
        }
 
        // Did we use a mirror?