Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs: make sure readFile[Sync] reads from the beginning #9699

Merged
merged 6 commits into from
Jan 14, 2017
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ ReadFileContext.prototype.read = function() {
req.oncomplete = readFileAfterRead;
req.context = this;

binding.read(this.fd, buffer, offset, length, -1, req);
binding.read(this.fd, buffer, offset, length, this.pos, req);
};

ReadFileContext.prototype.close = function(err) {
Expand Down Expand Up @@ -449,11 +449,11 @@ function tryCreateBuffer(size, fd, isUserFd) {
return buffer;
}

function tryReadSync(fd, isUserFd, buffer, pos, len) {
function tryReadSync(fd, isUserFd, buffer, pos, len, offset) {
var threw = true;
var bytesRead;
try {
bytesRead = fs.readSync(fd, buffer, pos, len);
bytesRead = fs.readSync(fd, buffer, pos, len, offset);
threw = false;
} finally {
if (threw && !isUserFd) fs.closeSync(fd);
Expand Down Expand Up @@ -482,15 +482,15 @@ fs.readFileSync = function(path, options) {

if (size !== 0) {
do {
bytesRead = tryReadSync(fd, isUserFd, buffer, pos, size - pos);
bytesRead = tryReadSync(fd, isUserFd, buffer, pos, size - pos, pos);
pos += bytesRead;
} while (bytesRead !== 0 && pos < size);
} else {
do {
// the kernel lies about many files.
// Go ahead and try to read some bytes.
buffer = Buffer.allocUnsafe(8192);
bytesRead = tryReadSync(fd, isUserFd, buffer, 0, 8192);
bytesRead = tryReadSync(fd, isUserFd, buffer, 0, 8192, pos);
if (bytesRead !== 0) {
buffers.push(buffer.slice(0, bytesRead));
}
Expand Down
23 changes: 23 additions & 0 deletions test/parallel/test-fs-readfile-fd-offset.js
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 = new Array(100).join('a');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'a'.repeat(100)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'm still not up to speed with ES6. In my defense, I copied from test-fs-readfilesync-pipe-large.js.

fs.writeFileSync(filename, dataExpected);
const fileLength = dataExpected.length;

['r', 'a+'].forEach(mode => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't the linter complaining that it should be (mode) => {?

const fd = fs.openSync(filename, mode);
assert.strictEqual(fileLength, fs.readFileSync(fd).length);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tiny nit: it should be assert.strictEqual(actual, expected) here and below.


// Reading again should result in the same length.
assert.strictEqual(fileLength, fs.readFileSync(fd).length);

fs.readFile(fd, common.mustCall((err, buf) => {
assert.ifError(err);
assert.strictEqual(fileLength, buf.length);
}));
});