Skip to content
This repository was archived by the owner on Oct 13, 2023. It is now read-only.

Commit 97c0414

Browse files
authored
adapter: StreamType::Unknown is actually Stdio (#161)
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 #146. Also, fix some warnings.
1 parent 6e90903 commit 97c0414

File tree

2 files changed

+14
-25
lines changed

2 files changed

+14
-25
lines changed

src/descriptors.rs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ impl Drop for Descriptor {
3030
match &stream.type_ {
3131
StreamType::File(file) => filesystem::drop_descriptor(file.fd),
3232
StreamType::Socket(_) => unreachable!(),
33-
StreamType::Unknown => {}
33+
StreamType::Stdio => {}
3434
}
3535
}
3636
Descriptor::Closed(_) => {}
@@ -106,8 +106,8 @@ impl Streams {
106106

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

112112
/// Streaming data with a file.
113113
File(File),
@@ -146,19 +146,19 @@ impl Descriptors {
146146
d.push(Descriptor::Streams(Streams {
147147
input: Cell::new(Some(stdio.stdin)),
148148
output: Cell::new(None),
149-
type_: StreamType::Unknown,
149+
type_: StreamType::Stdio,
150150
}))
151151
.trapping_unwrap();
152152
d.push(Descriptor::Streams(Streams {
153153
input: Cell::new(None),
154154
output: Cell::new(Some(stdio.stdout)),
155-
type_: StreamType::Unknown,
155+
type_: StreamType::Stdio,
156156
}))
157157
.trapping_unwrap();
158158
d.push(Descriptor::Streams(Streams {
159159
input: Cell::new(None),
160160
output: Cell::new(Some(stdio.stderr)),
161-
type_: StreamType::Unknown,
161+
type_: StreamType::Stdio,
162162
}))
163163
.trapping_unwrap();
164164

@@ -324,16 +324,6 @@ impl Descriptors {
324324
}
325325
}
326326

327-
pub fn get_file_or_dir(&self, fd: Fd) -> Result<&File, Errno> {
328-
match self.get(fd)? {
329-
Descriptor::Streams(Streams {
330-
type_: StreamType::File(file),
331-
..
332-
}) => Ok(file),
333-
_ => Err(wasi::ERRNO_BADF),
334-
}
335-
}
336-
337327
pub fn get_file_with_error(&self, fd: Fd, error: Errno) -> Result<&File, Errno> {
338328
match self.get(fd)? {
339329
Descriptor::Streams(Streams {

src/lib.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -498,9 +498,9 @@ pub unsafe extern "C" fn fd_fdstat_get(fd: Fd, stat: *mut Fdstat) -> Errno {
498498
| Descriptor::Streams(Streams {
499499
input,
500500
output,
501-
type_: StreamType::Unknown,
501+
type_: StreamType::Stdio,
502502
}) => {
503-
let fs_filetype = FILETYPE_UNKNOWN;
503+
let fs_filetype = FILETYPE_CHARACTER_DEVICE;
504504
let fs_flags = 0;
505505
let mut fs_rights_base = 0;
506506
if input.get().is_some() {
@@ -540,7 +540,7 @@ pub unsafe extern "C" fn fd_fdstat_set_flags(fd: Fd, flags: Fdflags) -> Errno {
540540
}) if !file.is_dir() => file,
541541
_ => Err(wasi::ERRNO_BADF)?,
542542
};
543-
file.append = (flags & FDFLAGS_APPEND == FDFLAGS_APPEND);
543+
file.append = flags & FDFLAGS_APPEND == FDFLAGS_APPEND;
544544
file.blocking = !(flags & FDFLAGS_NONBLOCK == FDFLAGS_NONBLOCK);
545545
Ok(())
546546
})
@@ -581,16 +581,15 @@ pub unsafe extern "C" fn fd_filestat_get(fd: Fd, buf: *mut Filestat) -> Errno {
581581
};
582582
Ok(())
583583
}
584-
// For unknown (effectively, stdio) streams, instead of returning an error, return a
585-
// Filestat with all zero fields and Filetype::Unknown.
584+
// Stdio is all zero fields, except for filetype character device
586585
Descriptor::Streams(Streams {
587-
type_: StreamType::Unknown,
586+
type_: StreamType::Stdio,
588587
..
589588
}) => {
590589
*buf = Filestat {
591590
dev: 0,
592591
ino: 0,
593-
filetype: FILETYPE_UNKNOWN,
592+
filetype: FILETYPE_CHARACTER_DEVICE,
594593
nlink: 0,
595594
size: 0,
596595
atim: 0,
@@ -1749,7 +1748,7 @@ pub unsafe extern "C" fn poll_oneoff(
17491748
}
17501749
*/
17511750
}
1752-
StreamType::Unknown => {
1751+
StreamType::Stdio => {
17531752
error = ERRNO_SUCCESS;
17541753
nbytes = 1;
17551754
flags = 0;
@@ -1766,7 +1765,7 @@ pub unsafe extern "C" fn poll_oneoff(
17661765
.trapping_unwrap();
17671766
match desc {
17681767
Descriptor::Streams(streams) => match streams.type_ {
1769-
StreamType::File(_) | StreamType::Unknown => {
1768+
StreamType::File(_) | StreamType::Stdio => {
17701769
error = ERRNO_SUCCESS;
17711770
nbytes = 1;
17721771
flags = 0;

0 commit comments

Comments
 (0)