Skip to content

Commit 1d3d02c

Browse files
committed
Fix fs.readfile('/dev/stdin')
There is no need for fs.readFile() to be using pread rather than read. The default semantics of read() are such that subsequent reads are where we want them anyway.
1 parent 7887e68 commit 1d3d02c

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

lib/fs.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,9 @@ fs.readFile = function(path, encoding_) {
135135
function read() {
136136
if (size === 0) {
137137
buffer = new Buffer(8192);
138-
fs.read(fd, buffer, 0, 8192, pos, afterRead);
138+
fs.read(fd, buffer, 0, 8192, -1, afterRead);
139139
} else {
140-
fs.read(fd, buffer, pos, size - pos, pos, afterRead);
140+
fs.read(fd, buffer, pos, size - pos, -1, afterRead);
141141
}
142142
}
143143

test/simple/test-fs-readfile-pipe.js

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
var common = require('../common');
23+
var assert = require('assert');
24+
25+
// simulate `cat readfile.js | node readfile.js`
26+
27+
// TODO: Have some way to make this work on windows.
28+
if (process.platform === 'win32') {
29+
console.error('No /dev/stdin on windows. Skipping test.');
30+
process.exit();
31+
}
32+
33+
var fs = require('fs');
34+
35+
var dataExpected = fs.readFileSync(__filename, 'utf8');
36+
37+
if (process.argv[2] === 'child') {
38+
fs.readFile('/dev/stdin', function(er, data) {
39+
if (er) throw er;
40+
process.stdout.write(data);
41+
});
42+
return;
43+
}
44+
45+
var exec = require('child_process').exec;
46+
var f = JSON.stringify(__filename);
47+
var node = JSON.stringify(process.execPath);
48+
var cmd = 'cat ' + f + ' | ' + node + ' ' + f + ' child';
49+
exec(cmd, function(err, stdout, stderr) {
50+
if (err) console.error(err);
51+
assert(!err, 'it exits normally');
52+
assert(stdout === dataExpected, 'it reads the file and outputs it');
53+
assert(stderr === '', 'it does not write to stderr');
54+
console.log('ok');
55+
});

0 commit comments

Comments
 (0)