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/lib/fs.js at master · rbs392/node Expand file tree/ Copy pathfs.jsMore file actions 2600 lines (2181 loc) · 67.9 KB / Copy pathfs.jsFile metadata and controls2600 lines (2181 loc) · 67.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 // Copyright Joyent, Inc. and other Node contributors. // // 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. // Maintainers, keep in mind that ES1-style octal literals (`0666`) are not // allowed in strict mode. Use ES6-style octal literals instead (`0o666`). 'use strict'; const constants = process.binding('constants').fs; const { S_IFIFO, S_IFLNK, S_IFMT, S_IFREG, S_IFSOCK } = constants; const util = require('util'); const pathModule = require('path'); const { isUint8Array } = require('internal/util/types'); const { createPromise, promiseResolve } = process.binding('util'); const binding = process.binding('fs'); const fs = exports; const { Buffer } = require('buffer'); const errors = require('internal/errors'); const { Readable, Writable } = require('stream'); const EventEmitter = require('events'); const { FSReqWrap } = binding; const { FSEvent } = process.binding('fs_event_wrap'); const internalFS = require('internal/fs'); const { getPathFromURL } = require('internal/url'); const internalUtil = require('internal/util'); const { assertEncoding, stringToFlags } = internalFS; Object.defineProperty(exports, 'constants', { configurable: false, enumerable: true, value: constants }); const kMinPoolSpace = 128; const { kMaxLength } = require('buffer'); const isWindows = process.platform === 'win32'; const DEBUG = process.env.NODE_DEBUG && /fs/.test(process.env.NODE_DEBUG); const errnoException = util._errnoException; let truncateWarn = true; function isInt32(n) { return n === (n | 0); } function isUint32(n) { return n === (n >>> 0); } function showTruncateDeprecation() { if (truncateWarn) { process.emitWarning( 'Using fs.truncate with a file descriptor is deprecated. Please use ' + 'fs.ftruncate with a file descriptor instead.', 'DeprecationWarning', 'DEP0081'); truncateWarn = false; } } function getOptions(options, defaultOptions) { if (options === null || options === undefined || typeof options === 'function') { return defaultOptions; } if (typeof options === 'string') { defaultOptions = util._extend({}, defaultOptions); defaultOptions.encoding = options; options = defaultOptions; } else if (typeof options !== 'object') { throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options', ['string', 'Object'], options); } if (options.encoding !== 'buffer') assertEncoding(options.encoding); return options; } function copyObject(source) { var target = {}; for (var key in source) target[key] = source[key]; return target; } function rethrow() { // TODO(thefourtheye) Throw error instead of warning in major version > 7 process.emitWarning( 'Calling an asynchronous function without callback is deprecated.', 'DeprecationWarning', 'DEP0013', rethrow ); // Only enable in debug mode. A backtrace uses ~1000 bytes of heap space and // is fairly slow to generate. if (DEBUG) { var backtrace = new Error(); return function(err) { if (err) { backtrace.stack = `${err.name}: ${err.message}` + backtrace.stack.substr(backtrace.name.length); throw backtrace; } }; } return function(err) { if (err) { throw err; // Forgot a callback but don't know where? Use NODE_DEBUG=fs } }; } function maybeCallback(cb) { return typeof cb === 'function' ? cb : rethrow(); } // Ensure that callbacks run in the global context. Only use this function // for callbacks that are passed to the binding layer, callbacks that are // invoked from JS already run in the proper scope. function makeCallback(cb) { if (cb === undefined) { return rethrow(); } if (typeof cb !== 'function') { throw new errors.TypeError('ERR_INVALID_CALLBACK'); } return function(...args) { return Reflect.apply(cb, undefined, args); }; } function validateBuffer(buffer) { if (!isUint8Array(buffer)) { const err = new errors.TypeError('ERR_INVALID_ARG_TYPE', 'buffer', ['Buffer', 'Uint8Array']); Error.captureStackTrace(err, validateBuffer); throw err; } } function validateLen(len) { let err; if (!isInt32(len)) err = new errors.TypeError('ERR_INVALID_ARG_TYPE', 'len', 'integer'); if (err !== undefined) { Error.captureStackTrace(err, validateLen); throw err; } } function validateOffsetLengthRead(offset, length, bufferLength) { let err; if (offset < 0 || offset >= bufferLength) { err = new errors.RangeError('ERR_OUT_OF_RANGE', 'offset'); } else if (length < 0 || offset + length > bufferLength) { err = new errors.RangeError('ERR_OUT_OF_RANGE', 'length'); } if (err !== undefined) { Error.captureStackTrace(err, validateOffsetLengthRead); throw err; } } function validateOffsetLengthWrite(offset, length, byteLength) { let err; if (offset > byteLength) { err = new errors.RangeError('ERR_OUT_OF_RANGE', 'offset'); } else if (offset + length > byteLength || offset + length > kMaxLength) { err = new errors.RangeError('ERR_OUT_OF_RANGE', 'length'); } if (err !== undefined) { Error.captureStackTrace(err, validateOffsetLengthWrite); throw err; } } function validatePath(path, propName) { let err; if (propName === undefined) { propName = 'path'; } if (typeof path !== 'string' && !isUint8Array(path)) { err = new errors.TypeError('ERR_INVALID_ARG_TYPE', propName, ['string', 'Buffer', 'URL']); } if (err !== undefined) { Error.captureStackTrace(err, validatePath); throw err; } } function validateUint32(value, propName) { let err; if (!isUint32(value)) err = new errors.TypeError('ERR_INVALID_ARG_TYPE', propName, 'integer'); if (err !== undefined) { Error.captureStackTrace(err, validateUint32); throw err; } } // Special case of `makeCallback()` that is specific to async `*stat()` calls as // an optimization, since the data passed back to the callback needs to be // transformed anyway. function makeStatsCallback(cb) { if (cb === undefined) { return rethrow(); } if (typeof cb !== 'function') { throw new errors.TypeError('ERR_INVALID_CALLBACK'); } return function(err) { if (err) return cb(err); cb(err, statsFromValues()); }; } function nullCheck(path, callback) { if (('' + path).indexOf('\u0000') !== -1) { const er = new errors.Error('ERR_INVALID_ARG_TYPE', 'path', 'string without null bytes', path); if (typeof callback !== 'function') throw er; process.nextTick(callback, er); return false; } return true; } function isFd(path) { return (path >>> 0) === path; } // Constructor for file stats. function Stats( dev, mode, nlink, uid, gid, rdev, blksize, ino, size, blocks, atim_msec, mtim_msec, ctim_msec, birthtim_msec ) { this.dev = dev; this.mode = mode; this.nlink = nlink; this.uid = uid; this.gid = gid; this.rdev = rdev; this.blksize = blksize; this.ino = ino; this.size = size; this.blocks = blocks; this.atimeMs = atim_msec; this.mtimeMs = mtim_msec; this.ctimeMs = ctim_msec; this.birthtimeMs = birthtim_msec; this.atime = new Date(atim_msec + 0.5); this.mtime = new Date(mtim_msec + 0.5); this.ctime = new Date(ctim_msec + 0.5); this.birthtime = new Date(birthtim_msec + 0.5); } fs.Stats = Stats; Stats.prototype._checkModeProperty = function(property) { return ((this.mode & S_IFMT) === property); }; Stats.prototype.isDirectory = function() { return this._checkModeProperty(constants.S_IFDIR); }; Stats.prototype.isFile = function() { return this._checkModeProperty(S_IFREG); }; Stats.prototype.isBlockDevice = function() { return this._checkModeProperty(constants.S_IFBLK); }; Stats.prototype.isCharacterDevice = function() { return this._checkModeProperty(constants.S_IFCHR); }; Stats.prototype.isSymbolicLink = function() { return this._checkModeProperty(S_IFLNK); }; Stats.prototype.isFIFO = function() { return this._checkModeProperty(S_IFIFO); }; Stats.prototype.isSocket = function() { return this._checkModeProperty(S_IFSOCK); }; const statValues = binding.getStatValues(); function statsFromValues() { return new Stats(statValues[0], statValues[1], statValues[2], statValues[3], statValues[4], statValues[5], statValues[6] < 0 ? undefined : statValues[6], statValues[7], statValues[8], statValues[9] < 0 ? undefined : statValues[9], statValues[10], statValues[11], statValues[12], statValues[13]); } // Don't allow mode to accidentally be overwritten. Object.defineProperties(fs, { F_OK: { enumerable: true, value: constants.F_OK || 0 }, R_OK: { enumerable: true, value: constants.R_OK || 0 }, W_OK: { enumerable: true, value: constants.W_OK || 0 }, X_OK: { enumerable: true, value: constants.X_OK || 0 }, }); function handleError(val, callback) { if (val instanceof Error) { if (typeof callback === 'function') { process.nextTick(callback, val); return true; } else throw val; } return false; } fs.access = function(path, mode, callback) { if (typeof mode === 'function') { callback = mode; mode = fs.F_OK; } else if (typeof callback !== 'function') { throw new errors.TypeError('ERR_INVALID_CALLBACK'); } if (handleError((path = getPathFromURL(path)), callback)) return; validatePath(path); if (!nullCheck(path, callback)) return; mode = mode | 0; var req = new FSReqWrap(); req.oncomplete = makeCallback(callback); binding.access(pathModule.toNamespacedPath(path), mode, req); }; fs.accessSync = function(path, mode) { handleError((path = getPathFromURL(path))); validatePath(path); nullCheck(path); if (mode === undefined) mode = fs.F_OK; else mode = mode | 0; const ctx = { path }; binding.access(pathModule.toNamespacedPath(path), mode, undefined, ctx); if (ctx.errno !== undefined) { throw new errors.uvException(ctx); } }; fs.exists = function(path, callback) { if (handleError((path = getPathFromURL(path)), cb)) return; validatePath(path); if (!nullCheck(path, cb)) return; var req = new FSReqWrap(); req.oncomplete = cb; binding.stat(pathModule.toNamespacedPath(path), req); function cb(err) { if (callback) callback(err ? false : true); } }; Object.defineProperty(fs.exists, internalUtil.promisify.custom, { value: (path) => { const promise = createPromise(); fs.exists(path, (exists) => promiseResolve(promise, exists)); return promise; } }); fs.existsSync = function(path) { try { handleError((path = getPathFromURL(path))); try { validatePath(path); } catch (e) { return false; } nullCheck(path); const ctx = { path }; binding.stat(pathModule.toNamespacedPath(path), undefined, ctx); if (ctx.errno !== undefined) { return false; } return true; } catch (e) { return false; } }; fs.readFile = function(path, options, callback) { callback = maybeCallback(callback || options); options = getOptions(options, { flag: 'r' }); if (handleError((path = getPathFromURL(path)), callback)) return; if (!nullCheck(path, callback)) return; var context = new ReadFileContext(callback, options.encoding); context.isUserFd = isFd(path); // file descriptor ownership var req = new FSReqWrap(); req.context = context; req.oncomplete = readFileAfterOpen; if (context.isUserFd) { process.nextTick(function() { req.oncomplete(null, path); }); return; } validatePath(path); binding.open(pathModule.toNamespacedPath(path), stringToFlags(options.flag || 'r'), 0o666, req); }; const kReadFileBufferLength = 8 * 1024; function ReadFileContext(callback, encoding) { this.fd = undefined; this.isUserFd = undefined; this.size = undefined; this.callback = callback; this.buffers = null; this.buffer = null; this.pos = 0; this.encoding = encoding; this.err = null; } ReadFileContext.prototype.read = function() { var buffer; var offset; var length; if (this.size === 0) { buffer = this.buffer = Buffer.allocUnsafeSlow(kReadFileBufferLength); offset = 0; length = kReadFileBufferLength; } else { buffer = this.buffer; offset = this.pos; length = this.size - this.pos; } var req = new FSReqWrap(); req.oncomplete = readFileAfterRead; req.context = this; binding.read(this.fd, buffer, offset, length, -1, req); }; ReadFileContext.prototype.close = function(err) { var req = new FSReqWrap(); req.oncomplete = readFileAfterClose; req.context = this; this.err = err; if (this.isUserFd) { process.nextTick(function() { req.oncomplete(null); }); return; } binding.close(this.fd, req); }; function readFileAfterOpen(err, fd) { var context = this.context; if (err) { context.callback(err); return; } context.fd = fd; var req = new FSReqWrap(); req.oncomplete = readFileAfterStat; req.context = context; binding.fstat(fd, req); } function readFileAfterStat(err) { var context = this.context; if (err) return context.close(err); // Use stats array directly to avoid creating an fs.Stats instance just for // our internal use. var size; if ((statValues[1/*mode*/] & S_IFMT) === S_IFREG) size = context.size = statValues[8/*size*/]; else size = context.size = 0; if (size === 0) { context.buffers = []; context.read(); return; } if (size > kMaxLength) { err = new RangeError('File size is greater than possible Buffer: ' + `0x${kMaxLength.toString(16)} bytes`); return context.close(err); } try { context.buffer = Buffer.allocUnsafeSlow(size); } catch (err) { return context.close(err); } context.read(); } function readFileAfterRead(err, bytesRead) { var context = this.context; if (err) return context.close(err); if (bytesRead === 0) return context.close(); context.pos += bytesRead; if (context.size !== 0) { if (context.pos === context.size) context.close(); else context.read(); } else { // unknown size, just read until we don't get bytes. context.buffers.push(context.buffer.slice(0, bytesRead)); context.read(); } } function readFileAfterClose(err) { var context = this.context; var buffer = null; var callback = context.callback; if (context.err || err) return callback(context.err || err); try { if (context.size === 0) buffer = Buffer.concat(context.buffers, context.pos); else if (context.pos < context.size) buffer = context.buffer.slice(0, context.pos); else buffer = context.buffer; if (context.encoding) buffer = buffer.toString(context.encoding); } catch (err) { return callback(err); } callback(null, buffer); } function tryStatSync(fd, isUserFd) { const ctx = {}; binding.fstat(fd, undefined, ctx); if (ctx.errno !== undefined && !isUserFd) { fs.closeSync(fd); throw new errors.uvException(ctx); } } function tryCreateBuffer(size, fd, isUserFd) { var threw = true; var buffer; try { buffer = Buffer.allocUnsafe(size); threw = false; } finally { if (threw && !isUserFd) fs.closeSync(fd); } return buffer; } function tryReadSync(fd, isUserFd, buffer, pos, len) { var threw = true; var bytesRead; try { bytesRead = fs.readSync(fd, buffer, pos, len); threw = false; } finally { if (threw && !isUserFd) fs.closeSync(fd); } return bytesRead; } fs.readFileSync = function(path, options) { options = getOptions(options, { flag: 'r' }); var isUserFd = isFd(path); // file descriptor ownership var fd = isUserFd ? path : fs.openSync(path, options.flag || 'r', 0o666); tryStatSync(fd, isUserFd); // Use stats array directly to avoid creating an fs.Stats instance just for // our internal use. var size; if ((statValues[1/*mode*/] & S_IFMT) === S_IFREG) size = statValues[8/*size*/]; else size = 0; var pos = 0; var buffer; // single buffer with file data var buffers; // list for when size is unknown if (size === 0) { buffers = []; } else { buffer = tryCreateBuffer(size, fd, isUserFd); } var bytesRead; if (size !== 0) { do { bytesRead = tryReadSync(fd, isUserFd, buffer, pos, size - pos); pos += bytesRead; } while (bytesRead !== 0 && pos < size); } else { do { // the kernel lies about many files. // Go ahead and try to read some bytes. buffer = Buffer.allocUnsafe(8192); bytesRead = tryReadSync(fd, isUserFd, buffer, 0, 8192); if (bytesRead !== 0) { buffers.push(buffer.slice(0, bytesRead)); } pos += bytesRead; } while (bytesRead !== 0); } if (!isUserFd) fs.closeSync(fd); if (size === 0) { // data was collected into the buffers list. buffer = Buffer.concat(buffers, pos); } else if (pos < size) { buffer = buffer.slice(0, pos); } if (options.encoding) buffer = buffer.toString(options.encoding); return buffer; }; fs.close = function(fd, callback) { validateUint32(fd, 'fd'); const req = new FSReqWrap(); req.oncomplete = makeCallback(callback); binding.close(fd, req); }; fs.closeSync = function(fd) { validateUint32(fd, 'fd'); const ctx = {}; binding.close(fd, undefined, ctx); if (ctx.errno !== undefined) { throw new errors.uvException(ctx); } }; function modeNum(m, def) { if (typeof m === 'number') return m; if (typeof m === 'string') return parseInt(m, 8); if (def) return modeNum(def); return undefined; } fs.open = function(path, flags, mode, callback_) { var callback = makeCallback(arguments[arguments.length - 1]); mode = modeNum(mode, 0o666); if (handleError((path = getPathFromURL(path)), callback)) return; if (!nullCheck(path, callback)) return; validatePath(path); validateUint32(mode, 'mode'); const req = new FSReqWrap(); req.oncomplete = callback; binding.open(pathModule.toNamespacedPath(path), stringToFlags(flags), mode, req); }; fs.openSync = function(path, flags, mode) { mode = modeNum(mode, 0o666); handleError((path = getPathFromURL(path))); nullCheck(path); validatePath(path); validateUint32(mode, 'mode'); return binding.open(pathModule.toNamespacedPath(path), stringToFlags(flags), mode); }; fs.read = function(fd, buffer, offset, length, position, callback) { validateUint32(fd, 'fd'); validateBuffer(buffer); offset |= 0; length |= 0; if (length === 0) { return process.nextTick(function() { callback && callback(null, 0, buffer); }); } validateOffsetLengthRead(offset, length, buffer.length); if (!isUint32(position)) position = -1; function wrapper(err, bytesRead) { // Retain a reference to buffer so that it can't be GC'ed too soon. callback && callback(err, bytesRead || 0, buffer); } const req = new FSReqWrap(); req.oncomplete = wrapper; binding.read(fd, buffer, offset, length, position, req); }; Object.defineProperty(fs.read, internalUtil.customPromisifyArgs, { value: ['bytesRead', 'buffer'], enumerable: false }); fs.readSync = function(fd, buffer, offset, length, position) { validateUint32(fd, 'fd'); validateBuffer(buffer); offset |= 0; length |= 0; if (length === 0) { return 0; } validateOffsetLengthRead(offset, length, buffer.length); if (!isUint32(position)) position = -1; return binding.read(fd, buffer, offset, length, position); }; // usage: // fs.write(fd, buffer[, offset[, length[, position]]], callback); // OR // fs.write(fd, string[, position[, encoding]], callback); fs.write = function(fd, buffer, offset, length, position, callback) { function wrapper(err, written) { // Retain a reference to buffer so that it can't be GC'ed too soon. callback(err, written || 0, buffer); } validateUint32(fd, 'fd'); const req = new FSReqWrap(); req.oncomplete = wrapper; if (isUint8Array(buffer)) { callback = maybeCallback(callback || position || length || offset); if (typeof offset !== 'number') offset = 0; if (typeof length !== 'number') length = buffer.length - offset; if (typeof position !== 'number') position = null; validateOffsetLengthWrite(offset, length, buffer.byteLength); return binding.writeBuffer(fd, buffer, offset, length, position, req); } if (typeof buffer !== 'string') buffer += ''; if (typeof position !== 'function') { if (typeof offset === 'function') { position = offset; offset = null; } else { position = length; } length = 'utf8'; } callback = maybeCallback(position); return binding.writeString(fd, buffer, offset, length, req); }; Object.defineProperty(fs.write, internalUtil.customPromisifyArgs, { value: ['bytesWritten', 'buffer'], enumerable: false }); // usage: // fs.writeSync(fd, buffer[, offset[, length[, position]]]); // OR // fs.writeSync(fd, string[, position[, encoding]]); fs.writeSync = function(fd, buffer, offset, length, position) { validateUint32(fd, 'fd'); if (isUint8Array(buffer)) { if (position === undefined) position = null; if (typeof offset !== 'number') offset = 0; if (typeof length !== 'number') length = buffer.length - offset; validateOffsetLengthWrite(offset, length, buffer.byteLength); return binding.writeBuffer(fd, buffer, offset, length, position); } if (typeof buffer !== 'string') buffer += ''; if (offset === undefined) offset = null; return binding.writeString(fd, buffer, offset, length, position); }; fs.rename = function(oldPath, newPath, callback) { callback = makeCallback(callback); if (handleError((oldPath = getPathFromURL(oldPath)), callback)) return; if (handleError((newPath = getPathFromURL(newPath)), callback)) return; if (!nullCheck(oldPath, callback)) return; if (!nullCheck(newPath, callback)) return; validatePath(oldPath, 'oldPath'); validatePath(newPath, 'newPath'); const req = new FSReqWrap(); req.oncomplete = callback; binding.rename(pathModule.toNamespacedPath(oldPath), pathModule.toNamespacedPath(newPath), req); }; fs.renameSync = function(oldPath, newPath) { handleError((oldPath = getPathFromURL(oldPath))); handleError((newPath = getPathFromURL(newPath))); nullCheck(oldPath); nullCheck(newPath); validatePath(oldPath, 'oldPath'); validatePath(newPath, 'newPath'); return binding.rename(pathModule.toNamespacedPath(oldPath), pathModule.toNamespacedPath(newPath)); }; fs.truncate = function(path, len, callback) { if (typeof path === 'number') { showTruncateDeprecation(); return fs.ftruncate(path, len, callback); } if (typeof len === 'function') { callback = len; len = 0; } else if (len === undefined) { len = 0; } callback = maybeCallback(callback); fs.open(path, 'r+', function(er, fd) { if (er) return callback(er); var req = new FSReqWrap(); req.oncomplete = function oncomplete(er) { fs.close(fd, function(er2) { callback(er || er2); }); }; binding.ftruncate(fd, len, req); }); }; fs.truncateSync = function(path, len) { if (typeof path === 'number') { // legacy showTruncateDeprecation(); return fs.ftruncateSync(path, len); } if (len === undefined) { len = 0; } // allow error to be thrown, but still close fd. var fd = fs.openSync(path, 'r+'); var ret; try { ret = fs.ftruncateSync(fd, len); } finally { fs.closeSync(fd); } return ret; }; fs.ftruncate = function(fd, len = 0, callback) { if (typeof len === 'function') { callback = len; len = 0; } validateUint32(fd, 'fd'); validateLen(len); len = Math.max(0, len); const req = new FSReqWrap(); req.oncomplete = makeCallback(callback); binding.ftruncate(fd, len, req); }; fs.ftruncateSync = function(fd, len = 0) { validateUint32(fd, 'fd'); validateLen(len); len = Math.max(0, len); return binding.ftruncate(fd, len); }; fs.rmdir = function(path, callback) { callback = maybeCallback(callback); if (handleError((path = getPathFromURL(path)), callback)) return; |
| 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. |