/
fs.c
307 lines (266 loc) · 8.27 KB
/
fs.c
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
#include <stdbool.h>
#include <dirent.h>
#include <stdlib.h>
#include <string.h>
#include <switch.h>
#include <errno.h>
#include <sys/stat.h>
#include <quickjs/quickjs.h>
#include "fs.h"
#include "async.h"
typedef struct
{
int err;
const char *filename;
uint8_t *result;
size_t size;
} nx_fs_read_file_async_t;
typedef struct
{
int err;
const char *filename;
struct stat st;
} nx_fs_stat_async_t;
typedef struct
{
int err;
const char *filename;
} nx_fs_remove_async_t;
JSValue nx_readdir_sync(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
{
DIR *dir;
struct dirent *entry;
const char *path = JS_ToCString(ctx, argv[0]);
if (!path)
{
return JS_Throw(ctx, JS_NewError(ctx));
}
dir = opendir(path);
JS_FreeCString(ctx, path);
if (dir == NULL)
{
JSValue error = JS_NewError(ctx);
JS_DefinePropertyValueStr(ctx, error, "message", JS_NewString(ctx, strerror(errno)), JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
return JS_Throw(ctx, error);
}
int i = 0;
JSValue arr = JS_NewArray(ctx);
if (JS_IsException(arr))
{
closedir(dir);
return arr;
}
while ((entry = readdir(dir)) != NULL)
{
// Filter out `.` and `..`
if (!strcmp(".", entry->d_name) || !strcmp("..", entry->d_name))
{
continue;
}
JSValue str = JS_NewString(ctx, entry->d_name);
if (JS_IsException(str) || JS_SetPropertyUint32(ctx, arr, i, str) < 0)
{
JS_FreeValue(ctx, str);
JS_FreeValue(ctx, arr);
closedir(dir);
return JS_EXCEPTION;
}
i++;
}
if (errno) // Check if readdir ended with an error
{
JS_FreeValue(ctx, arr);
closedir(dir);
JSValue error = JS_NewError(ctx);
JS_DefinePropertyValueStr(ctx, error, "message", JS_NewString(ctx, strerror(errno)), JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
return JS_Throw(ctx, error);
}
closedir(dir);
return arr;
}
void free_array_buffer(JSRuntime *rt, void *opaque, void *ptr)
{
free(ptr);
}
void free_js_array_buffer(JSRuntime *rt, void *opaque, void *ptr)
{
js_free_rt(rt, ptr);
}
void nx_read_file_do(nx_work_t *req)
{
nx_fs_read_file_async_t *data = (nx_fs_read_file_async_t *)req->data;
FILE *file = fopen(data->filename, "rb");
if (file == NULL)
{
data->err = errno;
return;
}
fseek(file, 0, SEEK_END);
data->size = ftell(file);
rewind(file);
data->result = malloc(data->size);
if (data->result == NULL)
{
data->err = errno;
fclose(file);
return;
}
size_t result = fread(data->result, 1, data->size, file);
fclose(file);
if (result != data->size)
{
free(data->result);
data->result = NULL;
data->err = -1;
}
}
void nx_read_file_cb(JSContext *ctx, nx_work_t *req, JSValue *args)
{
nx_fs_read_file_async_t *data = (nx_fs_read_file_async_t *)req->data;
JS_FreeCString(ctx, data->filename);
if (data->err)
{
args[0] = JS_NewError(ctx);
JS_DefinePropertyValueStr(ctx, args[0], "message", JS_NewString(ctx, strerror(data->err)), JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
return;
}
args[1] = JS_NewArrayBuffer(ctx, data->result, data->size, free_array_buffer, NULL, false);
}
JSValue nx_read_file(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
{
NX_INIT_WORK_T(nx_fs_read_file_async_t);
data->filename = JS_ToCString(ctx, argv[1]);
nx_queue_async(ctx, req, nx_read_file_do, nx_read_file_cb, argv[0]);
return JS_UNDEFINED;
}
JSValue nx_read_file_sync(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
{
const char *filename = JS_ToCString(ctx, argv[0]);
FILE *file = fopen(filename, "rb");
if (file == NULL)
{
JS_ThrowTypeError(ctx, "%s: %s", strerror(errno), filename);
JS_FreeCString(ctx, filename);
return JS_EXCEPTION;
}
JS_FreeCString(ctx, filename);
fseek(file, 0, SEEK_END);
size_t size = ftell(file);
rewind(file);
uint8_t *buffer = js_malloc(ctx, size);
if (buffer == NULL)
{
fclose(file);
JS_ThrowOutOfMemory(ctx);
return JS_EXCEPTION;
}
size_t result = fread(buffer, 1, size, file);
fclose(file);
if (result != size)
{
js_free(ctx, buffer);
JS_ThrowTypeError(ctx, "Failed to read entire file. Got %lu, expected %lu", result, size);
return JS_EXCEPTION;
}
return JS_NewArrayBuffer(ctx, buffer, size, free_js_array_buffer, NULL, false);
}
JSValue nx_write_file_sync(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
{
const char *filename = JS_ToCString(ctx, argv[0]);
FILE *file = fopen(filename, "w");
if (file == NULL)
{
JS_ThrowTypeError(ctx, "%s: %s", strerror(errno), filename);
JS_FreeCString(ctx, filename);
return JS_EXCEPTION;
}
JS_FreeCString(ctx, filename);
size_t size;
uint8_t *buffer = JS_GetArrayBuffer(ctx, &size, argv[1]);
if (buffer == NULL)
{
fclose(file);
JS_ThrowOutOfMemory(ctx);
return JS_EXCEPTION;
}
size_t result = fwrite(buffer, 1, size, file);
fclose(file);
if (result != size)
{
JS_ThrowTypeError(ctx, "Failed to write entire file. Got %lu, expected %lu", result, result);
return JS_EXCEPTION;
}
return JS_UNDEFINED;
}
void nx_stat_do(nx_work_t *req)
{
nx_fs_stat_async_t *data = (nx_fs_stat_async_t *)req->data;
if (stat(data->filename, &data->st) != 0)
{
data->err = errno;
}
}
void nx_stat_cb(JSContext *ctx, nx_work_t *req, JSValue *args)
{
nx_fs_stat_async_t *data = (nx_fs_stat_async_t *)req->data;
JS_FreeCString(ctx, data->filename);
if (data->err)
{
args[0] = JS_NewError(ctx);
JS_DefinePropertyValueStr(ctx, args[0], "message", JS_NewString(ctx, strerror(data->err)), JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
return;
}
JSValue stat = JS_NewObject(ctx);
JS_SetPropertyStr(ctx, stat, "size", JS_NewInt32(ctx, data->st.st_size));
JS_SetPropertyStr(ctx, stat, "mtime", JS_NewInt32(ctx, data->st.st_mtim.tv_sec));
JS_SetPropertyStr(ctx, stat, "atime", JS_NewInt32(ctx, data->st.st_atim.tv_nsec));
JS_SetPropertyStr(ctx, stat, "ctime", JS_NewInt32(ctx, data->st.st_ctim.tv_nsec));
JS_SetPropertyStr(ctx, stat, "mode", JS_NewInt32(ctx, data->st.st_mode));
JS_SetPropertyStr(ctx, stat, "uid", JS_NewInt32(ctx, data->st.st_uid));
JS_SetPropertyStr(ctx, stat, "gid", JS_NewInt32(ctx, data->st.st_gid));
args[1] = stat;
}
JSValue nx_stat(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
{
NX_INIT_WORK_T(nx_fs_stat_async_t);
data->filename = JS_ToCString(ctx, argv[1]);
nx_queue_async(ctx, req, nx_stat_do, nx_stat_cb, argv[0]);
return JS_UNDEFINED;
}
void nx_remove_do(nx_work_t *req)
{
nx_fs_remove_async_t *data = (nx_fs_remove_async_t *)req->data;
if (remove(data->filename) != 0)
{
data->err = errno;
}
}
void nx_remove_cb(JSContext *ctx, nx_work_t *req, JSValue *args)
{
nx_fs_remove_async_t *data = (nx_fs_remove_async_t *)req->data;
JS_FreeCString(ctx, data->filename);
if (data->err)
{
args[0] = JS_NewError(ctx);
JS_DefinePropertyValueStr(ctx, args[0], "message", JS_NewString(ctx, strerror(data->err)), JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
}
}
JSValue nx_remove(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv)
{
NX_INIT_WORK_T(nx_fs_remove_async_t);
data->filename = JS_ToCString(ctx, argv[1]);
nx_queue_async(ctx, req, nx_remove_do, nx_remove_cb, argv[0]);
return JS_UNDEFINED;
}
static const JSCFunctionListEntry function_list[] = {
// JS_CFUNC_DEF("readDir", 0, nx_readdir),
JS_CFUNC_DEF("readFile", 2, nx_read_file),
JS_CFUNC_DEF("readDirSync", 1, nx_readdir_sync),
JS_CFUNC_DEF("readFileSync", 1, nx_read_file_sync),
JS_CFUNC_DEF("writeFileSync", 1, nx_write_file_sync),
JS_CFUNC_DEF("stat", 2, nx_stat),
JS_CFUNC_DEF("remove", 2, nx_remove)};
void nx_init_fs(JSContext *ctx, JSValueConst native_obj)
{
JS_SetPropertyFunctionList(ctx, native_obj, function_list, countof(function_list));
}