vfs: read RealFSProvider files from open fd · nodejs/node@bbdd764 · GitHub
Skip to content

Commit bbdd764

Browse files
trivikrrichardlau
authored andcommitted
vfs: read RealFSProvider files from open fd
Read RealFileHandle contents through the open file descriptor instead of reopening the original real path. This keeps already-open VFS file descriptors usable after the backing file is renamed. Use positioned reads so readFileSync() and readFile() preserve the handle's current offset. Signed-off-by: Kamat, Trivikram <16024985+trivikr@users.noreply.github.com> Assisted-by: openai:gpt-5.5 PR-URL: #64104 Fixes: #64103 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent f4e7bf1 commit bbdd764

3 files changed

Lines changed: 163 additions & 2 deletions

File tree

lib/internal/vfs/providers/real.js

Lines changed: 85 additions & 2 deletions

test/parallel/test-vfs-fs-readFileSync.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,32 @@ assert.strictEqual(
4343
}
4444

4545
myVfs.unmount();
46+
47+
// readFileSync via a RealFSProvider fd remains usable after the backing path
48+
// is renamed.
49+
{
50+
const root = path.join('/tmp', 'vfs-real-readFileSync-' + process.pid);
51+
const realMountPoint = path.join('/tmp', 'vfs-real-readFileSync-mount-' + process.pid);
52+
fs.rmSync(root, { recursive: true, force: true });
53+
fs.rmSync(realMountPoint, { recursive: true, force: true });
54+
fs.mkdirSync(root, { recursive: true });
55+
fs.mkdirSync(realMountPoint, { recursive: true });
56+
57+
const realVfs = vfs
58+
.create(new vfs.RealFSProvider(root), { emitExperimentalWarning: false })
59+
.mount(realMountPoint);
60+
try {
61+
fs.writeFileSync(path.join(root, 'a.txt'), 'still readable');
62+
const fd = fs.openSync(path.join(realMountPoint, 'a.txt'), 'r');
63+
try {
64+
fs.renameSync(path.join(root, 'a.txt'), path.join(root, 'b.txt'));
65+
assert.strictEqual(fs.readFileSync(fd, 'utf8'), 'still readable');
66+
} finally {
67+
fs.closeSync(fd);
68+
}
69+
} finally {
70+
realVfs.unmount();
71+
fs.rmSync(root, { recursive: true, force: true });
72+
fs.rmSync(realMountPoint, { recursive: true, force: true });
73+
}
74+
}

test/parallel/test-vfs-real-provider-handle.js

Lines changed: 49 additions & 0 deletions

0 commit comments

Comments
 (0)