Browse Source

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.
v0.9.1-release
isaacs 13 years ago
parent
commit
1d3d02c70d
  1. 4
      lib/fs.js
  2. 55
      test/simple/test-fs-readfile-pipe.js

4
lib/fs.js

@ -135,9 +135,9 @@ fs.readFile = function(path, encoding_) {
function read() {
if (size === 0) {
buffer = new Buffer(8192);
fs.read(fd, buffer, 0, 8192, pos, afterRead);
fs.read(fd, buffer, 0, 8192, -1, afterRead);
} else {
fs.read(fd, buffer, pos, size - pos, pos, afterRead);
fs.read(fd, buffer, pos, size - pos, -1, afterRead);
}
}

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

@ -0,0 +1,55 @@
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
var common = require('../common');
var assert = require('assert');
// simulate `cat readfile.js | node readfile.js`
// TODO: Have some way to make this work on windows.
if (process.platform === 'win32') {
console.error('No /dev/stdin on windows. Skipping test.');
process.exit();
}
var fs = require('fs');
var dataExpected = fs.readFileSync(__filename, 'utf8');
if (process.argv[2] === 'child') {
fs.readFile('/dev/stdin', function(er, data) {
if (er) throw er;
process.stdout.write(data);
});
return;
}
var exec = require('child_process').exec;
var f = JSON.stringify(__filename);
var node = JSON.stringify(process.execPath);
var cmd = 'cat ' + f + ' | ' + node + ' ' + f + ' child';
exec(cmd, function(err, stdout, stderr) {
if (err) console.error(err);
assert(!err, 'it exits normally');
assert(stdout === dataExpected, 'it reads the file and outputs it');
assert(stderr === '', 'it does not write to stderr');
console.log('ok');
});
Loading…
Cancel
Save