fs: use stat.st_size only to read regular files

Using st_size to read non-regular files can lead to not reading all the
data.

PR-URL: https://github.com/iojs/io.js/pull/1074
Reviewed-By: Bert Belder <bertbelder@gmail.com>
This commit is contained in:
Santiago Gimeno 2015-03-05 18:33:38 +01:00 committed by Bert Belder
parent 0782c24993
commit a6af709489

View File

@ -317,7 +317,7 @@ function readFileAfterStat(err, st) {
if (err) if (err)
return context.close(err); return context.close(err);
var size = context.size = st.size; var size = context.size = st.isFile() ? st.size : 0;
if (size === 0) { if (size === 0) {
context.buffers = []; context.buffers = [];
@ -395,10 +395,12 @@ fs.readFileSync = function(path, options) {
var flag = options.flag || 'r'; var flag = options.flag || 'r';
var fd = fs.openSync(path, flag, 0o666); var fd = fs.openSync(path, flag, 0o666);
var st;
var size; var size;
var threw = true; var threw = true;
try { try {
size = fs.fstatSync(fd).size; st = fs.fstatSync(fd);
size = st.isFile() ? st.size : 0;
threw = false; threw = false;
} finally { } finally {
if (threw) fs.closeSync(fd); if (threw) fs.closeSync(fd);