-
Notifications
You must be signed in to change notification settings - Fork 30.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Keen Yee Liau
committed
Sep 20, 2018
1 parent
bad670c
commit 6456af8
Showing
2 changed files
with
57 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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?'); | ||
}); | ||
} |