-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLuaWFS.cpp
More file actions
459 lines (400 loc) · 11.7 KB
/
LuaWFS.cpp
File metadata and controls
459 lines (400 loc) · 11.7 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/*-------------------------------------------------------------------------
This source file is a part of mil
For the latest info, see http://www.marrin.org/
Copyright (c) 2026, Chris Marrin
All rights reserved.
-------------------------------------------------------------------------*/
#define LUA_LIB
#include "WebFileSystem.h"
#include "lua.hpp"
#include <cstring>
static const char* LUA_WFSHANDLE = "File*";
struct WFSStream
{
fs::File f; /* stream (NULL for incompletely created streams) */
lua_CFunction closef; /* to close stream (NULL for closed streams) */
};
static inline WFSStream* towfsstream(lua_State* L) { return reinterpret_cast<WFSStream*>(luaL_checkudata(L, 1, LUA_WFSHANDLE)); }
static inline bool isclosed(WFSStream* p) { return p->closef == nullptr; }
/* Check whether 'mode' matches '[rwa]%+?[L_MODEEXT]*' */
static int l_checkmode(const char* mode)
{
return (*mode != '\0' && strchr("rwa", *(mode++)) != nullptr &&
(*mode != '+' || ((void)(++mode), 1)));
}
static int f_tostring (lua_State *L)
{
WFSStream* p = towfsstream(L);
if (isclosed(p)) {
lua_pushliteral(L, "file (closed)");
} else {
lua_pushfstring(L, "file (%p)", p);
}
return 1;
}
static fs::File& tofile(lua_State *L)
{
WFSStream* p = towfsstream(L);
if (l_unlikely(isclosed(p))) {
luaL_error(L, "attempt to use a closed file");
}
lua_assert(p->f.isOpenFile());
return p->f;
}
/*
** When creating file handles, always creates a 'closed' file handle
** before opening the actual file; so, if there is a memory error, the
** handle is in a consistent state.
*/
static WFSStream* newprefile(lua_State* L)
{
WFSStream* p = reinterpret_cast<WFSStream*>(lua_newuserdatauv(L, sizeof(WFSStream), 0));
p->closef = nullptr; /* mark file handle as 'closed' */
// Since the File is a c++ object we need to do a placement new on it
new (&(p->f)) fs::File();
luaL_setmetatable(L, LUA_WFSHANDLE);
return p;
}
/*
** Calls the 'close' function from a file handle. The 'volatile' avoids
** a bug in some versions of the Clang compiler (e.g., clang 3.0 for
** 32 bits).
*/
static int aux_close(lua_State *L)
{
WFSStream* p = towfsstream(L);
volatile lua_CFunction cf = p->closef;
p->closef = nullptr; /* mark stream as closed */
return (*cf)(L); /* close it */
}
static int f_close(lua_State* L)
{
tofile(L); /* make sure argument is an open stream */
return aux_close(L);
}
static int f_gc (lua_State* L)
{
WFSStream* p = towfsstream(L);
if (!isclosed(p)) {
aux_close(L); /* ignore closed and incompletely open files */
}
// File is a c++ object, but there's no need to call the dtor
// since it's just been closed
return 0;
}
/*
** function to close regular files
*/
static int io_fclose(lua_State* L)
{
WFSStream* p = towfsstream(L);
errno = 0;
p->f.close();
return luaL_fileresult(L, 1, nullptr);
}
static WFSStream* newfile(lua_State* L)
{
WFSStream* p = newprefile(L);
p->closef = &io_fclose;
return p;
}
static int io_exists(lua_State* L)
{
const char* filename = luaL_checkstring(L, 1);
errno = 0;
lua_pushboolean(L, mil::WebFileSystem::exists(filename));
return 1;
}
static int io_open(lua_State* L)
{
const char* filename = luaL_checkstring(L, 1);
const char* mode = luaL_optstring(L, 2, "r");
WFSStream* p = newfile(L);
const char* md = mode; /* to traverse/check mode */
luaL_argcheck(L, l_checkmode(md), 2, "invalid mode");
errno = 0;
p->f = mil::WebFileSystem::open(filename, mode);
return (!p->f && !p->f.isDirectory()) ? luaL_fileresult(L, 0, filename) : 1;
}
static int io_remove (lua_State* L)
{
const char *filename = luaL_checkstring(L, 1);
errno = 0;
return luaL_fileresult(L, mil::WebFileSystem::remove(filename), filename);
}
static int io_rename (lua_State* L)
{
const char *fromname = luaL_checkstring(L, 1);
const char *toname = luaL_checkstring(L, 2);
errno = 0;
return luaL_fileresult(L, mil::WebFileSystem::rename(fromname, toname), nullptr);
}
static int io_mkdir(lua_State* L)
{
const char *path = luaL_checkstring(L, 1);
errno = 0;
return luaL_fileresult(L, mil::WebFileSystem::mkdir(path), path);
}
static int io_rmdir(lua_State* L)
{
const char *path = luaL_checkstring(L, 1);
return luaL_fileresult(L, mil::WebFileSystem::rmdir(path), path);
}
static int io_totalbytes(lua_State* L)
{
lua_pushinteger(L, lua_Integer(mil::WebFileSystem::totalBytes()));
return 1;
}
static int io_usedbytes(lua_State* L)
{
lua_pushinteger(L, lua_Integer(mil::WebFileSystem::usedBytes()));
return 1;
}
static int f_seek(lua_State* L)
{
static fs::SeekMode mode[] = { fs::SeekSet, fs::SeekCur, fs::SeekEnd };
static const char *const modenames[] = {"set", "cur", "end", NULL};
fs::File& f = tofile(L);
int op = luaL_checkoption(L, 2, "cur", modenames);
lua_Integer p3 = luaL_optinteger(L, 3, 0);
uint32_t offset = uint32_t(p3);
luaL_argcheck(L, (lua_Integer)offset == p3, 3, "not an integer in proper range");
errno = 0;
op = f.seek(offset, mode[op]);
if (l_unlikely(op)) {
return luaL_fileresult(L, 0, nullptr); /* error */
} else {
lua_pushinteger(L, (lua_Integer)f.position());
}
return 1;
}
static int f_filename(lua_State* L)
{
fs::File& f = tofile(L);
errno = 0;
lua_pushstring(L, f.name());
return 1;
}
static int f_opennextfile(lua_State* L)
{
fs::File& f = tofile(L);
errno = 0;
if (!f.isDirectory()) {
return luaL_fileresult(L, 0, nullptr);
}
WFSStream* p = newfile(L);
errno = 0;
p->f = f.openNextFile();
return 1;
}
static int test_eof(lua_State *L, fs::File& f)
{
int c = f.peek();
lua_pushliteral(L, "");
return (c != EOF);
}
static int read_line(lua_State* L, fs::File& f, bool chop)
{
luaL_Buffer b;
int c = 0;
luaL_buffinit(L, &b);
do { /* may need to read several chunks to get whole line */
char *buff = luaL_prepbuffer(&b); /* preallocate buffer space */
int i = 0;
while (i < LUAL_BUFFERSIZE && (c = f.read()) != EOF && c != '\n') {
buff[i++] = c; /* read up to end of line or buffer limit */
}
luaL_addsize(&b, i);
} while (c != EOF && c != '\n'); /* repeat until end of line */
if (!chop && c == '\n') { /* want a newline and have one? */
luaL_addchar(&b, c); /* add ending newline to result */
}
luaL_pushresult(&b); /* close buffer */
/* return ok if read something (either a newline or something else) */
return (c == '\n' || lua_rawlen(L, -1) > 0);
}
static int read_chars(lua_State* L, fs::File& f, size_t n)
{
size_t nr; /* number of chars actually read */
uint8_t* p;
luaL_Buffer b;
luaL_buffinit(L, &b);
p = reinterpret_cast<uint8_t*>(luaL_prepbuffsize(&b, n)); /* prepare buffer to read whole block */
nr = f.read(p, n); /* try to read 'n' chars */
luaL_addsize(&b, nr);
luaL_pushresult(&b); /* close buffer */
return (nr > 0); /* true iff read something */
}
static int g_read(lua_State* L, fs::File& f, int first)
{
int nargs = lua_gettop(L) - 1;
int n, success;
errno = 0;
if (nargs == 0) { /* no arguments? */
success = read_line(L, f, 1);
n = first + 1; /* to return 1 result */
} else {
/* ensure stack space for all results and for auxlib's buffer */
luaL_checkstack(L, nargs+LUA_MINSTACK, "too many arguments");
success = 1;
for (n = first; nargs-- && success; n++) {
if (lua_type(L, n) == LUA_TNUMBER) {
size_t l = (size_t)luaL_checkinteger(L, n);
success = (l == 0) ? test_eof(L, f) : read_chars(L, f, l);
} else {
const char *p = luaL_checkstring(L, n);
if (*p == '*') {
p++; /* skip optional '*' (for compatibility) */
}
switch (*p) {
case 'l': /* line */
success = read_line(L, f, 1);
break;
case 'L': /* line with end-of-line */
success = read_line(L, f, 0);
break;
default:
return luaL_argerror(L, n, "invalid format");
}
}
}
}
if (!f) {
return luaL_fileresult(L, 0, NULL);
}
if (!success) {
lua_pop(L, 1); /* remove last result */
luaL_pushfail(L); /* push nil instead */
}
return n - first;
}
static int f_read(lua_State* L)
{
return g_read(L, tofile(L), 2);
}
// We support writing strings only
static int g_write(lua_State* L, fs::File& f, int arg)
{
int nargs = lua_gettop(L) - arg;
int status = 1;
errno = 0;
for ( ; nargs--; arg++) {
size_t l;
const uint8_t* s = reinterpret_cast<const uint8_t*>(luaL_checklstring(L, arg, &l));
status = status && (f.write(s, l) == l);
}
if (l_likely(status)) {
return 1; /* file handle already on stack top */
} else {
return luaL_fileresult(L, status, NULL);
}
}
static int f_write(lua_State* L)
{
fs::File& f = tofile(L);
lua_pushvalue(L, 1); /* push file at the stack top (to be returned) */
return g_write(L, f, 2);
}
// peek at the next char and return it
static int f_peek(lua_State* L)
{
fs::File& f = tofile(L);
errno = 0;
if (!f) {
return luaL_fileresult(L, 0, nullptr);
}
lua_pushnumber(L, f.peek());
return 1;
}
static int f_flush(lua_State* L)
{
fs::File& f = tofile(L);
errno = 0;
f.flush();
return luaL_fileresult(L, 1, nullptr);
}
static int f_position(lua_State* L)
{
fs::File& f = tofile(L);
errno = 0;
if (!f) {
return luaL_fileresult(L, 0, nullptr);
}
lua_pushinteger(L, lua_Integer(f.position()));
return 1;
}
static int f_size(lua_State* L)
{
fs::File& f = tofile(L);
errno = 0;
if (!f) {
return luaL_fileresult(L, 0, nullptr);
}
lua_pushinteger(L, lua_Integer(f.size()));
return 1;
}
static int f_isdir(lua_State* L)
{
fs::File& f = tofile(L);
errno = 0;
lua_pushboolean(L, lua_Integer(f.isDirectory()));
return 1;
}
/*
** functions for 'wfs' library
*/
static const luaL_Reg wfslib[] = {
{"exists", io_exists},
{"open", io_open},
{"remove", io_remove},
{"rmdir", io_rmdir},
{"mkdir", io_mkdir},
{"rename", io_rename},
{"total_bytes", io_totalbytes},
{"used_bytes", io_usedbytes},
{NULL, NULL}
};
/*
** methods for file handles
*/
static const luaL_Reg meth[] = {
{"filename", f_filename},
{"read", f_read},
{"write", f_write},
{"flush", f_flush},
{"peek", f_peek},
{"position", f_position},
{"seek", f_seek},
{"size", f_size},
{"isdir", f_isdir},
{"close", f_close},
{"open_next_file", f_opennextfile},
{NULL, NULL}
};
/*
** metamethods for file handles
*/
static const luaL_Reg metameth[] = {
{"__index", NULL}, /* placeholder */
{"__gc", f_gc},
{"__close", f_gc},
{"__tostring", f_tostring},
{NULL, NULL}
};
static void createmeta (lua_State* L)
{
luaL_newmetatable(L, LUA_WFSHANDLE); /* metatable for file handles */
luaL_setfuncs(L, metameth, 0); /* add metamethods to new metatable */
luaL_newlibtable(L, meth); /* create method table */
luaL_setfuncs(L, meth, 0); /* add file methods to method table */
lua_setfield(L, -2, "__index"); /* metatable.__index = method table */
lua_pop(L, 1); /* pop metatable */
}
extern "C" {
LUAMOD_API int luaopen_wfs(lua_State* L)
{
luaL_newlib(L, wfslib); /* new module */
createmeta(L);
return 1;
}
}