volume.js 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.FSWatcher = exports.StatWatcher = exports.Volume = void 0;
  4. exports.pathToSteps = pathToSteps;
  5. exports.dataToStr = dataToStr;
  6. exports.toUnixTimestamp = toUnixTimestamp;
  7. const pathModule = require("path");
  8. const core_1 = require("../core");
  9. const Stats_1 = require("./Stats");
  10. const Dirent_1 = require("./Dirent");
  11. const StatFs_1 = require("./StatFs");
  12. const buffer_1 = require("../internal/buffer");
  13. const queueMicrotask_1 = require("../queueMicrotask");
  14. const setTimeoutUnref_1 = require("../setTimeoutUnref");
  15. const stream_1 = require("stream");
  16. const constants_1 = require("../constants");
  17. const events_1 = require("events");
  18. const encoding_1 = require("../encoding");
  19. const FileHandle_1 = require("./FileHandle");
  20. const util = require("util");
  21. const FsPromises_1 = require("./FsPromises");
  22. const print_1 = require("../print");
  23. const constants_2 = require("./constants");
  24. const options_1 = require("./options");
  25. const util_1 = require("./util");
  26. const Dir_1 = require("./Dir");
  27. const util_2 = require("../core/util");
  28. const resolveCrossPlatform = pathModule.resolve;
  29. const { O_RDONLY, O_WRONLY, O_RDWR, O_CREAT, O_EXCL, O_TRUNC, O_APPEND, O_DIRECTORY, O_SYMLINK, F_OK, COPYFILE_EXCL, COPYFILE_FICLONE_FORCE, } = constants_1.constants;
  30. const { sep, relative, join, dirname, normalize } = pathModule.posix ? pathModule.posix : pathModule;
  31. // ---------------------------------------- Constants
  32. const kMinPoolSpace = 128;
  33. // ---------------------------------------- Utility functions
  34. function pathToSteps(path) {
  35. return (0, util_2.filenameToSteps)((0, util_1.pathToFilename)(path));
  36. }
  37. function dataToStr(data, encoding = encoding_1.ENCODING_UTF8) {
  38. if (buffer_1.Buffer.isBuffer(data))
  39. return data.toString(encoding);
  40. else if (data instanceof Uint8Array)
  41. return (0, buffer_1.bufferFrom)(data).toString(encoding);
  42. else
  43. return String(data);
  44. }
  45. // converts Date or number to a fractional UNIX timestamp
  46. function toUnixTimestamp(time) {
  47. // tslint:disable-next-line triple-equals
  48. if (typeof time === 'string' && +time == time) {
  49. return +time;
  50. }
  51. if (time instanceof Date) {
  52. return time.getTime() / 1000;
  53. }
  54. if (isFinite(time)) {
  55. if (time < 0) {
  56. return Date.now() / 1000;
  57. }
  58. return time;
  59. }
  60. throw new Error('Cannot parse time: ' + time);
  61. }
  62. function validateUid(uid) {
  63. if (typeof uid !== 'number')
  64. throw TypeError(constants_2.ERRSTR.UID);
  65. }
  66. function validateGid(gid) {
  67. if (typeof gid !== 'number')
  68. throw TypeError(constants_2.ERRSTR.GID);
  69. }
  70. const notImplemented = () => {
  71. throw new Error('Not implemented');
  72. };
  73. /**
  74. * `Volume` represents a file system.
  75. */
  76. class Volume {
  77. get promises() {
  78. if (this.promisesApi === null)
  79. throw new Error('Promise is not supported in this environment.');
  80. return this.promisesApi;
  81. }
  82. constructor(_core = new core_1.Superblock()) {
  83. this._core = _core;
  84. this.promisesApi = new FsPromises_1.FsPromises(this, FileHandle_1.FileHandle);
  85. this.openSync = (path, flags, mode = 438 /* MODE.DEFAULT */) => {
  86. // Validate (1) mode; (2) path; (3) flags - in that order.
  87. const modeNum = (0, util_1.modeToNumber)(mode);
  88. const fileName = (0, util_1.pathToFilename)(path);
  89. const flagsNum = (0, util_1.flagsToNumber)(flags);
  90. return this._core.open(fileName, flagsNum, modeNum, !(flagsNum & O_SYMLINK));
  91. };
  92. this.open = (path, flags, a, b) => {
  93. let mode = a;
  94. let callback = b;
  95. if (typeof a === 'function') {
  96. mode = 438 /* MODE.DEFAULT */;
  97. callback = a;
  98. }
  99. mode = mode || 438 /* MODE.DEFAULT */;
  100. const modeNum = (0, util_1.modeToNumber)(mode);
  101. const fileName = (0, util_1.pathToFilename)(path);
  102. const flagsNum = (0, util_1.flagsToNumber)(flags);
  103. this.wrapAsync(this._core.open, [fileName, flagsNum, modeNum, !(flagsNum & O_SYMLINK)], callback);
  104. };
  105. this.closeSync = (fd) => {
  106. this._core.close(fd);
  107. };
  108. this.close = (fd, callback) => {
  109. (0, util_2.validateFd)(fd);
  110. const file = this._core.getFileByFdOrThrow(fd, 'close');
  111. this.wrapAsync(this._core.close, [file.fd], callback);
  112. };
  113. this.readSync = (fd, buffer, offset, length, position) => {
  114. (0, util_2.validateFd)(fd);
  115. return this._core.read(fd, buffer, offset, length, position);
  116. };
  117. this.read = (fd, buffer, offset, length, position, callback) => {
  118. (0, util_1.validateCallback)(callback);
  119. if (length === 0) {
  120. // This `if` branch is from Node.js
  121. return (0, queueMicrotask_1.default)(() => {
  122. if (callback)
  123. callback(null, 0, buffer);
  124. });
  125. }
  126. Promise.resolve().then(() => {
  127. try {
  128. const bytes = this._core.read(fd, buffer, offset, length, position);
  129. callback(null, bytes, buffer);
  130. }
  131. catch (err) {
  132. callback(err);
  133. }
  134. });
  135. };
  136. this.readv = (fd, buffers, a, b) => {
  137. let position = a;
  138. let callback = b;
  139. if (typeof a === 'function')
  140. [position, callback] = [null, a];
  141. (0, util_1.validateCallback)(callback);
  142. Promise.resolve().then(() => {
  143. try {
  144. const bytes = this._core.readv(fd, buffers, position);
  145. callback(null, bytes, buffers);
  146. }
  147. catch (err) {
  148. callback(err);
  149. }
  150. });
  151. };
  152. this.readvSync = (fd, buffers, position) => {
  153. (0, util_2.validateFd)(fd);
  154. return this._core.readv(fd, buffers, position !== null && position !== void 0 ? position : null);
  155. };
  156. this._readfile = (id, flagsNum, encoding) => {
  157. let result;
  158. const isUserFd = typeof id === 'number';
  159. const userOwnsFd = isUserFd && (0, util_2.isFd)(id);
  160. let fd;
  161. if (userOwnsFd)
  162. fd = id;
  163. else {
  164. const filename = (0, util_1.pathToFilename)(id);
  165. // Check if original path had trailing slash (indicates directory intent)
  166. const originalPath = String(id);
  167. const hasTrailingSlash = originalPath.length > 1 && originalPath.endsWith('/');
  168. const link = this._core.getResolvedLinkOrThrow(filename, 'open');
  169. const node = link.getNode();
  170. if (node.isDirectory())
  171. throw (0, util_1.createError)("EISDIR" /* ERROR_CODE.EISDIR */, 'open', link.getPath());
  172. // If path had trailing slash but resolved to a file, throw ENOTDIR
  173. if (hasTrailingSlash && node.isFile()) {
  174. throw (0, util_1.createError)("ENOTDIR" /* ERROR_CODE.ENOTDIR */, 'open', originalPath);
  175. }
  176. fd = this.openSync(id, flagsNum);
  177. }
  178. try {
  179. result = (0, util_1.bufferToEncoding)(this._core.getFileByFdOrThrow(fd).getBuffer(), encoding);
  180. }
  181. finally {
  182. if (!userOwnsFd) {
  183. this.closeSync(fd);
  184. }
  185. }
  186. return result;
  187. };
  188. this.readFileSync = (file, options) => {
  189. const opts = (0, options_1.getReadFileOptions)(options);
  190. const flagsNum = (0, util_1.flagsToNumber)(opts.flag);
  191. return this._readfile(file, flagsNum, opts.encoding);
  192. };
  193. this.readFile = (id, a, b) => {
  194. const [opts, callback] = (0, options_1.optsAndCbGenerator)(options_1.getReadFileOptions)(a, b);
  195. const flagsNum = (0, util_1.flagsToNumber)(opts.flag);
  196. this.wrapAsync(this._readfile, [id, flagsNum, opts.encoding], callback);
  197. };
  198. this.writeSync = (fd, a, b, c, d) => {
  199. const [, buf, offset, length, position] = (0, util_1.getWriteSyncArgs)(fd, a, b, c, d);
  200. return this._write(fd, buf, offset, length, position);
  201. };
  202. this.write = (fd, a, b, c, d, e) => {
  203. const [, asStr, buf, offset, length, position, cb] = (0, util_1.getWriteArgs)(fd, a, b, c, d, e);
  204. Promise.resolve().then(() => {
  205. try {
  206. const bytes = this._write(fd, buf, offset, length, position);
  207. if (!asStr) {
  208. cb(null, bytes, buf);
  209. }
  210. else {
  211. cb(null, bytes, a);
  212. }
  213. }
  214. catch (err) {
  215. cb(err);
  216. }
  217. });
  218. };
  219. this.writev = (fd, buffers, a, b) => {
  220. let position = a;
  221. let callback = b;
  222. if (typeof a === 'function')
  223. [position, callback] = [null, a];
  224. (0, util_1.validateCallback)(callback);
  225. Promise.resolve().then(() => {
  226. try {
  227. const bytes = this.writevBase(fd, buffers, position);
  228. callback(null, bytes, buffers);
  229. }
  230. catch (err) {
  231. callback(err);
  232. }
  233. });
  234. };
  235. this.writevSync = (fd, buffers, position) => {
  236. (0, util_2.validateFd)(fd);
  237. return this.writevBase(fd, buffers, position !== null && position !== void 0 ? position : null);
  238. };
  239. this.writeFileSync = (id, data, options) => {
  240. const opts = (0, options_1.getWriteFileOptions)(options);
  241. const flagsNum = (0, util_1.flagsToNumber)(opts.flag);
  242. const modeNum = (0, util_1.modeToNumber)(opts.mode);
  243. const buf = (0, util_2.dataToBuffer)(data, opts.encoding);
  244. this._core.writeFile(id, buf, flagsNum, modeNum);
  245. };
  246. this.writeFile = (id, data, a, b) => {
  247. let options = a;
  248. let callback = b;
  249. if (typeof a === 'function')
  250. [options, callback] = [options_1.writeFileDefaults, a];
  251. const cb = (0, util_1.validateCallback)(callback);
  252. const opts = (0, options_1.getWriteFileOptions)(options);
  253. const flagsNum = (0, util_1.flagsToNumber)(opts.flag);
  254. const modeNum = (0, util_1.modeToNumber)(opts.mode);
  255. const buf = (0, util_2.dataToBuffer)(data, opts.encoding);
  256. this.wrapAsync(this._core.writeFile, [id, buf, flagsNum, modeNum], cb);
  257. };
  258. this.copyFileSync = (src, dest, flags) => {
  259. const srcFilename = (0, util_1.pathToFilename)(src);
  260. const destFilename = (0, util_1.pathToFilename)(dest);
  261. return this._copyFile(srcFilename, destFilename, (flags || 0) | 0);
  262. };
  263. this.copyFile = (src, dest, a, b) => {
  264. const srcFilename = (0, util_1.pathToFilename)(src);
  265. const destFilename = (0, util_1.pathToFilename)(dest);
  266. let flags;
  267. let callback;
  268. if (typeof a === 'function')
  269. [flags, callback] = [0, a];
  270. else
  271. [flags, callback] = [a, b];
  272. (0, util_1.validateCallback)(callback);
  273. this.wrapAsync(this._copyFile, [srcFilename, destFilename, flags], callback);
  274. };
  275. this._cp = (src, dest, options) => {
  276. if (options.filter && !options.filter(src, dest))
  277. return;
  278. const srcStat = options.dereference ? this.statSync(src) : this.lstatSync(src);
  279. let destStat = null;
  280. try {
  281. destStat = this.lstatSync(dest);
  282. }
  283. catch (err) {
  284. if (err.code !== 'ENOENT') {
  285. throw err;
  286. }
  287. }
  288. // Check if src and dest are the same (both exist and have same inode)
  289. if (destStat && srcStat.ino === destStat.ino && srcStat.dev === destStat.dev)
  290. throw (0, util_1.createError)("EINVAL" /* ERROR_CODE.EINVAL */, 'cp', src, dest);
  291. // Check type compatibility
  292. if (destStat) {
  293. if (srcStat.isDirectory() && !destStat.isDirectory())
  294. throw (0, util_1.createError)("EISDIR" /* ERROR_CODE.EISDIR */, 'cp', src, dest);
  295. if (!srcStat.isDirectory() && destStat.isDirectory())
  296. throw (0, util_1.createError)("ENOTDIR" /* ERROR_CODE.ENOTDIR */, 'cp', src, dest);
  297. }
  298. // Check if trying to copy directory to subdirectory of itself
  299. if (srcStat.isDirectory() && this.isSrcSubdir(src, dest))
  300. throw (0, util_1.createError)("EINVAL" /* ERROR_CODE.EINVAL */, 'cp', src, dest);
  301. ENDURE_PARENT_DIR_EXISTS: {
  302. const parent = dirname(dest);
  303. if (!this.existsSync(parent))
  304. this.mkdirSync(parent, { recursive: true });
  305. }
  306. // Handle different file types
  307. if (srcStat.isDirectory()) {
  308. if (!options.recursive)
  309. throw (0, util_1.createError)("EISDIR" /* ERROR_CODE.EISDIR */, 'cp', src);
  310. this.cpDirSync(srcStat, destStat, src, dest, options);
  311. }
  312. else if (srcStat.isFile() || srcStat.isCharacterDevice() || srcStat.isBlockDevice()) {
  313. this.cpFileSync(srcStat, destStat, src, dest, options);
  314. }
  315. else if (srcStat.isSymbolicLink() && !options.dereference) {
  316. // Only handle as symlink if not dereferencing
  317. this.cpSymlinkSync(destStat, src, dest, options);
  318. }
  319. else {
  320. throw (0, util_1.createError)("EINVAL" /* ERROR_CODE.EINVAL */, 'cp', src);
  321. }
  322. };
  323. this.linkSync = (existingPath, newPath) => {
  324. const existingPathFilename = (0, util_1.pathToFilename)(existingPath);
  325. const newPathFilename = (0, util_1.pathToFilename)(newPath);
  326. this._core.link(existingPathFilename, newPathFilename);
  327. };
  328. this.link = (existingPath, newPath, callback) => {
  329. const existingPathFilename = (0, util_1.pathToFilename)(existingPath);
  330. const newPathFilename = (0, util_1.pathToFilename)(newPath);
  331. this.wrapAsync(this._core.link, [existingPathFilename, newPathFilename], callback);
  332. };
  333. this.unlinkSync = (path) => {
  334. const filename = (0, util_1.pathToFilename)(path);
  335. this._core.unlink(filename);
  336. };
  337. this.unlink = (path, callback) => {
  338. const filename = (0, util_1.pathToFilename)(path);
  339. this.wrapAsync(this._core.unlink, [filename], callback);
  340. };
  341. /**
  342. * `type` argument works only on Windows.
  343. * @param target
  344. * @param path
  345. * @param type
  346. */
  347. this.symlinkSync = (target, path, type) => {
  348. const targetFilename = (0, util_1.pathToFilename)(target);
  349. const pathFilename = (0, util_1.pathToFilename)(path);
  350. this._core.symlink(targetFilename, pathFilename);
  351. };
  352. this.symlink = (target, path, a, b) => {
  353. const callback = (0, util_1.validateCallback)(typeof a === 'function' ? a : b);
  354. const targetFilename = (0, util_1.pathToFilename)(target);
  355. const pathFilename = (0, util_1.pathToFilename)(path);
  356. this.wrapAsync(this._core.symlink, [targetFilename, pathFilename], callback);
  357. };
  358. this._lstat = (filename, bigint = false, throwIfNoEntry = false) => {
  359. let link;
  360. try {
  361. link = this._core.getLinkOrThrow(filename, 'lstat');
  362. }
  363. catch (err) {
  364. if (err.code === "ENOENT" /* ERROR_CODE.ENOENT */ && !throwIfNoEntry)
  365. return undefined;
  366. else
  367. throw err;
  368. }
  369. return Stats_1.default.build(link.getNode(), bigint);
  370. };
  371. this.lstatSync = (path, options) => {
  372. const { throwIfNoEntry = true, bigint = false } = (0, options_1.getStatOptions)(options);
  373. return this._lstat((0, util_1.pathToFilename)(path), bigint, throwIfNoEntry);
  374. };
  375. this.renameSync = (oldPath, newPath) => {
  376. const oldPathFilename = (0, util_1.pathToFilename)(oldPath);
  377. const newPathFilename = (0, util_1.pathToFilename)(newPath);
  378. this._core.rename(oldPathFilename, newPathFilename);
  379. };
  380. this.rename = (oldPath, newPath, callback) => {
  381. const oldPathFilename = (0, util_1.pathToFilename)(oldPath);
  382. const newPathFilename = (0, util_1.pathToFilename)(newPath);
  383. this.wrapAsync(this._core.rename, [oldPathFilename, newPathFilename], callback);
  384. };
  385. this.existsSync = (path) => {
  386. try {
  387. return this._exists((0, util_1.pathToFilename)(path));
  388. }
  389. catch (err) {
  390. return false;
  391. }
  392. };
  393. this.exists = (path, callback) => {
  394. const filename = (0, util_1.pathToFilename)(path);
  395. if (typeof callback !== 'function')
  396. throw Error(constants_2.ERRSTR.CB);
  397. Promise.resolve().then(() => {
  398. try {
  399. callback(this._exists(filename));
  400. }
  401. catch (err) {
  402. callback(false);
  403. }
  404. });
  405. };
  406. this.accessSync = (path, mode = F_OK) => {
  407. const filename = (0, util_1.pathToFilename)(path);
  408. mode = mode | 0;
  409. this._access(filename, mode);
  410. };
  411. this.access = (path, a, b) => {
  412. let mode = F_OK;
  413. let callback;
  414. if (typeof a !== 'function')
  415. [mode, callback] = [a | 0, (0, util_1.validateCallback)(b)];
  416. else
  417. callback = a;
  418. const filename = (0, util_1.pathToFilename)(path);
  419. this.wrapAsync(this._access, [filename, mode], callback);
  420. };
  421. this.appendFileSync = (id, data, options) => {
  422. const opts = (0, options_1.getAppendFileOpts)(options);
  423. // Force append behavior when using a supplied file descriptor.
  424. if (!opts.flag || (0, util_2.isFd)(id))
  425. opts.flag = 'a';
  426. this.writeFileSync(id, data, opts);
  427. };
  428. this.appendFile = (id, data, a, b) => {
  429. const [opts, callback] = (0, options_1.getAppendFileOptsAndCb)(a, b);
  430. // Force append behavior when using a supplied file descriptor.
  431. if (!opts.flag || (0, util_2.isFd)(id))
  432. opts.flag = 'a';
  433. this.writeFile(id, data, opts, callback);
  434. };
  435. this._readdir = (filename, options) => {
  436. const steps = (0, util_2.filenameToSteps)(filename);
  437. const link = this._core.getResolvedLinkOrThrow(filename, 'scandir');
  438. const node = link.getNode();
  439. if (!node.isDirectory())
  440. throw (0, util_1.createError)("ENOTDIR" /* ERROR_CODE.ENOTDIR */, 'scandir', filename);
  441. // Check we have permissions
  442. if (!node.canRead())
  443. throw (0, util_1.createError)("EACCES" /* ERROR_CODE.EACCES */, 'scandir', filename);
  444. const list = []; // output list
  445. for (const name of link.children.keys()) {
  446. const child = link.getChild(name);
  447. if (!child || name === '.' || name === '..')
  448. continue;
  449. list.push(Dirent_1.default.build(child, options.encoding));
  450. // recursion
  451. if (options.recursive && child.children.size) {
  452. const recurseOptions = Object.assign(Object.assign({}, options), { recursive: true, withFileTypes: true });
  453. const childList = this._readdir(child.getPath(), recurseOptions);
  454. list.push(...childList);
  455. }
  456. }
  457. if (!util_2.isWin && options.encoding !== 'buffer')
  458. list.sort((a, b) => {
  459. if (a.name < b.name)
  460. return -1;
  461. if (a.name > b.name)
  462. return 1;
  463. return 0;
  464. });
  465. if (options.withFileTypes)
  466. return list;
  467. let filename2 = filename;
  468. if (util_2.isWin)
  469. filename2 = filename2.replace(/\\/g, '/');
  470. return list.map(dirent => {
  471. if (options.recursive) {
  472. let fullPath = pathModule.join(dirent.parentPath, dirent.name.toString());
  473. if (util_2.isWin) {
  474. fullPath = fullPath.replace(/\\/g, '/');
  475. }
  476. return fullPath.replace(filename2 + pathModule.posix.sep, '');
  477. }
  478. return dirent.name;
  479. });
  480. };
  481. this.readdirSync = (path, options) => {
  482. const opts = (0, options_1.getReaddirOptions)(options);
  483. const filename = (0, util_1.pathToFilename)(path);
  484. return this._readdir(filename, opts);
  485. };
  486. this.readdir = (path, a, b) => {
  487. const [options, callback] = (0, options_1.getReaddirOptsAndCb)(a, b);
  488. const filename = (0, util_1.pathToFilename)(path);
  489. this.wrapAsync(this._readdir, [filename, options], callback);
  490. };
  491. this._readlink = (filename, encoding) => {
  492. const link = this._core.getLinkOrThrow(filename, 'readlink');
  493. const node = link.getNode();
  494. if (!node.isSymlink())
  495. throw (0, util_1.createError)("EINVAL" /* ERROR_CODE.EINVAL */, 'readlink', filename);
  496. return (0, encoding_1.strToEncoding)(node.symlink, encoding);
  497. };
  498. this.readlinkSync = (path, options) => {
  499. const opts = (0, options_1.getDefaultOpts)(options);
  500. const filename = (0, util_1.pathToFilename)(path);
  501. return this._readlink(filename, opts.encoding);
  502. };
  503. this.readlink = (path, a, b) => {
  504. const [opts, callback] = (0, options_1.getDefaultOptsAndCb)(a, b);
  505. const filename = (0, util_1.pathToFilename)(path);
  506. this.wrapAsync(this._readlink, [filename, opts.encoding], callback);
  507. };
  508. this._fsync = (fd) => {
  509. this._core.getFileByFdOrThrow(fd, 'fsync');
  510. };
  511. this.fsyncSync = (fd) => {
  512. this._fsync(fd);
  513. };
  514. this.fsync = (fd, callback) => {
  515. this.wrapAsync(this._fsync, [fd], callback);
  516. };
  517. this._fdatasync = (fd) => {
  518. this._core.getFileByFdOrThrow(fd, 'fdatasync');
  519. };
  520. this.fdatasyncSync = (fd) => {
  521. this._fdatasync(fd);
  522. };
  523. this.fdatasync = (fd, callback) => {
  524. this.wrapAsync(this._fdatasync, [fd], callback);
  525. };
  526. this._ftruncate = (fd, len) => {
  527. const file = this._core.getFileByFdOrThrow(fd, 'ftruncate');
  528. file.truncate(len);
  529. };
  530. this.ftruncateSync = (fd, len) => {
  531. this._ftruncate(fd, len);
  532. };
  533. this.ftruncate = (fd, a, b) => {
  534. const len = typeof a === 'number' ? a : 0;
  535. const callback = (0, util_1.validateCallback)(typeof a === 'number' ? b : a);
  536. this.wrapAsync(this._ftruncate, [fd, len], callback);
  537. };
  538. this._truncate = (path, len) => {
  539. const fd = this.openSync(path, 'r+');
  540. try {
  541. this.ftruncateSync(fd, len);
  542. }
  543. finally {
  544. this.closeSync(fd);
  545. }
  546. };
  547. /**
  548. * `id` should be a file descriptor or a path. `id` as file descriptor will
  549. * not be supported soon.
  550. */
  551. this.truncateSync = (id, len) => {
  552. if ((0, util_2.isFd)(id))
  553. return this.ftruncateSync(id, len);
  554. this._truncate(id, len);
  555. };
  556. this.truncate = (id, a, b) => {
  557. const len = typeof a === 'number' ? a : 0;
  558. const callback = (0, util_1.validateCallback)(typeof a === 'number' ? b : a);
  559. if ((0, util_2.isFd)(id))
  560. return this.ftruncate(id, len, callback);
  561. this.wrapAsync(this._truncate, [id, len], callback);
  562. };
  563. this._futimes = (fd, atime, mtime) => {
  564. const file = this._core.getFileByFdOrThrow(fd, 'futimes');
  565. const node = file.node;
  566. node.atime = new Date(atime * 1000);
  567. node.mtime = new Date(mtime * 1000);
  568. };
  569. this.futimesSync = (fd, atime, mtime) => {
  570. this._futimes(fd, toUnixTimestamp(atime), toUnixTimestamp(mtime));
  571. };
  572. this.futimes = (fd, atime, mtime, callback) => {
  573. this.wrapAsync(this._futimes, [fd, toUnixTimestamp(atime), toUnixTimestamp(mtime)], callback);
  574. };
  575. this._utimes = (filename, atime, mtime, followSymlinks = true) => {
  576. const core = this._core;
  577. const link = followSymlinks
  578. ? core.getResolvedLinkOrThrow(filename, 'utimes')
  579. : core.getLinkOrThrow(filename, 'lutimes');
  580. const node = link.getNode();
  581. node.atime = new Date(atime * 1000);
  582. node.mtime = new Date(mtime * 1000);
  583. };
  584. this.utimesSync = (path, atime, mtime) => {
  585. this._utimes((0, util_1.pathToFilename)(path), toUnixTimestamp(atime), toUnixTimestamp(mtime), true);
  586. };
  587. this.utimes = (path, atime, mtime, callback) => {
  588. this.wrapAsync(this._utimes, [(0, util_1.pathToFilename)(path), toUnixTimestamp(atime), toUnixTimestamp(mtime), true], callback);
  589. };
  590. this.lutimesSync = (path, atime, mtime) => {
  591. this._utimes((0, util_1.pathToFilename)(path), toUnixTimestamp(atime), toUnixTimestamp(mtime), false);
  592. };
  593. this.lutimes = (path, atime, mtime, callback) => {
  594. this.wrapAsync(this._utimes, [(0, util_1.pathToFilename)(path), toUnixTimestamp(atime), toUnixTimestamp(mtime), false], callback);
  595. };
  596. this.mkdirSync = (path, options) => {
  597. const opts = (0, options_1.getMkdirOptions)(options);
  598. const modeNum = (0, util_1.modeToNumber)(opts.mode, 0o777);
  599. const filename = (0, util_1.pathToFilename)(path);
  600. if (opts.recursive)
  601. return this._core.mkdirp(filename, modeNum);
  602. this._core.mkdir(filename, modeNum);
  603. };
  604. this.mkdir = (path, a, b) => {
  605. const opts = (0, options_1.getMkdirOptions)(a);
  606. const callback = (0, util_1.validateCallback)(typeof a === 'function' ? a : b);
  607. const modeNum = (0, util_1.modeToNumber)(opts.mode, 0o777);
  608. const filename = (0, util_1.pathToFilename)(path);
  609. if (opts.recursive)
  610. this.wrapAsync(this._core.mkdirp, [filename, modeNum], callback);
  611. else
  612. this.wrapAsync(this._core.mkdir, [filename, modeNum], callback);
  613. };
  614. this._mkdtemp = (prefix, encoding, retry = 5) => {
  615. const filename = prefix + (0, util_1.genRndStr6)();
  616. try {
  617. this._core.mkdir(filename, 511 /* MODE.DIR */);
  618. return (0, encoding_1.strToEncoding)(filename, encoding);
  619. }
  620. catch (err) {
  621. if (err.code === "EEXIST" /* ERROR_CODE.EEXIST */) {
  622. if (retry > 1)
  623. return this._mkdtemp(prefix, encoding, retry - 1);
  624. else
  625. throw Error('Could not create temp dir.');
  626. }
  627. else
  628. throw err;
  629. }
  630. };
  631. this.mkdtempSync = (prefix, options) => {
  632. const { encoding } = (0, options_1.getDefaultOpts)(options);
  633. if (!prefix || typeof prefix !== 'string')
  634. throw new TypeError('filename prefix is required');
  635. (0, util_1.nullCheck)(prefix);
  636. return this._mkdtemp(prefix, encoding);
  637. };
  638. this.mkdtemp = (prefix, a, b) => {
  639. const [{ encoding }, callback] = (0, options_1.getDefaultOptsAndCb)(a, b);
  640. if (!prefix || typeof prefix !== 'string')
  641. throw new TypeError('filename prefix is required');
  642. if (!(0, util_1.nullCheck)(prefix))
  643. return;
  644. this.wrapAsync(this._mkdtemp, [prefix, encoding], callback);
  645. };
  646. this.rmdirSync = (path, options) => {
  647. const opts = (0, options_1.getRmdirOptions)(options);
  648. this._core.rmdir((0, util_1.pathToFilename)(path), opts.recursive);
  649. };
  650. this.rmdir = (path, a, b) => {
  651. const opts = (0, options_1.getRmdirOptions)(a);
  652. const callback = (0, util_1.validateCallback)(typeof a === 'function' ? a : b);
  653. this.wrapAsync(this._core.rmdir, [(0, util_1.pathToFilename)(path), opts.recursive], callback);
  654. };
  655. this.rmSync = (path, options) => {
  656. this._core.rm((0, util_1.pathToFilename)(path), options === null || options === void 0 ? void 0 : options.force, options === null || options === void 0 ? void 0 : options.recursive);
  657. };
  658. this.rm = (path, a, b) => {
  659. const [opts, callback] = (0, options_1.getRmOptsAndCb)(a, b);
  660. this.wrapAsync(this._core.rm, [(0, util_1.pathToFilename)(path), opts === null || opts === void 0 ? void 0 : opts.force, opts === null || opts === void 0 ? void 0 : opts.recursive], callback);
  661. };
  662. this._fchmod = (fd, modeNum) => {
  663. const file = this._core.getFileByFdOrThrow(fd, 'fchmod');
  664. file.chmod(modeNum);
  665. };
  666. this.fchmodSync = (fd, mode) => {
  667. this._fchmod(fd, (0, util_1.modeToNumber)(mode));
  668. };
  669. this.fchmod = (fd, mode, callback) => {
  670. this.wrapAsync(this._fchmod, [fd, (0, util_1.modeToNumber)(mode)], callback);
  671. };
  672. this._chmod = (filename, modeNum, followSymlinks = true) => {
  673. const link = followSymlinks
  674. ? this._core.getResolvedLinkOrThrow(filename, 'chmod')
  675. : this._core.getLinkOrThrow(filename, 'chmod');
  676. const node = link.getNode();
  677. node.chmod(modeNum);
  678. };
  679. this.chmodSync = (path, mode) => {
  680. const modeNum = (0, util_1.modeToNumber)(mode);
  681. const filename = (0, util_1.pathToFilename)(path);
  682. this._chmod(filename, modeNum, true);
  683. };
  684. this.chmod = (path, mode, callback) => {
  685. const modeNum = (0, util_1.modeToNumber)(mode);
  686. const filename = (0, util_1.pathToFilename)(path);
  687. this.wrapAsync(this._chmod, [filename, modeNum], callback);
  688. };
  689. this._lchmod = (filename, modeNum) => {
  690. this._chmod(filename, modeNum, false);
  691. };
  692. this.lchmodSync = (path, mode) => {
  693. const modeNum = (0, util_1.modeToNumber)(mode);
  694. const filename = (0, util_1.pathToFilename)(path);
  695. this._lchmod(filename, modeNum);
  696. };
  697. this.lchmod = (path, mode, callback) => {
  698. const modeNum = (0, util_1.modeToNumber)(mode);
  699. const filename = (0, util_1.pathToFilename)(path);
  700. this.wrapAsync(this._lchmod, [filename, modeNum], callback);
  701. };
  702. this._fchown = (fd, uid, gid) => {
  703. this._core.getFileByFdOrThrow(fd, 'fchown').chown(uid, gid);
  704. };
  705. this.fchownSync = (fd, uid, gid) => {
  706. validateUid(uid);
  707. validateGid(gid);
  708. this._fchown(fd, uid, gid);
  709. };
  710. this.fchown = (fd, uid, gid, callback) => {
  711. validateUid(uid);
  712. validateGid(gid);
  713. this.wrapAsync(this._fchown, [fd, uid, gid], callback);
  714. };
  715. this._chown = (filename, uid, gid) => {
  716. const link = this._core.getResolvedLinkOrThrow(filename, 'chown');
  717. const node = link.getNode();
  718. node.chown(uid, gid);
  719. };
  720. this.chownSync = (path, uid, gid) => {
  721. validateUid(uid);
  722. validateGid(gid);
  723. this._chown((0, util_1.pathToFilename)(path), uid, gid);
  724. };
  725. this.chown = (path, uid, gid, callback) => {
  726. validateUid(uid);
  727. validateGid(gid);
  728. this.wrapAsync(this._chown, [(0, util_1.pathToFilename)(path), uid, gid], callback);
  729. };
  730. this._lchown = (filename, uid, gid) => {
  731. this._core.getLinkOrThrow(filename, 'lchown').getNode().chown(uid, gid);
  732. };
  733. this.lchownSync = (path, uid, gid) => {
  734. validateUid(uid);
  735. validateGid(gid);
  736. this._lchown((0, util_1.pathToFilename)(path), uid, gid);
  737. };
  738. this.lchown = (path, uid, gid, callback) => {
  739. validateUid(uid);
  740. validateGid(gid);
  741. this.wrapAsync(this._lchown, [(0, util_1.pathToFilename)(path), uid, gid], callback);
  742. };
  743. this.statWatchers = {};
  744. this.cpSync = (src, dest, options) => {
  745. var _a, _b, _c, _d, _e, _f, _g;
  746. const srcFilename = (0, util_1.pathToFilename)(src);
  747. const destFilename = (0, util_1.pathToFilename)(dest);
  748. const opts_ = {
  749. dereference: (_a = options === null || options === void 0 ? void 0 : options.dereference) !== null && _a !== void 0 ? _a : false,
  750. errorOnExist: (_b = options === null || options === void 0 ? void 0 : options.errorOnExist) !== null && _b !== void 0 ? _b : false,
  751. filter: options === null || options === void 0 ? void 0 : options.filter,
  752. force: (_c = options === null || options === void 0 ? void 0 : options.force) !== null && _c !== void 0 ? _c : true,
  753. mode: (_d = options === null || options === void 0 ? void 0 : options.mode) !== null && _d !== void 0 ? _d : 0,
  754. preserveTimestamps: (_e = options === null || options === void 0 ? void 0 : options.preserveTimestamps) !== null && _e !== void 0 ? _e : false,
  755. recursive: (_f = options === null || options === void 0 ? void 0 : options.recursive) !== null && _f !== void 0 ? _f : false,
  756. verbatimSymlinks: (_g = options === null || options === void 0 ? void 0 : options.verbatimSymlinks) !== null && _g !== void 0 ? _g : false,
  757. };
  758. return this._cp(srcFilename, destFilename, opts_);
  759. };
  760. this.cp = (src, dest, a, b) => {
  761. var _a, _b, _c, _d, _e, _f, _g;
  762. const srcFilename = (0, util_1.pathToFilename)(src);
  763. const destFilename = (0, util_1.pathToFilename)(dest);
  764. let options;
  765. let callback;
  766. if (typeof a === 'function')
  767. [options, callback] = [{}, a];
  768. else
  769. [options, callback] = [a || {}, b];
  770. (0, util_1.validateCallback)(callback);
  771. const opts_ = {
  772. dereference: (_a = options === null || options === void 0 ? void 0 : options.dereference) !== null && _a !== void 0 ? _a : false,
  773. errorOnExist: (_b = options === null || options === void 0 ? void 0 : options.errorOnExist) !== null && _b !== void 0 ? _b : false,
  774. filter: options === null || options === void 0 ? void 0 : options.filter,
  775. force: (_c = options === null || options === void 0 ? void 0 : options.force) !== null && _c !== void 0 ? _c : true,
  776. mode: (_d = options === null || options === void 0 ? void 0 : options.mode) !== null && _d !== void 0 ? _d : 0,
  777. preserveTimestamps: (_e = options === null || options === void 0 ? void 0 : options.preserveTimestamps) !== null && _e !== void 0 ? _e : false,
  778. recursive: (_f = options === null || options === void 0 ? void 0 : options.recursive) !== null && _f !== void 0 ? _f : false,
  779. verbatimSymlinks: (_g = options === null || options === void 0 ? void 0 : options.verbatimSymlinks) !== null && _g !== void 0 ? _g : false,
  780. };
  781. this.wrapAsync(this._cp, [srcFilename, destFilename, opts_], callback);
  782. };
  783. this.openAsBlob = async (path, options) => {
  784. const filename = (0, util_1.pathToFilename)(path);
  785. const link = this._core.getResolvedLinkOrThrow(filename, 'open');
  786. const node = link.getNode();
  787. if (node.isDirectory())
  788. throw (0, util_1.createError)("EISDIR" /* ERROR_CODE.EISDIR */, 'open', link.getPath());
  789. const buffer = node.getBuffer();
  790. const type = (options === null || options === void 0 ? void 0 : options.type) || '';
  791. return new Blob([buffer], { type });
  792. };
  793. this.glob = (pattern, ...args) => {
  794. const [options, callback] = args.length === 1 ? [{}, args[0]] : [args[0], args[1]];
  795. this.wrapAsync(this._globSync, [pattern, options || {}], callback);
  796. };
  797. this.globSync = (pattern, options = {}) => {
  798. return this._globSync(pattern, options);
  799. };
  800. this._globSync = (pattern, options = {}) => {
  801. const { globSync } = require('./glob');
  802. return globSync(this, pattern, options);
  803. };
  804. this._opendir = (filename, options) => {
  805. const link = this._core.getResolvedLinkOrThrow(filename, 'scandir');
  806. const node = link.getNode();
  807. if (!node.isDirectory())
  808. throw (0, util_1.createError)("ENOTDIR" /* ERROR_CODE.ENOTDIR */, 'scandir', filename);
  809. return new Dir_1.Dir(link, options);
  810. };
  811. this.opendirSync = (path, options) => {
  812. const opts = (0, options_1.getOpendirOptions)(options);
  813. const filename = (0, util_1.pathToFilename)(path);
  814. return this._opendir(filename, opts);
  815. };
  816. this.opendir = (path, a, b) => {
  817. const [options, callback] = (0, options_1.getOpendirOptsAndCb)(a, b);
  818. const filename = (0, util_1.pathToFilename)(path);
  819. this.wrapAsync(this._opendir, [filename, options], callback);
  820. };
  821. const self = this; // tslint:disable-line no-this-assignment
  822. this.StatWatcher = class extends StatWatcher {
  823. constructor() {
  824. super(self);
  825. }
  826. };
  827. const _ReadStream = FsReadStream;
  828. this.ReadStream = class extends _ReadStream {
  829. constructor(...args) {
  830. super(self, ...args);
  831. }
  832. };
  833. const _WriteStream = FsWriteStream;
  834. this.WriteStream = class extends _WriteStream {
  835. constructor(...args) {
  836. super(self, ...args);
  837. }
  838. };
  839. this.FSWatcher = class extends FSWatcher {
  840. constructor() {
  841. super(self);
  842. }
  843. };
  844. const _realpath = (filename, encoding) => {
  845. const realLink = this._core.getResolvedLinkOrThrow(filename, 'realpath');
  846. return (0, encoding_1.strToEncoding)(realLink.getPath() || '/', encoding);
  847. };
  848. const realpathImpl = (path, a, b) => {
  849. const [opts, callback] = (0, options_1.getRealpathOptsAndCb)(a, b);
  850. const pathFilename = (0, util_1.pathToFilename)(path);
  851. self.wrapAsync(_realpath, [pathFilename, opts.encoding], callback);
  852. };
  853. const realpathSyncImpl = (path, options) => _realpath((0, util_1.pathToFilename)(path), (0, options_1.getRealpathOptions)(options).encoding);
  854. this.realpath = realpathImpl;
  855. this.realpath.native = realpathImpl;
  856. this.realpathSync = realpathSyncImpl;
  857. this.realpathSync.native = realpathSyncImpl;
  858. }
  859. wrapAsync(method, args, callback) {
  860. (0, util_1.validateCallback)(callback);
  861. Promise.resolve().then(() => {
  862. let result;
  863. try {
  864. result = method.apply(this, args);
  865. }
  866. catch (err) {
  867. callback(err);
  868. return;
  869. }
  870. callback(null, result);
  871. });
  872. }
  873. toTree(opts = { separator: sep }) {
  874. return (0, print_1.toTreeSync)(this, opts);
  875. }
  876. reset() {
  877. this._core.reset();
  878. }
  879. toJSON(paths, json = {}, isRelative = false, asBuffer = false) {
  880. return this._core.toJSON(paths, json, isRelative, asBuffer);
  881. }
  882. fromJSON(json, cwd) {
  883. return this._core.fromJSON(json, cwd);
  884. }
  885. fromNestedJSON(json, cwd) {
  886. return this._core.fromNestedJSON(json, cwd);
  887. }
  888. // Legacy interface
  889. mountSync(mountpoint, json) {
  890. this._core.fromJSON(json, mountpoint);
  891. }
  892. _write(fd, buf, offset, length, position) {
  893. const file = this._core.getFileByFdOrThrow(fd, 'write');
  894. if (file.node.isSymlink()) {
  895. throw (0, util_1.createError)("EBADF" /* ERROR_CODE.EBADF */, 'write', file.link.getPath());
  896. }
  897. return file.write(buf, offset, length, position === -1 || typeof position !== 'number' ? undefined : position);
  898. }
  899. writevBase(fd, buffers, position) {
  900. const file = this._core.getFileByFdOrThrow(fd);
  901. let p = position !== null && position !== void 0 ? position : undefined;
  902. if (p === -1) {
  903. p = undefined;
  904. }
  905. let bytesWritten = 0;
  906. for (const buffer of buffers) {
  907. const nodeBuf = buffer_1.Buffer.from(buffer.buffer, buffer.byteOffset, buffer.byteLength);
  908. const bytes = file.write(nodeBuf, 0, nodeBuf.byteLength, p);
  909. p = undefined;
  910. bytesWritten += bytes;
  911. if (bytes < nodeBuf.byteLength)
  912. break;
  913. }
  914. return bytesWritten;
  915. }
  916. _copyFile(src, dest, flags) {
  917. const buf = this.readFileSync(src);
  918. if (flags & COPYFILE_EXCL && this.existsSync(dest))
  919. throw (0, util_1.createError)("EEXIST" /* ERROR_CODE.EEXIST */, 'copyFile', src, dest);
  920. if (flags & COPYFILE_FICLONE_FORCE)
  921. throw (0, util_1.createError)("ENOSYS" /* ERROR_CODE.ENOSYS */, 'copyFile', src, dest);
  922. this._core.writeFile(dest, buf, constants_2.FLAGS.w, 438 /* MODE.DEFAULT */);
  923. }
  924. isSrcSubdir(src, dest) {
  925. try {
  926. const normalizedSrc = normalize(src.startsWith('/') ? src : '/' + src);
  927. const normalizedDest = normalize(dest.startsWith('/') ? dest : '/' + dest);
  928. if (normalizedSrc === normalizedDest)
  929. return true;
  930. // Check if dest is under src by using relative path
  931. // If dest is under src, the relative path from src to dest won't start with '..'
  932. const relativePath = relative(normalizedSrc, normalizedDest);
  933. // If relative path is empty or doesn't start with '..', dest is under src
  934. return relativePath === '' || (!relativePath.startsWith('..') && !pathModule.isAbsolute(relativePath));
  935. }
  936. catch (error) {
  937. // If path operations fail, assume it's safe (don't block the copy)
  938. return false;
  939. }
  940. }
  941. cpFileSync(srcStat, destStat, src, dest, options) {
  942. if (destStat) {
  943. if (options.errorOnExist)
  944. throw (0, util_1.createError)("EEXIST" /* ERROR_CODE.EEXIST */, 'cp', dest);
  945. if (!options.force)
  946. return;
  947. this.unlinkSync(dest);
  948. }
  949. // Copy the file
  950. this.copyFileSync(src, dest, options.mode);
  951. // Preserve timestamps if requested
  952. if (options.preserveTimestamps)
  953. this.utimesSync(dest, srcStat.atime, srcStat.mtime);
  954. // Set file mode
  955. this.chmodSync(dest, Number(srcStat.mode));
  956. }
  957. cpDirSync(srcStat, destStat, src, dest, options) {
  958. if (!destStat) {
  959. this.mkdirSync(dest);
  960. }
  961. // Read directory contents
  962. const entries = this.readdirSync(src);
  963. for (const entry of entries) {
  964. const srcItem = join(src, entry);
  965. const destItem = join(dest, entry);
  966. // Apply filter to each item
  967. if (options.filter && !options.filter(srcItem, destItem)) {
  968. continue;
  969. }
  970. this._cp(srcItem, destItem, options);
  971. }
  972. // Set directory mode
  973. this.chmodSync(dest, Number(srcStat.mode));
  974. }
  975. cpSymlinkSync(destStat, src, dest, options) {
  976. let linkTarget = String(this.readlinkSync(src));
  977. if (!options.verbatimSymlinks && !pathModule.isAbsolute(linkTarget))
  978. linkTarget = resolveCrossPlatform(dirname(src), linkTarget);
  979. if (destStat)
  980. this.unlinkSync(dest);
  981. this.symlinkSync(linkTarget, dest);
  982. }
  983. lstat(path, a, b) {
  984. const [{ throwIfNoEntry = true, bigint = false }, callback] = (0, options_1.getStatOptsAndCb)(a, b);
  985. this.wrapAsync(this._lstat, [(0, util_1.pathToFilename)(path), bigint, throwIfNoEntry], callback);
  986. }
  987. _stat(filename, bigint = false, throwIfNoEntry = true) {
  988. let link;
  989. try {
  990. link = this._core.getResolvedLinkOrThrow(filename, 'stat');
  991. }
  992. catch (err) {
  993. if (err.code === "ENOENT" /* ERROR_CODE.ENOENT */ && !throwIfNoEntry)
  994. return undefined;
  995. else
  996. throw err;
  997. }
  998. return Stats_1.default.build(link.getNode(), bigint);
  999. }
  1000. statSync(path, options) {
  1001. const { bigint = true, throwIfNoEntry = true } = (0, options_1.getStatOptions)(options);
  1002. return this._stat((0, util_1.pathToFilename)(path), bigint, throwIfNoEntry);
  1003. }
  1004. stat(path, a, b) {
  1005. const [{ bigint = false, throwIfNoEntry = true }, callback] = (0, options_1.getStatOptsAndCb)(a, b);
  1006. this.wrapAsync(this._stat, [(0, util_1.pathToFilename)(path), bigint, throwIfNoEntry], callback);
  1007. }
  1008. fstatBase(fd, bigint = false) {
  1009. const file = this._core.getFileByFd(fd);
  1010. if (!file)
  1011. throw (0, util_1.createError)("EBADF" /* ERROR_CODE.EBADF */, 'fstat');
  1012. return Stats_1.default.build(file.node, bigint);
  1013. }
  1014. fstatSync(fd, options) {
  1015. return this.fstatBase(fd, (0, options_1.getStatOptions)(options).bigint);
  1016. }
  1017. fstat(fd, a, b) {
  1018. const [opts, callback] = (0, options_1.getStatOptsAndCb)(a, b);
  1019. this.wrapAsync(this.fstatBase, [fd, opts.bigint], callback);
  1020. }
  1021. _exists(filename) {
  1022. return !!this._stat(filename);
  1023. }
  1024. _access(filename, mode) {
  1025. // TODO: Need to check mode?
  1026. this._core.getLinkOrThrow(filename, 'access');
  1027. }
  1028. watchFile(path, a, b) {
  1029. const filename = (0, util_1.pathToFilename)(path);
  1030. let options = a;
  1031. let listener = b;
  1032. if (typeof options === 'function') {
  1033. listener = a;
  1034. options = null;
  1035. }
  1036. if (typeof listener !== 'function') {
  1037. throw Error('"watchFile()" requires a listener function');
  1038. }
  1039. let interval = 5007;
  1040. let persistent = true;
  1041. if (options && typeof options === 'object') {
  1042. if (typeof options.interval === 'number')
  1043. interval = options.interval;
  1044. if (typeof options.persistent === 'boolean')
  1045. persistent = options.persistent;
  1046. }
  1047. let watcher = this.statWatchers[filename];
  1048. if (!watcher) {
  1049. watcher = new this.StatWatcher();
  1050. watcher.start(filename, persistent, interval);
  1051. this.statWatchers[filename] = watcher;
  1052. }
  1053. watcher.addListener('change', listener);
  1054. return watcher;
  1055. }
  1056. unwatchFile(path, listener) {
  1057. const filename = (0, util_1.pathToFilename)(path);
  1058. const watcher = this.statWatchers[filename];
  1059. if (!watcher)
  1060. return;
  1061. if (typeof listener === 'function') {
  1062. watcher.removeListener('change', listener);
  1063. }
  1064. else {
  1065. watcher.removeAllListeners('change');
  1066. }
  1067. if (watcher.listenerCount('change') === 0) {
  1068. watcher.stop();
  1069. delete this.statWatchers[filename];
  1070. }
  1071. }
  1072. createReadStream(path, options) {
  1073. return new this.ReadStream(path, options);
  1074. }
  1075. createWriteStream(path, options) {
  1076. return new this.WriteStream(path, options);
  1077. }
  1078. // watch(path: PathLike): FSWatcher;
  1079. // watch(path: PathLike, options?: IWatchOptions | string): FSWatcher;
  1080. watch(path, options, listener) {
  1081. const filename = (0, util_1.pathToFilename)(path);
  1082. let givenOptions = options;
  1083. if (typeof options === 'function') {
  1084. listener = options;
  1085. givenOptions = null;
  1086. }
  1087. // tslint:disable-next-line prefer-const
  1088. let { persistent, recursive, encoding } = (0, options_1.getDefaultOpts)(givenOptions);
  1089. if (persistent === undefined)
  1090. persistent = true;
  1091. if (recursive === undefined)
  1092. recursive = false;
  1093. const watcher = new this.FSWatcher();
  1094. watcher.start(filename, persistent, recursive, encoding);
  1095. if (listener) {
  1096. watcher.addListener('change', listener);
  1097. }
  1098. return watcher;
  1099. }
  1100. _statfs(filename, bigint = false) {
  1101. // Verify the path exists to match Node.js behavior
  1102. this._core.getResolvedLinkOrThrow(filename, 'statfs');
  1103. return StatFs_1.default.build(this._core, bigint);
  1104. }
  1105. statfsSync(path, options) {
  1106. const { bigint = false } = (0, options_1.getStatfsOptions)(options);
  1107. return this._statfs((0, util_1.pathToFilename)(path), bigint);
  1108. }
  1109. statfs(path, a, b) {
  1110. const [{ bigint = false }, callback] = (0, options_1.getStatfsOptsAndCb)(a, b);
  1111. this.wrapAsync(this._statfs, [(0, util_1.pathToFilename)(path), bigint], callback);
  1112. }
  1113. }
  1114. exports.Volume = Volume;
  1115. Volume.fromJSON = (json, cwd) => new Volume(core_1.Superblock.fromJSON(json, cwd));
  1116. Volume.fromNestedJSON = (json, cwd) => new Volume(core_1.Superblock.fromNestedJSON(json, cwd));
  1117. function emitStop(self) {
  1118. self.emit('stop');
  1119. }
  1120. class StatWatcher extends events_1.EventEmitter {
  1121. constructor(vol) {
  1122. super();
  1123. this.onInterval = () => {
  1124. try {
  1125. const stats = this.vol.statSync(this.filename);
  1126. if (this.hasChanged(stats)) {
  1127. this.emit('change', stats, this.prev);
  1128. this.prev = stats;
  1129. }
  1130. }
  1131. finally {
  1132. this.loop();
  1133. }
  1134. };
  1135. this.vol = vol;
  1136. }
  1137. loop() {
  1138. this.timeoutRef = this.setTimeout(this.onInterval, this.interval);
  1139. }
  1140. hasChanged(stats) {
  1141. // if(!this.prev) return false;
  1142. if (stats.mtimeMs > this.prev.mtimeMs)
  1143. return true;
  1144. if (stats.nlink !== this.prev.nlink)
  1145. return true;
  1146. return false;
  1147. }
  1148. start(path, persistent = true, interval = 5007) {
  1149. this.filename = (0, util_1.pathToFilename)(path);
  1150. this.setTimeout = persistent
  1151. ? setTimeout.bind(typeof globalThis !== 'undefined' ? globalThis : global)
  1152. : setTimeoutUnref_1.default;
  1153. this.interval = interval;
  1154. this.prev = this.vol.statSync(this.filename);
  1155. this.loop();
  1156. }
  1157. stop() {
  1158. clearTimeout(this.timeoutRef);
  1159. (0, queueMicrotask_1.default)(() => {
  1160. emitStop.call(this, this);
  1161. });
  1162. }
  1163. }
  1164. exports.StatWatcher = StatWatcher;
  1165. /* tslint:disable no-var-keyword prefer-const */
  1166. // ---------------------------------------- ReadStream
  1167. var pool;
  1168. function allocNewPool(poolSize) {
  1169. pool = (0, buffer_1.bufferAllocUnsafe)(poolSize);
  1170. pool.used = 0;
  1171. }
  1172. util.inherits(FsReadStream, stream_1.Readable);
  1173. exports.ReadStream = FsReadStream;
  1174. function FsReadStream(vol, path, options) {
  1175. if (!(this instanceof FsReadStream))
  1176. return new FsReadStream(vol, path, options);
  1177. this._vol = vol;
  1178. // a little bit bigger buffer and water marks by default
  1179. options = Object.assign({}, (0, options_1.getOptions)(options, {}));
  1180. if (options.highWaterMark === undefined)
  1181. options.highWaterMark = 64 * 1024;
  1182. stream_1.Readable.call(this, options);
  1183. this.path = (0, util_1.pathToFilename)(path);
  1184. this.fd = options.fd === undefined ? null : typeof options.fd !== 'number' ? options.fd.fd : options.fd;
  1185. this.flags = options.flags === undefined ? 'r' : options.flags;
  1186. this.mode = options.mode === undefined ? 0o666 : options.mode;
  1187. this.start = options.start;
  1188. this.end = options.end;
  1189. this.autoClose = options.autoClose === undefined ? true : options.autoClose;
  1190. this.pos = undefined;
  1191. this.bytesRead = 0;
  1192. if (this.start !== undefined) {
  1193. if (typeof this.start !== 'number') {
  1194. throw new TypeError('"start" option must be a Number');
  1195. }
  1196. if (this.end === undefined) {
  1197. this.end = Infinity;
  1198. }
  1199. else if (typeof this.end !== 'number') {
  1200. throw new TypeError('"end" option must be a Number');
  1201. }
  1202. if (this.start > this.end) {
  1203. throw new Error('"start" option must be <= "end" option');
  1204. }
  1205. this.pos = this.start;
  1206. }
  1207. if (typeof this.fd !== 'number')
  1208. this.open();
  1209. this.on('end', function () {
  1210. if (this.autoClose) {
  1211. if (this.destroy)
  1212. this.destroy();
  1213. }
  1214. });
  1215. }
  1216. FsReadStream.prototype.open = function () {
  1217. var self = this; // tslint:disable-line no-this-assignment
  1218. this._vol.open(this.path, this.flags, this.mode, (er, fd) => {
  1219. if (er) {
  1220. if (self.autoClose) {
  1221. if (self.destroy)
  1222. self.destroy();
  1223. }
  1224. self.emit('error', er);
  1225. return;
  1226. }
  1227. self.fd = fd;
  1228. self.emit('open', fd);
  1229. // start the flow of data.
  1230. self.read();
  1231. });
  1232. };
  1233. FsReadStream.prototype._read = function (n) {
  1234. if (typeof this.fd !== 'number') {
  1235. return this.once('open', function () {
  1236. this._read(n);
  1237. });
  1238. }
  1239. if (this.destroyed)
  1240. return;
  1241. if (!pool || pool.length - pool.used < kMinPoolSpace) {
  1242. // discard the old pool.
  1243. allocNewPool(this._readableState.highWaterMark);
  1244. }
  1245. // Grab another reference to the pool in the case that while we're
  1246. // in the thread pool another read() finishes up the pool, and
  1247. // allocates a new one.
  1248. var thisPool = pool;
  1249. var toRead = Math.min(pool.length - pool.used, n);
  1250. var start = pool.used;
  1251. if (this.pos !== undefined)
  1252. toRead = Math.min(this.end - this.pos + 1, toRead);
  1253. // already read everything we were supposed to read!
  1254. // treat as EOF.
  1255. if (toRead <= 0)
  1256. return this.push(null);
  1257. // the actual read.
  1258. var self = this; // tslint:disable-line no-this-assignment
  1259. this._vol.read(this.fd, pool, pool.used, toRead, this.pos, onread);
  1260. // move the pool positions, and internal position for reading.
  1261. if (this.pos !== undefined)
  1262. this.pos += toRead;
  1263. pool.used += toRead;
  1264. function onread(er, bytesRead) {
  1265. if (er) {
  1266. if (self.autoClose && self.destroy) {
  1267. self.destroy();
  1268. }
  1269. self.emit('error', er);
  1270. }
  1271. else {
  1272. var b = null;
  1273. if (bytesRead > 0) {
  1274. self.bytesRead += bytesRead;
  1275. b = thisPool.slice(start, start + bytesRead);
  1276. }
  1277. self.push(b);
  1278. }
  1279. }
  1280. };
  1281. FsReadStream.prototype._destroy = function (err, cb) {
  1282. this.close(err2 => {
  1283. cb(err || err2);
  1284. });
  1285. };
  1286. FsReadStream.prototype.close = function (cb) {
  1287. var _a;
  1288. if (cb)
  1289. this.once('close', cb);
  1290. if (this.closed || typeof this.fd !== 'number') {
  1291. if (typeof this.fd !== 'number') {
  1292. this.once('open', closeOnOpen);
  1293. return;
  1294. }
  1295. return (0, queueMicrotask_1.default)(() => this.emit('close'));
  1296. }
  1297. // Since Node 18, there is only a getter for '.closed'.
  1298. // The first branch mimics other setters from Readable.
  1299. // See https://github.com/nodejs/node/blob/v18.0.0/lib/internal/streams/readable.js#L1243
  1300. if (typeof ((_a = this._readableState) === null || _a === void 0 ? void 0 : _a.closed) === 'boolean') {
  1301. this._readableState.closed = true;
  1302. }
  1303. else {
  1304. this.closed = true;
  1305. }
  1306. this._vol.close(this.fd, er => {
  1307. if (er)
  1308. this.emit('error', er);
  1309. else
  1310. this.emit('close');
  1311. });
  1312. this.fd = null;
  1313. };
  1314. // needed because as it will be called with arguments
  1315. // that does not match this.close() signature
  1316. function closeOnOpen(fd) {
  1317. this.close();
  1318. }
  1319. util.inherits(FsWriteStream, stream_1.Writable);
  1320. exports.WriteStream = FsWriteStream;
  1321. function FsWriteStream(vol, path, options) {
  1322. if (!(this instanceof FsWriteStream))
  1323. return new FsWriteStream(vol, path, options);
  1324. this._vol = vol;
  1325. options = Object.assign({}, (0, options_1.getOptions)(options, {}));
  1326. stream_1.Writable.call(this, options);
  1327. this.path = (0, util_1.pathToFilename)(path);
  1328. this.fd = options.fd === undefined ? null : typeof options.fd !== 'number' ? options.fd.fd : options.fd;
  1329. this.flags = options.flags === undefined ? 'w' : options.flags;
  1330. this.mode = options.mode === undefined ? 0o666 : options.mode;
  1331. this.start = options.start;
  1332. this.autoClose = options.autoClose === undefined ? true : !!options.autoClose;
  1333. this.pos = undefined;
  1334. this.bytesWritten = 0;
  1335. this.pending = true;
  1336. if (this.start !== undefined) {
  1337. if (typeof this.start !== 'number') {
  1338. throw new TypeError('"start" option must be a Number');
  1339. }
  1340. if (this.start < 0) {
  1341. throw new Error('"start" must be >= zero');
  1342. }
  1343. this.pos = this.start;
  1344. }
  1345. if (options.encoding)
  1346. this.setDefaultEncoding(options.encoding);
  1347. if (typeof this.fd !== 'number')
  1348. this.open();
  1349. // dispose on finish.
  1350. this.once('finish', function () {
  1351. if (this.autoClose) {
  1352. this.close();
  1353. }
  1354. });
  1355. }
  1356. FsWriteStream.prototype.open = function () {
  1357. this._vol.open(this.path, this.flags, this.mode, function (er, fd) {
  1358. if (er) {
  1359. if (this.autoClose && this.destroy) {
  1360. this.destroy();
  1361. }
  1362. this.emit('error', er);
  1363. return;
  1364. }
  1365. this.fd = fd;
  1366. this.pending = false;
  1367. this.emit('open', fd);
  1368. }.bind(this));
  1369. };
  1370. FsWriteStream.prototype._write = function (data, encoding, cb) {
  1371. if (!(data instanceof buffer_1.Buffer || data instanceof Uint8Array))
  1372. return this.emit('error', new Error('Invalid data'));
  1373. if (typeof this.fd !== 'number') {
  1374. return this.once('open', function () {
  1375. this._write(data, encoding, cb);
  1376. });
  1377. }
  1378. var self = this; // tslint:disable-line no-this-assignment
  1379. this._vol.write(this.fd, data, 0, data.length, this.pos, (er, bytes) => {
  1380. if (er) {
  1381. if (self.autoClose && self.destroy) {
  1382. self.destroy();
  1383. }
  1384. return cb(er);
  1385. }
  1386. self.bytesWritten += bytes;
  1387. cb();
  1388. });
  1389. if (this.pos !== undefined)
  1390. this.pos += data.length;
  1391. };
  1392. FsWriteStream.prototype._writev = function (data, cb) {
  1393. if (typeof this.fd !== 'number') {
  1394. return this.once('open', function () {
  1395. this._writev(data, cb);
  1396. });
  1397. }
  1398. const self = this; // tslint:disable-line no-this-assignment
  1399. const len = data.length;
  1400. const chunks = new Array(len);
  1401. var size = 0;
  1402. for (var i = 0; i < len; i++) {
  1403. var chunk = data[i].chunk;
  1404. chunks[i] = chunk;
  1405. size += chunk.length;
  1406. }
  1407. const buf = buffer_1.Buffer.concat(chunks);
  1408. this._vol.write(this.fd, buf, 0, buf.length, this.pos, (er, bytes) => {
  1409. if (er) {
  1410. if (self.destroy)
  1411. self.destroy();
  1412. return cb(er);
  1413. }
  1414. self.bytesWritten += bytes;
  1415. cb();
  1416. });
  1417. if (this.pos !== undefined)
  1418. this.pos += size;
  1419. };
  1420. FsWriteStream.prototype.close = function (cb) {
  1421. var _a;
  1422. if (cb)
  1423. this.once('close', cb);
  1424. if (this.closed || typeof this.fd !== 'number') {
  1425. if (typeof this.fd !== 'number') {
  1426. this.once('open', closeOnOpen);
  1427. return;
  1428. }
  1429. return (0, queueMicrotask_1.default)(() => this.emit('close'));
  1430. }
  1431. // Since Node 18, there is only a getter for '.closed'.
  1432. // The first branch mimics other setters from Writable.
  1433. // See https://github.com/nodejs/node/blob/v18.0.0/lib/internal/streams/writable.js#L766
  1434. if (typeof ((_a = this._writableState) === null || _a === void 0 ? void 0 : _a.closed) === 'boolean') {
  1435. this._writableState.closed = true;
  1436. }
  1437. else {
  1438. this.closed = true;
  1439. }
  1440. this._vol.close(this.fd, er => {
  1441. if (er)
  1442. this.emit('error', er);
  1443. else
  1444. this.emit('close');
  1445. });
  1446. this.fd = null;
  1447. };
  1448. FsWriteStream.prototype._destroy = FsReadStream.prototype._destroy;
  1449. // There is no shutdown() for files.
  1450. FsWriteStream.prototype.destroySoon = FsWriteStream.prototype.end;
  1451. // ---------------------------------------- FSWatcher
  1452. class FSWatcher extends events_1.EventEmitter {
  1453. constructor(vol) {
  1454. super();
  1455. this._filename = '';
  1456. this._filenameEncoded = '';
  1457. // _persistent: boolean = true;
  1458. this._recursive = false;
  1459. this._encoding = encoding_1.ENCODING_UTF8;
  1460. // inode -> removers
  1461. this._listenerRemovers = new Map();
  1462. this._onParentChild = (link) => {
  1463. if (link.getName() === this._getName()) {
  1464. this._emit('rename');
  1465. }
  1466. };
  1467. this._emit = (type) => {
  1468. this.emit('change', type, this._filenameEncoded);
  1469. };
  1470. this._persist = () => {
  1471. this._timer = setTimeout(this._persist, 1e6);
  1472. };
  1473. this._vol = vol;
  1474. // TODO: Emit "error" messages when watching.
  1475. // this._handle.onchange = function(status, eventType, filename) {
  1476. // if (status < 0) {
  1477. // self._handle.close();
  1478. // const error = !filename ?
  1479. // errnoException(status, 'Error watching file for changes:') :
  1480. // errnoException(status, `Error watching file ${filename} for changes:`);
  1481. // error.filename = filename;
  1482. // self.emit('error', error);
  1483. // } else {
  1484. // self.emit('change', eventType, filename);
  1485. // }
  1486. // };
  1487. }
  1488. _getName() {
  1489. return this._steps[this._steps.length - 1];
  1490. }
  1491. start(path, persistent = true, recursive = false, encoding = encoding_1.ENCODING_UTF8) {
  1492. this._filename = (0, util_1.pathToFilename)(path);
  1493. this._steps = (0, util_2.filenameToSteps)(this._filename);
  1494. this._filenameEncoded = (0, encoding_1.strToEncoding)(this._filename);
  1495. // this._persistent = persistent;
  1496. this._recursive = recursive;
  1497. this._encoding = encoding;
  1498. try {
  1499. this._link = this._vol._core.getLinkOrThrow(this._filename, 'FSWatcher');
  1500. }
  1501. catch (err) {
  1502. const error = new Error(`watch ${this._filename} ${err.code}`);
  1503. error.code = err.code;
  1504. error.errno = err.code;
  1505. throw error;
  1506. }
  1507. const watchLinkNodeChanged = (link) => {
  1508. var _a;
  1509. const filepath = link.getPath();
  1510. const node = link.getNode();
  1511. const onNodeChange = () => {
  1512. let filename = relative(this._filename, filepath);
  1513. if (!filename) {
  1514. filename = this._getName();
  1515. }
  1516. return this.emit('change', 'change', filename);
  1517. };
  1518. node.on('change', onNodeChange);
  1519. const removers = (_a = this._listenerRemovers.get(node.ino)) !== null && _a !== void 0 ? _a : [];
  1520. removers.push(() => node.removeListener('change', onNodeChange));
  1521. this._listenerRemovers.set(node.ino, removers);
  1522. };
  1523. const watchLinkChildrenChanged = (link) => {
  1524. var _a;
  1525. const node = link.getNode();
  1526. // when a new link added
  1527. const onLinkChildAdd = (l) => {
  1528. this.emit('change', 'rename', relative(this._filename, l.getPath()));
  1529. // 1. watch changes of the new link-node
  1530. watchLinkNodeChanged(l);
  1531. // 2. watch changes of the new link-node's children
  1532. watchLinkChildrenChanged(l);
  1533. };
  1534. // when a new link deleted
  1535. const onLinkChildDelete = (l) => {
  1536. // remove the listeners of the children nodes
  1537. const removeLinkNodeListeners = (curLink) => {
  1538. const ino = curLink.getNode().ino;
  1539. const removers = this._listenerRemovers.get(ino);
  1540. if (removers) {
  1541. removers.forEach(r => r());
  1542. this._listenerRemovers.delete(ino);
  1543. }
  1544. for (const [name, childLink] of curLink.children.entries()) {
  1545. if (childLink && name !== '.' && name !== '..') {
  1546. removeLinkNodeListeners(childLink);
  1547. }
  1548. }
  1549. };
  1550. removeLinkNodeListeners(l);
  1551. this.emit('change', 'rename', relative(this._filename, l.getPath()));
  1552. };
  1553. // children nodes changed
  1554. for (const [name, childLink] of link.children.entries()) {
  1555. if (childLink && name !== '.' && name !== '..') {
  1556. watchLinkNodeChanged(childLink);
  1557. }
  1558. }
  1559. // link children add/remove
  1560. link.on('child:add', onLinkChildAdd);
  1561. link.on('child:delete', onLinkChildDelete);
  1562. const removers = (_a = this._listenerRemovers.get(node.ino)) !== null && _a !== void 0 ? _a : [];
  1563. removers.push(() => {
  1564. link.removeListener('child:add', onLinkChildAdd);
  1565. link.removeListener('child:delete', onLinkChildDelete);
  1566. });
  1567. if (recursive) {
  1568. for (const [name, childLink] of link.children.entries()) {
  1569. if (childLink && name !== '.' && name !== '..') {
  1570. watchLinkChildrenChanged(childLink);
  1571. }
  1572. }
  1573. }
  1574. };
  1575. watchLinkNodeChanged(this._link);
  1576. watchLinkChildrenChanged(this._link);
  1577. const parent = this._link.parent;
  1578. if (parent) {
  1579. // parent.on('child:add', this._onParentChild);
  1580. parent.setMaxListeners(parent.getMaxListeners() + 1);
  1581. parent.on('child:delete', this._onParentChild);
  1582. }
  1583. if (persistent)
  1584. this._persist();
  1585. }
  1586. close() {
  1587. clearTimeout(this._timer);
  1588. this._listenerRemovers.forEach(removers => {
  1589. removers.forEach(r => r());
  1590. });
  1591. this._listenerRemovers.clear();
  1592. const parent = this._link.parent;
  1593. if (parent) {
  1594. // parent.removeListener('child:add', this._onParentChild);
  1595. parent.removeListener('child:delete', this._onParentChild);
  1596. }
  1597. }
  1598. }
  1599. exports.FSWatcher = FSWatcher;
  1600. //# sourceMappingURL=volume.js.map