#ifndef PW_FS_H #define PW_FS_H #include #define PW_FS_PATH_MAX 100 #define PW_FS_STREAM_MAX 16 /** * File-system node. */ union pw_fs_node { enum pw_fs_node_type { PW_FS_NODE_STATIC, PW_FS_NODE_BUFFER, PW_FS_NODE_OUTPUT, } type; struct pw_fs_node_static { enum pw_fs_node_type type; uint8_t const *data; size_t size; } r; struct pw_fs_node_buffer { enum pw_fs_node_type type; uint8_t *data; size_t size; size_t capacity; } w; }; /** * A file node. */ struct pw_fs_entry { char path[PW_FS_PATH_MAX]; union pw_fs_node node; }; /** * Special file-system standard input/output devices. */ struct pw_fs_dev { union pw_fs_node in; union pw_fs_node out; union pw_fs_node err; }; /** * Mounted file-system entries. */ struct pw_fs_mount { struct pw_fs_entry **entries; size_t count; size_t capacity; }; /** * An open file stream. */ struct pw_fs_stream { union pw_fs_node *node; size_t pos; enum pw_fs_stream_flag { PW_FS_STREAM_READ = 1 << 0, PW_FS_STREAM_WRITE = 1 << 1, PW_FS_STREAM_APPEND = 1 << 2, } flags; }; typedef int32_t pw_fd; /** * PICO-8 compatible open flags. * * These correspond to the `musl` open flags that Emscripten is built with, * only the ones supported by this mock file-system. */ enum pw_fs_open_flag { PW_O_RDONLY = 00, PW_O_WRONLY = 01, PW_O_RDWR = 02, PW_O_ACCMODE = 03, PW_O_CREAT = 0100, PW_O_EXCL = 0200, PW_O_TRUNC = 01000, PW_O_APPEND = 02000, PW_O_DIRECTORY = 0200000, }; /** * PICO-8 file stat. */ struct pw_fs_stat { uint32_t mode; uint64_t size; }; /** * PICO-8 compatible seek position. */ enum pw_fs_seek_whence { PW_SEEK_SET = 0, PW_SEEK_CUR = 1, PW_SEEK_END = 2, }; /** * PICO-8 file descriptor operation. */ enum pw_fs_cntl_op { PW_F_GETFL = 3, PW_F_SETFL = 4, }; /** * PICO-8 file-system error number. */ enum pw_fs_errno { PW_EOK = 0, PW_ENOENT = 2, PW_EBADF = 9, PW_ENOMEM = 12, PW_EACCES = 13, PW_EEXIST = 17, PW_ENOTDIR = 20, PW_EINVAL = 22, PW_EMFILE = 24, PW_ENOTTY = 25, PW_EFBIG = 27, PW_EOVERFLOW = 75, }; /** * A simple file-system for storing PICO-8 cart data. */ struct pw_fs { char *path; bool dirty; struct pw_fs_mount root; struct pw_fs_dev dev; struct pw_fs_stream streams[PW_FS_STREAM_MAX]; enum pw_fs_errno err; }; /** * Loads a file-system from disk. * * File-systems are just stored as `tar` files. */ struct pw_fs *pw_fs_init(char const *path); /** * Frees resources associated with the file-system. */ void pw_fs_free(struct pw_fs *fs); /** * Synchronizes the file-system to its persistent storage. */ bool pw_fs_sync(struct pw_fs *fs); /** * Opens a file stream. */ bool pw_fs_open(struct pw_fs *fs, char const *path, enum pw_fs_open_flag flags, pw_fd *fd); /** * Stats a file. */ bool pw_fs_stat(struct pw_fs *fs, char const *path, struct pw_fs_stat *stat); /** * Closes a file stream. */ bool pw_fs_close(struct pw_fs *fs, pw_fd fd); /** * Seek. */ bool pw_fs_seek(struct pw_fs *fs, pw_fd fd, int64_t offset, enum pw_fs_seek_whence whence, int64_t *newpos); /** * Modify a file descriptor. * * `arg` is used for both passing in an argument for an operation and * returning a result for one. */ bool pw_fs_cntl(struct pw_fs *fs, pw_fd fd, enum pw_fs_cntl_op op, int32_t *arg); /** * Return a readonly buffer of size `*sz` to the contents of the file. It * will be at most `len` in size. */ uint8_t const *pw_fs_read(struct pw_fs *fs, pw_fd fd, uint32_t len, uint32_t *sz); /** * Return a writable buffer of size `len` to the contents of the file. */ uint8_t *pw_fs_write(struct pw_fs *fs, pw_fd fd, uint32_t len); /** * Flushes a file stream. */ bool pw_fs_flush(struct pw_fs *fs, pw_fd fd); /** * Returns the error code of the last failed operation. */ enum pw_fs_errno pw_fs_errno(struct pw_fs *fs); #endif