forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fs: ensure readFile[Sync] reads from the beginning
It would previously read from the current file position, which can be non-zero if the `fd` has been read from or written to. This contradicts the documentation which states that it "reads the entire contents of a file". PR-URL: nodejs#9699 Fixes: nodejs#9671 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Sam Roberts <vieuxtech@gmail.com>
- Loading branch information
1 parent
5fb91ed
commit a500d30
Showing
2 changed files
with
28 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
|
||
const filename = path.join(common.tmpDir, 'readfile.txt'); | ||
const dataExpected = 'a'.repeat(100); | ||
fs.writeFileSync(filename, dataExpected); | ||
const fileLength = dataExpected.length; | ||
|
||
['r', 'a+'].forEach((mode) => { | ||
const fd = fs.openSync(filename, mode); | ||
assert.strictEqual(fs.readFileSync(fd).length, fileLength); | ||
|
||
// Reading again should result in the same length. | ||
assert.strictEqual(fs.readFileSync(fd).length, fileLength); | ||
|
||
fs.readFile(fd, common.mustCall((err, buf) => { | ||
assert.ifError(err); | ||
assert.strictEqual(buf.length, fileLength); | ||
})); | ||
}); |