// Copy the data if there is any
if (archive_entry_size(entry)) {
// Open the file
- f = pakfire_file_open(file);
+ f = pakfire_file_fopen(file);
if (!f) {
r = 1;
goto ERROR;
#include <ctype.h>
#include <errno.h>
+#include <fcntl.h>
#include <fnmatch.h>
#include <libgen.h>
#include <limits.h>
return levels;
}
-FILE* pakfire_file_open(struct pakfire_file* file) {
+int pakfire_file_open(struct pakfire_file* file) {
const char* path = NULL;
+ int fd = -EBADF;
// Fetch the absolute path of the file
path = pakfire_file_get_abspath(file);
if (!path)
- return NULL;
+ return -EBADF;
// Fix the path if we have entered the jail
if (pakfire_ctx_has_flag(file->ctx, PAKFIRE_CTX_IN_JAIL)) {
path = pakfire_relpath(file->pakfire, path);
if (!path)
- return NULL;
+ return -EBADF;
}
- FILE* f = fopen(path, "r+");
- if (!f)
+ // Open the file
+ fd = open(path, O_CLOEXEC);
+ if (fd < 0) {
ERROR(file->ctx, "Could not open %s: %m\n", path);
+ return -errno;
+ }
+
+ return fd;
+}
+
+FILE* pakfire_file_fopen(struct pakfire_file* file) {
+ int fd = -EBADF;
+
+ // Open the file descriptor
+ fd = pakfire_file_open(file);
+ if (fd < 0)
+ return NULL;
- return f;
+ // Return a file handle
+ return fdopen(fd, "r+");
}
int pakfire_file_payload_matches(struct pakfire_file* file,
return 0;
// Open the file
- f = pakfire_file_open(file);
+ f = pakfire_file_fopen(file);
if (!f) {
r = 1;
goto ERROR;
pakfire_digests_reset(digests, types);
// Open the file
- f = pakfire_file_open(file);
+ f = pakfire_file_fopen(file);
if (!f)
goto ERROR;
return r;
// Open the file
- f = pakfire_file_open(file);
+ f = pakfire_file_fopen(file);
if (!f) {
ERROR(file->ctx, "Could not open %s: %m\n", pakfire_file_get_path(file));
return 1;
const char* pakfire_file_get_abspath(struct pakfire_file* file);
-FILE* pakfire_file_open(struct pakfire_file* file);
+int pakfire_file_open(struct pakfire_file* file);
+FILE* pakfire_file_fopen(struct pakfire_file* file);
int pakfire_file_payload_matches(struct pakfire_file* file,
const void* needle, const size_t length);
#endif
static int pakfire_stripper_copy_sources(
- struct pakfire_stripper* stripper, struct pakfire_file* file) {
+ struct pakfire_stripper* stripper, struct pakfire_file* file, int fd) {
const char* filename = NULL;
char basename[PATH_MAX];
Dwarf* dwarf = NULL;
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);
+ dwarf = dwarf_begin(fd, DWARF_C_READ);
if (!dwarf) {
switch (dwarf_errno()) {
// If we don't have any DWARF information there is nothing to do
}
ERROR:
- if (f)
- fclose(f);
if (dwarf)
dwarf_end(dwarf);
static int pakfire_stripper_strip(
struct pakfire_ctx* ctx, struct pakfire_file* file, void* data) {
struct pakfire_stripper* stripper = data;
+ int fd = -EBADF;
int r;
+ // Open the file
+ fd = pakfire_file_open(file);
+ if (fd < 0) {
+ r = -errno;
+ goto ERROR;
+ }
+
// Copy sources
- r = pakfire_stripper_copy_sources(stripper, file);
+ r = pakfire_stripper_copy_sources(stripper, file, fd);
if (r < 0)
- return r;
+ goto ERROR;
- return 0;
+ERROR:
+ if (fd >= 0)
+ close(fd);
+
+ return r;
}
static int __pakfire_stripper_run(struct pakfire_ctx* ctx, void* data) {