From: Michael Tremer Date: Fri, 25 Oct 2024 11:07:42 +0000 (+0000) Subject: file: Add functions to store a file descriptor X-Git-Tag: 0.9.30~902 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=7da29d2c150e994c063351b4d07c3d7cc85fd6ca;p=pakfire.git file: Add functions to store a file descriptor Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/file.c b/src/libpakfire/file.c index 05c84181f..ac7b54a13 100644 --- a/src/libpakfire/file.c +++ b/src/libpakfire/file.c @@ -67,6 +67,9 @@ struct pakfire_file { // Use the libarchive entry to store common attributes struct archive_entry* entry; + // File Descriptor + int fd; + // Capabilities cap_t caps; @@ -385,6 +388,9 @@ PAKFIRE_EXPORT int pakfire_file_create(struct pakfire_file** file, struct pakfir // Initialize reference counter f->nrefs = 1; + // Initialize file descriptor + f->fd = -EBADF; + // Create a new archive entry f->entry = archive_entry_new(); if (!f->entry) { @@ -537,6 +543,9 @@ ERROR: } static void pakfire_file_free(struct pakfire_file* file) { + if (file->fd >= 0) + close(file->fd); + // Free capabilities if (file->caps) cap_free(file->caps); @@ -577,6 +586,27 @@ int pakfire_file_set_flags(struct pakfire_file* file, int flag) { return 0; } +// File Descriptor + +int pakfire_file_get_fd(struct pakfire_file* file) { + return file->fd; +} + +int pakfire_file_set_fd(struct pakfire_file* file, int fd) { + // Close any previously assigned file descriptors + if (file->fd >= 0) + close(file->fd); + + // Duplicate the file descriptor + file->fd = dup(fd); + if (file->fd < 0) { + CTX_ERROR(file->ctx, "Could not duplicate file descriptor: %m\n"); + return -errno; + } + + return 0; +} + #define pakfire_file_strmode(file, buffer) \ __pakfire_file_strmode(file, buffer, sizeof(buffer)) diff --git a/src/libpakfire/include/pakfire/file.h b/src/libpakfire/include/pakfire/file.h index 468fb8657..ad35d0930 100644 --- a/src/libpakfire/include/pakfire/file.h +++ b/src/libpakfire/include/pakfire/file.h @@ -132,6 +132,9 @@ enum pakfire_file_classes { PAKFIRE_FILE_RUNTIME_LINKER = (1 << 14), }; +int pakfire_file_get_fd(struct pakfire_file* file); +int pakfire_file_set_fd(struct pakfire_file* file, int fd); + int pakfire_file_has_payload(struct pakfire_file* file); int pakfire_file_read(struct pakfire_file* file, struct archive* reader, const char* path);