35 #include <sys/types.h> 41 #if defined(__MINGW32__) || defined(_MSC_VER) 51 using v8::ArrayBuffer;
53 using v8::Float64Array;
55 using v8::FunctionCallbackInfo;
56 using v8::FunctionTemplate;
57 using v8::HandleScope;
67 # define MIN(a, b) ((a) < (b) ? (a) : (b)) 70 #define TYPE_ERROR(msg) env->ThrowTypeError(msg) 72 #define GET_OFFSET(a) ((a)->IsNumber() ? (a)->IntegerValue() : -1) 74 class FSReqWrap:
public ReqWrap<uv_fs_t> {
76 enum Ownership { COPY, MOVE };
78 inline static FSReqWrap*
New(Environment* env,
81 const char*
data =
nullptr,
83 Ownership ownership = COPY);
85 inline void Dispose();
88 if (data_ != inline_data()) {
94 const char* syscall()
const {
return syscall_; }
95 const char*
data()
const {
return data_; }
98 size_t self_size()
const override {
return sizeof(*this); }
101 FSReqWrap(Environment* env,
106 : ReqWrap(env, req, AsyncWrap::PROVIDER_FSREQWRAP),
110 Wrap(
object(),
this);
118 void*
operator new(
size_t size) =
delete;
119 void*
operator new(
size_t size,
char* storage) {
return storage; }
120 char* inline_data() {
return reinterpret_cast<char*
>(
this + 1); }
122 const char* syscall_;
125 DISALLOW_COPY_AND_ASSIGN(FSReqWrap);
128 #define ASSERT_PATH(path) \ 129 if (*path == nullptr) \ 130 return TYPE_ERROR( #path " must be a string or Buffer"); 137 Ownership ownership) {
138 const bool copy = (data !=
nullptr && ownership == COPY);
139 const size_t size = copy ? 1 + strlen(data) : 0;
141 char*
const storage =
new char[
sizeof(*that) + size];
142 that =
new(storage) FSReqWrap(env, req, syscall, data, encoding);
144 that->data_ =
static_cast<char*
>(memcpy(that->inline_data(),
data, size));
149 void FSReqWrap::Dispose() {
151 delete[]
reinterpret_cast<char*
>(
this);
155 void NewFSReqWrap(
const FunctionCallbackInfo<Value>& args) {
156 CHECK(args.IsConstructCall());
157 ClearWrap(args.This());
161 inline bool IsInt64(
double x) {
162 return x ==
static_cast<double>(
static_cast<int64_t
>(x));
165 void After(uv_fs_t *
req) {
166 FSReqWrap* req_wrap =
static_cast<FSReqWrap*
>(req->data);
167 CHECK_EQ(req_wrap->req(),
req);
168 req_wrap->ReleaseEarly();
170 Environment* env = req_wrap->env();
171 HandleScope handle_scope(env->isolate());
172 Context::Scope context_scope(env->context());
179 Local<Value> argv[2];
180 MaybeLocal<Value> link;
183 if (req->result < 0) {
193 argv[0] = Null(env->isolate());
198 switch (req->fs_type) {
206 case UV_FS_FTRUNCATE:
208 case UV_FS_FDATASYNC:
225 static_cast<const uv_stat_t*
>(req->ptr));
244 static_cast<const char*
>(req->path),
247 if (link.IsEmpty()) {
252 "Invalid character encoding for filename",
256 argv[1] = link.ToLocalChecked();
263 static_cast<const char*
>(req->ptr),
266 if (link.IsEmpty()) {
271 "Invalid character encoding for link",
275 argv[1] = link.ToLocalChecked();
281 static_cast<const char*
>(req->ptr),
284 if (link.IsEmpty()) {
289 "Invalid character encoding for link",
293 argv[1] = link.ToLocalChecked();
305 Local<Array> names =
Array::New(env->isolate(), 0);
306 Local<Function> fn = env->push_values_to_array_function();
307 Local<Value> name_argv[NODE_PUSH_VAL_TO_ARRAY_MAX];
310 for (
int i = 0; ; i++) {
313 r = uv_fs_scandir_next(req, &ent);
320 static_cast<const char*
>(req->path));
324 MaybeLocal<Value> filename =
329 if (filename.IsEmpty()) {
334 "Invalid character encoding for filename",
339 name_argv[name_idx++] = filename.ToLocalChecked();
341 if (name_idx >= arraysize(name_argv)) {
342 fn->Call(env->context(), names, name_idx, name_argv)
349 fn->Call(env->context(), names, name_idx, name_argv)
358 CHECK(0 &&
"Unhandled eio response");
362 req_wrap->MakeCallback(env->oncomplete_string(), argc, argv);
364 uv_fs_req_cleanup(req_wrap->req());
373 ~fs_req_wrap() { uv_fs_req_cleanup(&req); }
377 DISALLOW_COPY_AND_ASSIGN(fs_req_wrap);
381 #define ASYNC_DEST_CALL(func, request, dest, encoding, ...) \ 382 Environment* env = Environment::GetCurrent(args); \ 383 CHECK(request->IsObject()); \ 384 FSReqWrap* req_wrap = FSReqWrap::New(env, request.As<Object>(), \ 385 #func, dest, encoding); \ 386 int err = uv_fs_ ## func(env->event_loop(), \ 390 req_wrap->Dispatched(); \ 392 uv_fs_t* uv_req = req_wrap->req(); \ 393 uv_req->result = err; \ 394 uv_req->path = nullptr; \ 396 req_wrap = nullptr; \ 398 args.GetReturnValue().Set(req_wrap->persistent()); \ 401 #define ASYNC_CALL(func, req, encoding, ...) \ 402 ASYNC_DEST_CALL(func, req, nullptr, encoding, __VA_ARGS__) \ 404 #define SYNC_DEST_CALL(func, path, dest, ...) \ 405 fs_req_wrap req_wrap; \ 406 env->PrintSyncTrace(); \ 407 int err = uv_fs_ ## func(env->event_loop(), \ 412 return env->ThrowUVException(err, #func, nullptr, path, dest); \ 415 #define SYNC_CALL(func, path, ...) \ 416 SYNC_DEST_CALL(func, path, nullptr, __VA_ARGS__) \ 418 #define SYNC_REQ req_wrap.req 420 #define SYNC_RESULT err 422 void Access(
const FunctionCallbackInfo<Value>& args) {
423 Environment* env = Environment::GetCurrent(args.GetIsolate());
424 HandleScope scope(env->isolate());
426 if (args.Length() < 2)
427 return TYPE_ERROR(
"path and mode are required");
428 if (!args[1]->IsInt32())
431 BufferValue path(env->isolate(), args[0]);
434 int mode =
static_cast<int>(args[1]->Int32Value());
436 if (args[2]->IsObject()) {
444 void Close(
const FunctionCallbackInfo<Value>& args) {
445 Environment* env = Environment::GetCurrent(args);
447 if (args.Length() < 1)
449 if (!args[0]->IsInt32())
450 return TYPE_ERROR(
"fd must be a file descriptor");
452 int fd = args[0]->Int32Value();
454 if (args[1]->IsObject()) {
464 fields[0] = s->st_dev;
465 fields[1] = s->st_mode;
466 fields[2] = s->st_nlink;
467 fields[3] = s->st_uid;
468 fields[4] = s->st_gid;
469 fields[5] = s->st_rdev;
470 #if defined(__POSIX__) 471 fields[6] = s->st_blksize;
475 fields[7] = s->st_ino;
476 fields[8] = s->st_size;
477 #if defined(__POSIX__) 478 fields[9] = s->st_blocks;
484 #define X(idx, name) \ 486 fields[idx] = ((unsigned long)(s->st_##name.tv_sec) * 1e3) + \ 488 ((unsigned long)(s->st_##name.tv_nsec) / 1e6); \ 500 static void InternalModuleReadFile(
const FunctionCallbackInfo<Value>& args) {
501 Environment* env = Environment::GetCurrent(args);
502 uv_loop_t* loop = env->event_loop();
504 CHECK(args[0]->IsString());
505 node::Utf8Value path(env->isolate(), args[0]);
507 if (strlen(*path) != path.length())
511 const int fd = uv_fs_open(loop, &open_req, *path, O_RDONLY, 0,
nullptr);
512 uv_fs_req_cleanup(&open_req);
518 const size_t kBlockSize = 32 << 10;
519 std::vector<char> chars;
523 const size_t start = chars.size();
524 chars.resize(start + kBlockSize);
527 buf.base = &chars[start];
528 buf.len = kBlockSize;
531 numchars = uv_fs_read(loop, &read_req, fd, &buf, 1, offset,
nullptr);
532 uv_fs_req_cleanup(&read_req);
534 CHECK_GE(numchars, 0);
536 }
while (static_cast<size_t>(numchars) == kBlockSize);
539 CHECK_EQ(0, uv_fs_close(loop, &close_req, fd,
nullptr));
540 uv_fs_req_cleanup(&close_req);
543 if (offset >= 3 && 0 == memcmp(&chars[0],
"\xEF\xBB\xBF", 3)) {
547 Local<String> chars_string =
548 String::NewFromUtf8(env->isolate(),
550 String::kNormalString,
552 args.GetReturnValue().Set(chars_string);
558 static void InternalModuleStat(
const FunctionCallbackInfo<Value>& args) {
559 Environment* env = Environment::GetCurrent(args);
561 CHECK(args[0]->IsString());
562 node::Utf8Value path(env->isolate(), args[0]);
565 int rc = uv_fs_stat(env->event_loop(), &
req, *path,
nullptr);
567 const uv_stat_t*
const s =
static_cast<const uv_stat_t*
>(req.ptr);
568 rc = !!(s->st_mode & S_IFDIR);
570 uv_fs_req_cleanup(&req);
572 args.GetReturnValue().Set(rc);
575 static void Stat(
const FunctionCallbackInfo<Value>& args) {
576 Environment* env = Environment::GetCurrent(args);
578 if (args.Length() < 1)
581 BufferValue path(env->isolate(), args[0]);
584 if (args[1]->IsObject()) {
589 static_cast<const uv_stat_t*
>(
SYNC_REQ.ptr));
593 static void LStat(
const FunctionCallbackInfo<Value>& args) {
594 Environment* env = Environment::GetCurrent(args);
596 if (args.Length() < 1)
599 BufferValue path(env->isolate(), args[0]);
602 if (args[1]->IsObject()) {
607 static_cast<const uv_stat_t*
>(
SYNC_REQ.ptr));
611 static void FStat(
const FunctionCallbackInfo<Value>& args) {
612 Environment* env = Environment::GetCurrent(args);
614 if (args.Length() < 1)
616 if (!args[0]->IsInt32())
617 return TYPE_ERROR(
"fd must be a file descriptor");
619 int fd = args[0]->Int32Value();
621 if (args[1]->IsObject()) {
626 static_cast<const uv_stat_t*
>(
SYNC_REQ.ptr));
630 static void Symlink(
const FunctionCallbackInfo<Value>& args) {
631 Environment* env = Environment::GetCurrent(args);
633 int len = args.Length();
639 BufferValue target(env->isolate(), args[0]);
641 BufferValue path(env->isolate(), args[1]);
646 if (args[2]->IsString()) {
647 node::Utf8Value mode(env->isolate(), args[2]);
648 if (strcmp(*mode,
"dir") == 0) {
649 flags |= UV_FS_SYMLINK_DIR;
650 }
else if (strcmp(*mode,
"junction") == 0) {
651 flags |= UV_FS_SYMLINK_JUNCTION;
652 }
else if (strcmp(*mode,
"file") != 0) {
653 return env->ThrowError(
"Unknown symlink type");
657 if (args[3]->IsObject()) {
664 static void Link(
const FunctionCallbackInfo<Value>& args) {
665 Environment* env = Environment::GetCurrent(args);
667 int len = args.Length();
673 BufferValue src(env->isolate(), args[0]);
676 BufferValue dest(env->isolate(), args[1]);
679 if (args[2]->IsObject()) {
686 static void ReadLink(
const FunctionCallbackInfo<Value>& args) {
687 Environment* env = Environment::GetCurrent(args);
689 const int argc = args.Length();
694 BufferValue path(env->isolate(), args[0]);
699 Local<Value> callback = Null(env->isolate());
703 if (callback->IsObject()) {
704 ASYNC_CALL(readlink, callback, encoding, *path)
707 const char* link_path =
static_cast<const char*
>(
SYNC_REQ.ptr);
716 return env->ThrowUVException(UV_EINVAL,
718 "Invalid character encoding for link",
721 args.GetReturnValue().Set(rc.ToLocalChecked());
725 static void Rename(
const FunctionCallbackInfo<Value>& args) {
726 Environment* env = Environment::GetCurrent(args);
728 int len = args.Length();
734 BufferValue old_path(env->isolate(), args[0]);
736 BufferValue new_path(env->isolate(), args[1]);
739 if (args[2]->IsObject()) {
742 SYNC_DEST_CALL(rename, *old_path, *new_path, *old_path, *new_path)
746 static void FTruncate(
const FunctionCallbackInfo<Value>& args) {
747 Environment* env = Environment::GetCurrent(args);
749 if (args.Length() < 2)
750 return TYPE_ERROR(
"fd and length are required");
751 if (!args[0]->IsInt32())
752 return TYPE_ERROR(
"fd must be a file descriptor");
754 int fd = args[0]->Int32Value();
759 Local<Value> len_v(args[1]);
760 if (!len_v->IsUndefined() &&
762 !IsInt64(len_v->NumberValue())) {
763 return env->ThrowTypeError(
"Not an integer");
766 const int64_t
len = len_v->IntegerValue();
768 if (args[2]->IsObject()) {
775 static void Fdatasync(
const FunctionCallbackInfo<Value>& args) {
776 Environment* env = Environment::GetCurrent(args);
778 if (args.Length() < 1)
780 if (!args[0]->IsInt32())
781 return TYPE_ERROR(
"fd must be a file descriptor");
783 int fd = args[0]->Int32Value();
785 if (args[1]->IsObject()) {
792 static void Fsync(
const FunctionCallbackInfo<Value>& args) {
793 Environment* env = Environment::GetCurrent(args);
795 if (args.Length() < 1)
797 if (!args[0]->IsInt32())
798 return TYPE_ERROR(
"fd must be a file descriptor");
800 int fd = args[0]->Int32Value();
802 if (args[1]->IsObject()) {
809 static void Unlink(
const FunctionCallbackInfo<Value>& args) {
810 Environment* env = Environment::GetCurrent(args);
812 if (args.Length() < 1)
815 BufferValue path(env->isolate(), args[0]);
818 if (args[1]->IsObject()) {
825 static void RMDir(
const FunctionCallbackInfo<Value>& args) {
826 Environment* env = Environment::GetCurrent(args);
828 if (args.Length() < 1)
831 BufferValue path(env->isolate(), args[0]);
834 if (args[1]->IsObject()) {
841 static void MKDir(
const FunctionCallbackInfo<Value>& args) {
842 Environment* env = Environment::GetCurrent(args);
844 if (args.Length() < 2)
845 return TYPE_ERROR(
"path and mode are required");
846 if (!args[1]->IsInt32())
849 BufferValue path(env->isolate(), args[0]);
852 int mode =
static_cast<int>(args[1]->Int32Value());
854 if (args[2]->IsObject()) {
861 static void RealPath(
const FunctionCallbackInfo<Value>& args) {
862 Environment* env = Environment::GetCurrent(args);
864 const int argc = args.Length();
869 BufferValue path(env->isolate(), args[0]);
874 Local<Value> callback = Null(env->isolate());
878 if (callback->IsObject()) {
879 ASYNC_CALL(realpath, callback, encoding, *path);
882 const char* link_path =
static_cast<const char*
>(
SYNC_REQ.ptr);
891 return env->ThrowUVException(UV_EINVAL,
893 "Invalid character encoding for path",
896 args.GetReturnValue().Set(rc.ToLocalChecked());
900 static void ReadDir(
const FunctionCallbackInfo<Value>& args) {
901 Environment* env = Environment::GetCurrent(args);
903 const int argc = args.Length();
908 BufferValue path(env->isolate(), args[0]);
913 Local<Value> callback = Null(env->isolate());
917 if (callback->IsObject()) {
918 ASYNC_CALL(scandir, callback, encoding, *path, 0 )
924 Local<Array> names =
Array::New(env->isolate(), 0);
925 Local<Function> fn = env->push_values_to_array_function();
926 Local<Value> name_v[NODE_PUSH_VAL_TO_ARRAY_MAX];
929 for (
int i = 0; ; i++) {
932 r = uv_fs_scandir_next(&
SYNC_REQ, &ent);
936 return env->ThrowUVException(r,
"readdir",
"", *path);
943 if (filename.IsEmpty()) {
945 return env->ThrowUVException(UV_EINVAL,
947 "Invalid character encoding for filename",
951 name_v[name_idx++] = filename.ToLocalChecked();
953 if (name_idx >= arraysize(name_v)) {
954 fn->Call(env->context(), names, name_idx, name_v)
961 fn->Call(env->context(), names, name_idx, name_v).ToLocalChecked();
964 args.GetReturnValue().Set(names);
968 static void Open(
const FunctionCallbackInfo<Value>& args) {
969 Environment* env = Environment::GetCurrent(args);
971 int len = args.Length();
978 if (!args[1]->IsInt32())
980 if (!args[2]->IsInt32())
983 BufferValue path(env->isolate(), args[0]);
986 int flags = args[1]->Int32Value();
987 int mode =
static_cast<int>(args[2]->Int32Value());
989 if (args[3]->IsObject()) {
992 SYNC_CALL(open, *path, *path, flags, mode)
998 static void CopyFile(
const FunctionCallbackInfo<Value>& args) {
999 Environment* env = Environment::GetCurrent(args);
1001 if (!args[0]->IsString())
1003 if (!args[1]->IsString())
1005 if (!args[2]->IsInt32())
1008 BufferValue src(env->isolate(), args[0]);
1010 BufferValue dest(env->isolate(), args[1]);
1012 int flags = args[2]->Int32Value();
1014 if (args[3]->IsObject()) {
1031 static void WriteBuffer(
const FunctionCallbackInfo<Value>& args) {
1032 Environment* env = Environment::GetCurrent(args);
1034 if (!args[0]->IsInt32())
1035 return env->ThrowTypeError(
"First argument must be file descriptor");
1039 int fd = args[0]->Int32Value();
1040 Local<Object> obj = args[1].As<Object>();
1043 size_t off = args[2]->Uint32Value();
1044 size_t len = args[3]->Uint32Value();
1046 Local<Value>
req = args[5];
1048 if (off > buffer_length)
1049 return env->ThrowRangeError(
"offset out of bounds");
1050 if (len > buffer_length)
1051 return env->ThrowRangeError(
"length out of bounds");
1052 if (off + len < off)
1053 return env->ThrowRangeError(
"off + len overflow");
1054 if (!Buffer::IsWithinBounds(off, len, buffer_length))
1055 return env->ThrowRangeError(
"off + len > buffer.length");
1059 uv_buf_t uvbuf = uv_buf_init(const_cast<char*>(buf), len);
1061 if (req->IsObject()) {
1066 SYNC_CALL(write,
nullptr, fd, &uvbuf, 1, pos)
1078 static void WriteBuffers(
const FunctionCallbackInfo<Value>& args) {
1079 Environment* env = Environment::GetCurrent(args);
1081 CHECK(args[0]->IsInt32());
1082 CHECK(args[1]->IsArray());
1084 int fd = args[0]->Int32Value();
1085 Local<Array> chunks = args[1].As<Array>();
1087 Local<Value>
req = args[3];
1089 MaybeStackBuffer<uv_buf_t> iovs(chunks->Length());
1091 for (uint32_t i = 0; i < iovs.length(); i++) {
1092 Local<Value> chunk = chunks->Get(i);
1095 return env->ThrowTypeError(
"Array elements all need to be buffers");
1100 if (req->IsObject()) {
1105 SYNC_CALL(write,
nullptr, fd, *iovs, iovs.length(), pos)
1118 static void WriteString(
const FunctionCallbackInfo<Value>& args) {
1119 Environment* env = Environment::GetCurrent(args);
1121 if (!args[0]->IsInt32())
1122 return env->ThrowTypeError(
"First argument must be file descriptor");
1125 Local<Value>
string = args[1];
1126 int fd = args[0]->Int32Value();
1127 char*
buf =
nullptr;
1130 FSReqWrap::Ownership ownership = FSReqWrap::COPY;
1133 if (!StringBytes::GetExternalParts(
string,
1134 const_cast<const char**>(&buf),
1137 len = StringBytes::StorageSize(env->isolate(), string, enc);
1138 buf =
new char[
len];
1141 len = StringBytes::Write(env->isolate(),
buf,
len, args[1], enc);
1142 ownership = FSReqWrap::MOVE;
1147 uv_buf_t uvbuf = uv_buf_init(const_cast<char*>(buf), len);
1149 if (!req->IsObject()) {
1152 inline explicit Delete(
char* pointer) : pointer_(pointer) {}
1153 inline ~Delete() {
delete[] pointer_; }
1154 char*
const pointer_;
1156 Delete delete_on_return(ownership == FSReqWrap::MOVE ? buf :
nullptr);
1157 SYNC_CALL(write,
nullptr, fd, &uvbuf, 1, pos)
1161 FSReqWrap* req_wrap =
1163 int err = uv_fs_write(env->event_loop(),
1170 req_wrap->Dispatched();
1172 uv_fs_t* uv_req = req_wrap->req();
1173 uv_req->result = err;
1174 uv_req->path =
nullptr;
1179 return args.GetReturnValue().Set(req_wrap->persistent());
1195 static void Read(
const FunctionCallbackInfo<Value>& args) {
1196 Environment* env = Environment::GetCurrent(args);
1198 if (args.Length() < 2)
1199 return TYPE_ERROR(
"fd and buffer are required");
1200 if (!args[0]->IsInt32())
1201 return TYPE_ERROR(
"fd must be a file descriptor");
1203 return TYPE_ERROR(
"Second argument needs to be a buffer");
1205 int fd = args[0]->Int32Value();
1212 char *
buf =
nullptr;
1214 Local<Object> buffer_obj = args[1]->ToObject(env->isolate());
1218 size_t off = args[2]->Int32Value();
1219 if (off >= buffer_length) {
1220 return env->ThrowError(
"Offset is out of bounds");
1223 len = args[3]->Int32Value();
1224 if (!Buffer::IsWithinBounds(off, len, buffer_length))
1225 return env->ThrowRangeError(
"Length extends beyond buffer");
1229 buf = buffer_data +
off;
1231 uv_buf_t uvbuf = uv_buf_init(const_cast<char*>(buf), len);
1235 if (req->IsObject()) {
1247 static void Chmod(
const FunctionCallbackInfo<Value>& args) {
1248 Environment* env = Environment::GetCurrent(args);
1250 if (args.Length() < 2)
1251 return TYPE_ERROR(
"path and mode are required");
1252 if (!args[1]->IsInt32())
1253 return TYPE_ERROR(
"mode must be an integer");
1255 BufferValue path(env->isolate(), args[0]);
1258 int mode =
static_cast<int>(args[1]->Int32Value());
1260 if (args[2]->IsObject()) {
1271 static void FChmod(
const FunctionCallbackInfo<Value>& args) {
1272 Environment* env = Environment::GetCurrent(args);
1274 if (args.Length() < 2)
1275 return TYPE_ERROR(
"fd and mode are required");
1276 if (!args[0]->IsInt32())
1277 return TYPE_ERROR(
"fd must be a file descriptor");
1278 if (!args[1]->IsInt32())
1279 return TYPE_ERROR(
"mode must be an integer");
1281 int fd = args[0]->Int32Value();
1282 int mode =
static_cast<int>(args[1]->Int32Value());
1284 if (args[2]->IsObject()) {
1295 static void Chown(
const FunctionCallbackInfo<Value>& args) {
1296 Environment* env = Environment::GetCurrent(args);
1298 int len = args.Length();
1305 if (!args[1]->IsUint32())
1306 return TYPE_ERROR(
"uid must be an unsigned int");
1307 if (!args[2]->IsUint32())
1308 return TYPE_ERROR(
"gid must be an unsigned int");
1310 BufferValue path(env->isolate(), args[0]);
1313 uv_uid_t uid =
static_cast<uv_uid_t
>(args[1]->Uint32Value());
1314 uv_gid_t gid =
static_cast<uv_gid_t
>(args[2]->Uint32Value());
1316 if (args[3]->IsObject()) {
1319 SYNC_CALL(chown, *path, *path, uid, gid);
1327 static void FChown(
const FunctionCallbackInfo<Value>& args) {
1328 Environment* env = Environment::GetCurrent(args);
1330 int len = args.Length();
1337 if (!args[0]->IsInt32())
1339 if (!args[1]->IsUint32())
1340 return TYPE_ERROR(
"uid must be an unsigned int");
1341 if (!args[2]->IsUint32())
1342 return TYPE_ERROR(
"gid must be an unsigned int");
1344 int fd = args[0]->Int32Value();
1345 uv_uid_t uid =
static_cast<uv_uid_t
>(args[1]->Uint32Value());
1346 uv_gid_t gid =
static_cast<uv_gid_t
>(args[2]->Uint32Value());
1348 if (args[3]->IsObject()) {
1356 static void UTimes(
const FunctionCallbackInfo<Value>& args) {
1357 Environment* env = Environment::GetCurrent(args);
1359 int len = args.Length();
1366 if (!args[1]->IsNumber())
1368 if (!args[2]->IsNumber())
1371 BufferValue path(env->isolate(), args[0]);
1374 const double atime =
static_cast<double>(args[1]->NumberValue());
1375 const double mtime =
static_cast<double>(args[2]->NumberValue());
1377 if (args[3]->IsObject()) {
1380 SYNC_CALL(utime, *path, *path, atime, mtime);
1384 static void FUTimes(
const FunctionCallbackInfo<Value>& args) {
1385 Environment* env = Environment::GetCurrent(args);
1387 int len = args.Length();
1394 if (!args[0]->IsInt32())
1396 if (!args[1]->IsNumber())
1398 if (!args[2]->IsNumber())
1401 const int fd = args[0]->Int32Value();
1402 const double atime =
static_cast<double>(args[1]->NumberValue());
1403 const double mtime =
static_cast<double>(args[2]->NumberValue());
1405 if (args[3]->IsObject()) {
1412 static void Mkdtemp(
const FunctionCallbackInfo<Value>& args) {
1413 Environment* env = Environment::GetCurrent(args);
1415 CHECK_GE(args.Length(), 2);
1417 BufferValue tmpl(env->isolate(), args[0]);
1418 if (*tmpl ==
nullptr)
1419 return TYPE_ERROR(
"template must be a string or Buffer");
1423 if (args[2]->IsObject()) {
1424 ASYNC_CALL(mkdtemp, args[2], encoding, *tmpl);
1427 const char* path =
static_cast<const char*
>(
SYNC_REQ.path);
1430 MaybeLocal<Value> rc =
1434 return env->ThrowUVException(UV_EINVAL,
1436 "Invalid character encoding for filename",
1439 args.GetReturnValue().Set(rc.ToLocalChecked());
1444 Environment* env = Environment::GetCurrent(args);
1445 double* fields = env->fs_stats_field_array();
1446 if (fields ==
nullptr) {
1449 fields =
new double[2 * 14];
1450 env->set_fs_stats_field_array(fields);
1454 sizeof(double) * 2 * 14);
1456 args.GetReturnValue().Set(fields_array);
1460 Local<Value> unused,
1461 Local<Context> context,
1463 Environment* env = Environment::GetCurrent(context);
1465 env->SetMethod(target,
"access", Access);
1466 env->SetMethod(target,
"close", Close);
1467 env->SetMethod(target,
"open", Open);
1468 env->SetMethod(target,
"read", Read);
1469 env->SetMethod(target,
"fdatasync", Fdatasync);
1470 env->SetMethod(target,
"fsync", Fsync);
1471 env->SetMethod(target,
"rename", Rename);
1472 env->SetMethod(target,
"ftruncate", FTruncate);
1473 env->SetMethod(target,
"rmdir", RMDir);
1474 env->SetMethod(target,
"mkdir", MKDir);
1475 env->SetMethod(target,
"readdir", ReadDir);
1476 env->SetMethod(target,
"internalModuleReadFile", InternalModuleReadFile);
1477 env->SetMethod(target,
"internalModuleStat", InternalModuleStat);
1478 env->SetMethod(target,
"stat", Stat);
1479 env->SetMethod(target,
"lstat", LStat);
1480 env->SetMethod(target,
"fstat", FStat);
1481 env->SetMethod(target,
"link", Link);
1482 env->SetMethod(target,
"symlink", Symlink);
1483 env->SetMethod(target,
"readlink", ReadLink);
1484 env->SetMethod(target,
"unlink", Unlink);
1485 env->SetMethod(target,
"writeBuffer", WriteBuffer);
1486 env->SetMethod(target,
"writeBuffers", WriteBuffers);
1487 env->SetMethod(target,
"writeString", WriteString);
1488 env->SetMethod(target,
"realpath", RealPath);
1489 env->SetMethod(target,
"copyFile", CopyFile);
1491 env->SetMethod(target,
"chmod", Chmod);
1492 env->SetMethod(target,
"fchmod", FChmod);
1495 env->SetMethod(target,
"chown", Chown);
1496 env->SetMethod(target,
"fchown", FChown);
1499 env->SetMethod(target,
"utimes", UTimes);
1500 env->SetMethod(target,
"futimes", FUTimes);
1502 env->SetMethod(target,
"mkdtemp", Mkdtemp);
1509 Local<FunctionTemplate> fst =
1511 fst->InstanceTemplate()->SetInternalFieldCount(1);
1512 AsyncWrap::AddWrapMethods(env, fst);
1513 Local<String> wrapString =
1514 FIXED_ONE_BYTE_STRING(env->isolate(),
"FSReqWrap");
1515 fst->SetClassName(wrapString);
1516 target->Set(wrapString, fst->GetFunction());
#define SYNC_CALL(func, path,...)
#define SYNC_DEST_CALL(func, path, dest,...)
#define ASYNC_CALL(func, req, encoding,...)
bool HasInstance(Local< Value > val)
NODE_MODULE_CONTEXT_AWARE_BUILTIN(inspector, node::inspector::Agent::InitInspector)
#define ASSERT_PATH(path)
void GetStatValues(const FunctionCallbackInfo< Value > &args)
union node::cares_wrap::@8::CaresAsyncData::@0 data
X(SetTTL, uv_udp_set_ttl) X(SetBroadcast
size_t Length(Local< Value > val)
void Initialize(Local< Object > target, Local< Value > unused, Local< Context > context, void *priv)
char * Data(Local< Value > val)
void FillStatsArray(double *fields, const uv_stat_t *s)
enum encoding ParseEncoding(const char *encoding, enum encoding default_encoding)
#define ASYNC_DEST_CALL(func, request, dest, encoding,...)
MaybeLocal< Object > New(Isolate *isolate, Local< String > string, enum encoding enc)
void InitFs(Local< Object > target, Local< Value > unused, Local< Context > context, void *priv)
NODE_DEPRECATED("Use ParseEncoding(isolate, ...)", inline enum encoding ParseEncoding(v8::Local< v8::Value > encoding_v, enum encoding default_encoding=LATIN1) { return ParseEncoding(v8::Isolate::GetCurrent(), encoding_v, default_encoding);}) NODE_EXTERN void FatalException(v8 NODE_DEPRECATED("Use FatalException(isolate, ...)", inline void FatalException(const v8::TryCatch &try_catch) { return FatalException(v8::Isolate::GetCurrent(), try_catch);}) NODE_EXTERN v8 NODE_EXTERN v8::Local< v8::Value > Encode(v8::Isolate *isolate, const uint16_t *buf, size_t len)
Local< Value > UVException(Isolate *isolate, int errorno, const char *syscall, const char *msg, const char *path)