From: Michael Tremer Date: Tue, 31 Dec 2024 13:56:54 +0000 (+0000) Subject: util: Implement rewind() that returns any errors X-Git-Tag: 0.9.30~629 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=3260871b08b544f6e8f6071ec807fcb8e2c0ecae;p=pakfire.git util: Implement rewind() that returns any errors 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 --- diff --git a/src/libpakfire/archive.c b/src/libpakfire/archive.c index 2421e1c1b..28c2a92d0 100644 --- a/src/libpakfire/archive.c +++ b/src/libpakfire/archive.c @@ -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) diff --git a/src/libpakfire/compress.c b/src/libpakfire/compress.c index 17a174fca..6d5d11e96 100644 --- a/src/libpakfire/compress.c +++ b/src/libpakfire/compress.c @@ -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)) { diff --git a/src/libpakfire/digest.c b/src/libpakfire/digest.c index c42632844..8663680d5 100644 --- a/src/libpakfire/digest.c +++ b/src/libpakfire/digest.c @@ -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; } diff --git a/src/libpakfire/include/pakfire/util.h b/src/libpakfire/include/pakfire/util.h index 9aee571aa..dba9f347a 100644 --- a/src/libpakfire/include/pakfire/util.h +++ b/src/libpakfire/include/pakfire/util.h @@ -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))); diff --git a/src/libpakfire/key.c b/src/libpakfire/key.c index b5423681e..3bd6625ff 100644 --- a/src/libpakfire/key.c +++ b/src/libpakfire/key.c @@ -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); diff --git a/src/libpakfire/xfer.c b/src/libpakfire/xfer.c index 0514f8220..d5d014b5b 100644 --- a/src/libpakfire/xfer.c +++ b/src/libpakfire/xfer.c @@ -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?