detect 0 length fs writes with tests

pull/5370/head
Marco Rogers 2010-08-17 00:10:49 -04:00 committed by Ryan Dahl
parent 4db608dbba
commit 6744e59e46
3 changed files with 9 additions and 0 deletions

View File

@ -208,6 +208,7 @@ fs.write = function (fd, buffer, offset, length, position, callback) {
offset = 0;
length = buffer.length;
}
if(!length) return;
binding.write(fd, buffer, offset, length, position, callback || noop);
};
@ -221,6 +222,7 @@ fs.writeSync = function (fd, buffer, offset, length, position) {
offset = 0;
length = buffer.length;
}
if(!length) return 0;
return binding.write(fd, buffer, offset, length, position);
};

View File

@ -8,6 +8,10 @@ fn = path.join(common.fixturesDir, 'write.txt');
foo = 'foo'
var fd = fs.openSync(fn, 'w');
written = fs.writeSync(fd, '');
assert.strictEqual(0, written);
fs.writeSync(fd, foo);
bar = 'bár'

View File

@ -10,6 +10,9 @@ var found;
fs.open(fn, 'w', 0644, function (err, fd) {
if (err) throw err;
console.log('open done');
fs.write(fd, '', 0, 'utf8', function(err, written) {
assert.fail('zero length write should not go through to callback');
});
fs.write(fd, expected, 0, "utf8", function (err, written) {
console.log('write done');
if (err) throw err;