]> git.ipfire.org Git - people/ric9/pakfire.git/commitdiff
stripper: Open the file only once
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 29 Dec 2024 19:27:14 +0000 (19:27 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 29 Dec 2024 19:27:14 +0000 (19:27 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/compress.c
src/libpakfire/file.c
src/libpakfire/include/pakfire/file.h
src/libpakfire/stripper.c

index 7900a027129814bac7527cf183340c3ae4efee62..e3cca03cf827a6129663ebb4f055780372f413a3 100644 (file)
@@ -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;
index 9b06d4d09157339ddfbf3b5e3d2f25c4e97484cd..d33e728e3ece8fb520f09e96a79d177d510287bb 100644 (file)
@@ -20,6 +20,7 @@
 
 #include <ctype.h>
 #include <errno.h>
+#include <fcntl.h>
 #include <fnmatch.h>
 #include <libgen.h>
 #include <limits.h>
@@ -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;
index 52b1cf02cd2412cc7331c9d65a801690b5cb3026..70f126654180b3ce225040eac686859ced49e64e 100644 (file)
@@ -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);
index 9218fcc6b072d44c3138935ad0d5e9c6ef7d7dc2..bd68e05111b03d2fc123cd97feb60d23f321a0ff 100644 (file)
@@ -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) {