From: Michael Tremer Date: Sun, 29 Dec 2024 19:27:14 +0000 (+0000) Subject: stripper: Open the file only once X-Git-Tag: 0.9.30~655 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b802994065d96fc8085ece1306d2fed8fe1906dd;p=pakfire.git stripper: Open the file only once Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/compress.c b/src/libpakfire/compress.c index 7900a0271..e3cca03cf 100644 --- a/src/libpakfire/compress.c +++ b/src/libpakfire/compress.c @@ -605,7 +605,7 @@ static int __pakfire_compress_entry(struct pakfire_ctx* ctx, struct pakfire_file // Copy the data if there is any if (archive_entry_size(entry)) { // Open the file - f = pakfire_file_open(file); + f = pakfire_file_fopen(file); if (!f) { r = 1; goto ERROR; diff --git a/src/libpakfire/file.c b/src/libpakfire/file.c index 9b06d4d09..d33e728e3 100644 --- a/src/libpakfire/file.c +++ b/src/libpakfire/file.c @@ -20,6 +20,7 @@ #include #include +#include #include #include #include @@ -1080,26 +1081,42 @@ static int pakfire_file_levels(struct pakfire_file* file) { return levels; } -FILE* pakfire_file_open(struct pakfire_file* file) { +int pakfire_file_open(struct pakfire_file* file) { const char* path = NULL; + int fd = -EBADF; // Fetch the absolute path of the file path = pakfire_file_get_abspath(file); if (!path) - return NULL; + return -EBADF; // Fix the path if we have entered the jail if (pakfire_ctx_has_flag(file->ctx, PAKFIRE_CTX_IN_JAIL)) { path = pakfire_relpath(file->pakfire, path); if (!path) - return NULL; + return -EBADF; } - FILE* f = fopen(path, "r+"); - if (!f) + // Open the file + fd = open(path, O_CLOEXEC); + if (fd < 0) { ERROR(file->ctx, "Could not open %s: %m\n", path); + return -errno; + } + + return fd; +} + +FILE* pakfire_file_fopen(struct pakfire_file* file) { + int fd = -EBADF; + + // Open the file descriptor + fd = pakfire_file_open(file); + if (fd < 0) + return NULL; - return f; + // Return a file handle + return fdopen(fd, "r+"); } int pakfire_file_payload_matches(struct pakfire_file* file, @@ -1122,7 +1139,7 @@ int pakfire_file_payload_matches(struct pakfire_file* file, return 0; // Open the file - f = pakfire_file_open(file); + f = pakfire_file_fopen(file); if (!f) { r = 1; goto ERROR; @@ -1170,7 +1187,7 @@ static int __pakfire_file_compute_digests(struct pakfire_file* file, pakfire_digests_reset(digests, types); // Open the file - f = pakfire_file_open(file); + f = pakfire_file_fopen(file); if (!f) goto ERROR; @@ -1433,7 +1450,7 @@ static int pakfire_file_classify_elf(struct pakfire_file* file) { return r; // Open the file - f = pakfire_file_open(file); + f = pakfire_file_fopen(file); if (!f) { ERROR(file->ctx, "Could not open %s: %m\n", pakfire_file_get_path(file)); return 1; diff --git a/src/libpakfire/include/pakfire/file.h b/src/libpakfire/include/pakfire/file.h index 52b1cf02c..70f126654 100644 --- a/src/libpakfire/include/pakfire/file.h +++ b/src/libpakfire/include/pakfire/file.h @@ -156,7 +156,8 @@ char* pakfire_file_dump(struct pakfire_file* file, int flags); const char* pakfire_file_get_abspath(struct pakfire_file* file); -FILE* pakfire_file_open(struct pakfire_file* file); +int pakfire_file_open(struct pakfire_file* file); +FILE* pakfire_file_fopen(struct pakfire_file* file); int pakfire_file_payload_matches(struct pakfire_file* file, const void* needle, const size_t length); diff --git a/src/libpakfire/stripper.c b/src/libpakfire/stripper.c index 9218fcc6b..bd68e0511 100644 --- a/src/libpakfire/stripper.c +++ b/src/libpakfire/stripper.c @@ -263,7 +263,7 @@ ERROR: #endif static int pakfire_stripper_copy_sources( - struct pakfire_stripper* stripper, struct pakfire_file* file) { + struct pakfire_stripper* stripper, struct pakfire_file* file, int fd) { const char* filename = NULL; char basename[PATH_MAX]; Dwarf* dwarf = NULL; @@ -274,18 +274,10 @@ static int pakfire_stripper_copy_sources( size_t cu_header_length; Dwarf_Die die_mem; size_t count; - FILE* f = NULL; int r; - // Open the file - f = pakfire_file_open(file); - if (!f) { - r = -errno; - goto ERROR; - } - // Read DWARF information - dwarf = dwarf_begin(fileno(f), DWARF_C_READ); + dwarf = dwarf_begin(fd, DWARF_C_READ); if (!dwarf) { switch (dwarf_errno()) { // If we don't have any DWARF information there is nothing to do @@ -351,8 +343,6 @@ static int pakfire_stripper_copy_sources( } ERROR: - if (f) - fclose(f); if (dwarf) dwarf_end(dwarf); @@ -362,14 +352,26 @@ ERROR: static int pakfire_stripper_strip( struct pakfire_ctx* ctx, struct pakfire_file* file, void* data) { struct pakfire_stripper* stripper = data; + int fd = -EBADF; int r; + // Open the file + fd = pakfire_file_open(file); + if (fd < 0) { + r = -errno; + goto ERROR; + } + // Copy sources - r = pakfire_stripper_copy_sources(stripper, file); + r = pakfire_stripper_copy_sources(stripper, file, fd); if (r < 0) - return r; + goto ERROR; - return 0; +ERROR: + if (fd >= 0) + close(fd); + + return r; } static int __pakfire_stripper_run(struct pakfire_ctx* ctx, void* data) {