mirror of
https://github.com/denoland/deno.git
synced 2025-09-27 04:39:10 +00:00
feat: redirect process stdio to file (#2554)
This commit is contained in:
parent
eb93dc58a1
commit
642eaf97c6
6 changed files with 189 additions and 58 deletions
|
@ -538,6 +538,9 @@ table Run {
|
||||||
stdin: ProcessStdio;
|
stdin: ProcessStdio;
|
||||||
stdout: ProcessStdio;
|
stdout: ProcessStdio;
|
||||||
stderr: ProcessStdio;
|
stderr: ProcessStdio;
|
||||||
|
stdin_rid: uint32;
|
||||||
|
stdout_rid: uint32;
|
||||||
|
stderr_rid: uint32;
|
||||||
}
|
}
|
||||||
|
|
||||||
table RunRes {
|
table RunRes {
|
||||||
|
|
24
cli/ops.rs
24
cli/ops.rs
|
@ -1793,9 +1793,27 @@ fn op_run(
|
||||||
c.env(entry.key().unwrap(), entry.value().unwrap());
|
c.env(entry.key().unwrap(), entry.value().unwrap());
|
||||||
});
|
});
|
||||||
|
|
||||||
c.stdin(subprocess_stdio_map(inner.stdin()));
|
// TODO: make this work with other resources, eg. sockets
|
||||||
c.stdout(subprocess_stdio_map(inner.stdout()));
|
let stdin_rid = inner.stdin_rid();
|
||||||
c.stderr(subprocess_stdio_map(inner.stderr()));
|
if stdin_rid > 0 {
|
||||||
|
c.stdin(resources::get_file(stdin_rid)?);
|
||||||
|
} else {
|
||||||
|
c.stdin(subprocess_stdio_map(inner.stdin()));
|
||||||
|
}
|
||||||
|
|
||||||
|
let stdout_rid = inner.stdout_rid();
|
||||||
|
if stdout_rid > 0 {
|
||||||
|
c.stdout(resources::get_file(stdout_rid)?);
|
||||||
|
} else {
|
||||||
|
c.stdout(subprocess_stdio_map(inner.stdout()));
|
||||||
|
}
|
||||||
|
|
||||||
|
let stderr_rid = inner.stderr_rid();
|
||||||
|
if stderr_rid > 0 {
|
||||||
|
c.stderr(resources::get_file(stderr_rid)?);
|
||||||
|
} else {
|
||||||
|
c.stderr(subprocess_stdio_map(inner.stderr()));
|
||||||
|
}
|
||||||
|
|
||||||
// Spawn the command.
|
// Spawn the command.
|
||||||
let child = c.spawn_async().map_err(DenoError::from)?;
|
let child = c.spawn_async().map_err(DenoError::from)?;
|
||||||
|
|
|
@ -492,29 +492,19 @@ pub fn get_repl(rid: ResourceId) -> DenoResult<Arc<Mutex<Repl>>> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn lookup(rid: ResourceId) -> Option<Resource> {
|
// TODO: revamp this after the following lands:
|
||||||
debug!("resource lookup {}", rid);
|
|
||||||
let table = RESOURCE_TABLE.lock().unwrap();
|
|
||||||
table.get(&rid).map(|_| Resource { rid })
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO(kevinkassimo): revamp this after the following lands:
|
|
||||||
// https://github.com/tokio-rs/tokio/pull/785
|
// https://github.com/tokio-rs/tokio/pull/785
|
||||||
pub fn seek(
|
pub fn get_file(rid: ResourceId) -> DenoResult<std::fs::File> {
|
||||||
resource: Resource,
|
|
||||||
offset: i32,
|
|
||||||
whence: u32,
|
|
||||||
) -> Box<dyn Future<Item = (), Error = DenoError> + Send> {
|
|
||||||
let mut table = RESOURCE_TABLE.lock().unwrap();
|
let mut table = RESOURCE_TABLE.lock().unwrap();
|
||||||
// We take ownership of File here.
|
// We take ownership of File here.
|
||||||
// It is put back below while still holding the lock.
|
// It is put back below while still holding the lock.
|
||||||
let maybe_repr = table.remove(&resource.rid);
|
let maybe_repr = table.remove(&rid);
|
||||||
|
|
||||||
match maybe_repr {
|
match maybe_repr {
|
||||||
None => panic!("bad rid"),
|
Some(Repr::FsFile(r)) => {
|
||||||
Some(Repr::FsFile(f)) => {
|
|
||||||
// Trait Clone not implemented on tokio::fs::File,
|
// Trait Clone not implemented on tokio::fs::File,
|
||||||
// so convert to std File first.
|
// so convert to std File first.
|
||||||
let std_file = f.into_std();
|
let std_file = r.into_std();
|
||||||
// Create a copy and immediately put back.
|
// Create a copy and immediately put back.
|
||||||
// We don't want to block other resource ops.
|
// We don't want to block other resource ops.
|
||||||
// try_clone() would yield a copy containing the same
|
// try_clone() would yield a copy containing the same
|
||||||
|
@ -523,36 +513,49 @@ pub fn seek(
|
||||||
// to write back.
|
// to write back.
|
||||||
let maybe_std_file_copy = std_file.try_clone();
|
let maybe_std_file_copy = std_file.try_clone();
|
||||||
// Insert the entry back with the same rid.
|
// Insert the entry back with the same rid.
|
||||||
table.insert(
|
table.insert(rid, Repr::FsFile(tokio_fs::File::from_std(std_file)));
|
||||||
resource.rid,
|
|
||||||
Repr::FsFile(tokio_fs::File::from_std(std_file)),
|
|
||||||
);
|
|
||||||
// Translate seek mode to Rust repr.
|
|
||||||
let seek_from = match whence {
|
|
||||||
0 => SeekFrom::Start(offset as u64),
|
|
||||||
1 => SeekFrom::Current(i64::from(offset)),
|
|
||||||
2 => SeekFrom::End(i64::from(offset)),
|
|
||||||
_ => {
|
|
||||||
return Box::new(futures::future::err(deno_error::new(
|
|
||||||
deno_error::ErrorKind::InvalidSeekMode,
|
|
||||||
format!("Invalid seek mode: {}", whence),
|
|
||||||
)));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
if maybe_std_file_copy.is_err() {
|
if maybe_std_file_copy.is_err() {
|
||||||
return Box::new(futures::future::err(DenoError::from(
|
return Err(DenoError::from(maybe_std_file_copy.unwrap_err()));
|
||||||
maybe_std_file_copy.unwrap_err(),
|
|
||||||
)));
|
|
||||||
}
|
}
|
||||||
let mut std_file_copy = maybe_std_file_copy.unwrap();
|
|
||||||
Box::new(futures::future::lazy(move || {
|
let std_file_copy = maybe_std_file_copy.unwrap();
|
||||||
let result = std_file_copy
|
|
||||||
.seek(seek_from)
|
Ok(std_file_copy)
|
||||||
.map(|_| {})
|
|
||||||
.map_err(DenoError::from);
|
|
||||||
futures::future::result(result)
|
|
||||||
}))
|
|
||||||
}
|
}
|
||||||
_ => panic!("cannot seek"),
|
_ => Err(bad_resource()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn lookup(rid: ResourceId) -> Option<Resource> {
|
||||||
|
debug!("resource lookup {}", rid);
|
||||||
|
let table = RESOURCE_TABLE.lock().unwrap();
|
||||||
|
table.get(&rid).map(|_| Resource { rid })
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn seek(
|
||||||
|
resource: Resource,
|
||||||
|
offset: i32,
|
||||||
|
whence: u32,
|
||||||
|
) -> Box<dyn Future<Item = (), Error = DenoError> + Send> {
|
||||||
|
// Translate seek mode to Rust repr.
|
||||||
|
let seek_from = match whence {
|
||||||
|
0 => SeekFrom::Start(offset as u64),
|
||||||
|
1 => SeekFrom::Current(i64::from(offset)),
|
||||||
|
2 => SeekFrom::End(i64::from(offset)),
|
||||||
|
_ => {
|
||||||
|
return Box::new(futures::future::err(deno_error::new(
|
||||||
|
deno_error::ErrorKind::InvalidSeekMode,
|
||||||
|
format!("Invalid seek mode: {}", whence),
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
match get_file(resource.rid) {
|
||||||
|
Ok(mut file) => Box::new(futures::future::lazy(move || {
|
||||||
|
let result = file.seek(seek_from).map(|_| {}).map_err(DenoError::from);
|
||||||
|
futures::future::result(result)
|
||||||
|
})),
|
||||||
|
Err(err) => Box::new(futures::future::err(err)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,9 +28,9 @@ export interface RunOptions {
|
||||||
args: string[];
|
args: string[];
|
||||||
cwd?: string;
|
cwd?: string;
|
||||||
env?: { [key: string]: string };
|
env?: { [key: string]: string };
|
||||||
stdout?: ProcessStdio;
|
stdout?: ProcessStdio | number;
|
||||||
stderr?: ProcessStdio;
|
stderr?: ProcessStdio | number;
|
||||||
stdin?: ProcessStdio;
|
stdin?: ProcessStdio | number;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function runStatus(rid: number): Promise<ProcessStatus> {
|
async function runStatus(rid: number): Promise<ProcessStatus> {
|
||||||
|
@ -149,6 +149,10 @@ function stdioMap(s: ProcessStdio): msg.ProcessStdio {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isRid(arg: unknown): arg is number {
|
||||||
|
return !isNaN(arg as number);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Spawns new subprocess.
|
* Spawns new subprocess.
|
||||||
*
|
*
|
||||||
|
@ -159,7 +163,8 @@ function stdioMap(s: ProcessStdio): msg.ProcessStdio {
|
||||||
* mapping.
|
* mapping.
|
||||||
*
|
*
|
||||||
* By default subprocess inherits stdio of parent process. To change that
|
* By default subprocess inherits stdio of parent process. To change that
|
||||||
* `opt.stdout`, `opt.stderr` and `opt.stdin` can be specified independently.
|
* `opt.stdout`, `opt.stderr` and `opt.stdin` can be specified independently -
|
||||||
|
* they can be set to either `ProcessStdio` or `rid` of open file.
|
||||||
*/
|
*/
|
||||||
export function run(opt: RunOptions): Process {
|
export function run(opt: RunOptions): Process {
|
||||||
const builder = flatbuffers.createBuilder();
|
const builder = flatbuffers.createBuilder();
|
||||||
|
@ -177,14 +182,49 @@ export function run(opt: RunOptions): Process {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const envOffset = msg.Run.createEnvVector(builder, kvOffset);
|
const envOffset = msg.Run.createEnvVector(builder, kvOffset);
|
||||||
|
|
||||||
|
let stdInOffset = stdioMap("inherit");
|
||||||
|
let stdOutOffset = stdioMap("inherit");
|
||||||
|
let stdErrOffset = stdioMap("inherit");
|
||||||
|
let stdinRidOffset = 0;
|
||||||
|
let stdoutRidOffset = 0;
|
||||||
|
let stderrRidOffset = 0;
|
||||||
|
|
||||||
|
if (opt.stdin) {
|
||||||
|
if (isRid(opt.stdin)) {
|
||||||
|
stdinRidOffset = opt.stdin;
|
||||||
|
} else {
|
||||||
|
stdInOffset = stdioMap(opt.stdin);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (opt.stdout) {
|
||||||
|
if (isRid(opt.stdout)) {
|
||||||
|
stdoutRidOffset = opt.stdout;
|
||||||
|
} else {
|
||||||
|
stdOutOffset = stdioMap(opt.stdout);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (opt.stderr) {
|
||||||
|
if (isRid(opt.stderr)) {
|
||||||
|
stderrRidOffset = opt.stderr;
|
||||||
|
} else {
|
||||||
|
stdErrOffset = stdioMap(opt.stderr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const inner = msg.Run.createRun(
|
const inner = msg.Run.createRun(
|
||||||
builder,
|
builder,
|
||||||
argsOffset,
|
argsOffset,
|
||||||
cwdOffset,
|
cwdOffset,
|
||||||
envOffset,
|
envOffset,
|
||||||
opt.stdin ? stdioMap(opt.stdin) : stdioMap("inherit"),
|
stdInOffset,
|
||||||
opt.stdout ? stdioMap(opt.stdout) : stdioMap("inherit"),
|
stdOutOffset,
|
||||||
opt.stderr ? stdioMap(opt.stderr) : stdioMap("inherit")
|
stdErrOffset,
|
||||||
|
stdinRidOffset,
|
||||||
|
stdoutRidOffset,
|
||||||
|
stderrRidOffset
|
||||||
);
|
);
|
||||||
const baseRes = dispatch.sendSync(builder, msg.Any.Run, inner);
|
const baseRes = dispatch.sendSync(builder, msg.Any.Run, inner);
|
||||||
assert(baseRes != null);
|
assert(baseRes != null);
|
||||||
|
|
|
@ -1,6 +1,21 @@
|
||||||
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
||||||
import { test, testPerm, assert, assertEquals } from "./test_util.ts";
|
import {
|
||||||
const { kill, run, DenoError, ErrorKind } = Deno;
|
test,
|
||||||
|
testPerm,
|
||||||
|
assert,
|
||||||
|
assertEquals,
|
||||||
|
assertStrContains
|
||||||
|
} from "./test_util.ts";
|
||||||
|
const {
|
||||||
|
kill,
|
||||||
|
run,
|
||||||
|
DenoError,
|
||||||
|
ErrorKind,
|
||||||
|
readFile,
|
||||||
|
open,
|
||||||
|
makeTempDir,
|
||||||
|
writeFile
|
||||||
|
} = Deno;
|
||||||
|
|
||||||
test(function runPermissions(): void {
|
test(function runPermissions(): void {
|
||||||
let caughtError = false;
|
let caughtError = false;
|
||||||
|
@ -71,7 +86,7 @@ testPerm(
|
||||||
{ write: true, run: true },
|
{ write: true, run: true },
|
||||||
async function runWithCwdIsAsync(): Promise<void> {
|
async function runWithCwdIsAsync(): Promise<void> {
|
||||||
const enc = new TextEncoder();
|
const enc = new TextEncoder();
|
||||||
const cwd = Deno.makeTempDirSync({ prefix: "deno_command_test" });
|
const cwd = await makeTempDir({ prefix: "deno_command_test" });
|
||||||
|
|
||||||
const exitCodeFile = "deno_was_here";
|
const exitCodeFile = "deno_was_here";
|
||||||
const pyProgramFile = "poll_exit.py";
|
const pyProgramFile = "poll_exit.py";
|
||||||
|
@ -205,6 +220,57 @@ testPerm({ run: true }, async function runStderrOutput(): Promise<void> {
|
||||||
p.close();
|
p.close();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
testPerm(
|
||||||
|
{ run: true, write: true, read: true },
|
||||||
|
async function runRedirectStdoutStderr(): Promise<void> {
|
||||||
|
const tempDir = await makeTempDir();
|
||||||
|
const fileName = tempDir + "/redirected_stdio.txt";
|
||||||
|
const file = await open(fileName, "w");
|
||||||
|
|
||||||
|
const p = run({
|
||||||
|
args: [
|
||||||
|
"python",
|
||||||
|
"-c",
|
||||||
|
"import sys; sys.stderr.write('error\\n'); sys.stdout.write('output\\n');"
|
||||||
|
],
|
||||||
|
stdout: file.rid,
|
||||||
|
stderr: file.rid
|
||||||
|
});
|
||||||
|
|
||||||
|
await p.status();
|
||||||
|
p.close();
|
||||||
|
file.close();
|
||||||
|
|
||||||
|
const fileContents = await readFile(fileName);
|
||||||
|
const decoder = new TextDecoder();
|
||||||
|
const text = decoder.decode(fileContents);
|
||||||
|
|
||||||
|
assertStrContains(text, "error");
|
||||||
|
assertStrContains(text, "output");
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
testPerm(
|
||||||
|
{ run: true, write: true, read: true },
|
||||||
|
async function runRedirectStdin(): Promise<void> {
|
||||||
|
const tempDir = await makeTempDir();
|
||||||
|
const fileName = tempDir + "/redirected_stdio.txt";
|
||||||
|
const encoder = new TextEncoder();
|
||||||
|
await writeFile(fileName, encoder.encode("hello"));
|
||||||
|
const file = await open(fileName, "r");
|
||||||
|
|
||||||
|
const p = run({
|
||||||
|
args: ["python", "-c", "import sys; assert 'hello' == sys.stdin.read();"],
|
||||||
|
stdin: file.rid
|
||||||
|
});
|
||||||
|
|
||||||
|
const status = await p.status();
|
||||||
|
assertEquals(status.code, 0);
|
||||||
|
p.close();
|
||||||
|
file.close();
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
testPerm({ run: true }, async function runEnv(): Promise<void> {
|
testPerm({ run: true }, async function runEnv(): Promise<void> {
|
||||||
const p = run({
|
const p = run({
|
||||||
args: [
|
args: [
|
||||||
|
|
|
@ -16,7 +16,8 @@ export {
|
||||||
assert,
|
assert,
|
||||||
assertEquals,
|
assertEquals,
|
||||||
assertNotEquals,
|
assertNotEquals,
|
||||||
assertStrictEq
|
assertStrictEq,
|
||||||
|
assertStrContains
|
||||||
} from "./deps/https/deno.land/std/testing/asserts.ts";
|
} from "./deps/https/deno.land/std/testing/asserts.ts";
|
||||||
|
|
||||||
interface TestPermissions {
|
interface TestPermissions {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue