-
Notifications
You must be signed in to change notification settings - Fork 29.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fs: ignore fstat errors on user fds in fs.readFile{Sync}
On Windows fstat cannot be called on std fds. In those cases, we already know that the fd is not for a regular file and can just use a list of buffers to store the data. Also makes sure that we don't close fds provided by users if we cannot call fstat on them.
- Loading branch information
1 parent
28a3e28
commit 15e8393
Showing
4 changed files
with
104 additions
and
20 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,6 @@ | ||
'use strict'; | ||
|
||
const fs = require('fs'); | ||
fs.readFile(0, (err, data) => { | ||
console.log(data.toString()); | ||
}); |
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,5 @@ | ||
'use strict'; | ||
|
||
const fs = require('fs'); | ||
const stdin = fs.readFileSync(0).toString(); | ||
console.log(stdin); |
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,42 @@ | ||
'use strict'; | ||
|
||
// This tests that fs.readFile{Sync} on closed stdin works. | ||
|
||
const { spawn } = require('child_process'); | ||
const common = require('../common'); | ||
const fixtures = require('../common/fixtures'); | ||
const assert = require('assert'); | ||
|
||
// fs.readFileSync(0) | ||
{ | ||
const script = fixtures.path('readfile-stdin-sync.js'); | ||
const child = spawn(process.execPath, [script], { | ||
stdio: ['ignore', 'pipe', 'pipe'] | ||
}); | ||
child.stdout.on('data', common.mustCall((chunk) => { | ||
assert.strictEqual(chunk.toString(), '\n'); | ||
})); | ||
child.stderr.on('data', (chunk) => { | ||
assert.fail(chunk.toString()); | ||
}); | ||
child.on('close', common.mustCall((code) => { | ||
assert.strictEqual(code, 0); | ||
})); | ||
} | ||
|
||
// fs.readFile(0) | ||
{ | ||
const script = fixtures.path('readfile-stdin-async.js'); | ||
const child = spawn(process.execPath, [script], { | ||
stdio: ['ignore', 'pipe', 'pipe'] | ||
}); | ||
child.stdout.on('data', common.mustCall((chunk) => { | ||
assert.strictEqual(chunk.toString(), '\n'); | ||
})); | ||
child.stderr.on('data', (chunk) => { | ||
assert.fail(chunk.toString()); | ||
}); | ||
child.on('close', common.mustCall((code) => { | ||
assert.strictEqual(code, 0); | ||
})); | ||
} |