From 976926376d249a92ee95cf585d58c31ccdd85545 Mon Sep 17 00:00:00 2001 From: Elijah Insua Date: Thu, 13 May 2010 12:42:17 -0700 Subject: [PATCH] Handle empty files with fs.readFile --- lib/fs.js | 4 ++++ test/fixtures/empty.txt | 0 test/simple/test-fs-readfile-empty.js | 10 ++++++++++ 3 files changed, 14 insertions(+) create mode 100644 test/fixtures/empty.txt create mode 100644 test/simple/test-fs-readfile-empty.js diff --git a/lib/fs.js b/lib/fs.js index 033b8b7a14..fcb89d29f5 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 0000000000..e69de29bb2 diff --git a/test/simple/test-fs-readfile-empty.js b/test/simple/test-fs-readfile-empty.js new file mode 100644 index 0000000000..8073fb5938 --- /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