fs: make sure readFile[Sync] reads from the beginning
Checklist
-
make -j8 test
(UNIX), orvcbuild test nosign
(Windows) passes -
tests and/or benchmarks are included -
commit message follows commit guidelines
Affected core subsystem(s)
fs
Description of change
Currently, fs.readFile[Sync]
starts reading from the current file position, which can be non-zero if the fd
was previously read or written from/to. This conflicts with the documentation which states that it "reads the entire contents of a file".
Fixes: https://github.com/nodejs/node/issues/9671
Provisional testcase for fs.readFile
:
const assert = require('assert');
const fs = require('fs');
const filleLength = 2;
const path = 'test-read-file';
const fd = fs.openSync(path, 'w+');
fs.writeSync(fd, Buffer.alloc(filleLength, 1), 0, filleLength);
fs.readFile(fd, (err, buf) => {
if (err) throw err;
assert.strictEqual(buf.length, filleLength);
});
For fs.readFileSync
:
'use strict';
const assert = require('assert');
const fs = require('fs');
const fileLength = 2;
const path = 'test-read-file';
const fd = fs.openSync(path, 'w+');
fs.writeSync(fd, Buffer.alloc(fileLength, 1), 0, fileLength);
assert.strictEqual(fs.readFileSync(fd).length, fileLength);