Skip to content

Commit

Permalink
fs: WriteStream should handle partial writes
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Keen Yee Liau committed Sep 20, 2018
1 parent bad670c commit 6456af8
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
18 changes: 15 additions & 3 deletions lib/internal/fs/streams.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,16 +308,28 @@ WriteStream.prototype._write = function(data, encoding, cb) {
});
}

fs.write(this.fd, data, 0, data.length, this.pos, (er, bytes) => {
const len = data.length;
let pos = this.pos;
let offset = 0;
const callback = (er, bytes) => {
if (er) {
if (this.autoClose) {
this.destroy();
}
return cb(er);
}
this.bytesWritten += bytes;
cb();
});
offset += bytes;
if (offset < len) {
if (pos !== undefined) {
pos += bytes;
}
fs.write(this.fd, data, offset, len - offset, pos, callback);
} else {
cb();
}
};
fs.write(this.fd, data, offset, len, pos, callback);

if (this.pos !== undefined)
this.pos += data.length;
Expand Down
42 changes: 42 additions & 0 deletions test/parallel/test-fs-write-stream-partial-write.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const path = require('path');
const fs = require('fs');

const tmpdir = require('../common/tmpdir');
const file = path.join(tmpdir.path, 'out.txt');

tmpdir.refresh();

{
const _fs_write = fs.write;
let i = 0;
fs.write = function(fd, data, offset, length, pos, cb) {
if (i === 0) {
const half = Math.floor(length / 2);
_fs_write(fd, data, offset, half, pos, cb);
} else {
assert(
offset > 0,
`second call to fs.write() must provide non-zero offset, got ${offset}`
);
_fs_write(fd, data, offset, length, pos, cb);
}
i++;
};

const stream = fs.createWriteStream(file);
const data = Buffer.from('Hello World, how are you?', 'ascii');
stream.write(data, 'ascii', common.mustCall(function(err) {
assert.ifError(err);
}));
stream.end();

process.on('exit', function() {
assert.strictEqual(
i, 2, 'expected fs.write() to be called twice but got ' + i);
const content = fs.readFileSync(file);
assert.strictEqual(content.toString(), 'Hello World, how are you?');
});
}

0 comments on commit 6456af8

Please sign in to comment.