Skip to content

Commit 0b4d0ac

Browse files
authored
vfs: add callback readv and writev handlers
A mounted VFS causes callback readv and writev to call missing handlers, breaking both virtual and real file descriptors. Add both handlers while preserving I/O for real descriptors. Signed-off-by: Daniel Cavalcante <dcavalcante@gmail.com> PR-URL: #66548 Fixes: #66532 Reviewed-By: Philipp Dunkel <pip@pipobscure.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 3f8e1f5 commit 0b4d0ac

2 files changed

Lines changed: 47 additions & 0 deletions

File tree

‎lib/internal/vfs/setup.js‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,12 +658,24 @@ function createVfsHandlers() {
658658
return vfd.entry.read(buffer, offset, length, position)
659659
.then(({ bytesRead }) => bytesRead);
660660
},
661+
readv(fd, buffers, position) {
662+
const vfd = getVirtualFd(fd);
663+
if (!vfd) return undefined;
664+
return vfd.entry.readv(buffers, position)
665+
.then(({ bytesRead }) => bytesRead);
666+
},
661667
write(fd, buffer, offset, length, position) {
662668
const vfd = getVirtualFd(fd);
663669
if (!vfd) return undefined;
664670
return vfd.entry.write(buffer, offset, length, position)
665671
.then(({ bytesWritten }) => bytesWritten);
666672
},
673+
writev(fd, buffers, position) {
674+
const vfd = getVirtualFd(fd);
675+
if (!vfd) return undefined;
676+
return vfd.entry.writev(buffers, position)
677+
.then(({ bytesWritten }) => bytesWritten);
678+
},
667679
fstat(fd, options) {
668680
const vfd = getVirtualFd(fd);
669681
if (!vfd) return undefined;
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Flags: --experimental-vfs
2+
'use strict';
3+
4+
const common = require('../common');
5+
const assert = require('node:assert');
6+
const fs = require('node:fs');
7+
const path = require('node:path');
8+
const tmpdir = require('../common/tmpdir');
9+
const vfs = require('node:vfs');
10+
11+
tmpdir.refresh();
12+
const mounted = vfs.create();
13+
mounted.mkdirSync('/files', { recursive: true });
14+
const mountPoint = mounted.mount();
15+
16+
const realFd = fs.openSync(tmpdir.resolve('real.txt'), 'w+');
17+
const virtualFd = fs.openSync(path.join(mountPoint, 'files/virtual.txt'), 'w+');
18+
let remaining = 2;
19+
20+
for (const fd of [realFd, virtualFd]) {
21+
const chunks = [Buffer.from('hello'), Buffer.from('world')];
22+
fs.writev(fd, chunks, null, common.mustSucceed((bytesWritten, buffers) => {
23+
assert.strictEqual(bytesWritten, 10);
24+
assert.strictEqual(buffers, chunks);
25+
26+
const received = [Buffer.alloc(5), Buffer.alloc(5)];
27+
fs.readv(fd, received, 0, common.mustSucceed((bytesRead, readBuffers) => {
28+
assert.strictEqual(bytesRead, 10);
29+
assert.strictEqual(readBuffers, received);
30+
assert.strictEqual(Buffer.concat(received).toString(), 'helloworld');
31+
fs.closeSync(fd);
32+
if (--remaining === 0) mounted.unmount();
33+
}));
34+
}));
35+
}

0 commit comments

Comments
 (0)