1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
#ifndef PW_FS_H
#define PW_FS_H
#include <SDL3/SDL_stdinc.h>
#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
|