11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciconst { 41cb0ef41Sopenharmony_ci ArrayPrototypePush, 51cb0ef41Sopenharmony_ci ArrayPrototypePop, 61cb0ef41Sopenharmony_ci Error, 71cb0ef41Sopenharmony_ci MathMax, 81cb0ef41Sopenharmony_ci MathMin, 91cb0ef41Sopenharmony_ci NumberIsSafeInteger, 101cb0ef41Sopenharmony_ci Promise, 111cb0ef41Sopenharmony_ci PromisePrototypeThen, 121cb0ef41Sopenharmony_ci PromiseResolve, 131cb0ef41Sopenharmony_ci PromiseReject, 141cb0ef41Sopenharmony_ci SafeArrayIterator, 151cb0ef41Sopenharmony_ci SafePromisePrototypeFinally, 161cb0ef41Sopenharmony_ci Symbol, 171cb0ef41Sopenharmony_ci SymbolAsyncDispose, 181cb0ef41Sopenharmony_ci Uint8Array, 191cb0ef41Sopenharmony_ci FunctionPrototypeBind, 201cb0ef41Sopenharmony_ci} = primordials; 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ciconst { fs: constants } = internalBinding('constants'); 231cb0ef41Sopenharmony_ciconst { 241cb0ef41Sopenharmony_ci F_OK, 251cb0ef41Sopenharmony_ci O_SYMLINK, 261cb0ef41Sopenharmony_ci O_WRONLY, 271cb0ef41Sopenharmony_ci S_IFMT, 281cb0ef41Sopenharmony_ci S_IFREG, 291cb0ef41Sopenharmony_ci} = constants; 301cb0ef41Sopenharmony_ci 311cb0ef41Sopenharmony_ciconst binding = internalBinding('fs'); 321cb0ef41Sopenharmony_ciconst { Buffer } = require('buffer'); 331cb0ef41Sopenharmony_ci 341cb0ef41Sopenharmony_ciconst { 351cb0ef41Sopenharmony_ci codes: { 361cb0ef41Sopenharmony_ci ERR_FS_FILE_TOO_LARGE, 371cb0ef41Sopenharmony_ci ERR_INVALID_ARG_VALUE, 381cb0ef41Sopenharmony_ci ERR_INVALID_STATE, 391cb0ef41Sopenharmony_ci ERR_METHOD_NOT_IMPLEMENTED, 401cb0ef41Sopenharmony_ci }, 411cb0ef41Sopenharmony_ci AbortError, 421cb0ef41Sopenharmony_ci aggregateTwoErrors, 431cb0ef41Sopenharmony_ci} = require('internal/errors'); 441cb0ef41Sopenharmony_ciconst { isArrayBufferView } = require('internal/util/types'); 451cb0ef41Sopenharmony_ciconst { rimrafPromises } = require('internal/fs/rimraf'); 461cb0ef41Sopenharmony_ciconst { 471cb0ef41Sopenharmony_ci constants: { 481cb0ef41Sopenharmony_ci kIoMaxLength, 491cb0ef41Sopenharmony_ci kMaxUserId, 501cb0ef41Sopenharmony_ci kReadFileBufferLength, 511cb0ef41Sopenharmony_ci kReadFileUnknownBufferLength, 521cb0ef41Sopenharmony_ci kWriteFileMaxChunkSize, 531cb0ef41Sopenharmony_ci }, 541cb0ef41Sopenharmony_ci copyObject, 551cb0ef41Sopenharmony_ci emitRecursiveRmdirWarning, 561cb0ef41Sopenharmony_ci getDirents, 571cb0ef41Sopenharmony_ci getOptions, 581cb0ef41Sopenharmony_ci getStatFsFromBinding, 591cb0ef41Sopenharmony_ci getStatsFromBinding, 601cb0ef41Sopenharmony_ci getValidatedPath, 611cb0ef41Sopenharmony_ci getValidMode, 621cb0ef41Sopenharmony_ci preprocessSymlinkDestination, 631cb0ef41Sopenharmony_ci stringToFlags, 641cb0ef41Sopenharmony_ci stringToSymlinkType, 651cb0ef41Sopenharmony_ci toUnixTimestamp, 661cb0ef41Sopenharmony_ci validateBufferArray, 671cb0ef41Sopenharmony_ci validateCpOptions, 681cb0ef41Sopenharmony_ci validateOffsetLengthRead, 691cb0ef41Sopenharmony_ci validateOffsetLengthWrite, 701cb0ef41Sopenharmony_ci validateRmOptions, 711cb0ef41Sopenharmony_ci validateRmdirOptions, 721cb0ef41Sopenharmony_ci validatePrimitiveStringAfterArrayBufferView, 731cb0ef41Sopenharmony_ci warnOnNonPortableTemplate, 741cb0ef41Sopenharmony_ci} = require('internal/fs/utils'); 751cb0ef41Sopenharmony_ciconst { opendir } = require('internal/fs/dir'); 761cb0ef41Sopenharmony_ciconst { 771cb0ef41Sopenharmony_ci parseFileMode, 781cb0ef41Sopenharmony_ci validateAbortSignal, 791cb0ef41Sopenharmony_ci validateBoolean, 801cb0ef41Sopenharmony_ci validateBuffer, 811cb0ef41Sopenharmony_ci validateEncoding, 821cb0ef41Sopenharmony_ci validateInteger, 831cb0ef41Sopenharmony_ci validateString, 841cb0ef41Sopenharmony_ci} = require('internal/validators'); 851cb0ef41Sopenharmony_ciconst pathModule = require('path'); 861cb0ef41Sopenharmony_ciconst { 871cb0ef41Sopenharmony_ci kEmptyObject, 881cb0ef41Sopenharmony_ci lazyDOMException, 891cb0ef41Sopenharmony_ci promisify, 901cb0ef41Sopenharmony_ci} = require('internal/util'); 911cb0ef41Sopenharmony_ciconst { EventEmitterMixin } = require('internal/event_target'); 921cb0ef41Sopenharmony_ciconst { StringDecoder } = require('string_decoder'); 931cb0ef41Sopenharmony_ciconst { watch } = require('internal/fs/watchers'); 941cb0ef41Sopenharmony_ciconst { isIterable } = require('internal/streams/utils'); 951cb0ef41Sopenharmony_ciconst assert = require('internal/assert'); 961cb0ef41Sopenharmony_ci 971cb0ef41Sopenharmony_ciconst kHandle = Symbol('kHandle'); 981cb0ef41Sopenharmony_ciconst kFd = Symbol('kFd'); 991cb0ef41Sopenharmony_ciconst kRefs = Symbol('kRefs'); 1001cb0ef41Sopenharmony_ciconst kClosePromise = Symbol('kClosePromise'); 1011cb0ef41Sopenharmony_ciconst kCloseResolve = Symbol('kCloseResolve'); 1021cb0ef41Sopenharmony_ciconst kCloseReject = Symbol('kCloseReject'); 1031cb0ef41Sopenharmony_ciconst kRef = Symbol('kRef'); 1041cb0ef41Sopenharmony_ciconst kUnref = Symbol('kUnref'); 1051cb0ef41Sopenharmony_ciconst kLocked = Symbol('kLocked'); 1061cb0ef41Sopenharmony_ci 1071cb0ef41Sopenharmony_ciconst { kUsePromises } = binding; 1081cb0ef41Sopenharmony_ciconst { Interface } = require('internal/readline/interface'); 1091cb0ef41Sopenharmony_ciconst { 1101cb0ef41Sopenharmony_ci JSTransferable, kDeserialize, kTransfer, kTransferList, 1111cb0ef41Sopenharmony_ci} = require('internal/worker/js_transferable'); 1121cb0ef41Sopenharmony_ci 1131cb0ef41Sopenharmony_ciconst getDirectoryEntriesPromise = promisify(getDirents); 1141cb0ef41Sopenharmony_ciconst validateRmOptionsPromise = promisify(validateRmOptions); 1151cb0ef41Sopenharmony_ci 1161cb0ef41Sopenharmony_cilet cpPromises; 1171cb0ef41Sopenharmony_cifunction lazyLoadCpPromises() { 1181cb0ef41Sopenharmony_ci return cpPromises ??= require('internal/fs/cp/cp').cpFn; 1191cb0ef41Sopenharmony_ci} 1201cb0ef41Sopenharmony_ci 1211cb0ef41Sopenharmony_ci// Lazy loaded to avoid circular dependency. 1221cb0ef41Sopenharmony_cilet fsStreams; 1231cb0ef41Sopenharmony_cifunction lazyFsStreams() { 1241cb0ef41Sopenharmony_ci return fsStreams ??= require('internal/fs/streams'); 1251cb0ef41Sopenharmony_ci} 1261cb0ef41Sopenharmony_ci 1271cb0ef41Sopenharmony_ciclass FileHandle extends EventEmitterMixin(JSTransferable) { 1281cb0ef41Sopenharmony_ci /** 1291cb0ef41Sopenharmony_ci * @param {InternalFSBinding.FileHandle | undefined} filehandle 1301cb0ef41Sopenharmony_ci */ 1311cb0ef41Sopenharmony_ci constructor(filehandle) { 1321cb0ef41Sopenharmony_ci super(); 1331cb0ef41Sopenharmony_ci this[kHandle] = filehandle; 1341cb0ef41Sopenharmony_ci this[kFd] = filehandle ? filehandle.fd : -1; 1351cb0ef41Sopenharmony_ci 1361cb0ef41Sopenharmony_ci this[kRefs] = 1; 1371cb0ef41Sopenharmony_ci this[kClosePromise] = null; 1381cb0ef41Sopenharmony_ci } 1391cb0ef41Sopenharmony_ci 1401cb0ef41Sopenharmony_ci getAsyncId() { 1411cb0ef41Sopenharmony_ci return this[kHandle].getAsyncId(); 1421cb0ef41Sopenharmony_ci } 1431cb0ef41Sopenharmony_ci 1441cb0ef41Sopenharmony_ci get fd() { 1451cb0ef41Sopenharmony_ci return this[kFd]; 1461cb0ef41Sopenharmony_ci } 1471cb0ef41Sopenharmony_ci 1481cb0ef41Sopenharmony_ci appendFile(data, options) { 1491cb0ef41Sopenharmony_ci return fsCall(writeFile, this, data, options); 1501cb0ef41Sopenharmony_ci } 1511cb0ef41Sopenharmony_ci 1521cb0ef41Sopenharmony_ci chmod(mode) { 1531cb0ef41Sopenharmony_ci return fsCall(fchmod, this, mode); 1541cb0ef41Sopenharmony_ci } 1551cb0ef41Sopenharmony_ci 1561cb0ef41Sopenharmony_ci chown(uid, gid) { 1571cb0ef41Sopenharmony_ci return fsCall(fchown, this, uid, gid); 1581cb0ef41Sopenharmony_ci } 1591cb0ef41Sopenharmony_ci 1601cb0ef41Sopenharmony_ci datasync() { 1611cb0ef41Sopenharmony_ci return fsCall(fdatasync, this); 1621cb0ef41Sopenharmony_ci } 1631cb0ef41Sopenharmony_ci 1641cb0ef41Sopenharmony_ci sync() { 1651cb0ef41Sopenharmony_ci return fsCall(fsync, this); 1661cb0ef41Sopenharmony_ci } 1671cb0ef41Sopenharmony_ci 1681cb0ef41Sopenharmony_ci read(buffer, offset, length, position) { 1691cb0ef41Sopenharmony_ci return fsCall(read, this, buffer, offset, length, position); 1701cb0ef41Sopenharmony_ci } 1711cb0ef41Sopenharmony_ci 1721cb0ef41Sopenharmony_ci readv(buffers, position) { 1731cb0ef41Sopenharmony_ci return fsCall(readv, this, buffers, position); 1741cb0ef41Sopenharmony_ci } 1751cb0ef41Sopenharmony_ci 1761cb0ef41Sopenharmony_ci readFile(options) { 1771cb0ef41Sopenharmony_ci return fsCall(readFile, this, options); 1781cb0ef41Sopenharmony_ci } 1791cb0ef41Sopenharmony_ci 1801cb0ef41Sopenharmony_ci readLines(options = undefined) { 1811cb0ef41Sopenharmony_ci return new Interface({ 1821cb0ef41Sopenharmony_ci input: this.createReadStream(options), 1831cb0ef41Sopenharmony_ci crlfDelay: Infinity, 1841cb0ef41Sopenharmony_ci }); 1851cb0ef41Sopenharmony_ci } 1861cb0ef41Sopenharmony_ci 1871cb0ef41Sopenharmony_ci stat(options) { 1881cb0ef41Sopenharmony_ci return fsCall(fstat, this, options); 1891cb0ef41Sopenharmony_ci } 1901cb0ef41Sopenharmony_ci 1911cb0ef41Sopenharmony_ci truncate(len = 0) { 1921cb0ef41Sopenharmony_ci return fsCall(ftruncate, this, len); 1931cb0ef41Sopenharmony_ci } 1941cb0ef41Sopenharmony_ci 1951cb0ef41Sopenharmony_ci utimes(atime, mtime) { 1961cb0ef41Sopenharmony_ci return fsCall(futimes, this, atime, mtime); 1971cb0ef41Sopenharmony_ci } 1981cb0ef41Sopenharmony_ci 1991cb0ef41Sopenharmony_ci write(buffer, offset, length, position) { 2001cb0ef41Sopenharmony_ci return fsCall(write, this, buffer, offset, length, position); 2011cb0ef41Sopenharmony_ci } 2021cb0ef41Sopenharmony_ci 2031cb0ef41Sopenharmony_ci writev(buffers, position) { 2041cb0ef41Sopenharmony_ci return fsCall(writev, this, buffers, position); 2051cb0ef41Sopenharmony_ci } 2061cb0ef41Sopenharmony_ci 2071cb0ef41Sopenharmony_ci writeFile(data, options) { 2081cb0ef41Sopenharmony_ci return fsCall(writeFile, this, data, options); 2091cb0ef41Sopenharmony_ci } 2101cb0ef41Sopenharmony_ci 2111cb0ef41Sopenharmony_ci close = () => { 2121cb0ef41Sopenharmony_ci if (this[kFd] === -1) { 2131cb0ef41Sopenharmony_ci return PromiseResolve(); 2141cb0ef41Sopenharmony_ci } 2151cb0ef41Sopenharmony_ci 2161cb0ef41Sopenharmony_ci if (this[kClosePromise]) { 2171cb0ef41Sopenharmony_ci return this[kClosePromise]; 2181cb0ef41Sopenharmony_ci } 2191cb0ef41Sopenharmony_ci 2201cb0ef41Sopenharmony_ci this[kRefs]--; 2211cb0ef41Sopenharmony_ci if (this[kRefs] === 0) { 2221cb0ef41Sopenharmony_ci this[kFd] = -1; 2231cb0ef41Sopenharmony_ci this[kClosePromise] = SafePromisePrototypeFinally( 2241cb0ef41Sopenharmony_ci this[kHandle].close(), 2251cb0ef41Sopenharmony_ci () => { this[kClosePromise] = undefined; }, 2261cb0ef41Sopenharmony_ci ); 2271cb0ef41Sopenharmony_ci } else { 2281cb0ef41Sopenharmony_ci this[kClosePromise] = SafePromisePrototypeFinally( 2291cb0ef41Sopenharmony_ci new Promise((resolve, reject) => { 2301cb0ef41Sopenharmony_ci this[kCloseResolve] = resolve; 2311cb0ef41Sopenharmony_ci this[kCloseReject] = reject; 2321cb0ef41Sopenharmony_ci }), () => { 2331cb0ef41Sopenharmony_ci this[kClosePromise] = undefined; 2341cb0ef41Sopenharmony_ci this[kCloseReject] = undefined; 2351cb0ef41Sopenharmony_ci this[kCloseResolve] = undefined; 2361cb0ef41Sopenharmony_ci }, 2371cb0ef41Sopenharmony_ci ); 2381cb0ef41Sopenharmony_ci } 2391cb0ef41Sopenharmony_ci 2401cb0ef41Sopenharmony_ci this.emit('close'); 2411cb0ef41Sopenharmony_ci return this[kClosePromise]; 2421cb0ef41Sopenharmony_ci }; 2431cb0ef41Sopenharmony_ci 2441cb0ef41Sopenharmony_ci async [SymbolAsyncDispose]() { 2451cb0ef41Sopenharmony_ci return this.close(); 2461cb0ef41Sopenharmony_ci } 2471cb0ef41Sopenharmony_ci 2481cb0ef41Sopenharmony_ci /** 2491cb0ef41Sopenharmony_ci * @typedef {import('../webstreams/readablestream').ReadableStream 2501cb0ef41Sopenharmony_ci * } ReadableStream 2511cb0ef41Sopenharmony_ci * @param {{ 2521cb0ef41Sopenharmony_ci * type?: string; 2531cb0ef41Sopenharmony_ci * }} [options] 2541cb0ef41Sopenharmony_ci * @returns {ReadableStream} 2551cb0ef41Sopenharmony_ci */ 2561cb0ef41Sopenharmony_ci readableWebStream(options = kEmptyObject) { 2571cb0ef41Sopenharmony_ci if (this[kFd] === -1) 2581cb0ef41Sopenharmony_ci throw new ERR_INVALID_STATE('The FileHandle is closed'); 2591cb0ef41Sopenharmony_ci if (this[kClosePromise]) 2601cb0ef41Sopenharmony_ci throw new ERR_INVALID_STATE('The FileHandle is closing'); 2611cb0ef41Sopenharmony_ci if (this[kLocked]) 2621cb0ef41Sopenharmony_ci throw new ERR_INVALID_STATE('The FileHandle is locked'); 2631cb0ef41Sopenharmony_ci this[kLocked] = true; 2641cb0ef41Sopenharmony_ci 2651cb0ef41Sopenharmony_ci if (options.type !== undefined) { 2661cb0ef41Sopenharmony_ci validateString(options.type, 'options.type'); 2671cb0ef41Sopenharmony_ci } 2681cb0ef41Sopenharmony_ci 2691cb0ef41Sopenharmony_ci let readable; 2701cb0ef41Sopenharmony_ci 2711cb0ef41Sopenharmony_ci if (options.type !== 'bytes') { 2721cb0ef41Sopenharmony_ci const { 2731cb0ef41Sopenharmony_ci newReadableStreamFromStreamBase, 2741cb0ef41Sopenharmony_ci } = require('internal/webstreams/adapters'); 2751cb0ef41Sopenharmony_ci readable = newReadableStreamFromStreamBase( 2761cb0ef41Sopenharmony_ci this[kHandle], 2771cb0ef41Sopenharmony_ci undefined, 2781cb0ef41Sopenharmony_ci { ondone: () => this[kUnref]() }); 2791cb0ef41Sopenharmony_ci } else { 2801cb0ef41Sopenharmony_ci const { 2811cb0ef41Sopenharmony_ci ReadableStream, 2821cb0ef41Sopenharmony_ci } = require('internal/webstreams/readablestream'); 2831cb0ef41Sopenharmony_ci 2841cb0ef41Sopenharmony_ci const readFn = FunctionPrototypeBind(this.read, this); 2851cb0ef41Sopenharmony_ci const ondone = FunctionPrototypeBind(this[kUnref], this); 2861cb0ef41Sopenharmony_ci 2871cb0ef41Sopenharmony_ci readable = new ReadableStream({ 2881cb0ef41Sopenharmony_ci type: 'bytes', 2891cb0ef41Sopenharmony_ci autoAllocateChunkSize: 16384, 2901cb0ef41Sopenharmony_ci 2911cb0ef41Sopenharmony_ci async pull(controller) { 2921cb0ef41Sopenharmony_ci const view = controller.byobRequest.view; 2931cb0ef41Sopenharmony_ci const { bytesRead } = await readFn(view, view.byteOffset, view.byteLength); 2941cb0ef41Sopenharmony_ci 2951cb0ef41Sopenharmony_ci if (bytesRead === 0) { 2961cb0ef41Sopenharmony_ci ondone(); 2971cb0ef41Sopenharmony_ci controller.close(); 2981cb0ef41Sopenharmony_ci } 2991cb0ef41Sopenharmony_ci 3001cb0ef41Sopenharmony_ci controller.byobRequest.respond(bytesRead); 3011cb0ef41Sopenharmony_ci }, 3021cb0ef41Sopenharmony_ci 3031cb0ef41Sopenharmony_ci cancel() { 3041cb0ef41Sopenharmony_ci ondone(); 3051cb0ef41Sopenharmony_ci }, 3061cb0ef41Sopenharmony_ci }); 3071cb0ef41Sopenharmony_ci } 3081cb0ef41Sopenharmony_ci 3091cb0ef41Sopenharmony_ci const { 3101cb0ef41Sopenharmony_ci readableStreamCancel, 3111cb0ef41Sopenharmony_ci } = require('internal/webstreams/readablestream'); 3121cb0ef41Sopenharmony_ci this[kRef](); 3131cb0ef41Sopenharmony_ci this.once('close', () => { 3141cb0ef41Sopenharmony_ci readableStreamCancel(readable); 3151cb0ef41Sopenharmony_ci }); 3161cb0ef41Sopenharmony_ci 3171cb0ef41Sopenharmony_ci return readable; 3181cb0ef41Sopenharmony_ci } 3191cb0ef41Sopenharmony_ci 3201cb0ef41Sopenharmony_ci /** 3211cb0ef41Sopenharmony_ci * @typedef {import('./streams').ReadStream 3221cb0ef41Sopenharmony_ci * } ReadStream 3231cb0ef41Sopenharmony_ci * @param {{ 3241cb0ef41Sopenharmony_ci * encoding?: string; 3251cb0ef41Sopenharmony_ci * autoClose?: boolean; 3261cb0ef41Sopenharmony_ci * emitClose?: boolean; 3271cb0ef41Sopenharmony_ci * start: number; 3281cb0ef41Sopenharmony_ci * end?: number; 3291cb0ef41Sopenharmony_ci * highWaterMark?: number; 3301cb0ef41Sopenharmony_ci * }} [options] 3311cb0ef41Sopenharmony_ci * @returns {ReadStream} 3321cb0ef41Sopenharmony_ci */ 3331cb0ef41Sopenharmony_ci createReadStream(options = undefined) { 3341cb0ef41Sopenharmony_ci const { ReadStream } = lazyFsStreams(); 3351cb0ef41Sopenharmony_ci return new ReadStream(undefined, { ...options, fd: this }); 3361cb0ef41Sopenharmony_ci } 3371cb0ef41Sopenharmony_ci 3381cb0ef41Sopenharmony_ci /** 3391cb0ef41Sopenharmony_ci * @typedef {import('./streams').WriteStream 3401cb0ef41Sopenharmony_ci * } WriteStream 3411cb0ef41Sopenharmony_ci * @param {{ 3421cb0ef41Sopenharmony_ci * encoding?: string; 3431cb0ef41Sopenharmony_ci * autoClose?: boolean; 3441cb0ef41Sopenharmony_ci * emitClose?: boolean; 3451cb0ef41Sopenharmony_ci * start: number; 3461cb0ef41Sopenharmony_ci * }} [options] 3471cb0ef41Sopenharmony_ci * @returns {WriteStream} 3481cb0ef41Sopenharmony_ci */ 3491cb0ef41Sopenharmony_ci createWriteStream(options = undefined) { 3501cb0ef41Sopenharmony_ci const { WriteStream } = lazyFsStreams(); 3511cb0ef41Sopenharmony_ci return new WriteStream(undefined, { ...options, fd: this }); 3521cb0ef41Sopenharmony_ci } 3531cb0ef41Sopenharmony_ci 3541cb0ef41Sopenharmony_ci [kTransfer]() { 3551cb0ef41Sopenharmony_ci if (this[kClosePromise] || this[kRefs] > 1) { 3561cb0ef41Sopenharmony_ci throw lazyDOMException('Cannot transfer FileHandle while in use', 3571cb0ef41Sopenharmony_ci 'DataCloneError'); 3581cb0ef41Sopenharmony_ci } 3591cb0ef41Sopenharmony_ci 3601cb0ef41Sopenharmony_ci const handle = this[kHandle]; 3611cb0ef41Sopenharmony_ci this[kFd] = -1; 3621cb0ef41Sopenharmony_ci this[kHandle] = null; 3631cb0ef41Sopenharmony_ci this[kRefs] = 0; 3641cb0ef41Sopenharmony_ci 3651cb0ef41Sopenharmony_ci return { 3661cb0ef41Sopenharmony_ci data: { handle }, 3671cb0ef41Sopenharmony_ci deserializeInfo: 'internal/fs/promises:FileHandle', 3681cb0ef41Sopenharmony_ci }; 3691cb0ef41Sopenharmony_ci } 3701cb0ef41Sopenharmony_ci 3711cb0ef41Sopenharmony_ci [kTransferList]() { 3721cb0ef41Sopenharmony_ci return [ this[kHandle] ]; 3731cb0ef41Sopenharmony_ci } 3741cb0ef41Sopenharmony_ci 3751cb0ef41Sopenharmony_ci [kDeserialize]({ handle }) { 3761cb0ef41Sopenharmony_ci this[kHandle] = handle; 3771cb0ef41Sopenharmony_ci this[kFd] = handle.fd; 3781cb0ef41Sopenharmony_ci } 3791cb0ef41Sopenharmony_ci 3801cb0ef41Sopenharmony_ci [kRef]() { 3811cb0ef41Sopenharmony_ci this[kRefs]++; 3821cb0ef41Sopenharmony_ci } 3831cb0ef41Sopenharmony_ci 3841cb0ef41Sopenharmony_ci [kUnref]() { 3851cb0ef41Sopenharmony_ci this[kRefs]--; 3861cb0ef41Sopenharmony_ci if (this[kRefs] === 0) { 3871cb0ef41Sopenharmony_ci this[kFd] = -1; 3881cb0ef41Sopenharmony_ci PromisePrototypeThen( 3891cb0ef41Sopenharmony_ci this[kHandle].close(), 3901cb0ef41Sopenharmony_ci this[kCloseResolve], 3911cb0ef41Sopenharmony_ci this[kCloseReject], 3921cb0ef41Sopenharmony_ci ); 3931cb0ef41Sopenharmony_ci } 3941cb0ef41Sopenharmony_ci } 3951cb0ef41Sopenharmony_ci} 3961cb0ef41Sopenharmony_ci 3971cb0ef41Sopenharmony_ciasync function handleFdClose(fileOpPromise, closeFunc) { 3981cb0ef41Sopenharmony_ci return PromisePrototypeThen( 3991cb0ef41Sopenharmony_ci fileOpPromise, 4001cb0ef41Sopenharmony_ci (result) => PromisePrototypeThen(closeFunc(), () => result), 4011cb0ef41Sopenharmony_ci (opError) => 4021cb0ef41Sopenharmony_ci PromisePrototypeThen( 4031cb0ef41Sopenharmony_ci closeFunc(), 4041cb0ef41Sopenharmony_ci () => PromiseReject(opError), 4051cb0ef41Sopenharmony_ci (closeError) => PromiseReject(aggregateTwoErrors(closeError, opError)), 4061cb0ef41Sopenharmony_ci ), 4071cb0ef41Sopenharmony_ci ); 4081cb0ef41Sopenharmony_ci} 4091cb0ef41Sopenharmony_ci 4101cb0ef41Sopenharmony_ciasync function fsCall(fn, handle, ...args) { 4111cb0ef41Sopenharmony_ci assert(handle[kRefs] !== undefined, 4121cb0ef41Sopenharmony_ci 'handle must be an instance of FileHandle'); 4131cb0ef41Sopenharmony_ci 4141cb0ef41Sopenharmony_ci if (handle.fd === -1) { 4151cb0ef41Sopenharmony_ci // eslint-disable-next-line no-restricted-syntax 4161cb0ef41Sopenharmony_ci const err = new Error('file closed'); 4171cb0ef41Sopenharmony_ci err.code = 'EBADF'; 4181cb0ef41Sopenharmony_ci err.syscall = fn.name; 4191cb0ef41Sopenharmony_ci throw err; 4201cb0ef41Sopenharmony_ci } 4211cb0ef41Sopenharmony_ci 4221cb0ef41Sopenharmony_ci try { 4231cb0ef41Sopenharmony_ci handle[kRef](); 4241cb0ef41Sopenharmony_ci return await fn(handle, ...new SafeArrayIterator(args)); 4251cb0ef41Sopenharmony_ci } finally { 4261cb0ef41Sopenharmony_ci handle[kUnref](); 4271cb0ef41Sopenharmony_ci } 4281cb0ef41Sopenharmony_ci} 4291cb0ef41Sopenharmony_ci 4301cb0ef41Sopenharmony_cifunction checkAborted(signal) { 4311cb0ef41Sopenharmony_ci if (signal?.aborted) 4321cb0ef41Sopenharmony_ci throw new AbortError(undefined, { cause: signal?.reason }); 4331cb0ef41Sopenharmony_ci} 4341cb0ef41Sopenharmony_ci 4351cb0ef41Sopenharmony_ciasync function writeFileHandle(filehandle, data, signal, encoding) { 4361cb0ef41Sopenharmony_ci checkAborted(signal); 4371cb0ef41Sopenharmony_ci if (isCustomIterable(data)) { 4381cb0ef41Sopenharmony_ci for await (const buf of data) { 4391cb0ef41Sopenharmony_ci checkAborted(signal); 4401cb0ef41Sopenharmony_ci const toWrite = 4411cb0ef41Sopenharmony_ci isArrayBufferView(buf) ? buf : Buffer.from(buf, encoding || 'utf8'); 4421cb0ef41Sopenharmony_ci let remaining = toWrite.byteLength; 4431cb0ef41Sopenharmony_ci while (remaining > 0) { 4441cb0ef41Sopenharmony_ci const writeSize = MathMin(kWriteFileMaxChunkSize, remaining); 4451cb0ef41Sopenharmony_ci const { bytesWritten } = await write( 4461cb0ef41Sopenharmony_ci filehandle, toWrite, toWrite.byteLength - remaining, writeSize); 4471cb0ef41Sopenharmony_ci remaining -= bytesWritten; 4481cb0ef41Sopenharmony_ci checkAborted(signal); 4491cb0ef41Sopenharmony_ci } 4501cb0ef41Sopenharmony_ci } 4511cb0ef41Sopenharmony_ci return; 4521cb0ef41Sopenharmony_ci } 4531cb0ef41Sopenharmony_ci data = new Uint8Array(data.buffer, data.byteOffset, data.byteLength); 4541cb0ef41Sopenharmony_ci let remaining = data.byteLength; 4551cb0ef41Sopenharmony_ci if (remaining === 0) return; 4561cb0ef41Sopenharmony_ci do { 4571cb0ef41Sopenharmony_ci checkAborted(signal); 4581cb0ef41Sopenharmony_ci const { bytesWritten } = 4591cb0ef41Sopenharmony_ci await write(filehandle, data, 0, 4601cb0ef41Sopenharmony_ci MathMin(kWriteFileMaxChunkSize, data.byteLength)); 4611cb0ef41Sopenharmony_ci remaining -= bytesWritten; 4621cb0ef41Sopenharmony_ci data = new Uint8Array( 4631cb0ef41Sopenharmony_ci data.buffer, 4641cb0ef41Sopenharmony_ci data.byteOffset + bytesWritten, 4651cb0ef41Sopenharmony_ci data.byteLength - bytesWritten, 4661cb0ef41Sopenharmony_ci ); 4671cb0ef41Sopenharmony_ci } while (remaining > 0); 4681cb0ef41Sopenharmony_ci} 4691cb0ef41Sopenharmony_ci 4701cb0ef41Sopenharmony_ciasync function readFileHandle(filehandle, options) { 4711cb0ef41Sopenharmony_ci const signal = options?.signal; 4721cb0ef41Sopenharmony_ci const encoding = options?.encoding; 4731cb0ef41Sopenharmony_ci const decoder = encoding && new StringDecoder(encoding); 4741cb0ef41Sopenharmony_ci 4751cb0ef41Sopenharmony_ci checkAborted(signal); 4761cb0ef41Sopenharmony_ci 4771cb0ef41Sopenharmony_ci const statFields = await binding.fstat(filehandle.fd, false, kUsePromises); 4781cb0ef41Sopenharmony_ci 4791cb0ef41Sopenharmony_ci checkAborted(signal); 4801cb0ef41Sopenharmony_ci 4811cb0ef41Sopenharmony_ci let size = 0; 4821cb0ef41Sopenharmony_ci let length = 0; 4831cb0ef41Sopenharmony_ci if ((statFields[1/* mode */] & S_IFMT) === S_IFREG) { 4841cb0ef41Sopenharmony_ci size = statFields[8/* size */]; 4851cb0ef41Sopenharmony_ci length = encoding ? MathMin(size, kReadFileBufferLength) : size; 4861cb0ef41Sopenharmony_ci } 4871cb0ef41Sopenharmony_ci if (length === 0) { 4881cb0ef41Sopenharmony_ci length = kReadFileUnknownBufferLength; 4891cb0ef41Sopenharmony_ci } 4901cb0ef41Sopenharmony_ci 4911cb0ef41Sopenharmony_ci if (size > kIoMaxLength) 4921cb0ef41Sopenharmony_ci throw new ERR_FS_FILE_TOO_LARGE(size); 4931cb0ef41Sopenharmony_ci 4941cb0ef41Sopenharmony_ci let totalRead = 0; 4951cb0ef41Sopenharmony_ci let buffer = Buffer.allocUnsafeSlow(length); 4961cb0ef41Sopenharmony_ci let result = ''; 4971cb0ef41Sopenharmony_ci let offset = 0; 4981cb0ef41Sopenharmony_ci let buffers; 4991cb0ef41Sopenharmony_ci const chunkedRead = length > kReadFileBufferLength; 5001cb0ef41Sopenharmony_ci 5011cb0ef41Sopenharmony_ci while (true) { 5021cb0ef41Sopenharmony_ci checkAborted(signal); 5031cb0ef41Sopenharmony_ci 5041cb0ef41Sopenharmony_ci if (chunkedRead) { 5051cb0ef41Sopenharmony_ci length = MathMin(size - totalRead, kReadFileBufferLength); 5061cb0ef41Sopenharmony_ci } 5071cb0ef41Sopenharmony_ci 5081cb0ef41Sopenharmony_ci const bytesRead = (await binding.read(filehandle.fd, buffer, offset, 5091cb0ef41Sopenharmony_ci length, -1, kUsePromises)) ?? 0; 5101cb0ef41Sopenharmony_ci totalRead += bytesRead; 5111cb0ef41Sopenharmony_ci 5121cb0ef41Sopenharmony_ci if (bytesRead === 0 || 5131cb0ef41Sopenharmony_ci totalRead === size || 5141cb0ef41Sopenharmony_ci (bytesRead !== buffer.length && !chunkedRead)) { 5151cb0ef41Sopenharmony_ci const singleRead = bytesRead === totalRead; 5161cb0ef41Sopenharmony_ci 5171cb0ef41Sopenharmony_ci const bytesToCheck = chunkedRead ? totalRead : bytesRead; 5181cb0ef41Sopenharmony_ci 5191cb0ef41Sopenharmony_ci if (bytesToCheck !== buffer.length) { 5201cb0ef41Sopenharmony_ci buffer = buffer.subarray(0, bytesToCheck); 5211cb0ef41Sopenharmony_ci } 5221cb0ef41Sopenharmony_ci 5231cb0ef41Sopenharmony_ci if (!encoding) { 5241cb0ef41Sopenharmony_ci if (size === 0 && !singleRead) { 5251cb0ef41Sopenharmony_ci ArrayPrototypePush(buffers, buffer); 5261cb0ef41Sopenharmony_ci return Buffer.concat(buffers, totalRead); 5271cb0ef41Sopenharmony_ci } 5281cb0ef41Sopenharmony_ci return buffer; 5291cb0ef41Sopenharmony_ci } 5301cb0ef41Sopenharmony_ci 5311cb0ef41Sopenharmony_ci if (singleRead) { 5321cb0ef41Sopenharmony_ci return buffer.toString(encoding); 5331cb0ef41Sopenharmony_ci } 5341cb0ef41Sopenharmony_ci result += decoder.end(buffer); 5351cb0ef41Sopenharmony_ci return result; 5361cb0ef41Sopenharmony_ci } 5371cb0ef41Sopenharmony_ci 5381cb0ef41Sopenharmony_ci if (encoding) { 5391cb0ef41Sopenharmony_ci result += decoder.write(buffer); 5401cb0ef41Sopenharmony_ci } else if (size !== 0) { 5411cb0ef41Sopenharmony_ci offset = totalRead; 5421cb0ef41Sopenharmony_ci } else { 5431cb0ef41Sopenharmony_ci buffers ??= []; 5441cb0ef41Sopenharmony_ci // Unknown file size requires chunks. 5451cb0ef41Sopenharmony_ci ArrayPrototypePush(buffers, buffer); 5461cb0ef41Sopenharmony_ci buffer = Buffer.allocUnsafeSlow(kReadFileUnknownBufferLength); 5471cb0ef41Sopenharmony_ci } 5481cb0ef41Sopenharmony_ci } 5491cb0ef41Sopenharmony_ci} 5501cb0ef41Sopenharmony_ci 5511cb0ef41Sopenharmony_ci// All of the functions are defined as async in order to ensure that errors 5521cb0ef41Sopenharmony_ci// thrown cause promise rejections rather than being thrown synchronously. 5531cb0ef41Sopenharmony_ciasync function access(path, mode = F_OK) { 5541cb0ef41Sopenharmony_ci path = getValidatedPath(path); 5551cb0ef41Sopenharmony_ci 5561cb0ef41Sopenharmony_ci mode = getValidMode(mode, 'access'); 5571cb0ef41Sopenharmony_ci return binding.access(pathModule.toNamespacedPath(path), mode, 5581cb0ef41Sopenharmony_ci kUsePromises); 5591cb0ef41Sopenharmony_ci} 5601cb0ef41Sopenharmony_ci 5611cb0ef41Sopenharmony_ciasync function cp(src, dest, options) { 5621cb0ef41Sopenharmony_ci options = validateCpOptions(options); 5631cb0ef41Sopenharmony_ci src = pathModule.toNamespacedPath(getValidatedPath(src, 'src')); 5641cb0ef41Sopenharmony_ci dest = pathModule.toNamespacedPath(getValidatedPath(dest, 'dest')); 5651cb0ef41Sopenharmony_ci return lazyLoadCpPromises()(src, dest, options); 5661cb0ef41Sopenharmony_ci} 5671cb0ef41Sopenharmony_ci 5681cb0ef41Sopenharmony_ciasync function copyFile(src, dest, mode) { 5691cb0ef41Sopenharmony_ci src = getValidatedPath(src, 'src'); 5701cb0ef41Sopenharmony_ci dest = getValidatedPath(dest, 'dest'); 5711cb0ef41Sopenharmony_ci mode = getValidMode(mode, 'copyFile'); 5721cb0ef41Sopenharmony_ci return binding.copyFile(pathModule.toNamespacedPath(src), 5731cb0ef41Sopenharmony_ci pathModule.toNamespacedPath(dest), 5741cb0ef41Sopenharmony_ci mode, 5751cb0ef41Sopenharmony_ci kUsePromises); 5761cb0ef41Sopenharmony_ci} 5771cb0ef41Sopenharmony_ci 5781cb0ef41Sopenharmony_ci// Note that unlike fs.open() which uses numeric file descriptors, 5791cb0ef41Sopenharmony_ci// fsPromises.open() uses the fs.FileHandle class. 5801cb0ef41Sopenharmony_ciasync function open(path, flags, mode) { 5811cb0ef41Sopenharmony_ci path = getValidatedPath(path); 5821cb0ef41Sopenharmony_ci const flagsNumber = stringToFlags(flags); 5831cb0ef41Sopenharmony_ci mode = parseFileMode(mode, 'mode', 0o666); 5841cb0ef41Sopenharmony_ci return new FileHandle( 5851cb0ef41Sopenharmony_ci await binding.openFileHandle(pathModule.toNamespacedPath(path), 5861cb0ef41Sopenharmony_ci flagsNumber, mode, kUsePromises)); 5871cb0ef41Sopenharmony_ci} 5881cb0ef41Sopenharmony_ci 5891cb0ef41Sopenharmony_ciasync function read(handle, bufferOrParams, offset, length, position) { 5901cb0ef41Sopenharmony_ci let buffer = bufferOrParams; 5911cb0ef41Sopenharmony_ci if (!isArrayBufferView(buffer)) { 5921cb0ef41Sopenharmony_ci // This is fh.read(params) 5931cb0ef41Sopenharmony_ci ({ 5941cb0ef41Sopenharmony_ci buffer = Buffer.alloc(16384), 5951cb0ef41Sopenharmony_ci offset = 0, 5961cb0ef41Sopenharmony_ci length = buffer.byteLength - offset, 5971cb0ef41Sopenharmony_ci position = null, 5981cb0ef41Sopenharmony_ci } = bufferOrParams ?? kEmptyObject); 5991cb0ef41Sopenharmony_ci 6001cb0ef41Sopenharmony_ci validateBuffer(buffer); 6011cb0ef41Sopenharmony_ci } 6021cb0ef41Sopenharmony_ci 6031cb0ef41Sopenharmony_ci if (offset !== null && typeof offset === 'object') { 6041cb0ef41Sopenharmony_ci // This is fh.read(buffer, options) 6051cb0ef41Sopenharmony_ci ({ 6061cb0ef41Sopenharmony_ci offset = 0, 6071cb0ef41Sopenharmony_ci length = buffer.byteLength - offset, 6081cb0ef41Sopenharmony_ci position = null, 6091cb0ef41Sopenharmony_ci } = offset); 6101cb0ef41Sopenharmony_ci } 6111cb0ef41Sopenharmony_ci 6121cb0ef41Sopenharmony_ci if (offset == null) { 6131cb0ef41Sopenharmony_ci offset = 0; 6141cb0ef41Sopenharmony_ci } else { 6151cb0ef41Sopenharmony_ci validateInteger(offset, 'offset', 0); 6161cb0ef41Sopenharmony_ci } 6171cb0ef41Sopenharmony_ci 6181cb0ef41Sopenharmony_ci length |= 0; 6191cb0ef41Sopenharmony_ci 6201cb0ef41Sopenharmony_ci if (length === 0) 6211cb0ef41Sopenharmony_ci return { bytesRead: length, buffer }; 6221cb0ef41Sopenharmony_ci 6231cb0ef41Sopenharmony_ci if (buffer.byteLength === 0) { 6241cb0ef41Sopenharmony_ci throw new ERR_INVALID_ARG_VALUE('buffer', buffer, 6251cb0ef41Sopenharmony_ci 'is empty and cannot be written'); 6261cb0ef41Sopenharmony_ci } 6271cb0ef41Sopenharmony_ci 6281cb0ef41Sopenharmony_ci validateOffsetLengthRead(offset, length, buffer.byteLength); 6291cb0ef41Sopenharmony_ci 6301cb0ef41Sopenharmony_ci if (!NumberIsSafeInteger(position)) 6311cb0ef41Sopenharmony_ci position = -1; 6321cb0ef41Sopenharmony_ci 6331cb0ef41Sopenharmony_ci const bytesRead = (await binding.read(handle.fd, buffer, offset, length, 6341cb0ef41Sopenharmony_ci position, kUsePromises)) || 0; 6351cb0ef41Sopenharmony_ci 6361cb0ef41Sopenharmony_ci return { bytesRead, buffer }; 6371cb0ef41Sopenharmony_ci} 6381cb0ef41Sopenharmony_ci 6391cb0ef41Sopenharmony_ciasync function readv(handle, buffers, position) { 6401cb0ef41Sopenharmony_ci validateBufferArray(buffers); 6411cb0ef41Sopenharmony_ci 6421cb0ef41Sopenharmony_ci if (typeof position !== 'number') 6431cb0ef41Sopenharmony_ci position = null; 6441cb0ef41Sopenharmony_ci 6451cb0ef41Sopenharmony_ci const bytesRead = (await binding.readBuffers(handle.fd, buffers, position, 6461cb0ef41Sopenharmony_ci kUsePromises)) || 0; 6471cb0ef41Sopenharmony_ci return { bytesRead, buffers }; 6481cb0ef41Sopenharmony_ci} 6491cb0ef41Sopenharmony_ci 6501cb0ef41Sopenharmony_ciasync function write(handle, buffer, offsetOrOptions, length, position) { 6511cb0ef41Sopenharmony_ci if (buffer?.byteLength === 0) 6521cb0ef41Sopenharmony_ci return { bytesWritten: 0, buffer }; 6531cb0ef41Sopenharmony_ci 6541cb0ef41Sopenharmony_ci let offset = offsetOrOptions; 6551cb0ef41Sopenharmony_ci if (isArrayBufferView(buffer)) { 6561cb0ef41Sopenharmony_ci if (typeof offset === 'object') { 6571cb0ef41Sopenharmony_ci ({ 6581cb0ef41Sopenharmony_ci offset = 0, 6591cb0ef41Sopenharmony_ci length = buffer.byteLength - offset, 6601cb0ef41Sopenharmony_ci position = null, 6611cb0ef41Sopenharmony_ci } = offsetOrOptions ?? kEmptyObject); 6621cb0ef41Sopenharmony_ci } 6631cb0ef41Sopenharmony_ci 6641cb0ef41Sopenharmony_ci if (offset == null) { 6651cb0ef41Sopenharmony_ci offset = 0; 6661cb0ef41Sopenharmony_ci } else { 6671cb0ef41Sopenharmony_ci validateInteger(offset, 'offset', 0); 6681cb0ef41Sopenharmony_ci } 6691cb0ef41Sopenharmony_ci if (typeof length !== 'number') 6701cb0ef41Sopenharmony_ci length = buffer.byteLength - offset; 6711cb0ef41Sopenharmony_ci if (typeof position !== 'number') 6721cb0ef41Sopenharmony_ci position = null; 6731cb0ef41Sopenharmony_ci validateOffsetLengthWrite(offset, length, buffer.byteLength); 6741cb0ef41Sopenharmony_ci const bytesWritten = 6751cb0ef41Sopenharmony_ci (await binding.writeBuffer(handle.fd, buffer, offset, 6761cb0ef41Sopenharmony_ci length, position, kUsePromises)) || 0; 6771cb0ef41Sopenharmony_ci return { bytesWritten, buffer }; 6781cb0ef41Sopenharmony_ci } 6791cb0ef41Sopenharmony_ci 6801cb0ef41Sopenharmony_ci validatePrimitiveStringAfterArrayBufferView(buffer, 'buffer'); 6811cb0ef41Sopenharmony_ci validateEncoding(buffer, length); 6821cb0ef41Sopenharmony_ci const bytesWritten = (await binding.writeString(handle.fd, buffer, offset, 6831cb0ef41Sopenharmony_ci length, kUsePromises)) || 0; 6841cb0ef41Sopenharmony_ci return { bytesWritten, buffer }; 6851cb0ef41Sopenharmony_ci} 6861cb0ef41Sopenharmony_ci 6871cb0ef41Sopenharmony_ciasync function writev(handle, buffers, position) { 6881cb0ef41Sopenharmony_ci validateBufferArray(buffers); 6891cb0ef41Sopenharmony_ci 6901cb0ef41Sopenharmony_ci if (typeof position !== 'number') 6911cb0ef41Sopenharmony_ci position = null; 6921cb0ef41Sopenharmony_ci 6931cb0ef41Sopenharmony_ci if (buffers.length === 0) { 6941cb0ef41Sopenharmony_ci return { bytesWritten: 0, buffers }; 6951cb0ef41Sopenharmony_ci } 6961cb0ef41Sopenharmony_ci 6971cb0ef41Sopenharmony_ci const bytesWritten = (await binding.writeBuffers(handle.fd, buffers, position, 6981cb0ef41Sopenharmony_ci kUsePromises)) || 0; 6991cb0ef41Sopenharmony_ci return { bytesWritten, buffers }; 7001cb0ef41Sopenharmony_ci} 7011cb0ef41Sopenharmony_ci 7021cb0ef41Sopenharmony_ciasync function rename(oldPath, newPath) { 7031cb0ef41Sopenharmony_ci oldPath = getValidatedPath(oldPath, 'oldPath'); 7041cb0ef41Sopenharmony_ci newPath = getValidatedPath(newPath, 'newPath'); 7051cb0ef41Sopenharmony_ci return binding.rename(pathModule.toNamespacedPath(oldPath), 7061cb0ef41Sopenharmony_ci pathModule.toNamespacedPath(newPath), 7071cb0ef41Sopenharmony_ci kUsePromises); 7081cb0ef41Sopenharmony_ci} 7091cb0ef41Sopenharmony_ci 7101cb0ef41Sopenharmony_ciasync function truncate(path, len = 0) { 7111cb0ef41Sopenharmony_ci const fd = await open(path, 'r+'); 7121cb0ef41Sopenharmony_ci return handleFdClose(ftruncate(fd, len), fd.close); 7131cb0ef41Sopenharmony_ci} 7141cb0ef41Sopenharmony_ci 7151cb0ef41Sopenharmony_ciasync function ftruncate(handle, len = 0) { 7161cb0ef41Sopenharmony_ci validateInteger(len, 'len'); 7171cb0ef41Sopenharmony_ci len = MathMax(0, len); 7181cb0ef41Sopenharmony_ci return binding.ftruncate(handle.fd, len, kUsePromises); 7191cb0ef41Sopenharmony_ci} 7201cb0ef41Sopenharmony_ci 7211cb0ef41Sopenharmony_ciasync function rm(path, options) { 7221cb0ef41Sopenharmony_ci path = pathModule.toNamespacedPath(getValidatedPath(path)); 7231cb0ef41Sopenharmony_ci options = await validateRmOptionsPromise(path, options, false); 7241cb0ef41Sopenharmony_ci return rimrafPromises(path, options); 7251cb0ef41Sopenharmony_ci} 7261cb0ef41Sopenharmony_ci 7271cb0ef41Sopenharmony_ciasync function rmdir(path, options) { 7281cb0ef41Sopenharmony_ci path = pathModule.toNamespacedPath(getValidatedPath(path)); 7291cb0ef41Sopenharmony_ci options = validateRmdirOptions(options); 7301cb0ef41Sopenharmony_ci 7311cb0ef41Sopenharmony_ci if (options.recursive) { 7321cb0ef41Sopenharmony_ci emitRecursiveRmdirWarning(); 7331cb0ef41Sopenharmony_ci const stats = await stat(path); 7341cb0ef41Sopenharmony_ci if (stats.isDirectory()) { 7351cb0ef41Sopenharmony_ci return rimrafPromises(path, options); 7361cb0ef41Sopenharmony_ci } 7371cb0ef41Sopenharmony_ci } 7381cb0ef41Sopenharmony_ci 7391cb0ef41Sopenharmony_ci return binding.rmdir(path, kUsePromises); 7401cb0ef41Sopenharmony_ci} 7411cb0ef41Sopenharmony_ci 7421cb0ef41Sopenharmony_ciasync function fdatasync(handle) { 7431cb0ef41Sopenharmony_ci return binding.fdatasync(handle.fd, kUsePromises); 7441cb0ef41Sopenharmony_ci} 7451cb0ef41Sopenharmony_ci 7461cb0ef41Sopenharmony_ciasync function fsync(handle) { 7471cb0ef41Sopenharmony_ci return binding.fsync(handle.fd, kUsePromises); 7481cb0ef41Sopenharmony_ci} 7491cb0ef41Sopenharmony_ci 7501cb0ef41Sopenharmony_ciasync function mkdir(path, options) { 7511cb0ef41Sopenharmony_ci if (typeof options === 'number' || typeof options === 'string') { 7521cb0ef41Sopenharmony_ci options = { mode: options }; 7531cb0ef41Sopenharmony_ci } 7541cb0ef41Sopenharmony_ci const { 7551cb0ef41Sopenharmony_ci recursive = false, 7561cb0ef41Sopenharmony_ci mode = 0o777, 7571cb0ef41Sopenharmony_ci } = options || kEmptyObject; 7581cb0ef41Sopenharmony_ci path = getValidatedPath(path); 7591cb0ef41Sopenharmony_ci validateBoolean(recursive, 'options.recursive'); 7601cb0ef41Sopenharmony_ci 7611cb0ef41Sopenharmony_ci return binding.mkdir(pathModule.toNamespacedPath(path), 7621cb0ef41Sopenharmony_ci parseFileMode(mode, 'mode', 0o777), recursive, 7631cb0ef41Sopenharmony_ci kUsePromises); 7641cb0ef41Sopenharmony_ci} 7651cb0ef41Sopenharmony_ci 7661cb0ef41Sopenharmony_ciasync function readdirRecursive(originalPath, options) { 7671cb0ef41Sopenharmony_ci const result = []; 7681cb0ef41Sopenharmony_ci const queue = [ 7691cb0ef41Sopenharmony_ci [ 7701cb0ef41Sopenharmony_ci originalPath, 7711cb0ef41Sopenharmony_ci await binding.readdir( 7721cb0ef41Sopenharmony_ci pathModule.toNamespacedPath(originalPath), 7731cb0ef41Sopenharmony_ci options.encoding, 7741cb0ef41Sopenharmony_ci !!options.withFileTypes, 7751cb0ef41Sopenharmony_ci kUsePromises, 7761cb0ef41Sopenharmony_ci ), 7771cb0ef41Sopenharmony_ci ], 7781cb0ef41Sopenharmony_ci ]; 7791cb0ef41Sopenharmony_ci 7801cb0ef41Sopenharmony_ci 7811cb0ef41Sopenharmony_ci if (options.withFileTypes) { 7821cb0ef41Sopenharmony_ci while (queue.length > 0) { 7831cb0ef41Sopenharmony_ci // If we want to implement BFS make this a `shift` call instead of `pop` 7841cb0ef41Sopenharmony_ci const { 0: path, 1: readdir } = ArrayPrototypePop(queue); 7851cb0ef41Sopenharmony_ci for (const dirent of getDirents(path, readdir)) { 7861cb0ef41Sopenharmony_ci ArrayPrototypePush(result, dirent); 7871cb0ef41Sopenharmony_ci if (dirent.isDirectory()) { 7881cb0ef41Sopenharmony_ci const direntPath = pathModule.join(path, dirent.name); 7891cb0ef41Sopenharmony_ci ArrayPrototypePush(queue, [ 7901cb0ef41Sopenharmony_ci direntPath, 7911cb0ef41Sopenharmony_ci await binding.readdir( 7921cb0ef41Sopenharmony_ci direntPath, 7931cb0ef41Sopenharmony_ci options.encoding, 7941cb0ef41Sopenharmony_ci true, 7951cb0ef41Sopenharmony_ci kUsePromises, 7961cb0ef41Sopenharmony_ci ), 7971cb0ef41Sopenharmony_ci ]); 7981cb0ef41Sopenharmony_ci } 7991cb0ef41Sopenharmony_ci } 8001cb0ef41Sopenharmony_ci } 8011cb0ef41Sopenharmony_ci } else { 8021cb0ef41Sopenharmony_ci while (queue.length > 0) { 8031cb0ef41Sopenharmony_ci const { 0: path, 1: readdir } = ArrayPrototypePop(queue); 8041cb0ef41Sopenharmony_ci for (const ent of readdir) { 8051cb0ef41Sopenharmony_ci const direntPath = pathModule.join(path, ent); 8061cb0ef41Sopenharmony_ci const stat = binding.internalModuleStat(direntPath); 8071cb0ef41Sopenharmony_ci ArrayPrototypePush( 8081cb0ef41Sopenharmony_ci result, 8091cb0ef41Sopenharmony_ci pathModule.relative(originalPath, direntPath), 8101cb0ef41Sopenharmony_ci ); 8111cb0ef41Sopenharmony_ci if (stat === 1) { 8121cb0ef41Sopenharmony_ci ArrayPrototypePush(queue, [ 8131cb0ef41Sopenharmony_ci direntPath, 8141cb0ef41Sopenharmony_ci await binding.readdir( 8151cb0ef41Sopenharmony_ci pathModule.toNamespacedPath(direntPath), 8161cb0ef41Sopenharmony_ci options.encoding, 8171cb0ef41Sopenharmony_ci false, 8181cb0ef41Sopenharmony_ci kUsePromises, 8191cb0ef41Sopenharmony_ci ), 8201cb0ef41Sopenharmony_ci ]); 8211cb0ef41Sopenharmony_ci } 8221cb0ef41Sopenharmony_ci } 8231cb0ef41Sopenharmony_ci } 8241cb0ef41Sopenharmony_ci } 8251cb0ef41Sopenharmony_ci 8261cb0ef41Sopenharmony_ci return result; 8271cb0ef41Sopenharmony_ci} 8281cb0ef41Sopenharmony_ci 8291cb0ef41Sopenharmony_ciasync function readdir(path, options) { 8301cb0ef41Sopenharmony_ci options = getOptions(options); 8311cb0ef41Sopenharmony_ci path = getValidatedPath(path); 8321cb0ef41Sopenharmony_ci if (options.recursive) { 8331cb0ef41Sopenharmony_ci return readdirRecursive(path, options); 8341cb0ef41Sopenharmony_ci } 8351cb0ef41Sopenharmony_ci const result = await binding.readdir( 8361cb0ef41Sopenharmony_ci pathModule.toNamespacedPath(path), 8371cb0ef41Sopenharmony_ci options.encoding, 8381cb0ef41Sopenharmony_ci !!options.withFileTypes, 8391cb0ef41Sopenharmony_ci kUsePromises, 8401cb0ef41Sopenharmony_ci ); 8411cb0ef41Sopenharmony_ci return options.withFileTypes ? 8421cb0ef41Sopenharmony_ci getDirectoryEntriesPromise(path, result) : 8431cb0ef41Sopenharmony_ci result; 8441cb0ef41Sopenharmony_ci} 8451cb0ef41Sopenharmony_ci 8461cb0ef41Sopenharmony_ciasync function readlink(path, options) { 8471cb0ef41Sopenharmony_ci options = getOptions(options); 8481cb0ef41Sopenharmony_ci path = getValidatedPath(path, 'oldPath'); 8491cb0ef41Sopenharmony_ci return binding.readlink(pathModule.toNamespacedPath(path), 8501cb0ef41Sopenharmony_ci options.encoding, kUsePromises); 8511cb0ef41Sopenharmony_ci} 8521cb0ef41Sopenharmony_ci 8531cb0ef41Sopenharmony_ciasync function symlink(target, path, type_) { 8541cb0ef41Sopenharmony_ci const type = (typeof type_ === 'string' ? type_ : null); 8551cb0ef41Sopenharmony_ci target = getValidatedPath(target, 'target'); 8561cb0ef41Sopenharmony_ci path = getValidatedPath(path); 8571cb0ef41Sopenharmony_ci return binding.symlink(preprocessSymlinkDestination(target, type, path), 8581cb0ef41Sopenharmony_ci pathModule.toNamespacedPath(path), 8591cb0ef41Sopenharmony_ci stringToSymlinkType(type), 8601cb0ef41Sopenharmony_ci kUsePromises); 8611cb0ef41Sopenharmony_ci} 8621cb0ef41Sopenharmony_ci 8631cb0ef41Sopenharmony_ciasync function fstat(handle, options = { bigint: false }) { 8641cb0ef41Sopenharmony_ci const result = await binding.fstat(handle.fd, options.bigint, kUsePromises); 8651cb0ef41Sopenharmony_ci return getStatsFromBinding(result); 8661cb0ef41Sopenharmony_ci} 8671cb0ef41Sopenharmony_ci 8681cb0ef41Sopenharmony_ciasync function lstat(path, options = { bigint: false }) { 8691cb0ef41Sopenharmony_ci path = getValidatedPath(path); 8701cb0ef41Sopenharmony_ci const result = await binding.lstat(pathModule.toNamespacedPath(path), 8711cb0ef41Sopenharmony_ci options.bigint, kUsePromises); 8721cb0ef41Sopenharmony_ci return getStatsFromBinding(result); 8731cb0ef41Sopenharmony_ci} 8741cb0ef41Sopenharmony_ci 8751cb0ef41Sopenharmony_ciasync function stat(path, options = { bigint: false }) { 8761cb0ef41Sopenharmony_ci path = getValidatedPath(path); 8771cb0ef41Sopenharmony_ci const result = await binding.stat(pathModule.toNamespacedPath(path), 8781cb0ef41Sopenharmony_ci options.bigint, kUsePromises); 8791cb0ef41Sopenharmony_ci return getStatsFromBinding(result); 8801cb0ef41Sopenharmony_ci} 8811cb0ef41Sopenharmony_ci 8821cb0ef41Sopenharmony_ciasync function statfs(path, options = { bigint: false }) { 8831cb0ef41Sopenharmony_ci path = getValidatedPath(path); 8841cb0ef41Sopenharmony_ci const result = await binding.statfs(pathModule.toNamespacedPath(path), 8851cb0ef41Sopenharmony_ci options.bigint, kUsePromises); 8861cb0ef41Sopenharmony_ci return getStatFsFromBinding(result); 8871cb0ef41Sopenharmony_ci} 8881cb0ef41Sopenharmony_ci 8891cb0ef41Sopenharmony_ciasync function link(existingPath, newPath) { 8901cb0ef41Sopenharmony_ci existingPath = getValidatedPath(existingPath, 'existingPath'); 8911cb0ef41Sopenharmony_ci newPath = getValidatedPath(newPath, 'newPath'); 8921cb0ef41Sopenharmony_ci return binding.link(pathModule.toNamespacedPath(existingPath), 8931cb0ef41Sopenharmony_ci pathModule.toNamespacedPath(newPath), 8941cb0ef41Sopenharmony_ci kUsePromises); 8951cb0ef41Sopenharmony_ci} 8961cb0ef41Sopenharmony_ci 8971cb0ef41Sopenharmony_ciasync function unlink(path) { 8981cb0ef41Sopenharmony_ci path = getValidatedPath(path); 8991cb0ef41Sopenharmony_ci return binding.unlink(pathModule.toNamespacedPath(path), kUsePromises); 9001cb0ef41Sopenharmony_ci} 9011cb0ef41Sopenharmony_ci 9021cb0ef41Sopenharmony_ciasync function fchmod(handle, mode) { 9031cb0ef41Sopenharmony_ci mode = parseFileMode(mode, 'mode'); 9041cb0ef41Sopenharmony_ci return binding.fchmod(handle.fd, mode, kUsePromises); 9051cb0ef41Sopenharmony_ci} 9061cb0ef41Sopenharmony_ci 9071cb0ef41Sopenharmony_ciasync function chmod(path, mode) { 9081cb0ef41Sopenharmony_ci path = getValidatedPath(path); 9091cb0ef41Sopenharmony_ci mode = parseFileMode(mode, 'mode'); 9101cb0ef41Sopenharmony_ci return binding.chmod(pathModule.toNamespacedPath(path), mode, kUsePromises); 9111cb0ef41Sopenharmony_ci} 9121cb0ef41Sopenharmony_ci 9131cb0ef41Sopenharmony_ciasync function lchmod(path, mode) { 9141cb0ef41Sopenharmony_ci if (O_SYMLINK === undefined) 9151cb0ef41Sopenharmony_ci throw new ERR_METHOD_NOT_IMPLEMENTED('lchmod()'); 9161cb0ef41Sopenharmony_ci 9171cb0ef41Sopenharmony_ci const fd = await open(path, O_WRONLY | O_SYMLINK); 9181cb0ef41Sopenharmony_ci return handleFdClose(fchmod(fd, mode), fd.close); 9191cb0ef41Sopenharmony_ci} 9201cb0ef41Sopenharmony_ci 9211cb0ef41Sopenharmony_ciasync function lchown(path, uid, gid) { 9221cb0ef41Sopenharmony_ci path = getValidatedPath(path); 9231cb0ef41Sopenharmony_ci validateInteger(uid, 'uid', -1, kMaxUserId); 9241cb0ef41Sopenharmony_ci validateInteger(gid, 'gid', -1, kMaxUserId); 9251cb0ef41Sopenharmony_ci return binding.lchown(pathModule.toNamespacedPath(path), 9261cb0ef41Sopenharmony_ci uid, gid, kUsePromises); 9271cb0ef41Sopenharmony_ci} 9281cb0ef41Sopenharmony_ci 9291cb0ef41Sopenharmony_ciasync function fchown(handle, uid, gid) { 9301cb0ef41Sopenharmony_ci validateInteger(uid, 'uid', -1, kMaxUserId); 9311cb0ef41Sopenharmony_ci validateInteger(gid, 'gid', -1, kMaxUserId); 9321cb0ef41Sopenharmony_ci return binding.fchown(handle.fd, uid, gid, kUsePromises); 9331cb0ef41Sopenharmony_ci} 9341cb0ef41Sopenharmony_ci 9351cb0ef41Sopenharmony_ciasync function chown(path, uid, gid) { 9361cb0ef41Sopenharmony_ci path = getValidatedPath(path); 9371cb0ef41Sopenharmony_ci validateInteger(uid, 'uid', -1, kMaxUserId); 9381cb0ef41Sopenharmony_ci validateInteger(gid, 'gid', -1, kMaxUserId); 9391cb0ef41Sopenharmony_ci return binding.chown(pathModule.toNamespacedPath(path), 9401cb0ef41Sopenharmony_ci uid, gid, kUsePromises); 9411cb0ef41Sopenharmony_ci} 9421cb0ef41Sopenharmony_ci 9431cb0ef41Sopenharmony_ciasync function utimes(path, atime, mtime) { 9441cb0ef41Sopenharmony_ci path = getValidatedPath(path); 9451cb0ef41Sopenharmony_ci return binding.utimes(pathModule.toNamespacedPath(path), 9461cb0ef41Sopenharmony_ci toUnixTimestamp(atime), 9471cb0ef41Sopenharmony_ci toUnixTimestamp(mtime), 9481cb0ef41Sopenharmony_ci kUsePromises); 9491cb0ef41Sopenharmony_ci} 9501cb0ef41Sopenharmony_ci 9511cb0ef41Sopenharmony_ciasync function futimes(handle, atime, mtime) { 9521cb0ef41Sopenharmony_ci atime = toUnixTimestamp(atime, 'atime'); 9531cb0ef41Sopenharmony_ci mtime = toUnixTimestamp(mtime, 'mtime'); 9541cb0ef41Sopenharmony_ci return binding.futimes(handle.fd, atime, mtime, kUsePromises); 9551cb0ef41Sopenharmony_ci} 9561cb0ef41Sopenharmony_ci 9571cb0ef41Sopenharmony_ciasync function lutimes(path, atime, mtime) { 9581cb0ef41Sopenharmony_ci path = getValidatedPath(path); 9591cb0ef41Sopenharmony_ci return binding.lutimes(pathModule.toNamespacedPath(path), 9601cb0ef41Sopenharmony_ci toUnixTimestamp(atime), 9611cb0ef41Sopenharmony_ci toUnixTimestamp(mtime), 9621cb0ef41Sopenharmony_ci kUsePromises); 9631cb0ef41Sopenharmony_ci} 9641cb0ef41Sopenharmony_ci 9651cb0ef41Sopenharmony_ciasync function realpath(path, options) { 9661cb0ef41Sopenharmony_ci options = getOptions(options); 9671cb0ef41Sopenharmony_ci path = getValidatedPath(path); 9681cb0ef41Sopenharmony_ci return binding.realpath(path, options.encoding, kUsePromises); 9691cb0ef41Sopenharmony_ci} 9701cb0ef41Sopenharmony_ci 9711cb0ef41Sopenharmony_ciasync function mkdtemp(prefix, options) { 9721cb0ef41Sopenharmony_ci options = getOptions(options); 9731cb0ef41Sopenharmony_ci 9741cb0ef41Sopenharmony_ci prefix = getValidatedPath(prefix, 'prefix'); 9751cb0ef41Sopenharmony_ci warnOnNonPortableTemplate(prefix); 9761cb0ef41Sopenharmony_ci 9771cb0ef41Sopenharmony_ci let path; 9781cb0ef41Sopenharmony_ci if (typeof prefix === 'string') { 9791cb0ef41Sopenharmony_ci path = `${prefix}XXXXXX`; 9801cb0ef41Sopenharmony_ci } else { 9811cb0ef41Sopenharmony_ci path = Buffer.concat([prefix, Buffer.from('XXXXXX')]); 9821cb0ef41Sopenharmony_ci } 9831cb0ef41Sopenharmony_ci 9841cb0ef41Sopenharmony_ci return binding.mkdtemp(path, options.encoding, kUsePromises); 9851cb0ef41Sopenharmony_ci} 9861cb0ef41Sopenharmony_ci 9871cb0ef41Sopenharmony_ciasync function writeFile(path, data, options) { 9881cb0ef41Sopenharmony_ci options = getOptions(options, { encoding: 'utf8', mode: 0o666, flag: 'w' }); 9891cb0ef41Sopenharmony_ci const flag = options.flag || 'w'; 9901cb0ef41Sopenharmony_ci 9911cb0ef41Sopenharmony_ci if (!isArrayBufferView(data) && !isCustomIterable(data)) { 9921cb0ef41Sopenharmony_ci validatePrimitiveStringAfterArrayBufferView(data, 'data'); 9931cb0ef41Sopenharmony_ci data = Buffer.from(data, options.encoding || 'utf8'); 9941cb0ef41Sopenharmony_ci } 9951cb0ef41Sopenharmony_ci 9961cb0ef41Sopenharmony_ci validateAbortSignal(options.signal); 9971cb0ef41Sopenharmony_ci if (path instanceof FileHandle) 9981cb0ef41Sopenharmony_ci return writeFileHandle(path, data, options.signal, options.encoding); 9991cb0ef41Sopenharmony_ci 10001cb0ef41Sopenharmony_ci checkAborted(options.signal); 10011cb0ef41Sopenharmony_ci 10021cb0ef41Sopenharmony_ci const fd = await open(path, flag, options.mode); 10031cb0ef41Sopenharmony_ci return handleFdClose( 10041cb0ef41Sopenharmony_ci writeFileHandle(fd, data, options.signal, options.encoding), fd.close); 10051cb0ef41Sopenharmony_ci} 10061cb0ef41Sopenharmony_ci 10071cb0ef41Sopenharmony_cifunction isCustomIterable(obj) { 10081cb0ef41Sopenharmony_ci return isIterable(obj) && !isArrayBufferView(obj) && typeof obj !== 'string'; 10091cb0ef41Sopenharmony_ci} 10101cb0ef41Sopenharmony_ci 10111cb0ef41Sopenharmony_ciasync function appendFile(path, data, options) { 10121cb0ef41Sopenharmony_ci options = getOptions(options, { encoding: 'utf8', mode: 0o666, flag: 'a' }); 10131cb0ef41Sopenharmony_ci options = copyObject(options); 10141cb0ef41Sopenharmony_ci options.flag = options.flag || 'a'; 10151cb0ef41Sopenharmony_ci return writeFile(path, data, options); 10161cb0ef41Sopenharmony_ci} 10171cb0ef41Sopenharmony_ci 10181cb0ef41Sopenharmony_ciasync function readFile(path, options) { 10191cb0ef41Sopenharmony_ci options = getOptions(options, { flag: 'r' }); 10201cb0ef41Sopenharmony_ci const flag = options.flag || 'r'; 10211cb0ef41Sopenharmony_ci 10221cb0ef41Sopenharmony_ci if (path instanceof FileHandle) 10231cb0ef41Sopenharmony_ci return readFileHandle(path, options); 10241cb0ef41Sopenharmony_ci 10251cb0ef41Sopenharmony_ci checkAborted(options.signal); 10261cb0ef41Sopenharmony_ci 10271cb0ef41Sopenharmony_ci const fd = await open(path, flag, 0o666); 10281cb0ef41Sopenharmony_ci return handleFdClose(readFileHandle(fd, options), fd.close); 10291cb0ef41Sopenharmony_ci} 10301cb0ef41Sopenharmony_ci 10311cb0ef41Sopenharmony_cimodule.exports = { 10321cb0ef41Sopenharmony_ci exports: { 10331cb0ef41Sopenharmony_ci access, 10341cb0ef41Sopenharmony_ci copyFile, 10351cb0ef41Sopenharmony_ci cp, 10361cb0ef41Sopenharmony_ci open, 10371cb0ef41Sopenharmony_ci opendir: promisify(opendir), 10381cb0ef41Sopenharmony_ci rename, 10391cb0ef41Sopenharmony_ci truncate, 10401cb0ef41Sopenharmony_ci rm, 10411cb0ef41Sopenharmony_ci rmdir, 10421cb0ef41Sopenharmony_ci mkdir, 10431cb0ef41Sopenharmony_ci readdir, 10441cb0ef41Sopenharmony_ci readlink, 10451cb0ef41Sopenharmony_ci symlink, 10461cb0ef41Sopenharmony_ci lstat, 10471cb0ef41Sopenharmony_ci stat, 10481cb0ef41Sopenharmony_ci statfs, 10491cb0ef41Sopenharmony_ci link, 10501cb0ef41Sopenharmony_ci unlink, 10511cb0ef41Sopenharmony_ci chmod, 10521cb0ef41Sopenharmony_ci lchmod, 10531cb0ef41Sopenharmony_ci lchown, 10541cb0ef41Sopenharmony_ci chown, 10551cb0ef41Sopenharmony_ci utimes, 10561cb0ef41Sopenharmony_ci lutimes, 10571cb0ef41Sopenharmony_ci realpath, 10581cb0ef41Sopenharmony_ci mkdtemp, 10591cb0ef41Sopenharmony_ci writeFile, 10601cb0ef41Sopenharmony_ci appendFile, 10611cb0ef41Sopenharmony_ci readFile, 10621cb0ef41Sopenharmony_ci watch, 10631cb0ef41Sopenharmony_ci constants, 10641cb0ef41Sopenharmony_ci }, 10651cb0ef41Sopenharmony_ci 10661cb0ef41Sopenharmony_ci FileHandle, 10671cb0ef41Sopenharmony_ci kRef, 10681cb0ef41Sopenharmony_ci kUnref, 10691cb0ef41Sopenharmony_ci}; 1070