From 80d318f51a2f06023f27e39662171882a2b34f45 Mon Sep 17 00:00:00 2001 From: Tim Schaub Date: Mon, 31 Aug 2020 23:38:03 -0600 Subject: [PATCH] Represent stdin, stdout, and stderr as files --- lib/binding.js | 14 +++++++++++- lib/file.js | 58 +++++++++++++++++++++++++++++++++++++++++++++++++- lib/item.js | 13 ++++++----- 3 files changed, 76 insertions(+), 9 deletions(-) diff --git a/lib/binding.js b/lib/binding.js index 0f70343c..13fe83f0 100644 --- a/lib/binding.js +++ b/lib/binding.js @@ -268,7 +268,19 @@ function Binding(system) { * Counter for file descriptors. * @type {number} */ - this._counter = 0; + this._counter = -1; + + const stdin = new FileDescriptor(constants.O_RDWR); + stdin.setItem(new File.StandardInput()); + this.trackDescriptor(stdin); + + const stdout = new FileDescriptor(constants.O_RDWR); + stdout.setItem(new File.StandardOutput()); + this.trackDescriptor(stdout); + + const stderr = new FileDescriptor(constants.O_RDWR); + stderr.setItem(new File.StandardError()); + this.trackDescriptor(stderr); } /** diff --git a/lib/file.js b/lib/file.js index 3edef1c7..fc1df181 100644 --- a/lib/file.js +++ b/lib/file.js @@ -10,7 +10,7 @@ const EMPTY = bufferAlloc(0); const constants = require('constants'); /** - * A directory. + * A file. * @constructor */ function File() { @@ -67,3 +67,59 @@ File.prototype.getStats = function() { * @type {function()} */ exports = module.exports = File; + +/** + * Standard input. + * @constructor + */ +function StandardInput() { + File.call(this); + this.setMode(438); // 0666 +} +util.inherits(StandardInput, File); + +exports.StandardInput = StandardInput; + +/** + * Standard output. + * @constructor + */ +function StandardOutput() { + File.call(this); + this.setMode(438); // 0666 +} +util.inherits(StandardOutput, File); + +/** + * Write the contents to stdout. + * @param {string|Buffer} content File contents. + */ +StandardOutput.prototype.setContent = function(content) { + if (process.stdout.isTTY) { + process.stdout.write(content); + } +}; + +exports.StandardOutput = StandardOutput; + +/** + * Standard error. + * @constructor + */ +function StandardError() { + File.call(this); + this.setMode(438); // 0666 +} +util.inherits(StandardError, File); + +/** + * Write the contents to stderr. + * @param {string|Buffer} content File contents. + */ +StandardError.prototype.setContent = function(content) { + if (process.stderr.isTTY) { + process.stderr.write(content); + } +}; + +exports.StandardError = StandardError; diff --git a/lib/item.js b/lib/item.js index 1ec3e75d..dcbd464a 100644 --- a/lib/item.js +++ b/lib/item.js @@ -92,14 +92,13 @@ function Item() { * Add execute if read allowed * See notes in index.js -> mapping#addDir */ -// prettier-ignore Item.fixWin32Permissions = mode => - (process.platform !== 'win32') - ? mode - : mode | - ((mode & permissions.USER_READ) && permissions.USER_EXEC) | - ((mode & permissions.GROUP_READ) && permissions.GROUP_EXEC) | - ((mode & permissions.OTHER_READ) && permissions.OTHER_EXEC); + process.platform !== 'win32' + ? mode + : mode | + (mode & permissions.USER_READ && permissions.USER_EXEC) | + (mode & permissions.GROUP_READ && permissions.GROUP_EXEC) | + (mode & permissions.OTHER_READ && permissions.OTHER_EXEC); /** * Determine if the current user has read permission.