From: Michael Tremer Date: Tue, 22 Oct 2024 09:23:34 +0000 (+0000) Subject: stripper: Identify source files X-Git-Tag: 0.9.30~953 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=070eb9e91e36847c6b1583e1f5332d23f1802b80;p=pakfire.git stripper: Identify source files Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/include/pakfire/stripper.h b/src/libpakfire/include/pakfire/stripper.h index 84d9dff0a..dd30ced67 100644 --- a/src/libpakfire/include/pakfire/stripper.h +++ b/src/libpakfire/include/pakfire/stripper.h @@ -23,6 +23,9 @@ #ifdef PAKFIRE_PRIVATE +#define BUILD_SRC_DIR "/build/source" +#define DEBUG_SRC_DIR "/usr/src/debug" + struct pakfire_stripper; int pakfire_stripper_create(struct pakfire_stripper** stripper, diff --git a/src/libpakfire/stripper.c b/src/libpakfire/stripper.c index b8e9c84a5..cc73f5193 100644 --- a/src/libpakfire/stripper.c +++ b/src/libpakfire/stripper.c @@ -21,8 +21,12 @@ #include #include +// libdw +#include + #include #include +#include #include #include @@ -147,6 +151,106 @@ ERROR: return r; } +static int pakfire_stripper_copy_source_file( + struct pakfire_stripper* stripper, const char* filename) { + printf("FILENAME = %s\n", filename); + + return 0; +} + +static int pakfire_stripper_copy_sources( + struct pakfire* pakfire, struct pakfire_file* file, void* data) { + struct pakfire_stripper* stripper = data; + const char* filename = NULL; + char basename[PATH_MAX]; + Dwarf* dwarf = NULL; + Dwarf_Files* files = NULL; + Dwarf_Die* die = NULL; + Dwarf_Off offset = 0; + Dwarf_Off next_offset; + 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); + if (!dwarf) { + CTX_ERROR(stripper->ctx, "Could not initialize DWARF context: %s\n", + dwarf_errmsg(-1)); + r = -errno; + goto ERROR; + } + + for (;;) { + // Fetch the next compilation unit + r = dwarf_nextcu(dwarf, offset, &next_offset, &cu_header_length, NULL, NULL, NULL); + if (r < 0) + goto ERROR; + + // Fetch the Debug Information Entry + die = dwarf_offdie(dwarf, offset + cu_header_length, &die_mem); + if (!die) + break; + + // Fetch the source files + r = dwarf_getsrcfiles(die, &files, &count); + if (r < 0) { + CTX_ERROR(stripper->ctx, "Could not fetch the source files: %s\n", + dwarf_errmsg(-1)); + r = -errno; + goto ERROR; + } + + // Iterate over all files... + for (unsigned int i = 0; i < count; i++) { + // Fetch the filename + filename = dwarf_filesrc(files, i, NULL, NULL); + + // If the source file is not in the right path, we ignore it + if (!pakfire_string_startswith(filename, BUILD_SRC_DIR)) + continue; + + // Determine the basename + r = pakfire_path_basename(basename, filename); + if (r < 0) + goto ERROR; + + // Ignore things like or + if (pakfire_string_startswith(basename, "<") && pakfire_string_endswith(basename, ">")) + continue; + + CTX_DEBUG(stripper->ctx, "Found source file: %s\n", filename); + + // Copy the file + r = pakfire_stripper_copy_source_file(stripper, filename); + if (r < 0) { + CTX_ERROR(stripper->ctx, "Could not copy source file %s: %s\n", + filename, strerror(-r)); + goto ERROR; + } + } + + offset = next_offset; + } + +ERROR: + if (f) + fclose(f); + if (dwarf) + dwarf_end(dwarf); + + return r; +} + int pakfire_stripper_run(struct pakfire_stripper* stripper) { int r; @@ -159,7 +263,10 @@ int pakfire_stripper_run(struct pakfire_stripper* stripper) { if (pakfire_filelist_is_empty(stripper->filelist)) return 0; - // XXX TODO + // Copy sources + r = pakfire_filelist_walk(stripper->filelist, pakfire_stripper_copy_sources, stripper, 0); + if (r < 0) + return r; return 0; }