ThiƩbaud Weksteen | 3b664ca | 2020-11-26 14:41:59 +0100 | [diff] [blame] | 1 | #![deny(unsafe_op_in_unsafe_fn)] |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 2 | #![allow(dead_code)] |
| 3 | |
Matthew Maurer | f4d8f81 | 2020-03-27 13:14:30 -0700 | [diff] [blame] | 4 | use super::err2io; |
Charisee | 635618d | 2023-06-01 20:46:00 +0000 | [diff] [blame] | 5 | use crate::io::{self, BorrowedCursor, IoSlice, IoSliceMut, SeekFrom}; |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 6 | use crate::mem; |
| 7 | use crate::net::Shutdown; |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame] | 8 | use crate::os::wasi::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd, RawFd}; |
| 9 | use crate::sys_common::{AsInner, AsInnerMut, FromInner, IntoInner}; |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 10 | |
| 11 | #[derive(Debug)] |
| 12 | pub struct WasiFd { |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame] | 13 | fd: OwnedFd, |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 14 | } |
| 15 | |
Matthew Maurer | f4d8f81 | 2020-03-27 13:14:30 -0700 | [diff] [blame] | 16 | fn iovec<'a>(a: &'a mut [IoSliceMut<'_>]) -> &'a [wasi::Iovec] { |
| 17 | assert_eq!(mem::size_of::<IoSliceMut<'_>>(), mem::size_of::<wasi::Iovec>()); |
| 18 | assert_eq!(mem::align_of::<IoSliceMut<'_>>(), mem::align_of::<wasi::Iovec>()); |
Matthew Maurer | 15a6560 | 2020-04-24 14:05:21 -0700 | [diff] [blame] | 19 | // SAFETY: `IoSliceMut` and `IoVec` have exactly the same memory layout |
| 20 | unsafe { mem::transmute(a) } |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 21 | } |
| 22 | |
Matthew Maurer | f4d8f81 | 2020-03-27 13:14:30 -0700 | [diff] [blame] | 23 | fn ciovec<'a>(a: &'a [IoSlice<'_>]) -> &'a [wasi::Ciovec] { |
| 24 | assert_eq!(mem::size_of::<IoSlice<'_>>(), mem::size_of::<wasi::Ciovec>()); |
| 25 | assert_eq!(mem::align_of::<IoSlice<'_>>(), mem::align_of::<wasi::Ciovec>()); |
Matthew Maurer | 15a6560 | 2020-04-24 14:05:21 -0700 | [diff] [blame] | 26 | // SAFETY: `IoSlice` and `CIoVec` have exactly the same memory layout |
| 27 | unsafe { mem::transmute(a) } |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | impl WasiFd { |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 31 | pub fn datasync(&self) -> io::Result<()> { |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame] | 32 | unsafe { wasi::fd_datasync(self.as_raw_fd() as wasi::Fd).map_err(err2io) } |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 33 | } |
| 34 | |
Chih-Hung Hsieh | fd666f2 | 2019-12-19 14:34:18 -0800 | [diff] [blame] | 35 | pub fn pread(&self, bufs: &mut [IoSliceMut<'_>], offset: u64) -> io::Result<usize> { |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame] | 36 | unsafe { wasi::fd_pread(self.as_raw_fd() as wasi::Fd, iovec(bufs), offset).map_err(err2io) } |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 37 | } |
| 38 | |
Chih-Hung Hsieh | fd666f2 | 2019-12-19 14:34:18 -0800 | [diff] [blame] | 39 | pub fn pwrite(&self, bufs: &[IoSlice<'_>], offset: u64) -> io::Result<usize> { |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame] | 40 | unsafe { |
| 41 | wasi::fd_pwrite(self.as_raw_fd() as wasi::Fd, ciovec(bufs), offset).map_err(err2io) |
| 42 | } |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Chih-Hung Hsieh | fd666f2 | 2019-12-19 14:34:18 -0800 | [diff] [blame] | 45 | pub fn read(&self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> { |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame] | 46 | unsafe { wasi::fd_read(self.as_raw_fd() as wasi::Fd, iovec(bufs)).map_err(err2io) } |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Charisee | 635618d | 2023-06-01 20:46:00 +0000 | [diff] [blame] | 49 | pub fn read_buf(&self, mut buf: BorrowedCursor<'_>) -> io::Result<()> { |
| 50 | unsafe { |
| 51 | let bufs = [wasi::Iovec { |
| 52 | buf: buf.as_mut().as_mut_ptr() as *mut u8, |
| 53 | buf_len: buf.capacity(), |
| 54 | }]; |
| 55 | match wasi::fd_read(self.as_raw_fd() as wasi::Fd, &bufs) { |
| 56 | Ok(n) => { |
| 57 | buf.advance(n); |
| 58 | Ok(()) |
| 59 | } |
| 60 | Err(e) => Err(err2io(e)), |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
Chih-Hung Hsieh | fd666f2 | 2019-12-19 14:34:18 -0800 | [diff] [blame] | 65 | pub fn write(&self, bufs: &[IoSlice<'_>]) -> io::Result<usize> { |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame] | 66 | unsafe { wasi::fd_write(self.as_raw_fd() as wasi::Fd, ciovec(bufs)).map_err(err2io) } |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | pub fn seek(&self, pos: SeekFrom) -> io::Result<u64> { |
| 70 | let (whence, offset) = match pos { |
Chih-Hung Hsieh | 8cd2c99 | 2019-12-19 15:08:11 -0800 | [diff] [blame] | 71 | SeekFrom::Start(pos) => (wasi::WHENCE_SET, pos as i64), |
| 72 | SeekFrom::End(pos) => (wasi::WHENCE_END, pos), |
| 73 | SeekFrom::Current(pos) => (wasi::WHENCE_CUR, pos), |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 74 | }; |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame] | 75 | unsafe { wasi::fd_seek(self.as_raw_fd() as wasi::Fd, offset, whence).map_err(err2io) } |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | pub fn tell(&self) -> io::Result<u64> { |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame] | 79 | unsafe { wasi::fd_tell(self.as_raw_fd() as wasi::Fd).map_err(err2io) } |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | // FIXME: __wasi_fd_fdstat_get |
| 83 | |
Matthew Maurer | f4d8f81 | 2020-03-27 13:14:30 -0700 | [diff] [blame] | 84 | pub fn set_flags(&self, flags: wasi::Fdflags) -> io::Result<()> { |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame] | 85 | unsafe { wasi::fd_fdstat_set_flags(self.as_raw_fd() as wasi::Fd, flags).map_err(err2io) } |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Chih-Hung Hsieh | 8cd2c99 | 2019-12-19 15:08:11 -0800 | [diff] [blame] | 88 | pub fn set_rights(&self, base: wasi::Rights, inheriting: wasi::Rights) -> io::Result<()> { |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame] | 89 | unsafe { |
| 90 | wasi::fd_fdstat_set_rights(self.as_raw_fd() as wasi::Fd, base, inheriting) |
| 91 | .map_err(err2io) |
| 92 | } |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | pub fn sync(&self) -> io::Result<()> { |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame] | 96 | unsafe { wasi::fd_sync(self.as_raw_fd() as wasi::Fd).map_err(err2io) } |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Chih-Hung Hsieh | 8cd2c99 | 2019-12-19 15:08:11 -0800 | [diff] [blame] | 99 | pub fn advise(&self, offset: u64, len: u64, advice: wasi::Advice) -> io::Result<()> { |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame] | 100 | unsafe { |
| 101 | wasi::fd_advise(self.as_raw_fd() as wasi::Fd, offset, len, advice).map_err(err2io) |
| 102 | } |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 103 | } |
| 104 | |
| 105 | pub fn allocate(&self, offset: u64, len: u64) -> io::Result<()> { |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame] | 106 | unsafe { wasi::fd_allocate(self.as_raw_fd() as wasi::Fd, offset, len).map_err(err2io) } |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Matthew Maurer | f4d8f81 | 2020-03-27 13:14:30 -0700 | [diff] [blame] | 109 | pub fn create_directory(&self, path: &str) -> io::Result<()> { |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame] | 110 | unsafe { wasi::path_create_directory(self.as_raw_fd() as wasi::Fd, path).map_err(err2io) } |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | pub fn link( |
| 114 | &self, |
Matthew Maurer | f4d8f81 | 2020-03-27 13:14:30 -0700 | [diff] [blame] | 115 | old_flags: wasi::Lookupflags, |
| 116 | old_path: &str, |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 117 | new_fd: &WasiFd, |
Matthew Maurer | f4d8f81 | 2020-03-27 13:14:30 -0700 | [diff] [blame] | 118 | new_path: &str, |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 119 | ) -> io::Result<()> { |
Chih-Hung Hsieh | 8cd2c99 | 2019-12-19 15:08:11 -0800 | [diff] [blame] | 120 | unsafe { |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame] | 121 | wasi::path_link( |
| 122 | self.as_raw_fd() as wasi::Fd, |
| 123 | old_flags, |
| 124 | old_path, |
| 125 | new_fd.as_raw_fd() as wasi::Fd, |
| 126 | new_path, |
| 127 | ) |
| 128 | .map_err(err2io) |
Chih-Hung Hsieh | 8cd2c99 | 2019-12-19 15:08:11 -0800 | [diff] [blame] | 129 | } |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | pub fn open( |
| 133 | &self, |
Matthew Maurer | f4d8f81 | 2020-03-27 13:14:30 -0700 | [diff] [blame] | 134 | dirflags: wasi::Lookupflags, |
| 135 | path: &str, |
| 136 | oflags: wasi::Oflags, |
Chih-Hung Hsieh | 8cd2c99 | 2019-12-19 15:08:11 -0800 | [diff] [blame] | 137 | fs_rights_base: wasi::Rights, |
| 138 | fs_rights_inheriting: wasi::Rights, |
Matthew Maurer | f4d8f81 | 2020-03-27 13:14:30 -0700 | [diff] [blame] | 139 | fs_flags: wasi::Fdflags, |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 140 | ) -> io::Result<WasiFd> { |
| 141 | unsafe { |
Chih-Hung Hsieh | 8cd2c99 | 2019-12-19 15:08:11 -0800 | [diff] [blame] | 142 | wasi::path_open( |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame] | 143 | self.as_raw_fd() as wasi::Fd, |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 144 | dirflags, |
Chih-Hung Hsieh | 8cd2c99 | 2019-12-19 15:08:11 -0800 | [diff] [blame] | 145 | path, |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 146 | oflags, |
| 147 | fs_rights_base, |
| 148 | fs_rights_inheriting, |
| 149 | fs_flags, |
Matthew Maurer | f4d8f81 | 2020-03-27 13:14:30 -0700 | [diff] [blame] | 150 | ) |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame] | 151 | .map(|fd| WasiFd::from_raw_fd(fd as RawFd)) |
Matthew Maurer | f4d8f81 | 2020-03-27 13:14:30 -0700 | [diff] [blame] | 152 | .map_err(err2io) |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | |
Matthew Maurer | f4d8f81 | 2020-03-27 13:14:30 -0700 | [diff] [blame] | 156 | pub fn readdir(&self, buf: &mut [u8], cookie: wasi::Dircookie) -> io::Result<usize> { |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame] | 157 | unsafe { |
| 158 | wasi::fd_readdir(self.as_raw_fd() as wasi::Fd, buf.as_mut_ptr(), buf.len(), cookie) |
| 159 | .map_err(err2io) |
| 160 | } |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Matthew Maurer | f4d8f81 | 2020-03-27 13:14:30 -0700 | [diff] [blame] | 163 | pub fn readlink(&self, path: &str, buf: &mut [u8]) -> io::Result<usize> { |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame] | 164 | unsafe { |
| 165 | wasi::path_readlink(self.as_raw_fd() as wasi::Fd, path, buf.as_mut_ptr(), buf.len()) |
| 166 | .map_err(err2io) |
| 167 | } |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 168 | } |
| 169 | |
Matthew Maurer | f4d8f81 | 2020-03-27 13:14:30 -0700 | [diff] [blame] | 170 | pub fn rename(&self, old_path: &str, new_fd: &WasiFd, new_path: &str) -> io::Result<()> { |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame] | 171 | unsafe { |
| 172 | wasi::path_rename( |
| 173 | self.as_raw_fd() as wasi::Fd, |
| 174 | old_path, |
| 175 | new_fd.as_raw_fd() as wasi::Fd, |
| 176 | new_path, |
| 177 | ) |
| 178 | .map_err(err2io) |
| 179 | } |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Matthew Maurer | f4d8f81 | 2020-03-27 13:14:30 -0700 | [diff] [blame] | 182 | pub fn filestat_get(&self) -> io::Result<wasi::Filestat> { |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame] | 183 | unsafe { wasi::fd_filestat_get(self.as_raw_fd() as wasi::Fd).map_err(err2io) } |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | pub fn filestat_set_times( |
| 187 | &self, |
Chih-Hung Hsieh | 8cd2c99 | 2019-12-19 15:08:11 -0800 | [diff] [blame] | 188 | atim: wasi::Timestamp, |
| 189 | mtim: wasi::Timestamp, |
Matthew Maurer | f4d8f81 | 2020-03-27 13:14:30 -0700 | [diff] [blame] | 190 | fstflags: wasi::Fstflags, |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 191 | ) -> io::Result<()> { |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame] | 192 | unsafe { |
| 193 | wasi::fd_filestat_set_times(self.as_raw_fd() as wasi::Fd, atim, mtim, fstflags) |
| 194 | .map_err(err2io) |
| 195 | } |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | pub fn filestat_set_size(&self, size: u64) -> io::Result<()> { |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame] | 199 | unsafe { wasi::fd_filestat_set_size(self.as_raw_fd() as wasi::Fd, size).map_err(err2io) } |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | pub fn path_filestat_get( |
| 203 | &self, |
Matthew Maurer | f4d8f81 | 2020-03-27 13:14:30 -0700 | [diff] [blame] | 204 | flags: wasi::Lookupflags, |
| 205 | path: &str, |
| 206 | ) -> io::Result<wasi::Filestat> { |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame] | 207 | unsafe { |
| 208 | wasi::path_filestat_get(self.as_raw_fd() as wasi::Fd, flags, path).map_err(err2io) |
| 209 | } |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | pub fn path_filestat_set_times( |
| 213 | &self, |
Matthew Maurer | f4d8f81 | 2020-03-27 13:14:30 -0700 | [diff] [blame] | 214 | flags: wasi::Lookupflags, |
| 215 | path: &str, |
Chih-Hung Hsieh | 8cd2c99 | 2019-12-19 15:08:11 -0800 | [diff] [blame] | 216 | atim: wasi::Timestamp, |
| 217 | mtim: wasi::Timestamp, |
Matthew Maurer | f4d8f81 | 2020-03-27 13:14:30 -0700 | [diff] [blame] | 218 | fstflags: wasi::Fstflags, |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 219 | ) -> io::Result<()> { |
Chih-Hung Hsieh | 8cd2c99 | 2019-12-19 15:08:11 -0800 | [diff] [blame] | 220 | unsafe { |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame] | 221 | wasi::path_filestat_set_times( |
| 222 | self.as_raw_fd() as wasi::Fd, |
| 223 | flags, |
| 224 | path, |
| 225 | atim, |
| 226 | mtim, |
| 227 | fstflags, |
| 228 | ) |
| 229 | .map_err(err2io) |
Chih-Hung Hsieh | 8cd2c99 | 2019-12-19 15:08:11 -0800 | [diff] [blame] | 230 | } |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Matthew Maurer | f4d8f81 | 2020-03-27 13:14:30 -0700 | [diff] [blame] | 233 | pub fn symlink(&self, old_path: &str, new_path: &str) -> io::Result<()> { |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame] | 234 | unsafe { |
| 235 | wasi::path_symlink(old_path, self.as_raw_fd() as wasi::Fd, new_path).map_err(err2io) |
| 236 | } |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Matthew Maurer | f4d8f81 | 2020-03-27 13:14:30 -0700 | [diff] [blame] | 239 | pub fn unlink_file(&self, path: &str) -> io::Result<()> { |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame] | 240 | unsafe { wasi::path_unlink_file(self.as_raw_fd() as wasi::Fd, path).map_err(err2io) } |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 241 | } |
| 242 | |
Matthew Maurer | f4d8f81 | 2020-03-27 13:14:30 -0700 | [diff] [blame] | 243 | pub fn remove_directory(&self, path: &str) -> io::Result<()> { |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame] | 244 | unsafe { wasi::path_remove_directory(self.as_raw_fd() as wasi::Fd, path).map_err(err2io) } |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 245 | } |
| 246 | |
Chris Wailes | 2805eef | 2022-04-07 11:22:56 -0700 | [diff] [blame] | 247 | pub fn sock_accept(&self, flags: wasi::Fdflags) -> io::Result<wasi::Fd> { |
| 248 | unsafe { wasi::sock_accept(self.as_raw_fd() as wasi::Fd, flags).map_err(err2io) } |
| 249 | } |
| 250 | |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 251 | pub fn sock_recv( |
| 252 | &self, |
Chih-Hung Hsieh | fd666f2 | 2019-12-19 14:34:18 -0800 | [diff] [blame] | 253 | ri_data: &mut [IoSliceMut<'_>], |
Matthew Maurer | f4d8f81 | 2020-03-27 13:14:30 -0700 | [diff] [blame] | 254 | ri_flags: wasi::Riflags, |
| 255 | ) -> io::Result<(usize, wasi::Roflags)> { |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame] | 256 | unsafe { |
| 257 | wasi::sock_recv(self.as_raw_fd() as wasi::Fd, iovec(ri_data), ri_flags).map_err(err2io) |
| 258 | } |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Matthew Maurer | f4d8f81 | 2020-03-27 13:14:30 -0700 | [diff] [blame] | 261 | pub fn sock_send(&self, si_data: &[IoSlice<'_>], si_flags: wasi::Siflags) -> io::Result<usize> { |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame] | 262 | unsafe { |
| 263 | wasi::sock_send(self.as_raw_fd() as wasi::Fd, ciovec(si_data), si_flags).map_err(err2io) |
| 264 | } |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | pub fn sock_shutdown(&self, how: Shutdown) -> io::Result<()> { |
| 268 | let how = match how { |
Matthew Maurer | f4d8f81 | 2020-03-27 13:14:30 -0700 | [diff] [blame] | 269 | Shutdown::Read => wasi::SDFLAGS_RD, |
| 270 | Shutdown::Write => wasi::SDFLAGS_WR, |
| 271 | Shutdown::Both => wasi::SDFLAGS_WR | wasi::SDFLAGS_RD, |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 272 | }; |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame] | 273 | unsafe { wasi::sock_shutdown(self.as_raw_fd() as wasi::Fd, how).map_err(err2io) } |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame] | 277 | impl AsInner<OwnedFd> for WasiFd { |
Chris Wailes | cd1aefd | 2023-07-13 13:36:21 -0700 | [diff] [blame^] | 278 | #[inline] |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame] | 279 | fn as_inner(&self) -> &OwnedFd { |
| 280 | &self.fd |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | impl AsInnerMut<OwnedFd> for WasiFd { |
Chris Wailes | cd1aefd | 2023-07-13 13:36:21 -0700 | [diff] [blame^] | 285 | #[inline] |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame] | 286 | fn as_inner_mut(&mut self) -> &mut OwnedFd { |
| 287 | &mut self.fd |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | impl IntoInner<OwnedFd> for WasiFd { |
| 292 | fn into_inner(self) -> OwnedFd { |
| 293 | self.fd |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | impl FromInner<OwnedFd> for WasiFd { |
| 298 | fn from_inner(owned_fd: OwnedFd) -> Self { |
| 299 | Self { fd: owned_fd } |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | impl AsFd for WasiFd { |
| 304 | fn as_fd(&self) -> BorrowedFd<'_> { |
| 305 | self.fd.as_fd() |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | impl AsRawFd for WasiFd { |
Chris Wailes | cd1aefd | 2023-07-13 13:36:21 -0700 | [diff] [blame^] | 310 | #[inline] |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame] | 311 | fn as_raw_fd(&self) -> RawFd { |
| 312 | self.fd.as_raw_fd() |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | impl IntoRawFd for WasiFd { |
| 317 | fn into_raw_fd(self) -> RawFd { |
| 318 | self.fd.into_raw_fd() |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | impl FromRawFd for WasiFd { |
| 323 | unsafe fn from_raw_fd(raw_fd: RawFd) -> Self { |
| 324 | unsafe { Self { fd: FromRawFd::from_raw_fd(raw_fd) } } |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 325 | } |
| 326 | } |