Skip to content

Commit

Permalink
adapter: StreamType::Unknown is actually Stdio (bytecodealliance#161)
Browse files Browse the repository at this point in the history
We have only ever used Unknown for the stdio streams, and I don't expect
us to use it for anything else in the future, so rename it.

Set the returned filetype to character device: Closes bytecodealliance#146.

Also, fix some warnings.
  • Loading branch information
pchickey committed May 3, 2023
1 parent 87f74e9 commit 1e04bf2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 25 deletions.
22 changes: 6 additions & 16 deletions src/descriptors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl Drop for Descriptor {
match &stream.type_ {
StreamType::File(file) => filesystem::drop_descriptor(file.fd),
StreamType::Socket(_) => unreachable!(),
StreamType::Unknown => {}
StreamType::Stdio => {}
}
}
Descriptor::Closed(_) => {}
Expand Down Expand Up @@ -106,8 +106,8 @@ impl Streams {

#[allow(dead_code)] // until Socket is implemented
pub enum StreamType {
/// It's a valid stream but we don't know where it comes from.
Unknown,
/// Stream is used for implementing stdio.
Stdio,

/// Streaming data with a file.
File(File),
Expand Down Expand Up @@ -146,19 +146,19 @@ impl Descriptors {
d.push(Descriptor::Streams(Streams {
input: Cell::new(Some(stdio.stdin)),
output: Cell::new(None),
type_: StreamType::Unknown,
type_: StreamType::Stdio,
}))
.trapping_unwrap();
d.push(Descriptor::Streams(Streams {
input: Cell::new(None),
output: Cell::new(Some(stdio.stdout)),
type_: StreamType::Unknown,
type_: StreamType::Stdio,
}))
.trapping_unwrap();
d.push(Descriptor::Streams(Streams {
input: Cell::new(None),
output: Cell::new(Some(stdio.stderr)),
type_: StreamType::Unknown,
type_: StreamType::Stdio,
}))
.trapping_unwrap();

Expand Down Expand Up @@ -324,16 +324,6 @@ impl Descriptors {
}
}

pub fn get_file_or_dir(&self, fd: Fd) -> Result<&File, Errno> {
match self.get(fd)? {
Descriptor::Streams(Streams {
type_: StreamType::File(file),
..
}) => Ok(file),
_ => Err(wasi::ERRNO_BADF),
}
}

pub fn get_file_with_error(&self, fd: Fd, error: Errno) -> Result<&File, Errno> {
match self.get(fd)? {
Descriptor::Streams(Streams {
Expand Down
17 changes: 8 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -498,9 +498,9 @@ pub unsafe extern "C" fn fd_fdstat_get(fd: Fd, stat: *mut Fdstat) -> Errno {
| Descriptor::Streams(Streams {
input,
output,
type_: StreamType::Unknown,
type_: StreamType::Stdio,
}) => {
let fs_filetype = FILETYPE_UNKNOWN;
let fs_filetype = FILETYPE_CHARACTER_DEVICE;
let fs_flags = 0;
let mut fs_rights_base = 0;
if input.get().is_some() {
Expand Down Expand Up @@ -540,7 +540,7 @@ pub unsafe extern "C" fn fd_fdstat_set_flags(fd: Fd, flags: Fdflags) -> Errno {
}) if !file.is_dir() => file,
_ => Err(wasi::ERRNO_BADF)?,
};
file.append = (flags & FDFLAGS_APPEND == FDFLAGS_APPEND);
file.append = flags & FDFLAGS_APPEND == FDFLAGS_APPEND;
file.blocking = !(flags & FDFLAGS_NONBLOCK == FDFLAGS_NONBLOCK);
Ok(())
})
Expand Down Expand Up @@ -581,16 +581,15 @@ pub unsafe extern "C" fn fd_filestat_get(fd: Fd, buf: *mut Filestat) -> Errno {
};
Ok(())
}
// For unknown (effectively, stdio) streams, instead of returning an error, return a
// Filestat with all zero fields and Filetype::Unknown.
// Stdio is all zero fields, except for filetype character device
Descriptor::Streams(Streams {
type_: StreamType::Unknown,
type_: StreamType::Stdio,
..
}) => {
*buf = Filestat {
dev: 0,
ino: 0,
filetype: FILETYPE_UNKNOWN,
filetype: FILETYPE_CHARACTER_DEVICE,
nlink: 0,
size: 0,
atim: 0,
Expand Down Expand Up @@ -1749,7 +1748,7 @@ pub unsafe extern "C" fn poll_oneoff(
}
*/
}
StreamType::Unknown => {
StreamType::Stdio => {
error = ERRNO_SUCCESS;
nbytes = 1;
flags = 0;
Expand All @@ -1766,7 +1765,7 @@ pub unsafe extern "C" fn poll_oneoff(
.trapping_unwrap();
match desc {
Descriptor::Streams(streams) => match streams.type_ {
StreamType::File(_) | StreamType::Unknown => {
StreamType::File(_) | StreamType::Stdio => {
error = ERRNO_SUCCESS;
nbytes = 1;
flags = 0;
Expand Down

0 comments on commit 1e04bf2

Please sign in to comment.