1 #![deny(unsafe_op_in_unsafe_fn)]
5 use crate::io::{self, IoSlice, IoSliceMut, SeekFrom};
7 use crate::net::Shutdown;
8 use crate::os::wasi::io::{AsFd, AsRawFd, BorrowedFd, FromRawFd, IntoRawFd, OwnedFd, RawFd};
9 use crate::sys_common::{AsInner, AsInnerMut, FromInner, IntoInner};
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>());
19 // SAFETY: `IoSliceMut` and `IoVec` have exactly the same memory layout
20 unsafe { mem::transmute(a) }
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>());
26 // SAFETY: `IoSlice` and `CIoVec` have exactly the same memory layout
27 unsafe { mem::transmute(a) }
31 pub fn datasync(&self) -> io::Result<()> {
32 unsafe { wasi::fd_datasync(self.as_raw_fd() as wasi::Fd).map_err(err2io) }
35 pub fn pread(&self, bufs: &mut [IoSliceMut<'_>], offset: u64) -> io::Result<usize> {
36 unsafe { wasi::fd_pread(self.as_raw_fd() as wasi::Fd, iovec(bufs), offset).map_err(err2io) }
39 pub fn pwrite(&self, bufs: &[IoSlice<'_>], offset: u64) -> io::Result<usize> {
41 wasi::fd_pwrite(self.as_raw_fd() as wasi::Fd, ciovec(bufs), offset).map_err(err2io)
45 pub fn read(&self, bufs: &mut [IoSliceMut<'_>]) -> io::Result<usize> {
46 unsafe { wasi::fd_read(self.as_raw_fd() as wasi::Fd, iovec(bufs)).map_err(err2io) }
49 pub fn write(&self, bufs: &[IoSlice<'_>]) -> io::Result<usize> {
50 unsafe { wasi::fd_write(self.as_raw_fd() as wasi::Fd, ciovec(bufs)).map_err(err2io) }
53 pub fn seek(&self, pos: SeekFrom) -> io::Result<u64> {
54 let (whence, offset) = match pos {
55 SeekFrom::Start(pos) => (wasi::WHENCE_SET, pos as i64),
56 SeekFrom::End(pos) => (wasi::WHENCE_END, pos),
57 SeekFrom::Current(pos) => (wasi::WHENCE_CUR, pos),
59 unsafe { wasi::fd_seek(self.as_raw_fd() as wasi::Fd, offset, whence).map_err(err2io) }
62 pub fn tell(&self) -> io::Result<u64> {
63 unsafe { wasi::fd_tell(self.as_raw_fd() as wasi::Fd).map_err(err2io) }
66 // FIXME: __wasi_fd_fdstat_get
68 pub fn set_flags(&self, flags: wasi::Fdflags) -> io::Result<()> {
69 unsafe { wasi::fd_fdstat_set_flags(self.as_raw_fd() as wasi::Fd, flags).map_err(err2io) }
72 pub fn set_rights(&self, base: wasi::Rights, inheriting: wasi::Rights) -> io::Result<()> {
74 wasi::fd_fdstat_set_rights(self.as_raw_fd() as wasi::Fd, base, inheriting)
79 pub fn sync(&self) -> io::Result<()> {
80 unsafe { wasi::fd_sync(self.as_raw_fd() as wasi::Fd).map_err(err2io) }
83 pub fn advise(&self, offset: u64, len: u64, advice: wasi::Advice) -> io::Result<()> {
85 wasi::fd_advise(self.as_raw_fd() as wasi::Fd, offset, len, advice).map_err(err2io)
89 pub fn allocate(&self, offset: u64, len: u64) -> io::Result<()> {
90 unsafe { wasi::fd_allocate(self.as_raw_fd() as wasi::Fd, offset, len).map_err(err2io) }
93 pub fn create_directory(&self, path: &str) -> io::Result<()> {
94 unsafe { wasi::path_create_directory(self.as_raw_fd() as wasi::Fd, path).map_err(err2io) }
99 old_flags: wasi::Lookupflags,
103 ) -> io::Result<()> {
106 self.as_raw_fd() as wasi::Fd,
109 new_fd.as_raw_fd() as wasi::Fd,
118 dirflags: wasi::Lookupflags,
120 oflags: wasi::Oflags,
121 fs_rights_base: wasi::Rights,
122 fs_rights_inheriting: wasi::Rights,
123 fs_flags: wasi::Fdflags,
124 ) -> io::Result<WasiFd> {
127 self.as_raw_fd() as wasi::Fd,
132 fs_rights_inheriting,
135 .map(|fd| WasiFd::from_raw_fd(fd as RawFd))
140 pub fn readdir(&self, buf: &mut [u8], cookie: wasi::Dircookie) -> io::Result<usize> {
142 wasi::fd_readdir(self.as_raw_fd() as wasi::Fd, buf.as_mut_ptr(), buf.len(), cookie)
147 pub fn readlink(&self, path: &str, buf: &mut [u8]) -> io::Result<usize> {
149 wasi::path_readlink(self.as_raw_fd() as wasi::Fd, path, buf.as_mut_ptr(), buf.len())
154 pub fn rename(&self, old_path: &str, new_fd: &WasiFd, new_path: &str) -> io::Result<()> {
157 self.as_raw_fd() as wasi::Fd,
159 new_fd.as_raw_fd() as wasi::Fd,
166 pub fn filestat_get(&self) -> io::Result<wasi::Filestat> {
167 unsafe { wasi::fd_filestat_get(self.as_raw_fd() as wasi::Fd).map_err(err2io) }
170 pub fn filestat_set_times(
172 atim: wasi::Timestamp,
173 mtim: wasi::Timestamp,
174 fstflags: wasi::Fstflags,
175 ) -> io::Result<()> {
177 wasi::fd_filestat_set_times(self.as_raw_fd() as wasi::Fd, atim, mtim, fstflags)
182 pub fn filestat_set_size(&self, size: u64) -> io::Result<()> {
183 unsafe { wasi::fd_filestat_set_size(self.as_raw_fd() as wasi::Fd, size).map_err(err2io) }
186 pub fn path_filestat_get(
188 flags: wasi::Lookupflags,
190 ) -> io::Result<wasi::Filestat> {
192 wasi::path_filestat_get(self.as_raw_fd() as wasi::Fd, flags, path).map_err(err2io)
196 pub fn path_filestat_set_times(
198 flags: wasi::Lookupflags,
200 atim: wasi::Timestamp,
201 mtim: wasi::Timestamp,
202 fstflags: wasi::Fstflags,
203 ) -> io::Result<()> {
205 wasi::path_filestat_set_times(
206 self.as_raw_fd() as wasi::Fd,
217 pub fn symlink(&self, old_path: &str, new_path: &str) -> io::Result<()> {
219 wasi::path_symlink(old_path, self.as_raw_fd() as wasi::Fd, new_path).map_err(err2io)
223 pub fn unlink_file(&self, path: &str) -> io::Result<()> {
224 unsafe { wasi::path_unlink_file(self.as_raw_fd() as wasi::Fd, path).map_err(err2io) }
227 pub fn remove_directory(&self, path: &str) -> io::Result<()> {
228 unsafe { wasi::path_remove_directory(self.as_raw_fd() as wasi::Fd, path).map_err(err2io) }
231 pub fn sock_accept(&self, flags: wasi::Fdflags) -> io::Result<wasi::Fd> {
232 unsafe { wasi::sock_accept(self.as_raw_fd() as wasi::Fd, flags).map_err(err2io) }
237 ri_data: &mut [IoSliceMut<'_>],
238 ri_flags: wasi::Riflags,
239 ) -> io::Result<(usize, wasi::Roflags)> {
241 wasi::sock_recv(self.as_raw_fd() as wasi::Fd, iovec(ri_data), ri_flags).map_err(err2io)
245 pub fn sock_send(&self, si_data: &[IoSlice<'_>], si_flags: wasi::Siflags) -> io::Result<usize> {
247 wasi::sock_send(self.as_raw_fd() as wasi::Fd, ciovec(si_data), si_flags).map_err(err2io)
251 pub fn sock_shutdown(&self, how: Shutdown) -> io::Result<()> {
252 let how = match how {
253 Shutdown::Read => wasi::SDFLAGS_RD,
254 Shutdown::Write => wasi::SDFLAGS_WR,
255 Shutdown::Both => wasi::SDFLAGS_WR | wasi::SDFLAGS_RD,
257 unsafe { wasi::sock_shutdown(self.as_raw_fd() as wasi::Fd, how).map_err(err2io) }
261 impl AsInner<OwnedFd> for WasiFd {
262 fn as_inner(&self) -> &OwnedFd {
267 impl AsInnerMut<OwnedFd> for WasiFd {
268 fn as_inner_mut(&mut self) -> &mut OwnedFd {
273 impl IntoInner<OwnedFd> for WasiFd {
274 fn into_inner(self) -> OwnedFd {
279 impl FromInner<OwnedFd> for WasiFd {
280 fn from_inner(owned_fd: OwnedFd) -> Self {
281 Self { fd: owned_fd }
285 impl AsFd for WasiFd {
286 fn as_fd(&self) -> BorrowedFd<'_> {
291 impl AsRawFd for WasiFd {
292 fn as_raw_fd(&self) -> RawFd {
297 impl IntoRawFd for WasiFd {
298 fn into_raw_fd(self) -> RawFd {
299 self.fd.into_raw_fd()
303 impl FromRawFd for WasiFd {
304 unsafe fn from_raw_fd(raw_fd: RawFd) -> Self {
305 unsafe { Self { fd: FromRawFd::from_raw_fd(raw_fd) } }