summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChloe M <chloe@faracom.org>2026-07-27 23:49:14 -0400
committerChloe M <chloe@faracom.org>2026-07-27 23:49:14 -0400
commit12ada77d1c0d62adb90409f25e4812f9ebf1ba7a (patch)
treefb021acb172dc5975b914b027a153c37d10ff21c
parentf7a1c5dacbb04001bafc5515373385fc2063f04f (diff)
packer: Create file view per idata descriptor
Signed-off-by: Chloe M <chloe@faracom.org>
-rw-r--r--core/rdar.c83
1 files changed, 83 insertions, 0 deletions
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 <sys/stat.h>
+#include <sys/mman.h>
#include <sys/queue.h>
#include <unistd.h>
+#include <fcntl.h>
#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
@@ -54,6 +57,17 @@ 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
* in the offset.
@@ -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;
};
@@ -103,6 +119,67 @@ version(void)
}
/*
+ * 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
*
* @path: Path of file
@@ -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);