51
51
class FSReqWrap: public ReqWrap<uv_fs_t> {
53
void* operator new(size_t size, char* storage) { return storage; }
53
55
FSReqWrap(const char* syscall)
57
const char* syscall() { return syscall_; }
60
inline const char* syscall() const { return syscall_; }
61
inline const char* dest() const { return dest_; }
62
inline unsigned int dest_len() const { return dest_len_; }
63
inline void dest_len(unsigned int dest_len) { dest_len_ = dest_len; }
60
66
const char* syscall_;
67
unsigned int dest_len_;
102
110
argv[0] = UVException(req->errorno,
104
112
req_wrap->syscall());
113
} else if ((req->errorno == UV_EEXIST ||
114
req->errorno == UV_ENOTEMPTY ||
115
req->errorno == UV_EPERM) &&
116
req_wrap->dest_len() > 0) {
117
argv[0] = UVException(req->errorno,
106
122
argv[0] = UVException(req->errorno,
215
#define ASYNC_CALL(func, callback, ...) \
216
FSReqWrap* req_wrap = new FSReqWrap(#func); \
217
int r = uv_fs_##func(uv_default_loop(), &req_wrap->req_, \
218
__VA_ARGS__, After); \
231
#define ASYNC_DEST_CALL(func, callback, dest_path, ...) \
232
FSReqWrap* req_wrap; \
233
char* dest_str = (dest_path); \
234
int dest_len = dest_str == NULL ? 0 : strlen(dest_str); \
235
char* storage = new char[sizeof(*req_wrap) + dest_len]; \
236
req_wrap = new (storage) FSReqWrap(#func); \
237
req_wrap->dest_len(dest_len); \
238
if (dest_str != NULL) { \
239
memcpy(const_cast<char*>(req_wrap->dest()), \
243
int r = uv_fs_##func(uv_default_loop(), \
219
247
req_wrap->object_->Set(oncomplete_sym, callback); \
220
248
req_wrap->Dispatched(); \
228
256
return scope.Close(req_wrap->object_);
230
#define SYNC_CALL(func, path, ...) \
258
#define ASYNC_CALL(func, callback, ...) \
259
ASYNC_DEST_CALL(func, callback, NULL, __VA_ARGS__) \
261
#define SYNC_DEST_CALL(func, path, dest, ...) \
231
262
fs_req_wrap req_wrap; \
232
int result = uv_fs_##func(uv_default_loop(), &req_wrap.req, __VA_ARGS__, NULL); \
263
int result = uv_fs_##func(uv_default_loop(), \
233
267
if (result < 0) { \
234
268
int code = uv_last_error(uv_default_loop()).code; \
235
return ThrowException(UVException(code, #func, "", path)); \
269
if (dest != NULL && \
270
(code == UV_EEXIST || \
271
code == UV_ENOTEMPTY || \
272
code == UV_EPERM)) { \
273
return ThrowException(UVException(code, #func, "", dest)); \
275
return ThrowException(UVException(code, #func, "", path)); \
279
#define SYNC_CALL(func, path, ...) \
280
SYNC_DEST_CALL(func, path, NULL, __VA_ARGS__) \
238
282
#define SYNC_REQ req_wrap.req
433
477
if (args[3]->IsFunction()) {
434
ASYNC_CALL(symlink, args[3], *dest, *path, flags)
478
ASYNC_DEST_CALL(symlink, args[3], *dest, *dest, *path, flags)
436
SYNC_CALL(symlink, *path, *dest, *path, flags)
480
SYNC_DEST_CALL(symlink, *path, *dest, *dest, *path, flags)
437
481
return Undefined();
451
495
String::Utf8Value new_path(args[1]);
453
497
if (args[2]->IsFunction()) {
454
ASYNC_CALL(link, args[2], *orig_path, *new_path)
498
ASYNC_DEST_CALL(link, args[2], *new_path, *orig_path, *new_path)
456
SYNC_CALL(link, *orig_path, *orig_path, *new_path)
500
SYNC_DEST_CALL(link, *orig_path, *new_path, *orig_path, *new_path)
457
501
return Undefined();
482
526
if (len < 2) return TYPE_ERROR("new path required");
483
527
if (!args[0]->IsString()) return TYPE_ERROR("old path must be a string");
484
528
if (!args[1]->IsString()) return TYPE_ERROR("new path must be a string");
486
530
String::Utf8Value old_path(args[0]);
487
531
String::Utf8Value new_path(args[1]);
489
533
if (args[2]->IsFunction()) {
490
ASYNC_CALL(rename, args[2], *old_path, *new_path)
534
ASYNC_DEST_CALL(rename, args[2], *new_path, *old_path, *new_path)
492
SYNC_CALL(rename, *old_path, *old_path, *new_path)
536
SYNC_DEST_CALL(rename, *old_path, *new_path, *old_path, *new_path)
493
537
return Undefined();