Skip to content

Commit 256773f

Browse files
author
Jakub Konka
committed
Clean up testing for TTY and escaping writes
1 parent c3628b0 commit 256773f

File tree

7 files changed

+20
-34
lines changed

7 files changed

+20
-34
lines changed

crates/wasi-common/src/entry.rs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::handle::{Handle, HandleRights};
2-
use crate::wasi::types::{Filetype, Rights};
2+
use crate::wasi::types::Filetype;
33
use crate::wasi::{Errno, Result};
44
use std::ops::Deref;
55
use std::path::PathBuf;
@@ -90,16 +90,4 @@ impl Entry {
9090
Err(Errno::Notcapable)
9191
}
9292
}
93-
94-
// TODO we should probably have a separate struct representing
95-
// char device
96-
/// Test whether this descriptor is considered a tty within WASI.
97-
/// Note that since WASI itself lacks an `isatty` syscall and relies
98-
/// on a conservative approximation, we use the same approximation here.
99-
pub(crate) fn isatty(&self) -> bool {
100-
let file_type = self.handle.get_file_type();
101-
let rights = self.handle.get_rights();
102-
let required_rights = HandleRights::from_base(Rights::FD_SEEK | Rights::FD_TELL);
103-
file_type == Filetype::CharacterDevice && rights.contains(&required_rights)
104-
}
10593
}

crates/wasi-common/src/handle.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,15 @@ pub(crate) trait Handle {
6161
fn is_directory(&self) -> bool {
6262
self.get_file_type() == types::Filetype::Directory
6363
}
64+
/// Test whether this descriptor is considered a tty within WASI.
65+
/// Note that since WASI itself lacks an `isatty` syscall and relies
66+
/// on a conservative approximation, we use the same approximation here.
67+
fn is_tty(&self) -> bool {
68+
let file_type = self.get_file_type();
69+
let rights = self.get_rights();
70+
let required_rights = HandleRights::from_base(Rights::FD_SEEK | Rights::FD_TELL);
71+
file_type == types::Filetype::CharacterDevice && rights.contains(&required_rights)
72+
}
6473
// TODO perhaps should be a separate trait?
6574
// FdOps
6675
fn advise(
@@ -118,7 +127,7 @@ pub(crate) trait Handle {
118127
fn sync(&self) -> Result<()> {
119128
Ok(())
120129
}
121-
fn write_vectored(&self, _iovs: &[io::IoSlice], _isatty: bool) -> Result<usize> {
130+
fn write_vectored(&self, _iovs: &[io::IoSlice]) -> Result<usize> {
122131
Err(Errno::Badf)
123132
}
124133
// TODO perhaps should be a separate trait?

crates/wasi-common/src/snapshots/wasi_snapshot_preview1.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,10 +438,9 @@ impl<'a> WasiSnapshotPreview1 for WasiCtx {
438438
}
439439
let required_rights = HandleRights::from_base(types::Rights::FD_WRITE);
440440
let entry = self.get_entry(fd)?;
441-
let isatty = entry.isatty();
442441
let host_nwritten = entry
443442
.as_handle(&required_rights)?
444-
.write_vectored(&slices, isatty)?
443+
.write_vectored(&slices)?
445444
.try_into()?;
446445
Ok(host_nwritten)
447446
}

crates/wasi-common/src/sys/osfile.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use super::sys_impl::oshandle::OsHandle;
22
use super::{fd, AsFile};
33
use crate::handle::{Handle, HandleRights};
4-
use crate::sandboxed_tty_writer::SandboxedTTYWriter;
54
use crate::wasi::{types, Errno, Result};
65
use std::any::Any;
76
use std::cell::Cell;
@@ -115,7 +114,7 @@ impl Handle for OsFile {
115114
let mut fd: &File = &self.as_file();
116115
let cur_pos = fd.seek(SeekFrom::Current(0))?;
117116
fd.seek(SeekFrom::Start(offset))?;
118-
let nwritten = self.write_vectored(&buf, false)?;
117+
let nwritten = self.write_vectored(&buf)?;
119118
fd.seek(SeekFrom::Start(cur_pos))?;
120119
Ok(nwritten)
121120
}
@@ -131,13 +130,8 @@ impl Handle for OsFile {
131130
self.as_file().sync_all()?;
132131
Ok(())
133132
}
134-
fn write_vectored(&self, iovs: &[io::IoSlice], isatty: bool) -> Result<usize> {
135-
let mut file: &File = &self.as_file();
136-
let nwritten = if isatty {
137-
SandboxedTTYWriter::new(&mut file).write_vectored(&iovs)?
138-
} else {
139-
file.write_vectored(&iovs)?
140-
};
133+
fn write_vectored(&self, iovs: &[io::IoSlice]) -> Result<usize> {
134+
let nwritten = self.as_file().write_vectored(&iovs)?;
141135
Ok(nwritten)
142136
}
143137
}

crates/wasi-common/src/sys/osother.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ impl Handle for OsOther {
7373
let nread = self.as_file().read_vectored(iovs)?;
7474
Ok(nread)
7575
}
76-
fn write_vectored(&self, iovs: &[io::IoSlice], isatty: bool) -> Result<usize> {
76+
fn write_vectored(&self, iovs: &[io::IoSlice]) -> Result<usize> {
7777
let mut fd: &File = &self.as_file();
78-
let nwritten = if isatty {
78+
let nwritten = if self.is_tty() {
7979
SandboxedTTYWriter::new(&mut fd).write_vectored(&iovs)?
8080
} else {
8181
fd.write_vectored(iovs)?

crates/wasi-common/src/sys/stdio.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,14 @@ impl Handle for Stdio {
6565
};
6666
Ok(nread)
6767
}
68-
fn write_vectored(&self, iovs: &[io::IoSlice], isatty: bool) -> Result<usize> {
68+
fn write_vectored(&self, iovs: &[io::IoSlice]) -> Result<usize> {
6969
let nwritten = match self {
7070
Self::In { .. } => return Err(Errno::Badf),
7171
Self::Out { .. } => {
7272
// lock for the duration of the scope
7373
let stdout = io::stdout();
7474
let mut stdout = stdout.lock();
75-
let nwritten = if isatty {
75+
let nwritten = if self.is_tty() {
7676
SandboxedTTYWriter::new(&mut stdout).write_vectored(&iovs)?
7777
} else {
7878
stdout.write_vectored(&iovs)?

crates/wasi-common/src/virtfs.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -282,11 +282,7 @@ impl Handle for InMemoryFile {
282282

283283
Ok(self.cursor.get())
284284
}
285-
fn write_vectored(&self, iovs: &[io::IoSlice], isatty: bool) -> Result<usize> {
286-
if isatty {
287-
unimplemented!("writes to virtual tty");
288-
}
289-
285+
fn write_vectored(&self, iovs: &[io::IoSlice]) -> Result<usize> {
290286
trace!("write_vectored(iovs={:?})", iovs);
291287
let mut data = self.data.borrow_mut();
292288

0 commit comments

Comments
 (0)