#include <errno.h>
#include <stdlib.h>
+#include <sys/sendfile.h>
+#include <sys/stat.h>
// libdw
#include <elfutils/libdw.h>
#include <pakfire/path.h>
#include <pakfire/string.h>
#include <pakfire/stripper.h>
+#include <pakfire/util.h>
struct pakfire_stripper {
struct pakfire_ctx* ctx;
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;
}
/*