From 1a08db34294753895988a0952ccd0cbff545c6a8 Mon Sep 17 00:00:00 2001 From: Nicholas Rodrigues Lordello Date: Fri, 23 Oct 2026 10:56:23 +0200 Subject: Implement Tar Reading and Writing We implement a reduced version of UStar as a storage mechanism for our virtual FS to persist to disk. We don't implement all UStar features, but it should be good enough. --- src/fs.c | 196 +++++++++++++++++++++++++++---------- src/fs.h | 4 +- src/tar.c | 326 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/tar.h | 50 ++++++++++ 4 files changed, 521 insertions(+), 55 deletions(-) create mode 100644 src/tar.c create mode 100644 src/tar.h diff --git a/src/fs.c b/src/fs.c index 72acde9..be4a2d4 100644 --- a/src/fs.c +++ b/src/fs.c @@ -3,6 +3,7 @@ #include "fs.h" #include "sysroot.h" +#include "tar.h" #define BUFFER_SIZE 1024 @@ -19,14 +20,65 @@ static bool init_output(union pw_fs_node *node) { return true; } -struct pw_fs *pw_fs_init(char const *tar) { +static int cmp_entry(void const *a, void const *b) { + struct pw_fs_entry const *const *ea = a; + struct pw_fs_entry const *const *eb = b; + return SDL_strcmp((*ea)->path, (*eb)->path); +} + +static bool append_entry(struct pw_fs_mount *m, struct pw_fs_entry *e) { + while (m->count >= m->capacity) { + size_t capacity = SDL_max(m->capacity, 16) * 2; + if (capacity < m->capacity) { + return SDL_OutOfMemory(); + } + + void *entries = SDL_realloc(m->entries, capacity * sizeof(*m->entries)); + if (!entries) { + return SDL_OutOfMemory(); + } + + m->entries = entries; + m->capacity = capacity; + } + + m->entries[m->count++] = e; + return true; +} + +static void sort_entries(struct pw_fs_mount *m) { + SDL_qsort(m->entries, m->count, sizeof(*m->entries), cmp_entry); +} + +static void free_entry(struct pw_fs_entry *e) { + union pw_fs_node *n = &e->node; + switch (n->type) { + case PW_FS_NODE_BUFFER: + case PW_FS_NODE_OUTPUT: + SDL_free(n->w.data); + break; + default: + break; + } + SDL_free(e); +} + +static void free_entries(struct pw_fs_mount *m) { + for (size_t i = 0; i < m->count; ++i) { + free_entry(m->entries[i]); + } + SDL_free(m->entries); +} + +struct pw_fs *pw_fs_init(char const *path) { + struct pw_tar *tar = NULL; struct pw_fs *fs = SDL_calloc(1, sizeof(*fs)); if (!fs) { return NULL; } - fs->tar = SDL_strdup(tar); - if (!fs->tar) { + fs->path = SDL_strdup(path); + if (!fs->path) { goto error; } @@ -35,17 +87,80 @@ struct pw_fs *pw_fs_init(char const *tar) { goto error; } fs->root.capacity = 32; - fs->root.entries[0] = SDL_calloc(1, sizeof(**fs->root.entries)); - if (!*fs->root.entries) { - goto error; + + { + struct pw_fs_entry *datapod = SDL_calloc(1, sizeof(*datapod)); + if (!datapod) { + goto error; + } + + SDL_strlcpy(datapod->path, "data.pod", PW_FS_PATH_MAX); + datapod->node.r.type = PW_FS_NODE_STATIC; + datapod->node.r.data = pw_sysroot_pico8_data_pod.data; + datapod->node.r.size = pw_sysroot_pico8_data_pod.size; + + if (!append_entry(&fs->root, datapod)) { + goto error; + } } - fs->root.count++; - SDL_strlcpy(fs->root.entries[0]->path, "data.pod", PW_FS_PATH_MAX); - fs->root.entries[0]->node.r.type = PW_FS_NODE_STATIC; - fs->root.entries[0]->node.r.data = pw_sysroot_pico8_data_pod.data; - fs->root.entries[0]->node.r.size = pw_sysroot_pico8_data_pod.size; - // TODO: read the tar and populate root... + tar = pw_tar_open(path, PW_TAR_OPEN_READ); + if (tar) { + bool ok; + for (;;) { + size_t blocks; + struct pw_fs_node_buffer w = {.type = PW_FS_NODE_BUFFER}; + char const *path = pw_tar_read(tar, &w.data, &w.size, &blocks); + if (!path) { + ok = false; + break; + } + + if (!w.data) { + ok = true; + break; + } + w.capacity = blocks * PW_TAR_BLOCK_SIZE; + + struct pw_fs_entry *e = SDL_calloc(1, sizeof(*e)); + if (!e) { + SDL_free(w.data); + goto error; + } + + size_t n = SDL_strlcpy(e->path, path, PW_FS_PATH_MAX); + if (n >= PW_FS_PATH_MAX) { + SDL_SetError("file path too long '%s'", path); + SDL_free(w.data); + ok = false; + break; + } + + if (!append_entry(&fs->root, e)) { + SDL_free(w.data); + goto error; + } + + e->node.w = w; + } + + if (ok) { + sort_entries(&fs->root); + } else { + SDL_LogWarn(0, "could not read cart storage: %s", SDL_GetError()); + + // Free the loaded entries, if any - since we don't know whether or not + // they should be there. + for (size_t i = 1; i < fs->root.count; ++i) { + free_entry(fs->root.entries[i]); + } + fs->root.count = 1; + } + + pw_tar_free(tar); + } else { + SDL_LogWarn(0, "could not open cart storage: %s", SDL_GetError()); + } fs->dev.in.r.type = PW_FS_NODE_STATIC; fs->dev.in.r.data = empty_bytes; @@ -63,11 +178,11 @@ struct pw_fs *pw_fs_init(char const *tar) { return fs; error: + if (tar) { + pw_tar_free(tar); + } if (fs->root.entries) { - for (size_t i = 0; i < fs->root.count; ++i) { - SDL_free(fs->root.entries[i]); - } - SDL_free(fs->root.entries); + free_entries(&fs->root); } if (fs->dev.out.w.data) { SDL_free(fs->dev.out.w.data); @@ -75,8 +190,8 @@ error: if (fs->dev.err.w.data) { SDL_free(fs->dev.err.w.data); } - if (fs->tar) { - SDL_free(fs->tar); + if (fs->path) { + SDL_free(fs->path); } SDL_free(fs); return NULL; @@ -89,15 +204,12 @@ void pw_fs_free(struct pw_fs *fs) { } if (fs->root.entries) { - for (size_t i = 0; i < fs->root.count; ++i) { - SDL_free(fs->root.entries[i]); - } - SDL_free(fs->root.entries); + free_entries(&fs->root); } SDL_free(fs->dev.out.w.data); SDL_free(fs->dev.err.w.data); - SDL_free(fs->tar); + SDL_free(fs->path); SDL_free(fs); } @@ -106,7 +218,7 @@ bool pw_fs_sync(struct pw_fs *fs) { return SDL_InvalidParamError("fs"); } - // TODO write entries to `fs->tar` + // TODO write entries to `fs->path` return true; } @@ -125,13 +237,7 @@ static struct pw_fs_entry *find_entry(struct pw_fs *fs, char const *path) { return *e; } -static int cmp_entry(void const *a, void const *b) { - struct pw_fs_entry const *const *ea = a; - struct pw_fs_entry const *const *eb = b; - return SDL_strcmp((*ea)->path, (*eb)->path); -} - -static struct pw_fs_entry *add_entry(struct pw_fs *fs, char const *path) { +static struct pw_fs_entry *create_entry(struct pw_fs *fs, char const *path) { struct pw_fs_entry *e = SDL_calloc(1, sizeof(*e)); if (!e) { fs->err = PW_ENOMEM; @@ -154,28 +260,11 @@ static struct pw_fs_entry *add_entry(struct pw_fs *fs, char const *path) { e->node.w.size = 0; e->node.w.capacity = BUFFER_SIZE; - while (fs->root.count >= fs->root.capacity) { - size_t capacity = SDL_max(fs->root.capacity, 16) * 2; - if (capacity < fs->root.capacity) { - fs->err = PW_ENOMEM; - SDL_OutOfMemory(); - goto error; - } - - void *entries = - SDL_realloc(fs->root.entries, capacity * sizeof(*fs->root.entries)); - if (!entries) { - fs->err = PW_ENOMEM; - goto error; - } - - fs->root.entries = entries; - fs->root.capacity = capacity; + if (!append_entry(&fs->root, e)) { + fs->err = PW_ENOMEM; + goto error; } - - fs->root.entries[fs->root.count++] = e; - SDL_qsort(fs->root.entries, fs->root.count, sizeof(*fs->root.entries), - cmp_entry); + sort_entries(&fs->root); fs->dirty = true; return e; @@ -253,7 +342,7 @@ bool pw_fs_open(struct pw_fs *fs, char const *path, enum pw_fs_open_flag flags, return SDL_SetError("cannot read write-only file"); } - if (!e && !(e = add_entry(fs, path))) { + if (!e && !(e = create_entry(fs, path))) { return false; } @@ -411,6 +500,7 @@ uint8_t *pw_fs_write(struct pw_fs *fs, pw_fd fd, uint32_t len) { if (!(s->flags & PW_FS_STREAM_WRITE)) { fs->err = PW_EBADF; SDL_SetError("cannot write to read-only file stream"); + return NULL; } struct pw_fs_node_buffer *w; diff --git a/src/fs.h b/src/fs.h index a214c4a..289fb9a 100644 --- a/src/fs.h +++ b/src/fs.h @@ -115,7 +115,7 @@ enum pw_fs_errno { * A simple file-system for storing PICO-8 cart data. */ struct pw_fs { - char *tar; + char *path; bool dirty; struct pw_fs_mount root; struct pw_fs_dev dev; @@ -128,7 +128,7 @@ struct pw_fs { * * File-systems are just stored as `tar` files. */ -struct pw_fs *pw_fs_init(char const *tar); +struct pw_fs *pw_fs_init(char const *path); /** * Frees resources associated with the file-system. diff --git a/src/tar.c b/src/tar.c new file mode 100644 index 0000000..f75aca1 --- /dev/null +++ b/src/tar.c @@ -0,0 +1,326 @@ +#include +#include + +#include "tar.h" + +static uint8_t empty_block[PW_TAR_BLOCK_SIZE] = {0}; + +static void init_write_block(uint8_t block[PW_TAR_BLOCK_SIZE]) { + // Since we support a reduced set of Tar features, we can prepare our + // write block up front and just write over the parts that change + // per added entry. + + SDL_memset(block, '\0', PW_TAR_BLOCK_SIZE); + SDL_memcpy(block + 100, "0000644", 8); + SDL_memcpy(block + 108, "0000000", 8); + SDL_memcpy(block + 116, "0000000", 8); + SDL_memcpy(block + 136, "00000000000", 12); + SDL_memcpy(block + 156, "0", 1); + SDL_memcpy(block + 257, "ustar", 6); + SDL_memcpy(block + 263, "00", 2); + SDL_memcpy(block + 265, "root", 5); + SDL_memcpy(block + 297, "root", 5); + SDL_memcpy(block + 329, "0000000", 8); + SDL_memcpy(block + 337, "0000000", 8); +} + +struct pw_tar *pw_tar_open(char const *path, enum pw_tar_open_flag flag) { + SDL_IOStream *stream; + switch (flag) { + case PW_TAR_OPEN_READ: + stream = SDL_IOFromFile(path, "rb"); + break; + case PW_TAR_OPEN_WRITE: + stream = SDL_IOFromFile(path, "wb"); + break; + default: + SDL_InvalidParamError("flag"); + return NULL; + } + if (!stream) { + return NULL; + } + + struct pw_tar *tar = SDL_malloc(sizeof(*tar)); + if (!tar) { + if (!SDL_CloseIO(stream)) { + SDL_LogWarn(0, "unexpected error closing tar IO stream: %s", + SDL_GetError()); + } + SDL_OutOfMemory(); + return NULL; + } + + if (flag == PW_TAR_OPEN_WRITE) { + init_write_block(tar->block); + } + + tar->stream = stream; + return tar; +} + +static void close_archive(struct pw_tar *tar) { + if (tar->stream && !SDL_CloseIO(tar->stream)) { + SDL_LogWarn(0, "unexpected error closing tar IO stream: %s", + SDL_GetError()); + } + tar->stream = NULL; +} + +void pw_tar_free(struct pw_tar *tar) { + if (!tar) { + SDL_InvalidParamError("tar"); + return; + } + + close_archive(tar); + SDL_free(tar); +} + +static bool read_exact(struct pw_tar *tar, uint8_t *buffer, size_t size) { + while (size) { + size_t n = SDL_ReadIO(tar->stream, buffer, size); + if (n == 0) { + if (SDL_GetIOStatus(tar->stream) == SDL_IO_STATUS_EOF) { + return SDL_SetError("unexpected end of file"); + } + return false; + } + + buffer += n; + size -= n; + } + + return true; +} + +static bool skip_blocks(struct pw_tar *tar, size_t blocks) { + while (blocks-- > 0) { + if (!read_exact(tar, tar->block, sizeof(tar->block))) { + return false; + } + } + return true; +} + +static bool is_eoa(struct pw_tar *tar) { + for (size_t i = 0; i < sizeof(tar->block); ++i) { + if (tar->block[i]) { + return false; + } + } + return true; +} + +static uint64_t parse_octal(struct pw_tar *tar, size_t offset, size_t len) { + uint8_t const *p = tar->block + offset; + size_t n = len; + + // Extremely permissive parsing, inspired from `libarchive`'s `untar.c`. + uint64_t i = 0; + while (n && (*p < '0' || *p > '7')) { + ++p; + --n; + } + while (n && *p >= '0' && *p <= '7') { + i *= 8; + i += *p - '0'; + ++p; + --n; + } + + return i; +} + +static bool check_magic(struct pw_tar *tar) { + static char const magic[] = "ustar\0" + "00"; + return !SDL_memcmp(tar->block + 257, magic, sizeof(magic)); +} + +static uint64_t compute_checksum(struct pw_tar *tar) { + uint64_t u = 0; + for (size_t n = 0; n < sizeof(tar->block); ++n) { + if (n < 148 || n > 155) { + u += (uint64_t)tar->block[n]; + } else { + u += ' '; + } + } + return u; +} + +static bool verify_checksum(struct pw_tar *tar) { + return compute_checksum(tar) == parse_octal(tar, 146, 8); +} + +static bool has_filename_prefix(struct pw_tar *tar) { return tar->block[345]; } + +static size_t block_count(size_t size) { + return (size + PW_TAR_BLOCK_SIZE - 1) / PW_TAR_BLOCK_SIZE; +} + +char const *pw_tar_read(struct pw_tar *tar, uint8_t **data, size_t *size, + size_t *blocks) { + if (!tar) { + SDL_InvalidParamError("tar"); + return NULL; + } + if (!tar->stream) { + SDL_SetError("read past end of archive"); + return NULL; + } + + uint64_t sz; + uint64_t blk; + uint8_t *dat = NULL; + + for (;;) { + if (!read_exact(tar, tar->block, sizeof(tar->block))) { + goto error; + } + + if (is_eoa(tar)) { + close_archive(tar); + sz = blk = 0; + break; + } + + if (!check_magic(tar)) { + SDL_SetError("not a tar archive"); + goto error; + } + if (has_filename_prefix(tar)) { + SDL_SetError("filename prefixes not supported"); + goto error; + } + + if (!verify_checksum(tar)) { + SDL_SetError("checksum verification failed"); + goto error; + } + + sz = parse_octal(tar, 124, 12); + blk = block_count(sz); + + char ft = (char)tar->block[156]; + if (ft && ft != '0') { + SDL_LogDebug(0, "skipping unsupported tar file type %c", ft); + + if (!skip_blocks(tar, blk)) { + goto error; + } + continue; + } + + dat = SDL_malloc(SDL_min(blk, 1) * sizeof(tar->block)); + if (!dat) { + goto error; + } + + if (!read_exact(tar, dat, blk * sizeof(tar->block))) { + goto error; + } + + break; + } + + *data = dat; + *size = sz; + *blocks = SDL_min(blk, 1); + return (char const *)tar->block; + +error: + close_archive(tar); + if (dat) { + SDL_free(dat); + } + return NULL; +} + +static bool write_exact(struct pw_tar *tar, uint8_t const *buffer, + size_t size) { + // SDL will always write `size` bytes unless it encounters an error, + // wo we don't need to loop for partial writes. + size_t n = SDL_WriteIO(tar->stream, buffer, size); + return n == size; +} + +static bool encode_octal(struct pw_tar *tar, size_t offset, size_t len, + uint64_t value) { + uint8_t *p = tar->block + offset; + size_t n = len; + uint64_t i = value; + + p[--n] = '\0'; + while (n) { + p[--n] = (i & 8) + '0'; + i /= 8; + } + + return !i; +} + +static bool encode_path(struct pw_tar *tar, char const *path) { + size_t n = SDL_strlcpy((char *)tar->block, path, 100); + if (n >= 100) { + return false; + } + + // Make sure to fill the rest of the path region with `0`s. + size_t sz = n + 1; + size_t dirty = 100 - sz; + if (dirty) { + SDL_memset(tar->block + sz, 0, dirty); + } + + return true; +} + +static void encode_checksum(struct pw_tar *tar) { + // The checksum _always_ fits in its location, so we don't need to + // check it. It can be at most `504 * 255 + 8 * 32 = 128776 = 0373410` + // and fits into 8 octal characters. + encode_octal(tar, 146, 8, compute_checksum(tar)); +} + +bool pw_tar_write(struct pw_tar *tar, char const *path, uint8_t const *data, + size_t size) { + if (!tar) { + return SDL_InvalidParamError("tar"); + } + if (!path) { + return SDL_InvalidParamError("path"); + } + + if (!tar->stream) { + return SDL_SetError("write past end of archive"); + } + + if (!data) { + bool ok = write_exact(tar, empty_block, sizeof(empty_block)); + close_archive(tar); + return ok; + } + + if (!encode_path(tar, path)) { + return SDL_SetError("path too long '%s'", path); + } + if (!encode_octal(tar, 124, 12, size)) { + return SDL_SetError("file too large %zu", size); + } + + encode_checksum(tar); + + size_t padding = block_count(size) * PW_TAR_BLOCK_SIZE - size; + if (!write_exact(tar, tar->block, sizeof(tar->block)) || + !write_exact(tar, data, size) || + !write_exact(tar, empty_block, padding)) { + // We cannot rely on the archive being in a good state (we may have + // partially written a block). + close_archive(tar); + return false; + } + + return true; +} diff --git a/src/tar.h b/src/tar.h new file mode 100644 index 0000000..59b1c19 --- /dev/null +++ b/src/tar.h @@ -0,0 +1,50 @@ +#ifndef PW_TAR_H +#define PW_TAR_H + +#include + +#define PW_TAR_BLOCK_SIZE 512 + +/** + * A UStar archive stream. + */ +struct pw_tar { + SDL_IOStream *stream; + uint8_t block[PW_TAR_BLOCK_SIZE]; +}; + +/** + * Specify Tar archive stream open mode. + */ +enum pw_tar_open_flag { + PW_TAR_OPEN_READ, + PW_TAR_OPEN_WRITE, +}; + +/** + * Opens a new Tar reader. + */ +struct pw_tar *pw_tar_open(char const *path, enum pw_tar_open_flag flag); + +/** + * Closes and frees the Tar reader. + */ +void pw_tar_free(struct pw_tar *tar); + +/** + * Reads the next entry in a Tar file, returning the its file name. + * + * Sets `data` to the file contents with `size`, `data` is allocated with + * `blocks * PW_TAR_BLOCK_SIZE` bytes, and must be freed by the caller. + */ +char const *pw_tar_read(struct pw_tar *tar, uint8_t **data, size_t *size, + size_t *blocks); + +/** + * Writes an entry to the Tar file. Write NULL data to indicate the end of + * the archive. + */ +bool pw_tar_write(struct pw_tar *tar, char const *path, uint8_t const *data, + size_t size); + +#endif -- cgit v1.2.3