forked from baudehlo/node-fs-ext
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs-ext.cc
375 lines (301 loc) · 9.15 KB
/
fs-ext.cc
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
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
#include <node.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/file.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <utime.h>
#ifdef _WIN32
#include <windows.h>
#endif
using namespace v8;
using namespace node;
#define THROW_BAD_ARGS \
ThrowException(Exception::TypeError(String::New("Bad argument")))
struct store_data_t {
Persistent<Function> *cb;
int fs_op; // operation type within this module
int fd;
int oper;
off_t offset;
struct utimbuf utime_buf;
char *path;
};
enum
{
FS_OP_FLOCK,
FS_OP_SEEK,
FS_OP_UTIME
};
static int After(eio_req *req) {
HandleScope scope;
store_data_t *store_data = static_cast<store_data_t *>(req->data);
ev_unref(EV_DEFAULT_UC);
// there is always at least one argument. "error"
int argc = 1;
// Allocate space for two args: error plus possible additional result
Local<Value> argv[2];
// NOTE: This may need to be changed if something returns a -1
// for a success, which is possible.
if (req->result == -1) {
// If the request doesn't have a path parameter set.
argv[0] = ErrnoException(req->errorno);
} else {
// error value is empty or null for non-error.
argv[0] = Local<Value>::New(Null());
switch (store_data->fs_op) {
// These operations have no data to pass other than "error".
case FS_OP_FLOCK:
case FS_OP_UTIME:
argc = 1;
break;
case FS_OP_SEEK:
argc = 2;
argv[1] = Number::New(store_data->offset);
break;
default:
assert(0 && "Unhandled op type value");
}
}
TryCatch try_catch;
(*(store_data->cb))->Call(v8::Context::GetCurrent()->Global(), argc, argv);
if (try_catch.HasCaught()) {
FatalException(try_catch);
}
// Dispose of the persistent handle
cb_destroy(store_data->cb);
delete store_data;
return 0;
}
static int EIO_Seek(eio_req *req) {
store_data_t* seek_data = static_cast<store_data_t *>(req->data);
off_t offs = lseek(seek_data->fd, seek_data->offset, seek_data->oper);
if (offs == -1) {
req->result = -1;
req->errorno = errno;
} else {
seek_data->offset = offs;
}
return 0;
}
static int EIO_Flock(eio_req *req) {
store_data_t* flock_data = static_cast<store_data_t *>(req->data);
#ifdef _WIN32
int i = _win32_flock(flock_data->fd, flock_data->oper);
#else
int i = flock(flock_data->fd, flock_data->oper);
#endif
req->result = i;
req->errorno = errno;
return 0;
}
#ifdef _WIN32
#define LK_LEN 0xffff0000
static int _win32_flock(int fd, int oper) {
OVERLAPPED o;
HANDLE fh;
int i = -1;
fh = (HANDLE)_get_osfhandle(fd);
if (fh == (HANDLE)-1)
return ThrowException(ErrnoException(errno));
memset(&o, 0, sizeof(o));
switch(oper) {
case LOCK_SH: /* shared lock */
if (LockFileEx(fh, 0, 0, LK_LEN, 0, &o))
i = 0;
break;
case LOCK_EX: /* exclusive lock */
if (LockFileEx(fh, LOCKFILE_EXCLUSIVE_LOCK, 0, LK_LEN, 0, &o))
i = 0;
break;
case LOCK_SH|LOCK_NB: /* non-blocking shared lock */
if (LockFileEx(fh, LOCKFILE_FAIL_IMMEDIATELY, 0, LK_LEN, 0, &o))
i = 0;
break;
case LOCK_EX|LOCK_NB: /* non-blocking exclusive lock */
if (LockFileEx(fh, LOCKFILE_EXCLUSIVE_LOCK|LOCKFILE_FAIL_IMMEDIATELY,
0, LK_LEN, 0, &o))
i = 0;
break;
case LOCK_UN: /* unlock lock */
if (UnlockFileEx(fh, 0, LK_LEN, 0, &o))
i = 0;
break;
default: /* unknown */
errno = EINVAL;
return -1;
}
if (i == -1) {
if (GetLastError() == ERROR_LOCK_VIOLATION)
errno = WSAEWOULDBLOCK;
else
errno = EINVAL;
}
return i;
}
#endif
static Handle<Value> Flock(const Arguments& args) {
HandleScope scope;
if (args.Length() < 2 || !args[0]->IsInt32() || !args[1]->IsInt32()) {
return THROW_BAD_ARGS;
}
store_data_t* flock_data = new store_data_t();
flock_data->fs_op = FS_OP_FLOCK;
flock_data->fd = args[0]->Int32Value();
flock_data->oper = args[1]->Int32Value();
if (args[2]->IsFunction()) {
flock_data->cb = cb_persist(args[2]);
eio_custom(EIO_Flock, EIO_PRI_DEFAULT, After, flock_data);
ev_ref(EV_DEFAULT_UC);
return Undefined();
} else {
#ifdef _WIN32
int i = _win32_flock(flock_data->fd, flock_data->oper);
#else
int i = flock(flock_data->fd, flock_data->oper);
#endif
delete flock_data;
if (i != 0) return ThrowException(ErrnoException(errno));
return Undefined();
}
}
#ifdef _LARGEFILE_SOURCE
static inline int IsInt64(double x) {
return x == static_cast<double>(static_cast<int64_t>(x));
}
#endif
#ifndef _LARGEFILE_SOURCE
#define ASSERT_OFFSET(a) \
if (!(a)->IsUndefined() && !(a)->IsNull() && !(a)->IsInt32()) { \
return ThrowException(Exception::TypeError(String::New("Not an integer"))); \
}
#define GET_OFFSET(a) ((a)->IsNumber() ? (a)->Int32Value() : -1)
#else
#define ASSERT_OFFSET(a) \
if (!(a)->IsUndefined() && !(a)->IsNull() && !IsInt64((a)->NumberValue())) { \
return ThrowException(Exception::TypeError(String::New("Not an integer"))); \
}
#define GET_OFFSET(a) ((a)->IsNumber() ? (a)->IntegerValue() : -1)
#endif
// fs.seek(fd, position, whence [, callback] )
static Handle<Value> Seek(const Arguments& args) {
HandleScope scope;
if (args.Length() < 3 ||
!args[0]->IsInt32() ||
!args[2]->IsInt32()) {
return THROW_BAD_ARGS;
}
int fd = args[0]->Int32Value();
ASSERT_OFFSET(args[1]);
off_t offs = GET_OFFSET(args[1]);
int whence = args[2]->Int32Value();
if ( ! args[3]->IsFunction()) {
off_t offs_result = lseek(fd, offs, whence);
if (offs_result == -1) return ThrowException(ErrnoException(errno));
return scope.Close(Number::New(offs_result));
}
store_data_t* seek_data = new store_data_t();
seek_data->cb = cb_persist(args[3]);
seek_data->fs_op = FS_OP_SEEK;
seek_data->fd = fd;
seek_data->offset = offs;
seek_data->oper = whence;
eio_custom(EIO_Seek, EIO_PRI_DEFAULT, After, seek_data);
ev_ref(EV_DEFAULT_UC);
return Undefined();
}
static int EIO_UTime(eio_req *req) {
store_data_t* utime_data = static_cast<store_data_t *>(req->data);
off_t i = utime(utime_data->path, &utime_data->utime_buf);
free( utime_data->path );
if (i == (off_t)-1) {
req->result = -1;
req->errorno = errno;
} else {
req->result = i;
}
return 0;
}
// Wrapper for utime(2).
// fs.utime( path, atime, mtime, [callback] )
static Handle<Value> UTime(const Arguments& args) {
HandleScope scope;
if (args.Length() < 3 ||
args.Length() > 4 ||
!args[0]->IsString() ||
!args[1]->IsNumber() ||
!args[2]->IsNumber() ) {
return THROW_BAD_ARGS;
}
String::Utf8Value path(args[0]->ToString());
time_t atime = args[1]->IntegerValue();
time_t mtime = args[2]->IntegerValue();
// Synchronous call needs much less work
if ( ! args[3]->IsFunction()) {
struct utimbuf buf;
buf.actime = atime;
buf.modtime = mtime;
int ret = utime(*path, &buf);
if (ret != 0) return ThrowException(ErrnoException(errno, "utime", "", *path));
return Undefined();
}
store_data_t* utime_data = new store_data_t();
utime_data->cb = cb_persist(args[3]);
utime_data->fs_op = FS_OP_UTIME;
utime_data->path = strdup(*path);
utime_data->utime_buf.actime = atime;
utime_data->utime_buf.modtime = mtime;
eio_custom(EIO_UTime, EIO_PRI_DEFAULT, After, utime_data);
ev_ref(EV_DEFAULT_UC);
return Undefined();
}
extern "C" void
init (Handle<Object> target)
{
HandleScope scope;
#ifdef SEEK_SET
NODE_DEFINE_CONSTANT(target, SEEK_SET);
#endif
#ifdef SEEK_CUR
NODE_DEFINE_CONSTANT(target, SEEK_CUR);
#endif
#ifdef SEEK_END
NODE_DEFINE_CONSTANT(target, SEEK_END);
#endif
#ifdef LOCK_SH
NODE_DEFINE_CONSTANT(target, LOCK_SH);
#endif
#ifdef LOCK_EX
NODE_DEFINE_CONSTANT(target, LOCK_EX);
#endif
#ifdef LOCK_NB
NODE_DEFINE_CONSTANT(target, LOCK_NB);
#endif
#ifdef LOCK_UN
NODE_DEFINE_CONSTANT(target, LOCK_UN);
#endif
NODE_SET_METHOD(target, "seek", Seek);
NODE_SET_METHOD(target, "flock", Flock);
NODE_SET_METHOD(target, "utime", UTime);
}