From e94c139a47055d3492d6ccccfce4acbcba9d7391 Mon Sep 17 00:00:00 2001 From: Xavier L'Heureux Date: Tue, 16 Jul 2019 15:02:54 -0400 Subject: Remove more unsupported functions and make it possible to run tests --- src/dir.rs | 23 ++++++-- src/fcntl.rs | 163 ++++++++++++++++++++++++++++++++---------------------- src/sys/signal.rs | 22 ++++---- src/sys/stat.rs | 1 + src/unistd.rs | 4 ++ 5 files changed, 131 insertions(+), 82 deletions(-) (limited to 'src') diff --git a/src/dir.rs b/src/dir.rs index 42731a4e..9648fd43 100644 --- a/src/dir.rs +++ b/src/dir.rs @@ -1,17 +1,25 @@ +#[cfg(not(target_os = "redox"))] use {Error, NixPath, Result}; +#[cfg(not(target_os = "redox"))] use errno::Errno; +#[cfg(not(target_os = "redox"))] use fcntl::{self, OFlag}; use libc; -use std::os::unix::io::{IntoRawFd, RawFd}; #[cfg(not(target_os = "redox"))] -use std::os::unix::io::AsRawFd; -use std::{ffi, ptr}; +use std::os::unix::io::{AsRawFd, IntoRawFd, RawFd}; +#[cfg(not(target_os = "redox"))] +use std::ptr; +use std::ffi; +#[cfg(not(target_os = "redox"))] use sys; #[cfg(target_os = "linux")] use libc::{dirent64 as dirent, readdir64_r as readdir_r}; -#[cfg(not(target_os = "linux"))] +#[cfg(target_os = "redox")] +use libc::dirent; + +#[cfg(not(any(target_os = "linux", target_os = "redox")))] use libc::{dirent, readdir_r}; /// An open directory. @@ -28,10 +36,12 @@ use libc::{dirent, readdir_r}; /// * returns entries' names as a `CStr` (no allocation or conversion beyond whatever libc /// does). #[derive(Clone, Debug, Eq, Hash, PartialEq)] +#[cfg(not(target_os = "redox"))] pub struct Dir( ptr::NonNull ); +#[cfg(not(target_os = "redox"))] impl Dir { /// Opens the given path as with `fcntl::open`. pub fn open(path: &P, oflag: OFlag, @@ -77,6 +87,7 @@ impl Dir { // call `readdir` simultaneously from multiple threads. // // `Dir` is safe to pass from one thread to another, as it's not reference-counted. +#[cfg(not(target_os = "redox"))] unsafe impl Send for Dir {} #[cfg(not(target_os = "redox"))] @@ -86,6 +97,7 @@ impl AsRawFd for Dir { } } +#[cfg(not(target_os = "redox"))] impl Drop for Dir { fn drop(&mut self) { unsafe { libc::closedir(self.0.as_ptr()) }; @@ -93,8 +105,10 @@ impl Drop for Dir { } #[derive(Debug, Eq, Hash, PartialEq)] +#[cfg(not(target_os = "redox"))] pub struct Iter<'d>(&'d mut Dir); +#[cfg(not(target_os = "redox"))] impl<'d> Iterator for Iter<'d> { type Item = Result; @@ -121,6 +135,7 @@ impl<'d> Iterator for Iter<'d> { } } +#[cfg(not(target_os = "redox"))] impl<'d> Drop for Iter<'d> { fn drop(&mut self) { unsafe { libc::rewinddir((self.0).0.as_ptr()) } diff --git a/src/fcntl.rs b/src/fcntl.rs index 661b5ded..6b554c23 100644 --- a/src/fcntl.rs +++ b/src/fcntl.rs @@ -1,29 +1,31 @@ -use {Result, NixPath}; use errno::Errno; -use libc::{self, c_int, c_uint, c_char, size_t, ssize_t}; -use sys::stat::Mode; +use libc::{self, c_char, c_int, c_uint, size_t, ssize_t}; +use std::ffi::OsString; #[cfg(not(target_os = "redox"))] use std::os::raw; -use std::os::unix::io::RawFd; -use std::ffi::OsString; use std::os::unix::ffi::OsStringExt; +use std::os::unix::io::RawFd; +use sys::stat::Mode; +use {NixPath, Result}; #[cfg(any(target_os = "android", target_os = "linux"))] use std::ptr; // For splice and copy_file_range #[cfg(any(target_os = "android", target_os = "linux"))] -use sys::uio::IoVec; // For vmsplice - -#[cfg(any(target_os = "linux", - target_os = "android", - target_os = "emscripten", - target_os = "fuchsia", - any(target_os = "wasi", target_env = "wasi"), - target_env = "uclibc", - target_env = "freebsd"))] +use sys::uio::IoVec; // For vmsplice + +#[cfg(any( + target_os = "linux", + target_os = "android", + target_os = "emscripten", + target_os = "fuchsia", + any(target_os = "wasi", target_env = "wasi"), + target_env = "uclibc", + target_env = "freebsd" +))] pub use self::posix_fadvise::*; #[cfg(not(target_os = "redox"))] -libc_bitflags!{ +libc_bitflags! { pub struct AtFlags: c_int { AT_REMOVEDIR; AT_SYMLINK_FOLLOW; @@ -168,7 +170,13 @@ pub fn open(path: &P, oflag: OFlag, mode: Mode) -> Result(dirfd: RawFd, path: &P, oflag: OFlag, mode: Mode) -> Result { +#[cfg(not(target_os = "redox"))] +pub fn openat( + dirfd: RawFd, + path: &P, + oflag: OFlag, + mode: Mode, +) -> Result { let fd = path.with_nix_path(|cstr| { let modebits = c_uint::from(mode.bits()); unsafe { libc::openat(dirfd, cstr.as_ptr(), oflag.bits(), modebits) } @@ -176,13 +184,21 @@ pub fn openat(dirfd: RawFd, path: &P, oflag: OFlag, mode: M Errno::result(fd) } -pub fn renameat(old_dirfd: Option, old_path: &P1, - new_dirfd: Option, new_path: &P2) - -> Result<()> { +#[cfg(not(target_os = "redox"))] +pub fn renameat( + old_dirfd: Option, + old_path: &P1, + new_dirfd: Option, + new_path: &P2, +) -> Result<()> { let res = old_path.with_nix_path(|old_cstr| { new_path.with_nix_path(|new_cstr| unsafe { - libc::renameat(at_rawfd(old_dirfd), old_cstr.as_ptr(), - at_rawfd(new_dirfd), new_cstr.as_ptr()) + libc::renameat( + at_rawfd(old_dirfd), + old_cstr.as_ptr(), + at_rawfd(new_dirfd), + new_cstr.as_ptr(), + ) }) })??; Errno::result(res).map(drop) @@ -194,25 +210,32 @@ fn wrap_readlink_result(mut v: Vec, len: ssize_t) -> Result { Ok(OsString::from_vec(v.to_vec())) } -fn readlink_maybe_at(dirfd: Option, path: &P, - v: &mut Vec) - -> Result { - path.with_nix_path(|cstr| { - unsafe { - match dirfd { - Some(dirfd) => libc::readlinkat(dirfd, cstr.as_ptr(), - v.as_mut_ptr() as *mut c_char, - v.capacity() as size_t), - None => libc::readlink(cstr.as_ptr(), - v.as_mut_ptr() as *mut c_char, - v.capacity() as size_t), - } +fn readlink_maybe_at( + dirfd: Option, + path: &P, + v: &mut Vec, +) -> Result { + path.with_nix_path(|cstr| unsafe { + match dirfd { + #[cfg(target_os = "redox")] + Some(_) => unreachable!(), + #[cfg(not(target_os = "redox"))] + Some(dirfd) => libc::readlinkat( + dirfd, + cstr.as_ptr(), + v.as_mut_ptr() as *mut c_char, + v.capacity() as size_t, + ), + None => libc::readlink( + cstr.as_ptr(), + v.as_mut_ptr() as *mut c_char, + v.capacity() as size_t, + ), } }) } -fn inner_readlink(dirfd: Option, path: &P) - -> Result { +fn inner_readlink(dirfd: Option, path: &P) -> Result { let mut v = Vec::with_capacity(libc::PATH_MAX as usize); // simple case: result is strictly less than `PATH_MAX` let res = readlink_maybe_at(dirfd, path, &mut v)?; @@ -224,7 +247,8 @@ fn inner_readlink(dirfd: Option, path: &P) // Uh oh, the result is too long... // Let's try to ask lstat how many bytes to allocate. let reported_size = super::sys::stat::lstat(path) - .and_then(|x| Ok(x.st_size)).unwrap_or(0); + .and_then(|x| Ok(x.st_size)) + .unwrap_or(0); let mut try_size = if reported_size > 0 { // Note: even if `lstat`'s apparently valid answer turns out to be // wrong, we will still read the full symlink no matter what. @@ -241,14 +265,13 @@ fn inner_readlink(dirfd: Option, path: &P) debug_assert!(len >= 0); if (len as usize) < v.capacity() { break wrap_readlink_result(v, res); - } - else { + } else { // Ugh! Still not big enough! match try_size.checked_shl(1) { Some(next_size) => try_size = next_size, // It's absurd that this would happen, but handle it sanely // anyway. - None => break Err(super::Error::Sys(Errno::ENAMETOOLONG)) + None => break Err(super::Error::Sys(Errno::ENAMETOOLONG)), } } } @@ -258,9 +281,8 @@ pub fn readlink(path: &P) -> Result { inner_readlink(None, path) } - -pub fn readlinkat(dirfd: RawFd, path: &P) - -> Result { +#[cfg(not(target_os = "redox"))] +pub fn readlinkat(dirfd: RawFd, path: &P) -> Result { inner_readlink(Some(dirfd), path) } @@ -324,7 +346,6 @@ pub enum FcntlArg<'a> { F_GETPIPE_SZ, #[cfg(any(target_os = "linux", target_os = "android"))] F_SETPIPE_SZ(c_int), - // TODO: Rest of flags } @@ -488,9 +509,7 @@ pub fn splice( .map(|offset| offset as *mut libc::loff_t) .unwrap_or(ptr::null_mut()); - let ret = unsafe { - libc::splice(fd_in, off_in, fd_out, off_out, len, flags.bits()) - }; + let ret = unsafe { libc::splice(fd_in, off_in, fd_out, off_out, len, flags.bits()) }; Errno::result(ret).map(|r| r as usize) } @@ -503,7 +522,12 @@ pub fn tee(fd_in: RawFd, fd_out: RawFd, len: usize, flags: SpliceFFlags) -> Resu #[cfg(any(target_os = "linux", target_os = "android"))] pub fn vmsplice(fd: RawFd, iov: &[IoVec<&[u8]>], flags: SpliceFFlags) -> Result { let ret = unsafe { - libc::vmsplice(fd, iov.as_ptr() as *const libc::iovec, iov.len(), flags.bits()) + libc::vmsplice( + fd, + iov.as_ptr() as *const libc::iovec, + iov.len(), + flags.bits(), + ) }; Errno::result(ret).map(|r| r as usize) } @@ -544,23 +568,30 @@ libc_bitflags!( /// Allows the caller to directly manipulate the allocated disk space for the /// file referred to by fd. #[cfg(any(target_os = "linux"))] -pub fn fallocate(fd: RawFd, mode: FallocateFlags, offset: libc::off_t, len: libc::off_t) -> Result<()> { +pub fn fallocate( + fd: RawFd, + mode: FallocateFlags, + offset: libc::off_t, + len: libc::off_t, +) -> Result<()> { let res = unsafe { libc::fallocate(fd, mode.bits(), offset, len) }; Errno::result(res).map(drop) } -#[cfg(any(target_os = "linux", - target_os = "android", - target_os = "emscripten", - target_os = "fuchsia", - any(target_os = "wasi", target_env = "wasi"), - target_env = "uclibc", - target_env = "freebsd"))] +#[cfg(any( + target_os = "linux", + target_os = "android", + target_os = "emscripten", + target_os = "fuchsia", + any(target_os = "wasi", target_env = "wasi"), + target_env = "uclibc", + target_env = "freebsd" +))] mod posix_fadvise { - use Result; - use libc; use errno::Errno; + use libc; use std::os::unix::io::RawFd; + use Result; libc_enum! { #[repr(i32)] @@ -574,10 +605,12 @@ mod posix_fadvise { } } - pub fn posix_fadvise(fd: RawFd, - offset: libc::off_t, - len: libc::off_t, - advice: PosixFadviseAdvice) -> Result { + pub fn posix_fadvise( + fd: RawFd, + offset: libc::off_t, + len: libc::off_t, + advice: PosixFadviseAdvice, + ) -> Result { let res = unsafe { libc::posix_fadvise(fd, offset, len, advice as libc::c_int) }; Errno::result(res) } @@ -591,11 +624,7 @@ mod posix_fadvise { any(target_os = "wasi", target_env = "wasi"), target_os = "freebsd" ))] -pub fn posix_fallocate( - fd: RawFd, - offset: libc::off_t, - len: libc::off_t -) -> Result<()> { +pub fn posix_fallocate(fd: RawFd, offset: libc::off_t, len: libc::off_t) -> Result<()> { let res = unsafe { libc::posix_fallocate(fd, offset, len) }; match Errno::result(res) { Err(err) => Err(err), diff --git a/src/sys/signal.rs b/src/sys/signal.rs index ad5cc031..b49a9f53 100644 --- a/src/sys/signal.rs +++ b/src/sys/signal.rs @@ -464,6 +464,7 @@ impl SigSet { /// Suspends execution of the calling thread until one of the signals in the /// signal mask becomes pending, and returns the accepted signal. + #[cfg(not(target_os = "redox"))] pub fn wait(&self) -> Result { let mut signum = mem::MaybeUninit::uninit(); let res = unsafe { libc::sigwait(&self.sigset as *const libc::sigset_t, signum.as_mut_ptr()) }; @@ -890,6 +891,7 @@ mod sigevent { #[cfg(test)] mod tests { + #[cfg(not(target_os = "redox"))] use std::thread; use super::*; @@ -945,6 +947,7 @@ mod tests { } #[test] + #[cfg(not(target_os = "redox"))] fn test_thread_signal_set_mask() { thread::spawn(|| { let prev_mask = SigSet::thread_get_mask() @@ -965,6 +968,7 @@ mod tests { } #[test] + #[cfg(not(target_os = "redox"))] fn test_thread_signal_block() { thread::spawn(|| { let mut mask = SigSet::empty(); @@ -977,6 +981,7 @@ mod tests { } #[test] + #[cfg(not(target_os = "redox"))] fn test_thread_signal_unblock() { thread::spawn(|| { let mut mask = SigSet::empty(); @@ -989,6 +994,7 @@ mod tests { } #[test] + #[cfg(not(target_os = "redox"))] fn test_thread_signal_swap() { thread::spawn(|| { let mut mask = SigSet::empty(); @@ -1011,23 +1017,14 @@ mod tests { } #[test] + #[cfg(not(target_os = "redox"))] fn test_sigaction() { use libc; thread::spawn(|| { extern fn test_sigaction_handler(_: libc::c_int) {} - #[cfg(not(target_os = "redox"))] extern fn test_sigaction_action(_: libc::c_int, _: *mut libc::siginfo_t, _: *mut libc::c_void) {} - #[cfg(not(target_os = "redox"))] - fn test_sigaction_sigaction(flags: SaFlags, mask: SigSet) { - let handler_act = SigHandler::SigAction(test_sigaction_action); - let action_act = SigAction::new(handler_act, flags, mask); - assert_eq!(action_act.handler(), handler_act); - } - #[cfg(target_os = "redox")] - fn test_sigaction_sigaction() {} - let handler_sig = SigHandler::Handler(test_sigaction_handler); let flags = SaFlags::SA_ONSTACK | SaFlags::SA_RESTART | @@ -1046,7 +1043,9 @@ mod tests { assert!(mask.contains(SIGUSR1)); assert!(!mask.contains(SIGUSR2)); - test_sigaction_sigaction(flags, mask); + let handler_act = SigHandler::SigAction(test_sigaction_action); + let action_act = SigAction::new(handler_act, flags, mask); + assert_eq!(action_act.handler(), handler_act); let action_dfl = SigAction::new(SigHandler::SigDfl, flags, mask); assert_eq!(action_dfl.handler(), SigHandler::SigDfl); @@ -1057,6 +1056,7 @@ mod tests { } #[test] + #[cfg(not(target_os = "redox"))] fn test_sigwait() { thread::spawn(|| { let mut mask = SigSet::empty(); diff --git a/src/sys/stat.rs b/src/sys/stat.rs index 5d426844..f8fae4ee 100644 --- a/src/sys/stat.rs +++ b/src/sys/stat.rs @@ -289,6 +289,7 @@ pub fn utimensat( Errno::result(res).map(drop) } +#[cfg(not(target_os = "redox"))] pub fn mkdirat(fd: RawFd, path: &P, mode: Mode) -> Result<()> { let res = path.with_nix_path(|cstr| { unsafe { libc::mkdirat(fd, cstr.as_ptr(), mode.bits() as mode_t) } diff --git a/src/unistd.rs b/src/unistd.rs index a663e7cd..7ee0a14e 100644 --- a/src/unistd.rs +++ b/src/unistd.rs @@ -291,6 +291,7 @@ pub fn setsid() -> Result { /// Obtain the process group ID of the process that is the session leader of the process specified /// by pid. If pid is zero, it specifies the calling process. #[inline] +#[cfg(not(target_os = "redox"))] pub fn getsid(pid: Option) -> Result { let res = unsafe { libc::getsid(pid.unwrap_or(Pid(0)).into()) }; Errno::result(res).map(Pid) @@ -1149,6 +1150,7 @@ fn pipe2_setflags(fd1: RawFd, fd2: RawFd, flags: OFlag) -> Result<()> { /// /// See also /// [truncate(2)](http://pubs.opengroup.org/onlinepubs/9699919799/functions/truncate.html) +#[cfg(not(target_os = "redox"))] pub fn truncate(path: &P, len: off_t) -> Result<()> { let res = path.with_nix_path(|cstr| { unsafe { @@ -1263,6 +1265,7 @@ pub enum UnlinkatFlags { /// /// # References /// See also [unlinkat(2)](http://pubs.opengroup.org/onlinepubs/9699919799/functions/unlinkat.html) +#[cfg(not(target_os = "redox"))] pub fn unlinkat( dirfd: Option, path: &P, @@ -1657,6 +1660,7 @@ pub fn initgroups(user: &CStr, group: Gid) -> Result<()> { /// /// See also [pause(2)](http://pubs.opengroup.org/onlinepubs/9699919799/functions/pause.html). #[inline] +#[cfg(not(target_os = "redox"))] pub fn pause() { unsafe { libc::pause() }; } -- cgit v1.2.3