diff options
Diffstat (limited to 'src/fs.c')
| -rw-r--r-- | src/fs.c | 124 |
1 files changed, 117 insertions, 7 deletions
@@ -184,8 +184,21 @@ struct pw_fs *pw_fs_init(char const *path) { pw_tar_free(tar); tar = NULL; + + fs->dirty = false; + } else if (path && SDL_GetPathInfo(path, NULL)) { + SDL_LogWarn(0, "corrupt cart storage: %s", SDL_GetError()); + + // In case of a corrupt cart storage, do not clobber it (in case it is + // a file intended for something else, and instead fallback to in-memory + // storage; just to be sure. + SDL_free(fs->path); + fs->path = NULL; + + fs->dirty = false; } else if (path) { - SDL_LogWarn(0, "could not open cart storage: %s", SDL_GetError()); + SDL_LogDebug(0, "initializing cart storage"); + fs->dirty = true; } else { SDL_LogDebug(0, "using in-memory cart storage"); } @@ -339,13 +352,18 @@ static int cmp_cart_path(void const *a, void const *b) { 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), compare); + sizeof(*fs->root.entries), cmp_path); + + // Under some conditions, PICO-8 will `LOAD("CART.P8")` by opening a file + // instead of using the embedded cart. To support this, for relative cart + // paths look in the `user_data/carts/` directory if we didn't find the + // file under root. + if (!e && !absolute && + (pw_hasext(path, ".p8") || pw_hasext(path, ".p8.png"))) { + e = SDL_bsearch(path, fs->root.entries, fs->root.count, + sizeof(*fs->root.entries), cmp_cart_path); + } if (!e) { return NULL; @@ -476,6 +494,52 @@ bool pw_fs_open(struct pw_fs *fs, char const *path, enum pw_fs_open_flag flags, return true; } +#define PW_S_IFCHR 0020000 +#define PW_S_IFREG 0100000 + +bool pw_fs_stat(struct pw_fs *fs, char const *path, struct pw_fs_stat *stat) { + char const *rawpath = path; + + if (!fs) { + return SDL_InvalidParamError("fs"); + } + if (!normalize_path(&path)) { + fs->err = PW_EINVAL; + return SDL_InvalidParamError("path"); + } + if (!stat) { + fs->err = PW_EINVAL; + return SDL_InvalidParamError("stat"); + } + + bool absolute = path != rawpath; + struct pw_fs_entry *e = find_entry(fs, path, absolute); + if (!e) { + fs->err = PW_ENOENT; + return SDL_SetError("file does not exist"); + } + + switch (e->node.type) { + case PW_FS_NODE_STATIC: + stat->mode = PW_S_IFCHR | 0444; + stat->size = 0; + break; + case PW_FS_NODE_BUFFER: + stat->mode = PW_S_IFREG | 0666; + stat->size = (uint64_t)SDL_min(e->node.w.size, UINT64_MAX); + break; + case PW_FS_NODE_OUTPUT: + stat->mode = PW_S_IFCHR | 0222; + stat->size = 0; + break; + default: + fs->err = PW_ENOENT; + return false; + } + + return true; +} + static struct pw_fs_stream *get_stream(struct pw_fs *fs, pw_fd fd) { if (!fs) { SDL_InvalidParamError("fs"); @@ -587,6 +651,52 @@ bool pw_fs_seek(struct pw_fs *fs, pw_fd fd, int64_t offset, return true; } +bool pw_fs_cntl(struct pw_fs *fs, pw_fd fd, enum pw_fs_cntl_op op, + int32_t *arg) { + struct pw_fs_stream *s = get_stream(fs, fd); + if (!s) { + return false; + } + if (!arg) { + fs->err = PW_EINVAL; + return false; + } + + switch (op) { + case PW_F_GETFL: { + enum pw_fs_open_flag f; + if (s->flags & PW_FS_STREAM_READ && s->flags & PW_FS_STREAM_WRITE) { + f = PW_O_RDWR; + } else if (s->flags & PW_FS_STREAM_READ) { + f = PW_O_RDONLY; + } else if (s->flags & PW_FS_STREAM_WRITE) { + f = PW_O_WRONLY; + } else { + fs->err = PW_EBADF; + return SDL_SetError("invalid file stream flags"); + } + if (s->flags & PW_FS_STREAM_APPEND) { + f |= PW_O_APPEND; + } + + *arg = (int32_t)f; + return true; + } + case PW_F_SETFL: + if (*arg & PW_O_APPEND) { + s->flags |= PW_FS_STREAM_APPEND; + } else { + s->flags &= ~PW_FS_STREAM_APPEND; + } + + *arg = 0; + return true; + default: + fs->err = PW_EINVAL; + return SDL_SetError("invalid file descriptor operation %d", op); + } +} + uint8_t const *pw_fs_read(struct pw_fs *fs, pw_fd fd, uint32_t len, uint32_t *sz) { struct pw_fs_stream *s = get_stream(fs, fd); |
