Begin porting yanix to WASI.

This isn't complete yet, but subsequent steps will depend on Rust libstd
and libc bindings changes that are in flight.
This commit is contained in:
Dan Gohman
2020-07-05 09:13:01 -07:00
committed by Jakub Konka
parent 25397d0c15
commit cf5289c553
9 changed files with 108 additions and 7 deletions

View File

@@ -18,6 +18,9 @@ cfg_if! {
target_os = "dragonfly"))] {
mod bsd;
pub(crate) use bsd::*;
} else if #[cfg(target_os = "wasi")] {
mod wasi;
pub(crate) use wasi::*;
} else {
compile_error!("yanix doesn't compile for this platform yet");
}

View File

@@ -0,0 +1,23 @@
use crate::from_success_code;
use std::io::Result;
use std::os::wasi::prelude::*;
#[derive(Debug, Copy, Clone)]
#[repr(i32)]
pub enum PosixFadviseAdvice {
Normal = libc::POSIX_FADV_NORMAL,
Sequential = libc::POSIX_FADV_SEQUENTIAL,
Random = libc::POSIX_FADV_RANDOM,
NoReuse = libc::POSIX_FADV_NOREUSE,
WillNeed = libc::POSIX_FADV_WILLNEED,
DontNeed = libc::POSIX_FADV_DONTNEED,
}
pub unsafe fn posix_fadvise(
fd: RawFd,
offset: libc::off_t,
len: libc::off_t,
advice: PosixFadviseAdvice,
) -> Result<()> {
from_success_code(libc::posix_fadvise(fd, offset, len, advice as libc::c_int))
}

View File

@@ -0,0 +1,21 @@
use std::{
io::{Error, Result},
os::wasi::prelude::*,
};
pub unsafe fn isatty(fd: RawFd) -> Result<bool> {
let res = libc::isatty(fd);
if res == 1 {
// isatty() returns 1 if fd is an open file descriptor referring to a terminal...
Ok(true)
} else {
// ... otherwise 0 is returned, and errno is set to indicate the error.
let errno = Error::last_os_error();
let raw_errno = errno.raw_os_error().unwrap();
if raw_errno == libc::ENOTTY {
Ok(false)
} else {
Err(errno)
}
}
}

View File

@@ -0,0 +1,33 @@
use crate::filetime::FileTime;
use crate::from_success_code;
use std::fs::File;
use std::io;
pub fn utimensat(
dirfd: &File,
path: &str,
atime: FileTime,
mtime: FileTime,
symlink_nofollow: bool,
) -> io::Result<()> {
use crate::filetime::to_timespec;
use std::ffi::CString;
use std::os::wasi::prelude::*;
let p = CString::new(path.as_bytes())?;
let times = [to_timespec(&atime)?, to_timespec(&mtime)?];
let flags = if symlink_nofollow {
libc::AT_SYMLINK_NOFOLLOW
} else {
0
};
from_success_code(unsafe {
libc::utimensat(
dirfd.as_raw_fd() as libc::c_int,
p.as_ptr(),
times.as_ptr(),
flags,
)
})
}

View File

@@ -0,0 +1,3 @@
pub(crate) mod fadvise;
pub(crate) mod file;
pub(crate) mod filetime;