From: Michael Tremer Date: Sun, 29 Dec 2024 18:29:34 +0000 (+0000) Subject: stripper: Implement copying sources X-Git-Tag: 0.9.30~661 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=038d93176141c96a24ea9339991b122d5e41e1ed;p=pakfire.git stripper: Implement copying sources Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/stripper.c b/src/libpakfire/stripper.c index bc46873bc..7e6ec1af4 100644 --- a/src/libpakfire/stripper.c +++ b/src/libpakfire/stripper.c @@ -20,6 +20,8 @@ #include #include +#include +#include // libdw #include @@ -29,6 +31,7 @@ #include #include #include +#include struct pakfire_stripper { struct pakfire_ctx* ctx; @@ -161,9 +164,93 @@ ERROR: static int pakfire_stripper_copy_source_file( struct pakfire_stripper* stripper, const char* filename) { - printf("FILENAME = %s\n", filename); + struct stat st = {}; + char path[PATH_MAX]; + int r; - return 0; + FILE* src = NULL; + FILE* dst = NULL; + + const char* buildroot = pakfire_relpath(stripper->pakfire, stripper->path); + + // Remove the original source path + r = pakfire_path_relative(path, BUILD_SRC_DIR, filename); + if (r < 0) + goto ERROR; + + // Add the debug directory source path + r = pakfire_path_append(path, DEBUG_SRC_DIR, path); + if (r < 0) + goto ERROR; + + // Add the buildroot + r = pakfire_path_append(path, buildroot, path); + if (r < 0) + goto ERROR; + + // Nothing to do if the file has already been copied + if (pakfire_path_exists(path)) + return 0; + + // Create all directories + r = pakfire_mkparentdir(path, 0755); + if (r < 0) + goto ERROR; + + // Open the destination file + dst = fopen(path, "wx"); + if (!dst) { + switch (errno) { + // If the file exist already, we are done + case EEXIST: + goto ERROR; + + default: + ERROR(stripper->ctx, "Could not open %s: %m\n", path); + r = -errno; + goto ERROR; + } + } + + // Open the source file + src = fopen(filename, "r"); + if (!src) { + ERROR(stripper->ctx, "Could not open %s: %m\n", filename); + r = -errno; + goto ERROR; + } + + // Stat the source file + r = fstat(fileno(src), &st); + if (r < 0) { + ERROR(stripper->ctx, "Could not stat %s: %m\n", filename); + r = -errno; + goto ERROR; + } + + // Copy all content + ssize_t bytes_written = sendfile(fileno(dst), fileno(src), 0, st.st_size); + if (bytes_written < st.st_size) { + ERROR(stripper->ctx, "Failed to copy source file %s: %m\n", filename); + r = -errno; + goto ERROR; + } + + // Change permissions + r = fchmod(fileno(dst), 0444); + if (r < 0) { + ERROR(stripper->ctx, "Could not change permissions of %s: %m\n", path); + r = -errno; + goto ERROR; + } + +ERROR: + if (dst) + fclose(dst); + if (src) + fclose(src); + + return r; } /*