aboutsummaryrefslogtreecommitdiff
path: root/src/fs.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/fs.c')
-rw-r--r--src/fs.c51
1 files changed, 44 insertions, 7 deletions
diff --git a/src/fs.c b/src/fs.c
index be4a2d4..91fc984 100644
--- a/src/fs.c
+++ b/src/fs.c
@@ -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");
}