diff options
| author | Nicholas Rodrigues Lordello <[email protected]> | 2026-10-23 14:15:15 +0200 |
|---|---|---|
| committer | Nicholas Rodrigues Lordello <[email protected]> | 2026-10-23 14:15:15 +0200 |
| commit | 24ecea092c4db12cee343a9885fa56b8587b5f93 (patch) | |
| tree | 07d15c1aa0ee7893904713f44ea44b401c8179c4 | |
| parent | 1a08db34294753895988a0952ccd0cbff545c6a8 (diff) | |
Write Virtual File-System to Disk
We added logic to read from a Tar archive, and write to Tar archive,
including seeding our virtual file-system with files from said
archive, but we forgot to add the writing of the Tar file when
syncing. Resolve that.
Additionally, normalize paths and strip leading `/`. Don't allow
empty paths. Paths that end in `/` are allowed - we don't have
special directory semantics (maybe can be reconsidered?)
| -rw-r--r-- | src/fs.c | 51 |
1 files changed, 44 insertions, 7 deletions
@@ -70,6 +70,16 @@ static void free_entries(struct pw_fs_mount *m) { SDL_free(m->entries); } +static bool normalize_path(char const **path) { + if (!*path) { + return false; + } + while (**path == '/') { + ++(*path); + } + return **path; +} + struct pw_fs *pw_fs_init(char const *path) { struct pw_tar *tar = NULL; struct pw_fs *fs = SDL_calloc(1, sizeof(*fs)); @@ -110,8 +120,8 @@ struct pw_fs *pw_fs_init(char const *path) { 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) { + char const *p = pw_tar_read(tar, &w.data, &w.size, &blocks); + if (!normalize_path(&p)) { ok = false; break; } @@ -128,9 +138,9 @@ struct pw_fs *pw_fs_init(char const *path) { goto error; } - size_t n = SDL_strlcpy(e->path, path, PW_FS_PATH_MAX); + size_t n = SDL_strlcpy(e->path, p, PW_FS_PATH_MAX); if (n >= PW_FS_PATH_MAX) { - SDL_SetError("file path too long '%s'", path); + SDL_SetError("file path too long '%s'", p); SDL_free(w.data); ok = false; break; @@ -218,8 +228,35 @@ bool pw_fs_sync(struct pw_fs *fs) { return SDL_InvalidParamError("fs"); } - // TODO write entries to `fs->path` - return true; + struct pw_tar *tar = pw_tar_open(fs->path, PW_TAR_OPEN_WRITE); + if (!tar) { + return false; + } + + bool ok = false; + for (size_t i = 0; i < fs->root.count; ++i) { + struct pw_fs_entry *e = fs->root.entries[i]; + + // Only persist `user_data` files written by PICO-8. + if (e->node.type != PW_FS_NODE_BUFFER || + SDL_memcmp(e->path, "user_data/", 10)) { + continue; + } + + struct pw_fs_node_buffer *w = &e->node.w; + if (!pw_tar_write(tar, e->path, w->data, w->size)) { + goto cleanup; + } + } + ok = pw_tar_write(tar, "", NULL, 0); + +cleanup: + if (!ok) { + SDL_LogWarn(0, "failed to persist user storage: %s", SDL_GetError()); + } + + pw_tar_free(tar); + return ok; } static int cmp_path(void const *a, void const *b) { @@ -282,7 +319,7 @@ bool pw_fs_open(struct pw_fs *fs, char const *path, enum pw_fs_open_flag flags, if (!fs) { return SDL_InvalidParamError("fs"); } - if (!path) { + if (!normalize_path(&path)) { fs->err = PW_EINVAL; return SDL_InvalidParamError("path"); } |
