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

stdin fd doesn't works when it's a pipe #15497

Closed
tai2 opened this issue Sep 20, 2017 · 4 comments
Closed

stdin fd doesn't works when it's a pipe #15497

tai2 opened this issue Sep 20, 2017 · 4 comments
Labels
fs Issues and PRs related to the fs subsystem / file system. invalid Issues and PRs that are invalid.

Comments

@tai2
Copy link

tai2 commented Sep 20, 2017

fs apis like fstatSync or readSync don't read state or data fed through pipe.

Reproducible code
https://wandbox.org/permlink/zU1uUULheUpxmgfN

cat data.txt | node prog.js # NG
node prog.js < data.txt # OK

But with stream api, it works properly.
https://wandbox.org/permlink/XW7rOuJQCCyqCFPX

This problem occurred only on linux as long as I tested, not on macOS.

@mscdex mscdex added the fs Issues and PRs related to the fs subsystem / file system. label Sep 20, 2017
@mscdex
Copy link
Contributor

mscdex commented Sep 20, 2017

Code for posterity:

var fs = require('fs')
var fd = process.stdin.fd;
var length = fs.fstatSync(fd).size;
var buffer = new Buffer(length);
var bytesRead = fs.readSync(fd, buffer, 0, length, 0);
var input = buffer.toString('utf8', 0, bytesRead);
console.log(input);

@tai2
Copy link
Author

tai2 commented Sep 20, 2017

This problem occurred only on linux as long as I tested, not on macOS.

Oops, it's a mistake. it doesn't works on macOS too.

The difference is that it reporting an error only on macOS

fs.js:682
  return binding.read(fd, buffer, offset, length, position);
                 ^

Error: ESPIPE: invalid seek, read
    at Object.fs.readSync (fs.js:682:18)
    at Object.<anonymous> (/Users/tai2/a.js:6:20)
    at Module._compile (module.js:624:30)
    at Object.Module._extensions..js (module.js:635:10)
    at Module.load (module.js:545:32)
    at tryModuleLoad (module.js:508:12)
    at Function.Module._load (module.js:500:3)
    at Function.Module.runMain (module.js:665:10)
    at startup (bootstrap_node.js:201:16)
    at bootstrap_node.js:626:3

@tai2
Copy link
Author

tai2 commented Sep 20, 2017

This works fine on macOS.

var fs = require('fs')
var fd = process.stdin.fd;
var length = fs.fstatSync(fd).size;
var buffer = new Buffer(length);
var bytesRead = fs.readSync(fd, buffer, 0, length, null);
var input = buffer.toString('utf8', 0, bytesRead);
console.log(input);
- var bytesRead = fs.readSync(fd, buffer, 0, length, 0);
+ var bytesRead = fs.readSync(fd, buffer, 0, length, null);

@bnoordhuis
Copy link
Member

This line doesn't make sense if fd refers to a pipe: var length = fs.fstatSync(fd).size;

See http://yarchive.net/comp/linux/stat_size.html for some background. I'll close this out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fs Issues and PRs related to the fs subsystem / file system. invalid Issues and PRs that are invalid.
Projects
None yet
Development

No branches or pull requests

3 participants