GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — node/src/node_file.cc at master · chrisdew/node Expand file tree/ Copy pathnode_file.ccMore file actions 1335 lines (1083 loc) · 38.9 KB / Copy pathnode_file.ccFile metadata and controls1335 lines (1083 loc) · 38.9 KB 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 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 #include "node.h" #include "node_file.h" #include "node_buffer.h" #include "node_internals.h" #include "node_stat_watcher.h" #include "env.h" #include "env-inl.h" #include "req-wrap.h" #include "req-wrap-inl.h" #include "string_bytes.h" #include "util.h" #include <fcntl.h> #include <sys/types.h> #include <sys/stat.h> #include <string.h> #include <errno.h> #include <limits.h> #if defined(__MINGW32__) || defined(_MSC_VER) # include <io.h> #endif #include <vector> namespace node { using v8::Array; using v8::Context; using v8::EscapableHandleScope; using v8::Function; using v8::FunctionCallbackInfo; using v8::FunctionTemplate; using v8::HandleScope; using v8::Integer; using v8::Local; using v8::Number; using v8::Object; using v8::String; using v8::Value; #ifndef MIN # define MIN(a, b) ((a) < (b) ? (a) : (b)) #endif #define TYPE_ERROR(msg) env->ThrowTypeError(msg) #define GET_OFFSET(a) ((a)->IsNumber() ? (a)->IntegerValue() : -1) class FSReqWrap: public ReqWrap<uv_fs_t> { public: enum Ownership { COPY, MOVE }; inline static FSReqWrap* New(Environment* env, Local<Object> req, const char* syscall, const char* data = nullptr, Ownership ownership = COPY); inline void Dispose(); void ReleaseEarly() { if (data_ != inline_data()) { delete[] data_; data_ = nullptr; } } const char* syscall() const { return syscall_; } const char* data() const { return data_; } size_t self_size() const override { return sizeof(*this); } private: FSReqWrap(Environment* env, Local<Object> req, const char* syscall, const char* data) : ReqWrap(env, req, AsyncWrap::PROVIDER_FSREQWRAP), syscall_(syscall), data_(data) { Wrap(object(), this); } ~FSReqWrap() { ReleaseEarly(); } void* operator new(size_t size) = delete; void* operator new(size_t size, char* storage) { return storage; } char* inline_data() { return reinterpret_cast<char*>(this + 1); } const char* syscall_; const char* data_; DISALLOW_COPY_AND_ASSIGN(FSReqWrap); }; FSReqWrap* FSReqWrap::New(Environment* env, Local<Object> req, const char* syscall, const char* data, Ownership ownership) { const bool copy = (data != nullptr && ownership == COPY); const size_t size = copy ? 1 + strlen(data) : 0; FSReqWrap* that; char* const storage = new char[sizeof(*that) + size]; that = new(storage) FSReqWrap(env, req, syscall, data); if (copy) that->data_ = static_cast<char*>(memcpy(that->inline_data(), data, size)); return that; } void FSReqWrap::Dispose() { this->~FSReqWrap(); delete[] reinterpret_cast<char*>(this); } static void NewFSReqWrap(const FunctionCallbackInfo<Value>& args) { CHECK(args.IsConstructCall()); } static inline bool IsInt64(double x) { return x == static_cast<double>(static_cast<int64_t>(x)); } static void After(uv_fs_t *req) { FSReqWrap* req_wrap = static_cast<FSReqWrap*>(req->data); CHECK_EQ(&req_wrap->req_, req); req_wrap->ReleaseEarly(); // Free memory that's no longer used now. Environment* env = req_wrap->env(); HandleScope handle_scope(env->isolate()); Context::Scope context_scope(env->context()); // there is always at least one argument. "error" int argc = 1; // Allocate space for two args. We may only use one depending on the case. // (Feel free to increase this if you need more) Local<Value> argv[2]; if (req->result < 0) { // An error happened. argv[0] = UVException(env->isolate(), req->result, req_wrap->syscall(), nullptr, req->path, req_wrap->data()); } else { // error value is empty or null for non-error. argv[0] = Null(env->isolate()); // All have at least two args now. argc = 2; switch (req->fs_type) { // These all have no data to pass. case UV_FS_ACCESS: case UV_FS_CLOSE: case UV_FS_RENAME: case UV_FS_UNLINK: case UV_FS_RMDIR: case UV_FS_MKDIR: case UV_FS_FTRUNCATE: case UV_FS_FSYNC: case UV_FS_FDATASYNC: case UV_FS_LINK: case UV_FS_SYMLINK: case UV_FS_CHMOD: case UV_FS_FCHMOD: case UV_FS_CHOWN: case UV_FS_FCHOWN: // These, however, don't. argc = 1; break; case UV_FS_UTIME: case UV_FS_FUTIME: argc = 0; break; case UV_FS_OPEN: argv[1] = Integer::New(env->isolate(), req->result); break; case UV_FS_WRITE: argv[1] = Integer::New(env->isolate(), req->result); break; case UV_FS_STAT: case UV_FS_LSTAT: case UV_FS_FSTAT: argv[1] = BuildStatsObject(env, static_cast<const uv_stat_t*>(req->ptr)); break; case UV_FS_READLINK: argv[1] = String::NewFromUtf8(env->isolate(), static_cast<const char*>(req->ptr)); break; case UV_FS_READ: // Buffer interface argv[1] = Integer::New(env->isolate(), req->result); break; case UV_FS_SCANDIR: { int r; Local<Array> names = Array::New(env->isolate(), 0); for (int i = 0; ; i++) { uv_dirent_t ent; r = uv_fs_scandir_next(req, &ent); if (r == UV_EOF) break; if (r != 0) { argv[0] = UVException(r, nullptr, req_wrap->syscall(), static_cast<const char*>(req->path)); break; } Local<String> name = String::NewFromUtf8(env->isolate(), ent.name); names->Set(i, name); } argv[1] = names; } break; default: CHECK(0 && "Unhandled eio response"); } } req_wrap->MakeCallback(env->oncomplete_string(), argc, argv); uv_fs_req_cleanup(&req_wrap->req_); req_wrap->Dispose(); } // This struct is only used on sync fs calls. // For async calls FSReqWrap is used. struct fs_req_wrap { fs_req_wrap() {} ~fs_req_wrap() { uv_fs_req_cleanup(&req); } uv_fs_t req; DISALLOW_COPY_AND_ASSIGN(fs_req_wrap); }; #define ASYNC_DEST_CALL(func, req, dest, ...) \ Environment* env = Environment::GetCurrent(args); \ CHECK(req->IsObject()); \ FSReqWrap* req_wrap = FSReqWrap::New(env, req.As<Object>(), #func, dest); \ int err = uv_fs_ ## func(env->event_loop(), \ &req_wrap->req_, \ __VA_ARGS__, \ After); \ req_wrap->Dispatched(); \ if (err < 0) { \ uv_fs_t* uv_req = &req_wrap->req_; \ uv_req->result = err; \ uv_req->path = nullptr; \ After(uv_req); \ req_wrap = nullptr; \ } else { \ args.GetReturnValue().Set(req_wrap->persistent()); \ } #define ASYNC_CALL(func, req, ...) \ ASYNC_DEST_CALL(func, req, nullptr, __VA_ARGS__) \ #define SYNC_DEST_CALL(func, path, dest, ...) \ fs_req_wrap req_wrap; \ env->PrintSyncTrace(); \ int err = uv_fs_ ## func(env->event_loop(), \ &req_wrap.req, \ __VA_ARGS__, \ nullptr); \ if (err < 0) { \ return env->ThrowUVException(err, #func, nullptr, path, dest); \ } \ #define SYNC_CALL(func, path, ...) \ SYNC_DEST_CALL(func, path, nullptr, __VA_ARGS__) \ #define SYNC_REQ req_wrap.req #define SYNC_RESULT err static void Access(const FunctionCallbackInfo<Value>& args) { Environment* env = Environment::GetCurrent(args.GetIsolate()); HandleScope scope(env->isolate()); if (args.Length() < 2) return TYPE_ERROR("path and mode are required"); if (!args[0]->IsString()) return TYPE_ERROR("path must be a string"); if (!args[1]->IsInt32()) return TYPE_ERROR("mode must be an integer"); node::Utf8Value path(env->isolate(), args[0]); int mode = static_cast<int>(args[1]->Int32Value()); if (args[2]->IsObject()) { ASYNC_CALL(access, args[2], *path, mode); } else { SYNC_CALL(access, *path, *path, mode); } } static void Close(const FunctionCallbackInfo<Value>& args) { Environment* env = Environment::GetCurrent(args); if (args.Length() < 1) return TYPE_ERROR("fd is required"); if (!args[0]->IsInt32()) return TYPE_ERROR("fd must be a file descriptor"); int fd = args[0]->Int32Value(); if (args[1]->IsObject()) { ASYNC_CALL(close, args[1], fd) } else { SYNC_CALL(close, 0, fd) } } Local<Value> BuildStatsObject(Environment* env, const uv_stat_t* s) { // If you hit this assertion, you forgot to enter the v8::Context first. CHECK_EQ(env->context(), env->isolate()->GetCurrentContext()); EscapableHandleScope handle_scope(env->isolate()); // The code below is very nasty-looking but it prevents a segmentation fault // when people run JS code like the snippet below. It's apparently more // common than you would expect, several people have reported this crash... // // function crash() { // fs.statSync('.'); // crash(); // } // // We need to check the return value of Integer::New() and Date::New() // and make sure that we bail out when V8 returns an empty handle. // Integers. #define X(name) \ Local<Value> name = Integer::New(env->isolate(), s->st_##name); \ if (name.IsEmpty()) \ return handle_scope.Escape(Local<Object>()); \ X(dev) X(mode) X(nlink) X(uid) X(gid) X(rdev) # if defined(__POSIX__) X(blksize) # else Local<Value> blksize = Undefined(env->isolate()); # endif #undef X // Numbers. #define X(name) \ Local<Value> name = Number::New(env->isolate(), \ static_cast<double>(s->st_##name)); \ if (name.IsEmpty()) \ return handle_scope.Escape(Local<Object>()); \ X(ino) X(size) # if defined(__POSIX__) X(blocks) # else Local<Value> blocks = Undefined(env->isolate()); # endif #undef X // Dates. #define X(name) \ Local<Value> name##_msec = \ Number::New(env->isolate(), \ (static_cast<double>(s->st_##name.tv_sec) * 1000) + \ (static_cast<double>(s->st_##name.tv_nsec / 1000000))); \ \ if (name##_msec.IsEmpty()) \ return handle_scope.Escape(Local<Object>()); \ X(atim) X(mtim) X(ctim) X(birthtim) #undef X // Pass stats as the first argument, this is the object we are modifying. Local<Value> argv[] = { dev, mode, nlink, uid, gid, rdev, blksize, ino, size, blocks, atim_msec, mtim_msec, ctim_msec, birthtim_msec }; // Call out to JavaScript to create the stats object. Local<Value> stats = env->fs_stats_constructor_function()->NewInstance(ARRAY_SIZE(argv), argv); if (stats.IsEmpty()) return handle_scope.Escape(Local<Object>()); return handle_scope.Escape(stats); } // Used to speed up module loading. Returns the contents of the file as // a string or undefined when the file cannot be opened. The speedup // comes from not creating Error objects on failure. static void InternalModuleReadFile(const FunctionCallbackInfo<Value>& args) { Environment* env = Environment::GetCurrent(args); uv_loop_t* loop = env->event_loop(); CHECK(args[0]->IsString()); node::Utf8Value path(env->isolate(), args[0]); uv_fs_t open_req; const int fd = uv_fs_open(loop, &open_req, *path, O_RDONLY, 0, nullptr); uv_fs_req_cleanup(&open_req); if (fd < 0) { return; } std::vector<char> chars; int64_t offset = 0; for (;;) { const size_t kBlockSize = 32 << 10; const size_t start = chars.size(); chars.resize(start + kBlockSize); uv_buf_t buf; buf.base = &chars[start]; buf.len = kBlockSize; uv_fs_t read_req; const ssize_t numchars = uv_fs_read(loop, &read_req, fd, &buf, 1, offset, nullptr); uv_fs_req_cleanup(&read_req); CHECK_GE(numchars, 0); if (static_cast<size_t>(numchars) < kBlockSize) { chars.resize(start + numchars); } if (numchars == 0) { break; } offset += numchars; } uv_fs_t close_req; CHECK_EQ(0, uv_fs_close(loop, &close_req, fd, nullptr)); uv_fs_req_cleanup(&close_req); size_t start = 0; if (chars.size() >= 3 && 0 == memcmp(&chars[0], "\xEF\xBB\xBF", 3)) { start = 3; // Skip UTF-8 BOM. } Local<String> chars_string = String::NewFromUtf8(env->isolate(), &chars[start], String::kNormalString, chars.size() - start); args.GetReturnValue().Set(chars_string); } // Used to speed up module loading. Returns 0 if the path refers to // a file, 1 when it's a directory or < 0 on error (usually -ENOENT.) // The speedup comes from not creating thousands of Stat and Error objects. static void InternalModuleStat(const FunctionCallbackInfo<Value>& args) { Environment* env = Environment::GetCurrent(args); CHECK(args[0]->IsString()); node::Utf8Value path(env->isolate(), args[0]); uv_fs_t req; int rc = uv_fs_stat(env->event_loop(), &req, *path, nullptr); if (rc == 0) { const uv_stat_t* const s = static_cast<const uv_stat_t*>(req.ptr); rc = !!(s->st_mode & S_IFDIR); } uv_fs_req_cleanup(&req); args.GetReturnValue().Set(rc); } static void Stat(const FunctionCallbackInfo<Value>& args) { Environment* env = Environment::GetCurrent(args); if (args.Length() < 1) return TYPE_ERROR("path required"); if (!args[0]->IsString()) return TYPE_ERROR("path must be a string"); node::Utf8Value path(env->isolate(), args[0]); if (args[1]->IsObject()) { ASYNC_CALL(stat, args[1], *path) } else { SYNC_CALL(stat, *path, *path) args.GetReturnValue().Set( BuildStatsObject(env, static_cast<const uv_stat_t*>(SYNC_REQ.ptr))); } } static void LStat(const FunctionCallbackInfo<Value>& args) { Environment* env = Environment::GetCurrent(args); if (args.Length() < 1) return TYPE_ERROR("path required"); if (!args[0]->IsString()) return TYPE_ERROR("path must be a string"); node::Utf8Value path(env->isolate(), args[0]); if (args[1]->IsObject()) { ASYNC_CALL(lstat, args[1], *path) } else { SYNC_CALL(lstat, *path, *path) args.GetReturnValue().Set( BuildStatsObject(env, static_cast<const uv_stat_t*>(SYNC_REQ.ptr))); } } static void FStat(const FunctionCallbackInfo<Value>& args) { Environment* env = Environment::GetCurrent(args); if (args.Length() < 1) return TYPE_ERROR("fd is required"); if (!args[0]->IsInt32()) return TYPE_ERROR("fd must be a file descriptor"); int fd = args[0]->Int32Value(); if (args[1]->IsObject()) { ASYNC_CALL(fstat, args[1], fd) } else { SYNC_CALL(fstat, 0, fd) args.GetReturnValue().Set( BuildStatsObject(env, static_cast<const uv_stat_t*>(SYNC_REQ.ptr))); } } static void Symlink(const FunctionCallbackInfo<Value>& args) { Environment* env = Environment::GetCurrent(args); int len = args.Length(); if (len < 1) return TYPE_ERROR("target path required"); if (len < 2) return TYPE_ERROR("src path required"); if (!args[0]->IsString()) return TYPE_ERROR("target path must be a string"); if (!args[1]->IsString()) return TYPE_ERROR("src path must be a string"); node::Utf8Value target(env->isolate(), args[0]); node::Utf8Value path(env->isolate(), args[1]); int flags = 0; if (args[2]->IsString()) { node::Utf8Value mode(env->isolate(), args[2]); if (strcmp(*mode, "dir") == 0) { flags |= UV_FS_SYMLINK_DIR; } else if (strcmp(*mode, "junction") == 0) { flags |= UV_FS_SYMLINK_JUNCTION; } else if (strcmp(*mode, "file") != 0) { return env->ThrowError("Unknown symlink type"); } } if (args[3]->IsObject()) { ASYNC_DEST_CALL(symlink, args[3], *path, *target, *path, flags) } else { SYNC_DEST_CALL(symlink, *target, *path, *target, *path, flags) } } static void Link(const FunctionCallbackInfo<Value>& args) { Environment* env = Environment::GetCurrent(args); int len = args.Length(); if (len < 1) return TYPE_ERROR("src path required"); if (len < 2) return TYPE_ERROR("dest path required"); if (!args[0]->IsString()) return TYPE_ERROR("src path must be a string"); if (!args[1]->IsString()) return TYPE_ERROR("dest path must be a string"); node::Utf8Value orig_path(env->isolate(), args[0]); node::Utf8Value new_path(env->isolate(), args[1]); if (args[2]->IsObject()) { ASYNC_DEST_CALL(link, args[2], *new_path, *orig_path, *new_path) } else { SYNC_DEST_CALL(link, *orig_path, *new_path, *orig_path, *new_path) } } static void ReadLink(const FunctionCallbackInfo<Value>& args) { Environment* env = Environment::GetCurrent(args); if (args.Length() < 1) return TYPE_ERROR("path required"); if (!args[0]->IsString()) return TYPE_ERROR("path must be a string"); node::Utf8Value path(env->isolate(), args[0]); if (args[1]->IsObject()) { ASYNC_CALL(readlink, args[1], *path) } else { SYNC_CALL(readlink, *path, *path) const char* link_path = static_cast<const char*>(SYNC_REQ.ptr); Local<String> rc = String::NewFromUtf8(env->isolate(), link_path); args.GetReturnValue().Set(rc); } } static void Rename(const FunctionCallbackInfo<Value>& args) { Environment* env = Environment::GetCurrent(args); int len = args.Length(); if (len < 1) return TYPE_ERROR("old path required"); if (len < 2) return TYPE_ERROR("new path required"); if (!args[0]->IsString()) return TYPE_ERROR("old path must be a string"); if (!args[1]->IsString()) return TYPE_ERROR("new path must be a string"); node::Utf8Value old_path(env->isolate(), args[0]); node::Utf8Value new_path(env->isolate(), args[1]); if (args[2]->IsObject()) { ASYNC_DEST_CALL(rename, args[2], *new_path, *old_path, *new_path) } else { SYNC_DEST_CALL(rename, *old_path, *new_path, *old_path, *new_path) } } static void FTruncate(const FunctionCallbackInfo<Value>& args) { Environment* env = Environment::GetCurrent(args); if (args.Length() < 2) return TYPE_ERROR("fd and length are required"); if (!args[0]->IsInt32()) return TYPE_ERROR("fd must be a file descriptor"); int fd = args[0]->Int32Value(); // FIXME(bnoordhuis) It's questionable to reject non-ints here but still // allow implicit coercion from null or undefined to zero. Probably best // handled in lib/fs.js. Local<Value> len_v(args[1]); if (!len_v->IsUndefined() && !len_v->IsNull() && !IsInt64(len_v->NumberValue())) { return env->ThrowTypeError("Not an integer"); } const int64_t len = len_v->IntegerValue(); if (args[2]->IsObject()) { ASYNC_CALL(ftruncate, args[2], fd, len) } else { SYNC_CALL(ftruncate, 0, fd, len) } } static void Fdatasync(const FunctionCallbackInfo<Value>& args) { Environment* env = Environment::GetCurrent(args); if (args.Length() < 1) return TYPE_ERROR("fd is required"); if (!args[0]->IsInt32()) return TYPE_ERROR("fd must be a file descriptor"); int fd = args[0]->Int32Value(); if (args[1]->IsObject()) { ASYNC_CALL(fdatasync, args[1], fd) } else { SYNC_CALL(fdatasync, 0, fd) } } static void Fsync(const FunctionCallbackInfo<Value>& args) { Environment* env = Environment::GetCurrent(args); if (args.Length() < 1) return TYPE_ERROR("fd is required"); if (!args[0]->IsInt32()) return TYPE_ERROR("fd must be a file descriptor"); int fd = args[0]->Int32Value(); if (args[1]->IsObject()) { ASYNC_CALL(fsync, args[1], fd) } else { SYNC_CALL(fsync, 0, fd) } } static void Unlink(const FunctionCallbackInfo<Value>& args) { Environment* env = Environment::GetCurrent(args); if (args.Length() < 1) return TYPE_ERROR("path required"); if (!args[0]->IsString()) return TYPE_ERROR("path must be a string"); node::Utf8Value path(env->isolate(), args[0]); if (args[1]->IsObject()) { ASYNC_CALL(unlink, args[1], *path) } else { SYNC_CALL(unlink, *path, *path) } } static void RMDir(const FunctionCallbackInfo<Value>& args) { Environment* env = Environment::GetCurrent(args); if (args.Length() < 1) return TYPE_ERROR("path required"); if (!args[0]->IsString()) return TYPE_ERROR("path must be a string"); node::Utf8Value path(env->isolate(), args[0]); if (args[1]->IsObject()) { ASYNC_CALL(rmdir, args[1], *path) } else { SYNC_CALL(rmdir, *path, *path) } } static void MKDir(const FunctionCallbackInfo<Value>& args) { Environment* env = Environment::GetCurrent(args); if (args.Length() < 2) return TYPE_ERROR("path and mode are required"); if (!args[0]->IsString()) return TYPE_ERROR("path must be a string"); if (!args[1]->IsInt32()) return TYPE_ERROR("mode must be an integer"); node::Utf8Value path(env->isolate(), args[0]); int mode = static_cast<int>(args[1]->Int32Value()); if (args[2]->IsObject()) { ASYNC_CALL(mkdir, args[2], *path, mode) } else { SYNC_CALL(mkdir, *path, *path, mode) } } static void ReadDir(const FunctionCallbackInfo<Value>& args) { Environment* env = Environment::GetCurrent(args); if (args.Length() < 1) return TYPE_ERROR("path required"); if (!args[0]->IsString()) return TYPE_ERROR("path must be a string"); node::Utf8Value path(env->isolate(), args[0]); if (args[1]->IsObject()) { ASYNC_CALL(scandir, args[1], *path, 0 /*flags*/) } else { SYNC_CALL(scandir, *path, *path, 0 /*flags*/) CHECK_GE(SYNC_REQ.result, 0); int r; Local<Array> names = Array::New(env->isolate(), 0); for (int i = 0; ; i++) { uv_dirent_t ent; r = uv_fs_scandir_next(&SYNC_REQ, &ent); if (r == UV_EOF) break; if (r != 0) return env->ThrowUVException(r, "readdir", "", *path); Local<String> name = String::NewFromUtf8(env->isolate(), ent.name); names->Set(i, name); } args.GetReturnValue().Set(names); } } static void Open(const FunctionCallbackInfo<Value>& args) { Environment* env = Environment::GetCurrent(args); int len = args.Length(); if (len < 1) return TYPE_ERROR("path required"); if (len < 2) return TYPE_ERROR("flags required"); if (len < 3) return TYPE_ERROR("mode required"); if (!args[0]->IsString()) return TYPE_ERROR("path must be a string"); if (!args[1]->IsInt32()) return TYPE_ERROR("flags must be an int"); if (!args[2]->IsInt32()) return TYPE_ERROR("mode must be an int"); node::Utf8Value path(env->isolate(), args[0]); int flags = args[1]->Int32Value(); int mode = static_cast<int>(args[2]->Int32Value()); if (args[3]->IsObject()) { ASYNC_CALL(open, args[3], *path, flags, mode) } else { SYNC_CALL(open, *path, *path, flags, mode) args.GetReturnValue().Set(SYNC_RESULT); } } // Wrapper for write(2). // // bytesWritten = write(fd, buffer, offset, length, position, callback) // 0 fd integer. file descriptor // 1 buffer the data to write // 2 offset where in the buffer to start from // 3 length how much to write // 4 position if integer, position to write at in the file. // if null, write from the current position static void WriteBuffer(const FunctionCallbackInfo<Value>& args) { Environment* env = Environment::GetCurrent(args); if (!args[0]->IsInt32()) return env->ThrowTypeError("First argument must be file descriptor"); CHECK(Buffer::HasInstance(args[1])); int fd = args[0]->Int32Value(); Local<Object> obj = args[1].As<Object>(); const char* buf = Buffer::Data(obj); size_t buffer_length = Buffer::Length(obj); size_t off = args[2]->Uint32Value(); size_t len = args[3]->Uint32Value(); int64_t pos = GET_OFFSET(args[4]); Local<Value> req = args[5]; if (off > buffer_length) return env->ThrowRangeError("offset out of bounds"); if (len > buffer_length) return env->ThrowRangeError("length out of bounds"); if (off + len < off) return env->ThrowRangeError("off + len overflow"); if (!Buffer::IsWithinBounds(off, len, buffer_length)) return env->ThrowRangeError("off + len > buffer.length"); buf += off; uv_buf_t uvbuf = uv_buf_init(const_cast<char*>(buf), len); if (req->IsObject()) { ASYNC_CALL(write, req, fd, &uvbuf, 1, pos) return; } SYNC_CALL(write, nullptr, fd, &uvbuf, 1, pos) args.GetReturnValue().Set(SYNC_RESULT); } // Wrapper for writev(2). // // bytesWritten = writev(fd, chunks, position, callback) // 0 fd integer. file descriptor // 1 chunks array of buffers to write // 2 position if integer, position to write at in the file. // if null, write from the current position static void WriteBuffers(const FunctionCallbackInfo<Value>& args) { Environment* env = Environment::GetCurrent(args); CHECK(args[0]->IsInt32()); CHECK(args[1]->IsArray()); int fd = args[0]->Int32Value(); Local<Array> chunks = args[1].As<Array>(); int64_t pos = GET_OFFSET(args[2]); Local<Value> req = args[3]; uint32_t chunkCount = chunks->Length(); uv_buf_t s_iovs[1024]; // use stack allocation when possible uv_buf_t* iovs; if (chunkCount > ARRAY_SIZE(s_iovs)) iovs = new uv_buf_t[chunkCount]; else iovs = s_iovs; for (uint32_t i = 0; i < chunkCount; i++) { Local<Value> chunk = chunks->Get(i); if (!Buffer::HasInstance(chunk)) { if (iovs != s_iovs) delete[] iovs; return env->ThrowTypeError("Array elements all need to be buffers"); } iovs[i] = uv_buf_init(Buffer::Data(chunk), Buffer::Length(chunk)); } if (req->IsObject()) { ASYNC_CALL(write, req, fd, iovs, chunkCount, pos) if (iovs != s_iovs) delete[] iovs; return; } SYNC_CALL(write, nullptr, fd, iovs, chunkCount, pos) if (iovs != s_iovs) delete[] iovs; args.GetReturnValue().Set(SYNC_RESULT); } // Wrapper for write(2). // // bytesWritten = write(fd, string, position, enc, callback) // 0 fd integer. file descriptor // 1 string non-buffer values are converted to strings // 2 position if integer, position to write at in the file. // if null, write from the current position // 3 enc encoding of string static void WriteString(const FunctionCallbackInfo<Value>& args) { Environment* env = Environment::GetCurrent(args); if (!args[0]->IsInt32()) return env->ThrowTypeError("First argument must be file descriptor"); Local<Value> req; Local<Value> string = args[1]; int fd = args[0]->Int32Value(); char* buf = nullptr; int64_t pos; size_t len; FSReqWrap::Ownership ownership = FSReqWrap::COPY; // will assign buf and len if string was external if (!StringBytes::GetExternalParts(env->isolate(), string, const_cast<const char**>(&buf), &len)) { enum encoding enc = ParseEncoding(env->isolate(), args[3], UTF8); len = StringBytes::StorageSize(env->isolate(), string, enc); buf = new char[len]; // StorageSize may return too large a char, so correct the actual length // by the write size len = StringBytes::Write(env->isolate(), buf, len, args[1], enc); ownership = FSReqWrap::MOVE; } |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |