]> git.ipfire.org Git - pakfire.git/commitdiff
stripper: Implement copying sources
authorMichael Tremer <michael.tremer@ipfire.org>
Sun, 29 Dec 2024 18:29:34 +0000 (18:29 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sun, 29 Dec 2024 18:29:34 +0000 (18:29 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/stripper.c

index bc46873bcfda3e9db0c0971199e77692ef6d1ca3..7e6ec1af41856379305263405bd48254a112a9bb 100644 (file)
@@ -20,6 +20,8 @@
 
 #include <errno.h>
 #include <stdlib.h>
+#include <sys/sendfile.h>
+#include <sys/stat.h>
 
 // libdw
 #include <elfutils/libdw.h>
@@ -29,6 +31,7 @@
 #include <pakfire/path.h>
 #include <pakfire/string.h>
 #include <pakfire/stripper.h>
+#include <pakfire/util.h>
 
 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;
 }
 
 /*