From: Elijah Insua Date: Thu, 13 May 2010 19:42:17 +0000 (-0700) Subject: Handle empty files with fs.readFile X-Git-Tag: v0.1.95~3 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=976926376d249a92ee95cf585d58c31ccdd85545;p=platform%2Fupstream%2Fnodejs.git Handle empty files with fs.readFile --- diff --git a/lib/fs.js b/lib/fs.js index 033b8b7..fcb89d2 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -51,6 +51,10 @@ fs.readFile = function (path, encoding_, callback) { var buffer = new Buffer(size); var offset = 0; function doRead() { + if (size < 1) { + callback(null, buffer); + return; + } // position is offset or null so we can read files on unseekable mediums binding.read(fd, buffer, offset, size - offset, offset || null, function (err, amount) { if (err) { diff --git a/test/fixtures/empty.txt b/test/fixtures/empty.txt new file mode 100644 index 0000000..e69de29 diff --git a/test/simple/test-fs-readfile-empty.js b/test/simple/test-fs-readfile-empty.js new file mode 100644 index 0000000..8073fb5 --- /dev/null +++ b/test/simple/test-fs-readfile-empty.js @@ -0,0 +1,10 @@ +require('../common'); + +var + path = require('path'), + fs = require('fs'), + fn = path.join(fixturesDir, 'empty.txt'); + +fs.readFile(fn, function(err, data) { + assert.ok(data); +}); \ No newline at end of file