GeezerVison is showing github.com — fs: use correct position when retrying writev · nodejs/node@af9e5ed
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: lib/internal/fs/streams.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -473,7 +473,7 @@ function writeAll(data, size, pos, cb, retries = 0) { | 473 | 473 | } | 474 | 474 |
| 475 | 475 | function writevAll(chunks, size, pos, cb, retries = 0) { | 476 | | - this[kFs].writev(this.fd, chunks, this.pos, (er, bytesWritten, buffers) => { | | 476 | + this[kFs].writev(this.fd, chunks, pos, (er, bytesWritten, buffers) => { | 477 | 477 | // No data currently available and operation should be retried later. | 478 | 478 | if (er?.code === 'EAGAIN') { | 479 | 479 | er = null; | |
Collapse file Copy file name to clipboard +74Lines changed: 74 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | +const common = require('../common'); | | 3 | +const tmpdir = require('../common/tmpdir'); | | 4 | +const assert = require('assert'); | | 5 | +const fs = require('fs'); | | 6 | + | | 7 | +tmpdir.refresh(); | | 8 | + | | 9 | +const data = 'AAAABBBBCCCC'; | | 10 | + | | 11 | +// Report back fewer bytes than were handed over, asynchronously: the retry has | | 12 | +// to observe the position as it is after _writev() advanced this.pos by the | | 13 | +// whole batch. | | 14 | +function shortWrite(bytes) { | | 15 | + return (fd, chunks, position, cb) => { | | 16 | + const head = Buffer.concat(chunks).subarray(0, bytes); | | 17 | + fs.write(fd, head, 0, bytes, position, | | 18 | + (er, bytesWritten) => cb(er, bytesWritten, chunks)); | | 19 | + }; | | 20 | +} | | 21 | + | | 22 | +function eagain(fd, chunks, position, cb) { | | 23 | + process.nextTick(cb, Object.assign(new Error(), { code: 'EAGAIN' }), 0, chunks); | | 24 | +} | | 25 | + | | 26 | +// The positions handed to writev() are the contract under test: watching the | | 27 | +// resulting file alone would still pass if some later rewrite happened to land | | 28 | +// the bytes correctly by another route. | | 29 | +function run(name, { prefix = '', start, firstCall }, expectedPositions) { | | 30 | + const file = tmpdir.resolve(`partial-writev-${name}.txt`); | | 31 | + fs.writeFileSync(file, prefix); | | 32 | + | | 33 | + const positions = []; | | 34 | + const stream = fs.createWriteStream(file, { | | 35 | + flags: 'r+', | | 36 | + start, | | 37 | + fs: { | | 38 | + ...fs, | | 39 | + writev: common.mustCall((fd, chunks, position, cb) => { | | 40 | + positions.push(position); | | 41 | + if (positions.length === 1) { | | 42 | + firstCall(fd, chunks, position, cb); | | 43 | + } else { | | 44 | + fs.writev(fd, chunks, position, cb); | | 45 | + } | | 46 | + }, 2), | | 47 | + }, | | 48 | + }); | | 49 | + | | 50 | + stream.cork(); | | 51 | + stream.write('AAAA'); | | 52 | + stream.write('BBBB'); | | 53 | + stream.write('CCCC'); | | 54 | + stream.uncork(); | | 55 | + stream.end(); | | 56 | + | | 57 | + stream.on('error', common.mustNotCall()); | | 58 | + stream.on('close', common.mustCall(() => { | | 59 | + if (expectedPositions !== undefined) { | | 60 | + assert.deepStrictEqual(positions, expectedPositions, `${name} positions`); | | 61 | + } | | 62 | + assert.strictEqual(fs.readFileSync(file, 'utf8'), prefix + data, name); | | 63 | + })); | | 64 | +} | | 65 | + | | 66 | +// Partial write: resume right after the bytes that reached the disk. | | 67 | +run('start-5', { prefix: '#####', start: 5, firstCall: shortWrite(3) }, [5, 8]); | | 68 | +run('start-0', { start: 0, firstCall: shortWrite(3) }, [0, 3]); | | 69 | + | | 70 | +// EAGAIN: nothing was written, so the retry goes back to the same position. | | 71 | +run('eagain', { prefix: '#####', start: 5, firstCall: eagain }, [5, 5]); | | 72 | + | | 73 | +// Without a start the stream follows the fd offset; only the bytes matter. | | 74 | +run('no-start', { firstCall: shortWrite(3) }); |
|