Handle empty files with fs.readFile
authorElijah Insua <tmpvar@gmail.com>
Thu, 13 May 2010 19:42:17 +0000 (12:42 -0700)
committerRyan Dahl <ry@tinyclouds.org>
Thu, 13 May 2010 19:42:17 +0000 (12:42 -0700)
lib/fs.js
test/fixtures/empty.txt [new file with mode: 0644]
test/simple/test-fs-readfile-empty.js [new file with mode: 0644]

index 033b8b7..fcb89d2 100644 (file)
--- 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 (file)
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 (file)
index 0000000..8073fb5
--- /dev/null
@@ -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