int sourcesfd;
};
-static int pakfire_stripper_open_sources(struct pakfire_stripper* stripper) {
+static int pakfire_stripper_open_sources(struct pakfire_stripper* self) {
// Open the source directory
- stripper->sourcesfd = pakfire_openat(stripper->pakfire, BUILD_SRC_DIR, O_DIRECTORY|O_PATH);
- if (stripper->sourcesfd < 0) {
+ self->sourcesfd = pakfire_openat(self->pakfire, BUILD_SRC_DIR, O_DIRECTORY|O_PATH);
+ if (self->sourcesfd < 0) {
switch (errno) {
case ENOENT:
break;
default:
- ERROR(stripper->ctx, "Could not open %s: %m\n", BUILD_SRC_DIR);
+ ERROR(self->ctx, "Could not open %s: %m\n", BUILD_SRC_DIR);
return -errno;
}
}
int pakfire_stripper_create(struct pakfire_stripper** stripper,
struct pakfire* pakfire, struct pakfire_jail* jail, const char* path) {
- struct pakfire_stripper* s = NULL;
+ struct pakfire_stripper* self = NULL;
int r;
// Allocate a new object
- s = calloc(1, sizeof(*s));
- if (!s)
+ self = calloc(1, sizeof(*self));
+ if (!self)
return -errno;
// Store a reference to the context
- s->ctx = pakfire_ctx(pakfire);
+ self->ctx = pakfire_ctx(pakfire);
// Store a reference to Pakfire
- s->pakfire = pakfire_ref(pakfire);
+ self->pakfire = pakfire_ref(pakfire);
// Store a reference to the jail
- s->jail = pakfire_jail_ref(jail);
+ self->jail = pakfire_jail_ref(jail);
// Initialize the reference counter
- s->nrefs = 1;
+ self->nrefs = 1;
// Initialize file descriptors
- s->sourcesfd = -EBADF;
+ self->sourcesfd = -EBADF;
// Store the path
- r = pakfire_string_set(s->path, path);
+ r = pakfire_string_set(self->path, path);
if (r < 0)
goto ERROR;
// Create a filelist
- r = pakfire_filelist_create(&s->filelist, s->pakfire);
+ r = pakfire_filelist_create(&self->filelist, self->pakfire);
if (r < 0)
goto ERROR;
// Create a list for the sources
- r = pakfire_filelist_create(&s->sources, s->pakfire);
+ r = pakfire_filelist_create(&self->sources, self->pakfire);
if (r < 0)
goto ERROR;
// Open the source directory
- r = pakfire_stripper_open_sources(s);
+ r = pakfire_stripper_open_sources(self);
if (r < 0)
goto ERROR;
// Return the pointer
- *stripper = pakfire_stripper_ref(s);
+ *stripper = pakfire_stripper_ref(self);
ERROR:
- if (s)
- pakfire_stripper_unref(s);
+ if (self)
+ pakfire_stripper_unref(self);
return r;
}
-static void pakfire_stripper_free(struct pakfire_stripper* stripper) {
- if (stripper->sources)
- pakfire_filelist_unref(stripper->sources);
- if (stripper->sourcesfd)
- close(stripper->sourcesfd);
- if (stripper->filelist)
- pakfire_filelist_unref(stripper->filelist);
- if (stripper->pakfire)
- pakfire_unref(stripper->pakfire);
- if (stripper->jail)
- pakfire_jail_unref(stripper->jail);
- if (stripper->ctx)
- pakfire_ctx_unref(stripper->ctx);
- free(stripper);
+static void pakfire_stripper_free(struct pakfire_stripper* self) {
+ if (self->sources)
+ pakfire_filelist_unref(self->sources);
+ if (self->sourcesfd)
+ close(self->sourcesfd);
+ if (self->filelist)
+ pakfire_filelist_unref(self->filelist);
+ if (self->pakfire)
+ pakfire_unref(self->pakfire);
+ if (self->jail)
+ pakfire_jail_unref(self->jail);
+ if (self->ctx)
+ pakfire_ctx_unref(self->ctx);
+ free(self);
}
-struct pakfire_stripper* pakfire_stripper_ref(struct pakfire_stripper* stripper) {
- ++stripper->nrefs;
+struct pakfire_stripper* pakfire_stripper_ref(struct pakfire_stripper* self) {
+ ++self->nrefs;
- return stripper;
+ return self;
}
-struct pakfire_stripper* pakfire_stripper_unref(struct pakfire_stripper* stripper) {
- if (--stripper->nrefs > 0)
- return stripper;
+struct pakfire_stripper* pakfire_stripper_unref(struct pakfire_stripper* self) {
+ if (--self->nrefs > 0)
+ return self;
- pakfire_stripper_free(stripper);
+ pakfire_stripper_free(self);
return NULL;
}
static int pakfire_stripper_find_elf(
struct pakfire_ctx* ctx, struct pakfire_file* file, void* data) {
- struct pakfire_stripper* stripper = data;
+ struct pakfire_stripper* self = data;
int r;
// Never try to strip any firmware
// Add ELF files to the filelist
if (pakfire_file_matches_class(file, PAKFIRE_FILE_ELF)) {
- r = pakfire_filelist_add(stripper->filelist, file);
+ r = pakfire_filelist_add(self->filelist, file);
if (r < 0)
return r;
}
/*
Scan all files in path and identify ELF files
*/
-static int pakfire_stripper_scan(struct pakfire_stripper* stripper) {
+static int pakfire_stripper_scan(struct pakfire_stripper* self) {
struct pakfire_filelist* filelist = NULL;
int r;
// Create a new filelist
- r = pakfire_filelist_create(&filelist, stripper->pakfire);
+ r = pakfire_filelist_create(&filelist, self->pakfire);
if (r < 0)
goto ERROR;
// Scan path for all files
- r = pakfire_filelist_scan(filelist, stripper->path, NULL, NULL, 0);
+ r = pakfire_filelist_scan(filelist, self->path, NULL, NULL, 0);
if (r < 0)
goto ERROR;
// Walk through all files to find ELF files
- r = pakfire_filelist_walk(filelist, pakfire_stripper_find_elf, stripper, 0, NULL);
+ r = pakfire_filelist_walk(filelist, pakfire_stripper_find_elf, self, 0, NULL);
if (r < 0)
goto ERROR;
static int pakfire_stripper_collect_sources(
struct pakfire_ctx* ctx, struct pakfire_elf* elf, const char* filename, void* data) {
- struct pakfire_stripper* stripper = data;
+ struct pakfire_stripper* self = data;
struct pakfire_file* file = NULL;
int r;
return 0;
// Don't add files more than once
- if (pakfire_filelist_contains(stripper->sources, filename))
+ if (pakfire_filelist_contains(self->sources, filename))
return 0;
// Create a new file object
- r = pakfire_file_create(&file, stripper->pakfire, filename);
+ r = pakfire_file_create(&file, self->pakfire, filename);
if (r < 0)
goto ERROR;
// Add the file to the list
- r = pakfire_filelist_add(stripper->sources, file);
+ r = pakfire_filelist_add(self->sources, file);
if (r < 0)
goto ERROR;
static int pakfire_stripper_copy_sources(
struct pakfire_ctx* ctx, struct pakfire_file* file, void* data) {
- struct pakfire_stripper* stripper = data;
+ struct pakfire_stripper* self = data;
struct stat st = {};
char p[PATH_MAX];
FILE* dst = NULL;
goto ERROR;
// Open the source file
- fd = openat(stripper->sourcesfd, p, O_RDONLY);
+ fd = openat(self->sourcesfd, p, O_RDONLY);
if (fd < 0) {
switch (errno) {
// If the source file does not exist, we cannot copy anything
goto ERROR;
default:
- ERROR(stripper->ctx, "Could not open /%s: %m\n", p);
+ ERROR(self->ctx, "Could not open /%s: %m\n", p);
r = -errno;
goto ERROR;
}
// Stat the source file
r = fstat(fd, &st);
if (r < 0) {
- ERROR(stripper->ctx, "Could not stat /%s: %m\n", p);
+ ERROR(self->ctx, "Could not stat /%s: %m\n", p);
r = -errno;
goto ERROR;
}
// Add the buildroot
- r = pakfire_path_append(p, stripper->path, path);
+ r = pakfire_path_append(p, self->path, path);
if (r < 0)
goto ERROR;
// Open the destination file
dst = fopen(p, "wx");
if (!dst) {
- ERROR(stripper->ctx, "Could not open %s: %m\n", p);
+ ERROR(self->ctx, "Could not open %s: %m\n", p);
r = -errno;
goto ERROR;
}
// Copy all content
ssize_t bytes_written = sendfile(fileno(dst), fd, 0, st.st_size);
if (bytes_written < st.st_size) {
- ERROR(stripper->ctx, "Failed to copy source file %s: %m\n", path);
+ ERROR(self->ctx, "Failed to copy source file %s: %m\n", path);
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);
+ ERROR(self->ctx, "Could not change permissions of %s: %m\n", path);
r = -errno;
goto ERROR;
}
return r;
}
-static int pakfire_stripper_strip_debug_sections(struct pakfire_stripper* stripper,
+static int pakfire_stripper_strip_debug_sections(struct pakfire_stripper* self,
struct pakfire_file* file, struct pakfire_elf* elf) {
const char* build_id = NULL;
char build_id_path[PATH_MAX];
// Fetch the Build ID
build_id = pakfire_elf_build_id(elf);
if (!build_id) {
- ERROR(stripper->ctx, "No Build ID\n");
+ ERROR(self->ctx, "No Build ID\n");
r = -ENOTSUP;
goto ERROR;
}
// Make Build ID path
r = pakfire_string_format(build_id_path, "%s/usr/lib/debug/.build-id/%c%c/%s.debug",
- stripper->path, build_id[0], build_id[1], build_id + 2);
+ self->path, build_id[0], build_id[1], build_id + 2);
if (r < 0)
goto ERROR;
goto ERROR;
// Make a path for a new temporary file
- r = pakfire_path(stripper->pakfire, tmppath, "%s", PAKFIRE_TMP_DIR "/.pakfire-strip.XXXXXXX");
+ r = pakfire_path(self->pakfire, tmppath, "%s", PAKFIRE_TMP_DIR "/.pakfire-strip.XXXXXXX");
if (r < 0)
goto ERROR;
};
// Run the command
- r = pakfire_jail_exec_command(stripper->jail, extract_argv, NULL, 0);
+ r = pakfire_jail_exec_command(self->jail, extract_argv, NULL, 0);
if (r < 0) {
- ERROR(stripper->ctx, "Could not extract debug sections from %s: %s\n",
+ ERROR(self->ctx, "Could not extract debug sections from %s: %s\n",
pakfire_file_get_path(file), strerror(-r));
return r;
// The command returned an error
} else if (r > 0) {
- ERROR(stripper->ctx, "Could not extract debug sections from %s: %d\n",
+ ERROR(self->ctx, "Could not extract debug sections from %s: %d\n",
pakfire_file_get_path(file), r);
return -ENOTSUP;
}
path,
// The stripped output
- pakfire_relpath(stripper->pakfire, tmppath),
+ pakfire_relpath(self->pakfire, tmppath),
NULL,
};
// Run the command
- r = pakfire_jail_exec_command(stripper->jail, strip_argv, NULL, 0);
+ r = pakfire_jail_exec_command(self->jail, strip_argv, NULL, 0);
if (r < 0) {
- ERROR(stripper->ctx, "Could not strip debug sections from %s: %s\n",
+ ERROR(self->ctx, "Could not strip debug sections from %s: %s\n",
pakfire_file_get_path(file), strerror(-r));
goto ERROR;
// The command returned an error
} else if (r > 0) {
- ERROR(stripper->ctx, "Could not strip debug sections from %s\n",
+ ERROR(self->ctx, "Could not strip debug sections from %s\n",
pakfire_file_get_path(file));
r = -ENOTSUP;
goto ERROR;
static int pakfire_stripper_strip(
struct pakfire_ctx* ctx, struct pakfire_file* file, void* data) {
- struct pakfire_stripper* stripper = data;
+ struct pakfire_stripper* self = data;
struct pakfire_elf* elf = NULL;
int r;
// Open the ELF file
- r = pakfire_elf_open_file(&elf, stripper->ctx, file);
+ r = pakfire_elf_open_file(&elf, self->ctx, file);
if (r < 0)
goto ERROR;
if (!pakfire_elf_is_stripped(elf)) {
// Collect sources
r = pakfire_elf_foreach_source_file(elf,
- pakfire_stripper_collect_sources, stripper);
+ pakfire_stripper_collect_sources, self);
if (r < 0)
goto ERROR;
// Strip debug information
- r = pakfire_stripper_strip_debug_sections(stripper, file, elf);
+ r = pakfire_stripper_strip_debug_sections(self, file, elf);
if (r < 0)
goto ERROR;
}
return r;
}
-int pakfire_stripper_run(struct pakfire_stripper* stripper) {
+int pakfire_stripper_run(struct pakfire_stripper* self) {
int r;
// Scan for all ELF files in path
- r = pakfire_stripper_scan(stripper);
+ r = pakfire_stripper_scan(self);
if (r < 0)
return r;
// If the filelist is empty, there is nothing to do
- if (pakfire_filelist_is_empty(stripper->filelist))
+ if (pakfire_filelist_is_empty(self->filelist))
return 0;
// Strip all files
- r = pakfire_filelist_walk(stripper->filelist, pakfire_stripper_strip,
- stripper, PAKFIRE_FILELIST_SHOW_PROGRESS, _("Stripping Files..."));
+ r = pakfire_filelist_walk(self->filelist, pakfire_stripper_strip,
+ self, PAKFIRE_FILELIST_SHOW_PROGRESS, _("Stripping Files..."));
if (r < 0)
return r;
// Copy sources
- r = pakfire_filelist_walk(stripper->sources, pakfire_stripper_copy_sources,
- stripper, PAKFIRE_FILELIST_SHOW_PROGRESS, _("Copying Sources..."));
+ r = pakfire_filelist_walk(self->sources, pakfire_stripper_copy_sources,
+ self, PAKFIRE_FILELIST_SHOW_PROGRESS, _("Copying Sources..."));
if (r < 0)
return r;