Files
node/lib/internal/vfs/dir.js
T
Philipp Dunkel ea51c141d5 vfs: close the fs hook gaps for mounted paths
Several `node:fs` entry points behave differently for a mounted path
than for a real one, because of how the call reaches the VFS hooks.
Make them behave as they do for a real path:

* Add the `watchFile`, `unwatchFile` and `promisesWatch` handlers,
  backed by the provider's stat watcher and async watcher; those calls
  threw a TypeError before. Have `watch` refuse a path that does not
  exist with ENOENT instead of handing back a watcher that polls
  forever and keeps the process alive.
* Convert timestamps and validate arguments before the hook runs in
  `utimes`, `lutimes` and `readdir` (sync, callback and promise forms),
  so a mounted path gets the same ERR_INVALID_ARG_* errors and the same
  seconds-since-epoch numbers as a real one.
* Pass the mode and times through to the `fchmod` and `futimes` hooks
  and route them to the handle's entry, so descriptor operations take
  effect like their path forms instead of being no-ops; the memory
  handle validates the way a FileHandle would since one calls it
  directly.
* Treat a `mkdtemp` prefix as text rather than a path when it ends in a
  separator, so the directory is created inside the intended parent.
* Map the first directory a recursive `mkdir` created back under the
  mount point instead of returning the provider-relative path.
* Make disposing an already closed virtual `Dir` a no-op, as on the
  native `Dir`, instead of rejecting with ERR_DIR_CLOSED.

test-vfs-fs-hook-gaps adds a test per gap, stating the real-fs outcome
as the expectation. The existing file handle test asserted that
`chmod()` and `utimes()` without arguments were no-ops; they now
validate and apply, so it exercises that instead.

Signed-off-by: Philipp Dunkel <pip@pipobscure.com>
PR-URL: https://github.com/nodejs/node/pull/65852
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: Filip Skokan <panva.ip@gmail.com>
2026-09-15 16:06:32 +00:00

110 lines
1.9 KiB
JavaScript

'use strict';
const {
SymbolAsyncDispose,
SymbolAsyncIterator,
SymbolDispose,
} = primordials;
const {
codes: {
ERR_DIR_CLOSED,
},
} = require('internal/errors');
/**
* Virtual directory handle returned by VFS opendir/opendirSync.
* Mimics the subset of the native Dir interface used by Node.js internals
* (e.g. fs.cp, fs.promises.cp).
*/
class VirtualDir {
#path;
#entries;
#index;
#closed;
constructor(dirPath, entries) {
this.#path = dirPath;
this.#entries = entries;
this.#index = 0;
this.#closed = false;
}
get path() {
return this.#path;
}
readSync() {
if (this.#closed) {
throw new ERR_DIR_CLOSED();
}
if (this.#index >= this.#entries.length) {
return null;
}
return this.#entries[this.#index++];
}
async read(callback) {
if (typeof callback === 'function') {
try {
const result = this.readSync();
process.nextTick(callback, null, result);
} catch (err) {
process.nextTick(callback, err);
}
return;
}
return this.readSync();
}
closeSync() {
if (this.#closed) {
throw new ERR_DIR_CLOSED();
}
this.#closed = true;
}
async close(callback) {
if (typeof callback === 'function') {
this.closeSync();
process.nextTick(callback, null);
return;
}
this.closeSync();
}
async *entries() {
if (this.#closed) {
throw new ERR_DIR_CLOSED();
}
try {
let entry;
while ((entry = this.readSync()) !== null) {
yield entry;
}
} finally {
if (!this.#closed) {
this.closeSync();
}
}
}
[SymbolDispose]() {
if (!this.#closed) {
this.closeSync();
}
}
async [SymbolAsyncDispose]() {
if (!this.#closed) {
this.closeSync();
}
}
}
VirtualDir.prototype[SymbolAsyncIterator] = VirtualDir.prototype.entries;
module.exports = {
VirtualDir,
};