-
Notifications
You must be signed in to change notification settings - Fork 3
/
lua_uv_fs.cpp
339 lines (292 loc) · 9.05 KB
/
lua_uv_fs.cpp
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
#include "luaopen_uv.h"
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
int lua_uv_string_to_flags(lua_State* L, const char* string) {
if (strcmp(string, "r") == 0) return O_RDONLY;
if (strcmp(string, "r+") == 0) return O_RDWR;
if (strcmp(string, "w") == 0) return O_CREAT | O_TRUNC | O_WRONLY;
if (strcmp(string, "w+") == 0) return O_CREAT | O_TRUNC | O_RDWR;
if (strcmp(string, "a") == 0) return O_APPEND | O_CREAT | O_WRONLY;
if (strcmp(string, "a+") == 0) return O_APPEND | O_CREAT | O_RDWR;
return luaL_error(L, "Unknown file open flag'%s'", string);
}
uv_file lua_uv_check_file(lua_State * L, int idx=-1) {
return *(uv_file *)luaL_checkudata(L, idx, classname<uv_file>());
}
int lua_uv_fs___tostring(lua_State * L) {
uv_file f = lua_uv_check_file(L, 1);
lua_pushfstring(L, "uv.file(%d)", f);
return 1;
}
void lua_uv_fs_close_resume(uv_fs_t* req) {
lua_State * L = (lua_State *)req->data;
uv_fs_req_cleanup(req);
callback_resume_after(L);
Lua(L).resume(0);
}
int lua_uv_fs_close(lua_State * L) {
uv_loop_t * loop = lua_uv_loop(L);
uv_file file = lua_uv_check_file(L, 1);
// unset metatable on f:
lua_pushnil(L);
lua_setmetatable(L, 1);
// are we in a coroutine?
if (lua_pushthread(L)) {
// not a coroutine; make a blocking call:
uv_fs_t req;
uv_fs_close(loop, &req, file, NULL);
lua_uv_ok(L);
return 0;
} else {
uv_fs_t * req = callback_resume_before<uv_fs_t>(L, 1);
uv_fs_close(loop, req, file, lua_uv_fs_close_resume);
return lua_yield(L, 2);
}
}
int lua_uv_fs___gc(lua_State * L) {
return lua_uv_fs_close(L);
}
int lua_uv_fs_open_result(lua_State * L, uv_fs_t* req) {
ssize_t result = req->result;
if (req->result < 0) {
lua_pushnil(L);
lua_pushstring(L, "fs_open error");
lua_pushinteger(L, req->result);
uv_fs_req_cleanup(req);
return 3;
}
// create new userdata containing file:
*(uv_file *)lua_newuserdata(L, sizeof(uv_file *)) = (uv_file)result;
luaL_getmetatable(L, classname<uv_file>());
lua_setmetatable(L, -2);
uv_fs_req_cleanup(req);
return 1;
}
void lua_uv_fs_open_resume(uv_fs_t* req) {
lua_State * L = (lua_State *)req->data;
callback_resume_after(L);
Lua(L).resume(lua_uv_fs_open_result(L, req));
}
int lua_uv_fs_open(lua_State * L) {
uv_loop_t * loop = lua_uv_loop(L);
const char * path = luaL_checkstring(L, 1);
int flags = lua_uv_string_to_flags(L, luaL_optstring(L, 2, "r"));
int mode = strtoul(luaL_optstring(L, 3, "0666"), NULL, 8);
// are we in a coroutine?
if (lua_pushthread(L)) {
// not a coroutine; make a blocking call:
uv_fs_t req;
uv_fs_open(loop, &req, path, flags, mode, NULL);
return lua_uv_fs_open_result(L, &req);
} else {
uv_fs_t * req = callback_resume_before<uv_fs_t>(L, 0);
uv_fs_open(loop, req, path, flags, mode, lua_uv_fs_open_resume);
return lua_yield(L, 1);
}
}
void lua_uv_fs_read_resume(uv_fs_t* req) {
lua_State * L = (lua_State *)req->data;
uv_buf_t * buf = (uv_buf_t *)lua_touserdata(L, 2);
callback_resume_after(L);
lua_pushstring(L, buf->base);
uv_fs_req_cleanup(req);
Lua(L).resume(1);
}
int lua_uv_fs_read(lua_State * L) {
uv_loop_t * loop = lua_uv_loop(L);
uv_file file = lua_uv_check_file(L, 1);
size_t size = (size_t)luaL_optinteger(L, 2, 1024);
size_t offset = (size_t)luaL_optinteger(L, 3, -1);
// create a buffer & store on the stack:
uv_buf_t * buf = lua_uv_buf_init(L, size);
// are we on the main thread?
if (Lua(L).mainthread()) {
// on main thread; do it immediately:
uv_fs_t req;
uv_fs_read(loop, &req, file, buf->base, size, offset, NULL);
lua_uv_ok(L);
lua_pushstring(L, buf->base);
return 1;
} else {
uv_fs_t * req = callback_resume_before<uv_fs_t>(L, 1);
uv_fs_read(loop, req, file, buf->base, size, offset, lua_uv_fs_read_resume);
return lua_yield(L, 2);
}
}
int lua_uv_fs_write_result(lua_State * L, uv_fs_t * req) {
ssize_t result = req->result;
if (req->result < 0) {
lua_pushboolean(L, 0);
lua_pushstring(L, "fs_write error");
lua_pushinteger(L, req->result);
uv_fs_req_cleanup(req);
return 3;
}
lua_pushboolean(L, 1);
lua_pushinteger(L, result);
uv_fs_req_cleanup(req);
return 2;
}
void lua_uv_fs_write_resume(uv_fs_t* req) {
lua_State * L = (lua_State *)req->data;
callback_resume_after(L);
Lua(L).resume(lua_uv_fs_write_result(L, req));
}
int lua_uv_fs_write(lua_State * L) {
uv_loop_t * loop = lua_uv_loop(L);
uv_file file = lua_uv_check_file(L, 1);
const char * buf = luaL_checkstring(L, 2);
size_t size = strlen(buf);
size_t offset = (size_t)luaL_optinteger(L, 3, -1);
// are we in a coroutine?
if (lua_pushthread(L)) {
// on main thread; do it immediately:
uv_fs_t req;
uv_fs_write(loop, &req, file, (void *)buf, size, offset, NULL);
lua_uv_ok(L);
return lua_uv_fs_write_result(L, &req);
} else {
uv_fs_t * req = callback_resume_before<uv_fs_t>(L, 1);
uv_fs_write(loop, req, file, (void *)buf, size, offset, lua_uv_fs_write_resume);
return lua_yield(L, 2);
}
}
int lua_uv_fs_readdir_result(lua_State * lua, uv_fs_t* req) {
Lua L((lua_State *)lua);
int results = req->result;
char * buf = (char *)req->ptr;
while (results--) {
size_t len = strlen(buf);
lua_pushlstring(L, buf, len);
buf += len + 1;
}
uv_fs_req_cleanup(req);
return results;
}
void lua_uv_fs_readdir_resume(uv_fs_t* req) {
lua_State * L = (lua_State *)req->data;
callback_resume_after(L);
Lua(L).resume(lua_uv_fs_readdir_result(L, req));
}
int lua_uv_fs_readdir(lua_State * L) {
uv_loop_t * loop = lua_uv_loop(L);
const char * path = luaL_checkstring(L, 1);
int flags = luaL_optinteger(L, 2, 0);
// are we in a coroutine?
if (lua_pushthread(L)) {
// not a coroutine; make a blocking call:
uv_fs_t req;
uv_fs_readdir(loop, &req, path, flags, NULL);
return lua_uv_fs_readdir_result(L, &req);
} else {
uv_fs_t * req = callback_resume_before<uv_fs_t>(L, 1);
uv_fs_readdir(loop, req, path, flags, lua_uv_fs_readdir_resume);
return lua_yield(L, 2);
}
}
int lua_uv_fs_stat_result(lua_State * lua, uv_fs_t* req) {
Lua L((lua_State *)lua);
struct stat* s;
if (req->result < 0) {
lua_pushnil(L);
lua_pushstring(L, "fs_stat error");
lua_pushinteger(L, req->result);
uv_fs_req_cleanup(req);
return 3;
}
s = (struct stat *)req->ptr;
lua_newtable(L);
#if _WIN32
L.push((double)s->st_atime); L.setfield(-2, "accessed");
L.push((double)s->st_mtime); L.setfield(-2, "modified");
#elif !defined(_POSIX_C_SOURCE) || defined(_DARWIN_C_SOURCE)
L.push((double)s->st_atimespec.tv_sec); L.setfield(-2, "accessed");
L.push((double)s->st_mtimespec.tv_sec); L.setfield(-2, "modified");
#else
L.push((double)s->st_atim.tv_sec); L.setfield(-2, "accessed");
L.push((double)s->st_mtim.tv_sec); L.setfield(-2, "modified");
#endif
L.push(s->st_size); L.setfield(-2, "size");
uv_fs_req_cleanup(req);
return 1;
}
void lua_uv_fs_stat_resume(uv_fs_t* req) {
lua_State * L = (lua_State *)req->data;
callback_resume_after(L);
Lua(L).resume(lua_uv_fs_stat_result(L, req));
}
/*
returns table:
{ accessed = <seconds>, modified = <seconds>, size = <bytes> }
or error code: <integer>
*/
int lua_uv_fs_stat(lua_State * L) {
uv_loop_t * loop = lua_uv_loop(L);
const char * path = luaL_checkstring(L, 1);
// are we in a coroutine?
if (lua_pushthread(L)) {
// not a coroutine; make a blocking call:
uv_fs_t req;
uv_fs_stat(loop, &req, path, NULL);
return lua_uv_fs_stat_result(L, &req);
} else {
uv_fs_t * req = callback_resume_before<uv_fs_t>(L, 1);
uv_fs_stat(loop, req, path, lua_uv_fs_stat_resume);
return lua_yield(L, 2);
}
}
int lua_uv_fs_event_do(lua_State * L, uv_fs_event_t* handle, const char* filename, int events, int status) {
return 0;
}
void lua_uv_fs_event_cb(uv_fs_event_t* req, const char* filename, int events, int status) {
Lua L((lua_State *)req->data);
if (status != 0) {
luaL_error(L, "fs_event status error: %d (%s)", status, filename);
}
if (filename) {
L.push(filename);
} else {
L.push();
}
switch (events) {
case UV_RENAME: L.push("rename"); break;
case UV_CHANGE: L.push("change"); break;
default: L.push(); break;
}
L.resume(2);
}
int lua_uv_fs_event_init(lua_State * L) {
uv_loop_t * loop = lua_uv_loop(L);
const char * path = luaL_checkstring(L, 1);
int flags = luaL_optinteger(L, 2, 0);
// are we on the main thread?
if (Lua(L).mainthread()) {
return luaL_error(L, "must be called within a coroutine");
} else {
// in a coroutine, do it asynchronously
uv_fs_event_t * req = new uv_fs_event_t;
req->data = L;
int res = uv_fs_event_init(loop, req, path, lua_uv_fs_event_cb, flags);
if (res < 0) luaL_error(L, "fs_event_init (%d)", res);
return lua_yield(L, 0);
}
}
// bind loop as an upvalue of all the methods:
#define LUA_UV_CLOSURE(PREFIX, NAME) \
lua_pushvalue(L, loopidx); \
lua_pushcclosure(L, lua_##PREFIX##_##NAME, 1); \
lua_setfield(L, -2, #NAME);
void init_fs_metatable(lua_State * L, int loopidx) {
// define uv_file metatable
luaL_newmetatable(L, classname<uv_file>());
lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index");
LUA_UV_CLOSURE(uv_fs, __tostring);
LUA_UV_CLOSURE(uv_fs, __gc);
LUA_UV_CLOSURE(uv_fs, close);
LUA_UV_CLOSURE(uv_fs, read);
LUA_UV_CLOSURE(uv_fs, write);
lua_pop(L, 1);
}