Skip to content

Commit 58e1202

Browse files
committed
vfs: add callback readv and writev handlers
A mounted VSF causes callback readv and writev to call missing handlers, breaking both virtual and real file descriptors. This adds both handelrs and preserves I/O for real descriptors. Signed-off-by: Daniel Cavalcante <dcavalcante@gmail.com>
1 parent 9746ebc commit 58e1202

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
@@ -621,12 +621,24 @@ function createVfsHandlers() {
621621
return vfd.entry.read(buffer, offset, length, position)
622622
.then(({ bytesRead }) => bytesRead);
623623
},
624+
readv(fd, buffers, position) {
625+
const vfd = getVirtualFd(fd);
626+
if (!vfd) return undefined;
627+
return vfd.entry.readv(buffers, position)
628+
.then(({ bytesRead }) => bytesRead);
629+
},
624630
write(fd, buffer, offset, length, position) {
625631
const vfd = getVirtualFd(fd);
626632
if (!vfd) return undefined;
627633
return vfd.entry.write(buffer, offset, length, position)
628634
.then(({ bytesWritten }) => bytesWritten);
629635
},
636+
writev(fd, buffers, position) {
637+
const vfd = getVirtualFd(fd);
638+
if (!vfd) return undefined;
639+
return vfd.entry.writev(buffers, position)
640+
.then(({ bytesWritten }) => bytesWritten);
641+
},
630642
fstat(fd, options) {
631643
const vfd = getVirtualFd(fd);
632644
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)