From 12ada77d1c0d62adb90409f25e4812f9ebf1ba7a Mon Sep 17 00:00:00 2001 From: Chloe M Date: Mon, 27 Jul 2026 23:49:14 -0400 Subject: packer: Create file view per idata descriptor Signed-off-by: Chloe M --- core/rdar.c | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) (limited to 'core/rdar.c') diff --git a/core/rdar.c b/core/rdar.c index 2cd1e66..b3a7c64 100644 --- a/core/rdar.c +++ b/core/rdar.c @@ -4,8 +4,11 @@ */ #define _DEFAULT_SOURCE +#include +#include #include #include +#include #include #include #include @@ -53,6 +56,17 @@ static const char *input_dir = NULL; static const char *output_path = NULL; static TAILQ_HEAD(, idata_desc) d_desc_table; +/* + * Represents a file that has been mapped into memory + * + * @data: File data + * @length: Length of data + */ +struct file_view { + char *data; + size_t length; +}; + /* * A data descriptor holds information about a specific * file along with an offset to which it is located at @@ -75,10 +89,12 @@ struct data_desc { * before being flushed to the archive * * @data: Actual data descriptor + * @file: File view this descriptor references * @link: TAILQ link */ struct idata_desc { struct data_desc data; + struct file_view file; TAILQ_ENTRY(idata_desc) link; }; @@ -102,6 +118,67 @@ version(void) printf("Version v%d.%d\n", Major, Minor); } +/* + * Destroy a file view + * + * @view: View to destory + */ +static void +view_destroy(struct file_view *view) +{ + munmap(view->data, view->length); + view->data = NULL; + view->length = 0; +} + +/* + * Create a mapped file view from a file path + */ +static int +view_from_path(const char *path, struct file_view *result) +{ + struct stat sb; + int fd; + + if (path == NULL || result == NULL) { + return -1; + } + + fd = open(path, O_RDONLY); + if (fd < 0) { + printf("fatal: failed to open '%s'\n", path); + perror("open"); + return -1; + } + + if (stat(path, &sb) < 0) { + printf("fatal: failed to stat '%s'\n", path); + perror("stat"); + close(fd); + return -1; + } + + result->data = mmap( + NULL, + sb.st_size, + PROT_READ, + MAP_SHARED, + fd, + 0 + ); + + if (result->data == NULL) { + printf("fatal: failed to map '%s'\n", path); + perror("mmap"); + close(fd); + return -1; + } + + result->length = sb.st_size; + close(fd); + return 0; +} + /* * Allocate an internal data descriptor * @@ -127,6 +204,11 @@ idata_from_file(const char *path) return NULL; } + if (view_from_path(path, &idata_desc->file) < 0) { + free(idata_desc); + return NULL; + } + /* Initialize the data descriptor */ data_desc = &idata_desc->data; memcpy(data_desc->magic, RDAR_MAGIC, RDAR_MAGIC_LEN); @@ -167,6 +249,7 @@ idata_table_destroy(void) desc = TAILQ_FIRST(&d_desc_table); while (desc != NULL) { printf("-- %s\n", desc->data.path); + view_destroy(&desc->file); next = TAILQ_NEXT(desc, link); TAILQ_REMOVE(&d_desc_table, desc, link); -- cgit v1.2.3