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 · cspotcode/node Expand file tree/ Copy pathfs.jsMore file actions 3047 lines (2715 loc) · 78.4 KB / Copy pathfs.jsFile metadata and controls3047 lines (2715 loc) · 78.4 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'; // When using FSReqCallback, make sure to create the object only *after* all // parameter validation has happened, so that the objects are not kept in memory // in case they are created but never used due to an exception. const { ArrayPrototypePush, BigIntPrototypeToString, MathMax, Number, ObjectCreate, ObjectDefineProperties, ObjectDefineProperty, Promise, ReflectApply, RegExpPrototypeExec, SafeMap, String, StringPrototypeCharCodeAt, StringPrototypeIndexOf, StringPrototypeSlice, } = primordials; const { fs: constants } = internalBinding('constants'); const { S_IFIFO, S_IFLNK, S_IFMT, S_IFREG, S_IFSOCK, F_OK, R_OK, W_OK, X_OK, O_WRONLY, O_SYMLINK } = constants; const pathModule = require('path'); const { isArrayBufferView } = require('internal/util/types'); // We need to get the statValues from the binding at the callsite since // it's re-initialized after deserialization. const binding = internalBinding('fs'); const { Buffer } = require('buffer'); const { aggregateTwoErrors, codes: { ERR_FS_FILE_TOO_LARGE, ERR_INVALID_ARG_VALUE, ERR_FEATURE_UNAVAILABLE_ON_PLATFORM, }, AbortError, uvErrmapGet, uvException } = require('internal/errors'); const { FSReqCallback } = binding; const { toPathIfFileURL } = require('internal/url'); const internalUtil = require('internal/util'); const { constants: { kIoMaxLength, kMaxUserId, }, copyObject, Dirent, emitRecursiveRmdirWarning, getDirents, getOptions, getValidatedFd, getValidatedPath, getValidMode, handleErrorFromBinding, nullCheck, preprocessSymlinkDestination, Stats, getStatsFromBinding, realpathCacheKey, stringToFlags, stringToSymlinkType, toUnixTimestamp, validateBufferArray, validateCpOptions, validateOffsetLengthRead, validateOffsetLengthWrite, validatePath, validatePosition, validateRmOptions, validateRmOptionsSync, validateRmdirOptions, validateStringAfterArrayBufferView, warnOnNonPortableTemplate } = require('internal/fs/utils'); const { Dir, opendir, opendirSync } = require('internal/fs/dir'); const { CHAR_FORWARD_SLASH, CHAR_BACKWARD_SLASH, } = require('internal/constants'); const { isUint32, parseFileMode, validateBoolean, validateBuffer, validateCallback, validateEncoding, validateFunction, validateInteger, validateString, } = require('internal/validators'); const watchers = require('internal/fs/watchers'); const ReadFileContext = require('internal/fs/read_file_context'); let truncateWarn = true; let fs; // Lazy loaded let cpFn; let cpSyncFn; let promises = null; let ReadStream; let WriteStream; let rimraf; let rimrafSync; // These have to be separate because of how graceful-fs happens to do it's // monkeypatching. let FileReadStream; let FileWriteStream; const isWindows = process.platform === 'win32'; const isOSX = process.platform === 'darwin'; 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 maybeCallback(cb) { validateCallback(cb); return cb; } // 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) { validateCallback(cb); return (...args) => ReflectApply(cb, this, args); } // 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) { validateCallback(cb); return (err, stats) => { if (err) return cb(err); cb(err, getStatsFromBinding(stats)); }; } const isFd = isUint32; function isFileType(stats, fileType) { // Use stats array directly to avoid creating an fs.Stats instance just for // our internal use. let mode = stats[1]; if (typeof mode === 'bigint') mode = Number(mode); return (mode & S_IFMT) === fileType; } /** * Tests a user's permissions for the file or directory * specified by `path`. * @param {string | Buffer | URL} path * @param {number} [mode] * @param {(err?: Error) => any} callback * @returns {void} */ function access(path, mode, callback) { if (typeof mode === 'function') { callback = mode; mode = F_OK; } path = getValidatedPath(path); mode = getValidMode(mode, 'access'); callback = makeCallback(callback); const req = new FSReqCallback(); req.oncomplete = callback; binding.access(pathModule.toNamespacedPath(path), mode, req); } /** * Synchronously tests a user's permissions for the file or * directory specified by `path`. * @param {string | Buffer | URL} path * @param {number} [mode] * @returns {void} */ function accessSync(path, mode) { path = getValidatedPath(path); mode = getValidMode(mode, 'access'); const ctx = { path }; binding.access(pathModule.toNamespacedPath(path), mode, undefined, ctx); handleErrorFromBinding(ctx); } /** * Tests whether or not the given path exists. * @param {string | Buffer | URL} path * @param {(exists?: boolean) => any} callback * @returns {void} */ function exists(path, callback) { maybeCallback(callback); function suppressedCallback(err) { callback(err ? false : true); } try { fs.access(path, F_OK, suppressedCallback); } catch { return callback(false); } } ObjectDefineProperty(exists, internalUtil.promisify.custom, { value: (path) => { return new Promise((resolve) => fs.exists(path, resolve)); } }); // fs.existsSync never throws, it only returns true or false. // Since fs.existsSync never throws, users have established // the expectation that passing invalid arguments to it, even like // fs.existsSync(), would only get a false in return, so we cannot signal // validation errors to users properly out of compatibility concerns. // TODO(joyeecheung): deprecate the never-throw-on-invalid-arguments behavior /** * Synchronously tests whether or not the given path exists. * @param {string | Buffer | URL} path * @returns {boolean} */ function existsSync(path) { try { path = getValidatedPath(path); } catch { return false; } const ctx = { path }; const nPath = pathModule.toNamespacedPath(path); binding.access(nPath, F_OK, undefined, ctx); // In case of an invalid symlink, `binding.access()` on win32 // will **not** return an error and is therefore not enough. // Double check with `binding.stat()`. if (isWindows && ctx.errno === undefined) { binding.stat(nPath, false, undefined, ctx); } return ctx.errno === undefined; } function readFileAfterOpen(err, fd) { const context = this.context; if (err) { context.callback(err); return; } context.fd = fd; const req = new FSReqCallback(); req.oncomplete = readFileAfterStat; req.context = context; binding.fstat(fd, false, req); } function readFileAfterStat(err, stats) { const context = this.context; if (err) return context.close(err); const size = context.size = isFileType(stats, S_IFREG) ? stats[8] : 0; if (size > kIoMaxLength) { err = new ERR_FS_FILE_TOO_LARGE(size); return context.close(err); } try { if (size === 0) { context.buffers = []; } else { context.buffer = Buffer.allocUnsafeSlow(size); } } catch (err) { return context.close(err); } context.read(); } function checkAborted(signal, callback) { if (signal?.aborted) { callback(new AbortError(undefined, { cause: signal?.reason })); return true; } return false; } /** * Asynchronously reads the entire contents of a file. * @param {string | Buffer | URL | number} path * @param {{ * encoding?: string | null; * flag?: string; * signal?: AbortSignal; * } | string} [options] * @param {( * err?: Error, * data?: string | Buffer * ) => any} callback * @returns {void} */ function readFile(path, options, callback) { callback = maybeCallback(callback || options); options = getOptions(options, { flag: 'r' }); const context = new ReadFileContext(callback, options.encoding); context.isUserFd = isFd(path); // File descriptor ownership if (options.signal) { context.signal = options.signal; } if (context.isUserFd) { process.nextTick(function tick(context) { ReflectApply(readFileAfterOpen, { context }, [null, path]); }, context); return; } if (checkAborted(options.signal, callback)) return; const flagsNumber = stringToFlags(options.flag, 'options.flag'); path = getValidatedPath(path); const req = new FSReqCallback(); req.context = context; req.oncomplete = readFileAfterOpen; binding.open(pathModule.toNamespacedPath(path), flagsNumber, 0o666, req); } function tryStatSync(fd, isUserFd) { const ctx = {}; const stats = binding.fstat(fd, false, undefined, ctx); if (ctx.errno !== undefined && !isUserFd) { fs.closeSync(fd); throw uvException(ctx); } return stats; } function tryCreateBuffer(size, fd, isUserFd) { let threw = true; let buffer; try { if (size > kIoMaxLength) { throw new ERR_FS_FILE_TOO_LARGE(size); } buffer = Buffer.allocUnsafe(size); threw = false; } finally { if (threw && !isUserFd) fs.closeSync(fd); } return buffer; } function tryReadSync(fd, isUserFd, buffer, pos, len) { let threw = true; let bytesRead; try { bytesRead = fs.readSync(fd, buffer, pos, len); threw = false; } finally { if (threw && !isUserFd) fs.closeSync(fd); } return bytesRead; } /** * Synchronously reads the entire contents of a file. * @param {string | Buffer | URL | number} path * @param {{ * encoding?: string | null; * flag?: string; * }} [options] * @returns {string | Buffer} */ function readFileSync(path, options) { options = getOptions(options, { flag: 'r' }); const isUserFd = isFd(path); // File descriptor ownership const fd = isUserFd ? path : fs.openSync(path, options.flag, 0o666); const stats = tryStatSync(fd, isUserFd); const size = isFileType(stats, S_IFREG) ? stats[8] : 0; let pos = 0; let buffer; // Single buffer with file data let buffers; // List for when size is unknown if (size === 0) { buffers = []; } else { buffer = tryCreateBuffer(size, fd, isUserFd); } let 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) { ArrayPrototypePush(buffers, 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; } function defaultCloseCallback(err) { if (err != null) throw err; } /** * Closes the file descriptor. * @param {number} fd * @param {(err?: Error) => any} [callback] * @returns {void} */ function close(fd, callback = defaultCloseCallback) { fd = getValidatedFd(fd); if (callback !== defaultCloseCallback) callback = makeCallback(callback); const req = new FSReqCallback(); req.oncomplete = callback; binding.close(fd, req); } /** * Synchronously closes the file descriptor. * @param {number} fd * @returns {void} */ function closeSync(fd) { fd = getValidatedFd(fd); const ctx = {}; binding.close(fd, undefined, ctx); handleErrorFromBinding(ctx); } /** * Asynchronously opens a file. * @param {string | Buffer | URL} path * @param {string | number} [flags] * @param {string | number} [mode] * @param {( * err?: Error, * fd?: number * ) => any} callback * @returns {void} */ function open(path, flags, mode, callback) { path = getValidatedPath(path); if (arguments.length < 3) { callback = flags; flags = 'r'; mode = 0o666; } else if (typeof mode === 'function') { callback = mode; mode = 0o666; } else { mode = parseFileMode(mode, 'mode', 0o666); } const flagsNumber = stringToFlags(flags); callback = makeCallback(callback); const req = new FSReqCallback(); req.oncomplete = callback; binding.open(pathModule.toNamespacedPath(path), flagsNumber, mode, req); } /** * Synchronously opens a file. * @param {string | Buffer | URL} path * @param {string | number} [flags] * @param {string | number} [mode] * @returns {number} */ function openSync(path, flags, mode) { path = getValidatedPath(path); const flagsNumber = stringToFlags(flags); mode = parseFileMode(mode, 'mode', 0o666); const ctx = { path }; const result = binding.open(pathModule.toNamespacedPath(path), flagsNumber, mode, undefined, ctx); handleErrorFromBinding(ctx); return result; } /** * Reads file from the specified `fd` (file descriptor). * @param {number} fd * @param {Buffer | TypedArray | DataView} buffer * @param {number} offset * @param {number} length * @param {number | bigint} position * @param {( * err?: Error, * bytesRead?: number, * buffer?: Buffer * ) => any} callback * @returns {void} */ function read(fd, buffer, offset, length, position, callback) { fd = getValidatedFd(fd); if (arguments.length <= 3) { // Assume fs.read(fd, options, callback) let options = {}; if (arguments.length < 3) { // This is fs.read(fd, callback) // buffer will be the callback callback = buffer; } else { // This is fs.read(fd, {}, callback) // buffer will be the options object // offset is the callback options = buffer; callback = offset; } ({ buffer = Buffer.alloc(16384), offset = 0, length = buffer.byteLength, position } = options); } validateBuffer(buffer); callback = maybeCallback(callback); if (offset == null) { offset = 0; } else { validateInteger(offset, 'offset', 0); } length |= 0; if (length === 0) { return process.nextTick(function tick() { callback(null, 0, buffer); }); } if (buffer.byteLength === 0) { throw new ERR_INVALID_ARG_VALUE('buffer', buffer, 'is empty and cannot be written'); } validateOffsetLengthRead(offset, length, buffer.byteLength); if (position == null) position = -1; validatePosition(position, 'position'); function wrapper(err, bytesRead) { // Retain a reference to buffer so that it can't be GC'ed too soon. callback(err, bytesRead || 0, buffer); } const req = new FSReqCallback(); req.oncomplete = wrapper; binding.read(fd, buffer, offset, length, position, req); } ObjectDefineProperty(read, internalUtil.customPromisifyArgs, { value: ['bytesRead', 'buffer'], enumerable: false }); /** * Synchronously reads the file from the * specified `fd` (file descriptor). * @param {number} fd * @param {Buffer | TypedArray | DataView} buffer * @param {{ * offset?: number; * length?: number; * position?: number | bigint; * }} [offset] * @returns {number} */ function readSync(fd, buffer, offset, length, position) { fd = getValidatedFd(fd); validateBuffer(buffer); if (arguments.length <= 3) { // Assume fs.read(fd, buffer, options) const options = offset || {}; ({ offset = 0, length = buffer.byteLength, position } = options); } if (offset == null) { offset = 0; } else { validateInteger(offset, 'offset', 0); } length |= 0; if (length === 0) { return 0; } if (buffer.byteLength === 0) { throw new ERR_INVALID_ARG_VALUE('buffer', buffer, 'is empty and cannot be written'); } validateOffsetLengthRead(offset, length, buffer.byteLength); if (position == null) position = -1; validatePosition(position, 'position'); const ctx = {}; const result = binding.read(fd, buffer, offset, length, position, undefined, ctx); handleErrorFromBinding(ctx); return result; } /** * Reads file from the specified `fd` (file descriptor) * and writes to an array of `ArrayBufferView`s. * @param {number} fd * @param {ArrayBufferView[]} buffers * @param {number} [position] * @param {( * err?: Error, * bytesRead?: number, * buffers?: ArrayBufferView[]; * ) => any} callback * @returns {void} */ function readv(fd, buffers, position, callback) { function wrapper(err, read) { callback(err, read || 0, buffers); } fd = getValidatedFd(fd); validateBufferArray(buffers); callback = maybeCallback(callback || position); const req = new FSReqCallback(); req.oncomplete = wrapper; if (typeof position !== 'number') position = null; return binding.readBuffers(fd, buffers, position, req); } ObjectDefineProperty(readv, internalUtil.customPromisifyArgs, { value: ['bytesRead', 'buffers'], enumerable: false }); /** * Synchronously reads file from the * specified `fd` (file descriptor) and writes to an array * of `ArrayBufferView`s. * @param {number} fd * @param {ArrayBufferView[]} buffers * @param {number} [position] * @returns {number} */ function readvSync(fd, buffers, position) { fd = getValidatedFd(fd); validateBufferArray(buffers); const ctx = {}; if (typeof position !== 'number') position = null; const result = binding.readBuffers(fd, buffers, position, undefined, ctx); handleErrorFromBinding(ctx); return result; } /** * Writes `buffer` to the specified `fd` (file descriptor). * @param {number} fd * @param {Buffer | TypedArray | DataView | string | object} buffer * @param {number} [offset] * @param {number} [length] * @param {number} [position] * @param {( * err?: Error, * bytesWritten?: number; * buffer?: Buffer | TypedArray | DataView * ) => any} callback * @returns {void} */ function write(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); } fd = getValidatedFd(fd); if (isArrayBufferView(buffer)) { callback = maybeCallback(callback || position || length || offset); if (offset == null || typeof offset === 'function') { offset = 0; } else { validateInteger(offset, 'offset', 0); } if (typeof length !== 'number') length = buffer.byteLength - offset; if (typeof position !== 'number') position = null; validateOffsetLengthWrite(offset, length, buffer.byteLength); const req = new FSReqCallback(); req.oncomplete = wrapper; return binding.writeBuffer(fd, buffer, offset, length, position, req); } validateStringAfterArrayBufferView(buffer, 'buffer'); if (typeof position !== 'function') { if (typeof offset === 'function') { position = offset; offset = null; } else { position = length; } length = 'utf8'; } const str = String(buffer); validateEncoding(str, length); callback = maybeCallback(position); const req = new FSReqCallback(); req.oncomplete = wrapper; return binding.writeString(fd, str, offset, length, req); } ObjectDefineProperty(write, internalUtil.customPromisifyArgs, { value: ['bytesWritten', 'buffer'], enumerable: false }); /** * Synchronously writes `buffer` to the * specified `fd` (file descriptor). * @param {number} fd * @param {Buffer | TypedArray | DataView | string | object} buffer * @param {number} [offset] * @param {number} [length] * @param {number} [position] * @returns {number} */ function writeSync(fd, buffer, offset, length, position) { fd = getValidatedFd(fd); const ctx = {}; let result; if (isArrayBufferView(buffer)) { if (position === undefined) position = null; if (offset == null) { offset = 0; } else { validateInteger(offset, 'offset', 0); } if (typeof length !== 'number') length = buffer.byteLength - offset; validateOffsetLengthWrite(offset, length, buffer.byteLength); result = binding.writeBuffer(fd, buffer, offset, length, position, undefined, ctx); } else { validateStringAfterArrayBufferView(buffer, 'buffer'); validateEncoding(buffer, length); if (offset === undefined) offset = null; result = binding.writeString(fd, buffer, offset, length, undefined, ctx); } handleErrorFromBinding(ctx); return result; } /** * Writes an array of `ArrayBufferView`s to the * specified `fd` (file descriptor). * @param {number} fd * @param {ArrayBufferView[]} buffers * @param {number} [position] * @param {( * err?: Error, * bytesWritten?: number, * buffers?: ArrayBufferView[] * ) => any} callback * @returns {void} */ function writev(fd, buffers, position, callback) { function wrapper(err, written) { callback(err, written || 0, buffers); } fd = getValidatedFd(fd); validateBufferArray(buffers); callback = maybeCallback(callback || position); const req = new FSReqCallback(); req.oncomplete = wrapper; if (typeof position !== 'number') position = null; return binding.writeBuffers(fd, buffers, position, req); } ObjectDefineProperty(writev, internalUtil.customPromisifyArgs, { value: ['bytesWritten', 'buffer'], enumerable: false }); /** * Synchronously writes an array of `ArrayBufferView`s * to the specified `fd` (file descriptor). * @param {number} fd * @param {ArrayBufferView[]} buffers * @param {number} [position] * @returns {number} */ function writevSync(fd, buffers, position) { fd = getValidatedFd(fd); validateBufferArray(buffers); const ctx = {}; if (typeof position !== 'number') position = null; const result = binding.writeBuffers(fd, buffers, position, undefined, ctx); handleErrorFromBinding(ctx); return result; } /** * Asynchronously renames file at `oldPath` to * the pathname provided as `newPath`. * @param {string | Buffer | URL} oldPath * @param {string | Buffer | URL} newPath * @param {(err?: Error) => any} callback * @returns {void} */ function rename(oldPath, newPath, callback) { callback = makeCallback(callback); oldPath = getValidatedPath(oldPath, 'oldPath'); newPath = getValidatedPath(newPath, 'newPath'); const req = new FSReqCallback(); req.oncomplete = callback; binding.rename(pathModule.toNamespacedPath(oldPath), pathModule.toNamespacedPath(newPath), req); } /** * Synchronously renames file at `oldPath` to * the pathname provided as `newPath`. * @param {string | Buffer | URL} oldPath * @param {string | Buffer | URL} newPath * @returns {void} */ function renameSync(oldPath, newPath) { oldPath = getValidatedPath(oldPath, 'oldPath'); newPath = getValidatedPath(newPath, 'newPath'); const ctx = { path: oldPath, dest: newPath }; binding.rename(pathModule.toNamespacedPath(oldPath), pathModule.toNamespacedPath(newPath), undefined, ctx); handleErrorFromBinding(ctx); } /** * Truncates the file. * @param {string | Buffer | URL} path * @param {number} [len] * @param {(err?: Error) => any} callback * @returns {void} */ function truncate(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; } |
| 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. |