Skip to content

Commit 6456af8

Browse files
author
Keen Yee Liau
committed
fs: WriteStream should handle partial writes
Given a buffer of length l, fs.write() will not necessarily write the entire buffer to the file. This can occur if, for example, there is insufficient space on the underlying physical medium. WriteStream did not handle this case, and when partial write occurs, it will errorneously report that the write is successful. This commit changes the _write() behavior to continue the write operation, picking up from where the last operation left off. More information about the write() system call is available at http://man7.org/linux/man-pages/man2/write.2.html
1 parent bad670c commit 6456af8

File tree

2 files changed

+57
-3
lines changed

2 files changed

+57
-3
lines changed

lib/internal/fs/streams.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -308,16 +308,28 @@ WriteStream.prototype._write = function(data, encoding, cb) {
308308
});
309309
}
310310

311-
fs.write(this.fd, data, 0, data.length, this.pos, (er, bytes) => {
311+
const len = data.length;
312+
let pos = this.pos;
313+
let offset = 0;
314+
const callback = (er, bytes) => {
312315
if (er) {
313316
if (this.autoClose) {
314317
this.destroy();
315318
}
316319
return cb(er);
317320
}
318321
this.bytesWritten += bytes;
319-
cb();
320-
});
322+
offset += bytes;
323+
if (offset < len) {
324+
if (pos !== undefined) {
325+
pos += bytes;
326+
}
327+
fs.write(this.fd, data, offset, len - offset, pos, callback);
328+
} else {
329+
cb();
330+
}
331+
};
332+
fs.write(this.fd, data, offset, len, pos, callback);
321333

322334
if (this.pos !== undefined)
323335
this.pos += data.length;
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'use strict';
2+
const common = require('../common');
3+
const assert = require('assert');
4+
const path = require('path');
5+
const fs = require('fs');
6+
7+
const tmpdir = require('../common/tmpdir');
8+
const file = path.join(tmpdir.path, 'out.txt');
9+
10+
tmpdir.refresh();
11+
12+
{
13+
const _fs_write = fs.write;
14+
let i = 0;
15+
fs.write = function(fd, data, offset, length, pos, cb) {
16+
if (i === 0) {
17+
const half = Math.floor(length / 2);
18+
_fs_write(fd, data, offset, half, pos, cb);
19+
} else {
20+
assert(
21+
offset > 0,
22+
`second call to fs.write() must provide non-zero offset, got ${offset}`
23+
);
24+
_fs_write(fd, data, offset, length, pos, cb);
25+
}
26+
i++;
27+
};
28+
29+
const stream = fs.createWriteStream(file);
30+
const data = Buffer.from('Hello World, how are you?', 'ascii');
31+
stream.write(data, 'ascii', common.mustCall(function(err) {
32+
assert.ifError(err);
33+
}));
34+
stream.end();
35+
36+
process.on('exit', function() {
37+
assert.strictEqual(
38+
i, 2, 'expected fs.write() to be called twice but got ' + i);
39+
const content = fs.readFileSync(file);
40+
assert.strictEqual(content.toString(), 'Hello World, how are you?');
41+
});
42+
}

0 commit comments

Comments
 (0)