aboutsummaryrefslogtreecommitdiff
path: root/src/fs.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/fs.c')
-rw-r--r--src/fs.c36
1 files changed, 32 insertions, 4 deletions
diff --git a/src/fs.c b/src/fs.c
index 365b0df..e4a2d4e 100644
--- a/src/fs.c
+++ b/src/fs.c
@@ -3,6 +3,7 @@
#include <SDL3/SDL_log.h>
#include "fs.h"
+#include "path.h"
#include "sysroot.h"
#include "tar.h"
@@ -188,6 +189,10 @@ struct pw_fs *pw_fs_init(char const *path) {
SDL_LogDebug(0, "using in-memory cart storage");
}
+ for (size_t i = 0; i < fs->root.count; ++i) {
+ SDL_LogDebug(0, "mounted %s", fs->root.entries[i]->path);
+ }
+
fs->dev.in.r.type = PW_FS_NODE_STATIC;
fs->dev.in.r.data = empty_bytes;
fs->dev.in.r.size = 0;
@@ -321,9 +326,29 @@ static int cmp_path(void const *a, void const *b) {
return SDL_strcmp(path, (*entry)->path);
}
-static struct pw_fs_entry *find_entry(struct pw_fs *fs, char const *path) {
+static char const cart_dir[] = "user_data/carts/";
+
+static int cmp_cart_path(void const *a, void const *b) {
+ char const *cart = a;
+ struct pw_fs_entry const *const *entry = b;
+
+ char const *path = (*entry)->path;
+ if (SDL_strstr(path, cart_dir) == path) {
+ return SDL_strcmp(cart, path + sizeof(cart_dir) - 1);
+ } else {
+ return SDL_strcmp(cart_dir, path);
+ }
+}
+
+static struct pw_fs_entry *find_entry(struct pw_fs *fs, char const *path,
+ bool absolute) {
+ SDL_CompareCallback compare = cmp_path;
+ if (!absolute && (pw_hasext(path, ".p8") || pw_hasext(path, ".p8.png"))) {
+ compare = cmp_cart_path;
+ }
+
struct pw_fs_entry **e = SDL_bsearch(path, fs->root.entries, fs->root.count,
- sizeof(*fs->root.entries), cmp_path);
+ sizeof(*fs->root.entries), compare);
if (!e) {
return NULL;
}
@@ -372,6 +397,8 @@ error:
bool pw_fs_open(struct pw_fs *fs, char const *path, enum pw_fs_open_flag flags,
pw_fd *fd) {
+ char const *rawpath = path;
+
if (!fs) {
return SDL_InvalidParamError("fs");
}
@@ -415,7 +442,8 @@ bool pw_fs_open(struct pw_fs *fs, char const *path, enum pw_fs_open_flag flags,
s->flags |= PW_FS_STREAM_APPEND;
}
- struct pw_fs_entry *e = find_entry(fs, path);
+ bool absolute = path != rawpath;
+ struct pw_fs_entry *e = find_entry(fs, path, absolute);
if (e && (flags & PW_O_CREAT) && (flags & PW_O_EXCL)) {
fs->err = PW_EEXIST;
return SDL_SetError("file already exists");
@@ -720,7 +748,7 @@ bool pw_fs_flush(struct pw_fs *fs, pw_fd fd) {
}
b->data[i] = '\0';
- SDL_Log("PICO-8>%c %s", marker, b->data + offset);
+ SDL_LogDebug(0, "PICO-8>%c %s", marker, b->data + offset);
offset = i + 1;
}