Browse Source

detect 0 length fs writes with tests

v0.7.4-release
Marco Rogers 15 years ago
committed by Ryan Dahl
parent
commit
6744e59e46
  1. 2
      lib/fs.js
  2. 4
      test/simple/test-fs-write-sync.js
  3. 3
      test/simple/test-fs-write.js

2
lib/fs.js

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

4
test/simple/test-fs-write-sync.js

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

3
test/simple/test-fs-write.js

@ -10,6 +10,9 @@ var found;
fs.open(fn, 'w', 0644, function (err, fd) { fs.open(fn, 'w', 0644, function (err, fd) {
if (err) throw err; if (err) throw err;
console.log('open done'); 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) { fs.write(fd, expected, 0, "utf8", function (err, written) {
console.log('write done'); console.log('write done');
if (err) throw err; if (err) throw err;

Loading…
Cancel
Save