diff --git a/src/dir.rs b/src/dir.rs index ea93c5f8bd..522018fc76 100644 --- a/src/dir.rs +++ b/src/dir.rs @@ -45,7 +45,7 @@ impl Dir { /// Opens the given path as with `fcntl::openat`. pub fn openat( - dirfd: &Fd, + dirfd: Fd, path: &P, oflag: OFlag, mode: sys::stat::Mode, diff --git a/src/fcntl.rs b/src/fcntl.rs index 0490a022e0..5a7d4eef08 100644 --- a/src/fcntl.rs +++ b/src/fcntl.rs @@ -9,6 +9,7 @@ use std::os::unix::io::FromRawFd; use std::os::unix::io::OwnedFd; use std::os::unix::io::RawFd; +use crate::IntoPathFd; #[cfg(feature = "fs")] use crate::{sys::stat::Mode, NixPath, Result}; #[cfg(any(target_os = "android", target_os = "linux"))] @@ -211,15 +212,15 @@ pub fn open( // The conversion is not identical on all operating systems. #[allow(clippy::useless_conversion)] #[cfg(not(target_os = "redox"))] -pub fn openat( - dirfd: &Fd, +pub fn openat( + dirfd: Fd, path: &P, oflag: OFlag, mode: Mode, ) -> Result { let fd = path.with_nix_path(|cstr| unsafe { libc::openat( - dirfd.as_fd().as_raw_fd(), + dirfd.as_raw_path_fd(), cstr.as_ptr(), oflag.bits(), mode.bits() as c_uint, @@ -230,18 +231,18 @@ pub fn openat( } #[cfg(not(target_os = "redox"))] -pub fn renameat( - old_dirfd: &Fd1, +pub fn renameat( + old_dirfd: Fd1, old_path: &P1, - new_dirfd: &Fd2, + new_dirfd: Fd2, new_path: &P2, ) -> Result<()> { let res = old_path.with_nix_path(|old_cstr| { new_path.with_nix_path(|new_cstr| unsafe { libc::renameat( - old_dirfd.as_fd().as_raw_fd(), + old_dirfd.as_raw_path_fd(), old_cstr.as_ptr(), - new_dirfd.as_fd().as_raw_fd(), + new_dirfd.as_raw_path_fd(), new_cstr.as_ptr(), ) }) @@ -264,19 +265,19 @@ libc_bitflags! { feature! { #![feature = "fs"] #[cfg(all(target_os = "linux", target_env = "gnu"))] -pub fn renameat2( - old_dirfd: &Fd1, +pub fn renameat2( + old_dirfd: Fd1, old_path: &P1, - new_dirfd: &Fd2, + new_dirfd: Fd2, new_path: &P2, flags: RenameFlags, ) -> Result<()> { let res = old_path.with_nix_path(|old_cstr| { new_path.with_nix_path(|new_cstr| unsafe { libc::renameat2( - old_dirfd.as_fd().as_raw_fd(), + old_dirfd.as_raw_path_fd(), old_cstr.as_ptr(), - new_dirfd.as_fd().as_raw_fd(), + new_dirfd.as_raw_path_fd(), new_cstr.as_ptr(), flags.bits(), ) @@ -404,10 +405,10 @@ pub fn readlink(path: &P) -> Result { #[cfg(not(target_os = "redox"))] pub fn readlinkat( - dirfd: &Fd, + dirfd: Fd, path: &P, ) -> Result { - inner_readlink(Some(dirfd), path) + inner_readlink(Some(&dirfd), path) } } @@ -599,9 +600,9 @@ feature! { /// returned. #[cfg(any(target_os = "android", target_os = "linux"))] pub fn copy_file_range( - fd_in: &Fd1, + fd_in: Fd1, off_in: Option<&mut libc::loff_t>, - fd_out: &Fd2, + fd_out: Fd2, off_out: Option<&mut libc::loff_t>, len: usize, ) -> Result { @@ -628,9 +629,9 @@ pub fn copy_file_range( #[cfg(any(target_os = "linux", target_os = "android"))] pub fn splice( - fd_in: &Fd1, + fd_in: Fd1, off_in: Option<&mut libc::loff_t>, - fd_out: &Fd2, + fd_out: Fd2, off_out: Option<&mut libc::loff_t>, len: usize, flags: SpliceFFlags, @@ -657,8 +658,8 @@ pub fn splice( #[cfg(any(target_os = "linux", target_os = "android"))] pub fn tee( - fd_in: &Fd1, - fd_out: &Fd2, + fd_in: Fd1, + fd_out: Fd2, len: usize, flags: SpliceFFlags, ) -> Result { @@ -675,7 +676,7 @@ pub fn tee( #[cfg(any(target_os = "linux", target_os = "android"))] pub fn vmsplice( - fd: &Fd, + fd: Fd, iov: &[std::io::IoSlice<'_>], flags: SpliceFFlags, ) -> Result { @@ -734,7 +735,7 @@ feature! { #[cfg(any(target_os = "linux"))] #[cfg(feature = "fs")] pub fn fallocate( - fd: &Fd, + fd: Fd, mode: FallocateFlags, offset: libc::off_t, len: libc::off_t, @@ -810,7 +811,7 @@ impl SpacectlRange { /// ``` #[cfg(target_os = "freebsd")] pub fn fspacectl( - fd: &Fd, + fd: Fd, range: SpacectlRange, ) -> Result { let mut rqsr = libc::spacectl_range { @@ -861,7 +862,7 @@ pub fn fspacectl( /// ``` #[cfg(target_os = "freebsd")] pub fn fspacectl_all( - fd: &Fd, + fd: Fd, offset: libc::off_t, len: libc::off_t, ) -> Result<()> { @@ -917,7 +918,7 @@ mod posix_fadvise { feature! { #![feature = "fs"] pub fn posix_fadvise( - fd: &Fd, + fd: Fd, offset: libc::off_t, len: libc::off_t, advice: PosixFadviseAdvice, @@ -950,7 +951,7 @@ mod posix_fadvise { target_os = "freebsd" ))] pub fn posix_fallocate( - fd: &Fd, + fd: Fd, offset: libc::off_t, len: libc::off_t, ) -> Result<()> { diff --git a/src/kmod.rs b/src/kmod.rs index d7146612d5..d3725c3f8a 100644 --- a/src/kmod.rs +++ b/src/kmod.rs @@ -80,7 +80,7 @@ libc_bitflags!( /// /// See [`man init_module(2)`](https://man7.org/linux/man-pages/man2/init_module.2.html) for more information. pub fn finit_module( - fd: &Fd, + fd: Fd, param_values: &CStr, flags: ModuleInitFlags, ) -> Result<()> { diff --git a/src/lib.rs b/src/lib.rs index 5b5d8ce921..432585d77c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -162,8 +162,7 @@ pub mod unistd; use std::ffi::{CStr, CString, OsStr}; use std::mem::MaybeUninit; use std::os::unix::ffi::OsStrExt; -#[cfg(not(target_os = "redox"))] -use std::os::unix::io::BorrowedFd; +use std::os::unix::io::{AsFd, AsRawFd, RawFd}; use std::path::{Path, PathBuf}; use std::{ptr, result, slice}; @@ -184,16 +183,29 @@ pub type Result = result::Result; /// ones. pub type Error = Errno; -/// A file descriptor representing the current working directory. +/// A trait that models requirements for path file descriptors. +pub trait IntoPathFd { + /// Extracts the raw file descriptor. + fn as_raw_path_fd(&self) -> RawFd; +} + +impl IntoPathFd for Fd { + fn as_raw_path_fd(&self) -> RawFd { + self.as_fd().as_raw_fd() + } +} + +/// The `AT_FDCWD` marker FD. +#[cfg(not(target_os = "redox"))] +#[derive(Copy, Clone, Debug)] +pub struct Cwd; + #[cfg(not(target_os = "redox"))] -pub const AT_FDCWD: &BorrowedFd<'static> = unsafe { - &BorrowedFd::borrow_raw(if cfg!(target_os = "haiku") { - // Hack to work around BorrowedFd not allowing -1 - -2 - } else { +impl IntoPathFd for Cwd { + fn as_raw_path_fd(&self) -> RawFd { libc::AT_FDCWD - }) -}; + } +} /// Common trait used to represent file system paths by many Nix functions. pub trait NixPath { diff --git a/src/sys/epoll.rs b/src/sys/epoll.rs index 02c25d4d04..bd25754afd 100644 --- a/src/sys/epoll.rs +++ b/src/sys/epoll.rs @@ -2,7 +2,7 @@ use crate::errno::Errno; use crate::Result; use libc::{self, c_int}; use std::mem; -use std::os::unix::io::{FromRawFd,RawFd, OwnedFd, AsFd, AsRawFd}; +use std::os::unix::io::{AsFd, AsRawFd, FromRawFd, OwnedFd, RawFd}; libc_bitflags!( pub struct EpollFlags: c_int { @@ -78,22 +78,22 @@ impl EpollEvent { /// # fn main() -> nix::Result<()> { /// const DATA: u64 = 17; /// const MILLIS: u64 = 100; -/// +/// /// // Create epoll /// let epoll = Epoll::new(EpollCreateFlags::empty())?; -/// +/// /// // Create eventfd & Add event /// let eventfd = unsafe { OwnedFd::from_raw_fd(eventfd(0, EfdFlags::empty())?) }; /// epoll.add(&eventfd, EpollEvent::new(EpollFlags::EPOLLIN,DATA))?; -/// +/// /// // Arm eventfd & Time wait -/// write(eventfd.as_raw_fd(), &1u64.to_ne_bytes())?; +/// write(&eventfd, &1u64.to_ne_bytes())?; /// let now = Instant::now(); -/// +/// /// // Wait on event /// let mut events = [EpollEvent::empty()]; /// epoll.wait(&mut events, MILLIS as isize)?; -/// +/// /// // Assert data correct & timeout didn't occur /// assert_eq!(events[0].data(), DATA); /// assert!(now.elapsed() < Duration::from_millis(MILLIS)); @@ -104,7 +104,7 @@ impl EpollEvent { pub struct Epoll(pub OwnedFd); impl Epoll { /// Creates a new epoll instance and returns a file descriptor referring to that instance. - /// + /// /// [`epoll_create1`](https://man7.org/linux/man-pages/man2/epoll_create1.2.html). pub fn new(flags: EpollCreateFlags) -> Result { let res = unsafe { libc::epoll_create1(flags.bits()) }; @@ -113,30 +113,38 @@ impl Epoll { Ok(Self(owned_fd)) } /// Add an entry to the interest list of the epoll file descriptor for - /// specified in events. - /// + /// specified in events. + /// /// [`epoll_ctl`](https://man7.org/linux/man-pages/man2/epoll_ctl.2.html) with `EPOLL_CTL_ADD`. pub fn add(&self, fd: Fd, mut event: EpollEvent) -> Result<()> { - self.epoll_ctl(EpollOp::EpollCtlAdd,fd,&mut event) + self.epoll_ctl(EpollOp::EpollCtlAdd, fd, &mut event) } /// Remove (deregister) the target file descriptor `fd` from the interest list. - /// + /// /// [`epoll_ctl`](https://man7.org/linux/man-pages/man2/epoll_ctl.2.html) with `EPOLL_CTL_DEL` . pub fn delete(&self, fd: Fd) -> Result<()> { - self.epoll_ctl(EpollOp::EpollCtlDel,fd,None) + self.epoll_ctl(EpollOp::EpollCtlDel, fd, None) } /// Change the settings associated with `fd` in the interest list to the new settings specified /// in `event`. - /// + /// /// [`epoll_ctl`](https://man7.org/linux/man-pages/man2/epoll_ctl.2.html) with `EPOLL_CTL_MOD`. - pub fn modify(&self,fd: Fd, event: &mut EpollEvent) -> Result<()> { - self.epoll_ctl(EpollOp::EpollCtlMod,fd,event) + pub fn modify( + &self, + fd: Fd, + event: &mut EpollEvent, + ) -> Result<()> { + self.epoll_ctl(EpollOp::EpollCtlMod, fd, event) } /// Waits for I/O events, blocking the calling thread if no events are currently available. /// (This can be thought of as fetching items from the ready list of the epoll instance.) - /// + /// /// [`epoll_wait`](https://man7.org/linux/man-pages/man2/epoll_wait.2.html) - pub fn wait(&self, events: &mut [EpollEvent], timeout: isize) -> Result { + pub fn wait( + &self, + events: &mut [EpollEvent], + timeout: isize, + ) -> Result { let res = unsafe { libc::epoll_wait( self.0.as_raw_fd(), @@ -145,15 +153,15 @@ impl Epoll { timeout as c_int, ) }; - + Errno::result(res).map(|r| r as usize) } /// This system call is used to add, modify, or remove entries in the interest list of the epoll /// instance referred to by `self`. It requests that the operation `op` be performed for the /// target file descriptor, `fd`. - /// + /// /// When possible prefer [`Epoll::add`], [`Epoll::delete`] and [`Epoll::modify`]. - /// + /// /// [`epoll_ctl`](https://man7.org/linux/man-pages/man2/epoll_ctl.2.html) fn epoll_ctl<'a, Fd: AsFd, T>( &self, @@ -165,9 +173,17 @@ impl Epoll { T: Into>, { let event: Option<&mut EpollEvent> = event.into(); - let ptr = event.map(|x|&mut x.event as *mut libc::epoll_event).unwrap_or(std::ptr::null_mut()); + let ptr = event + .map(|x| &mut x.event as *mut libc::epoll_event) + .unwrap_or(std::ptr::null_mut()); unsafe { - Errno::result(libc::epoll_ctl(self.0.as_raw_fd(), op as c_int, fd.as_fd().as_raw_fd(), ptr)).map(drop) + Errno::result(libc::epoll_ctl( + self.0.as_raw_fd(), + op as c_int, + fd.as_fd().as_raw_fd(), + ptr, + )) + .map(drop) } } } @@ -231,4 +247,4 @@ pub fn epoll_wait( }; Errno::result(res).map(|r| r as usize) -} \ No newline at end of file +} diff --git a/src/sys/stat.rs b/src/sys/stat.rs index f2b33a06f7..d4a227305c 100644 --- a/src/sys/stat.rs +++ b/src/sys/stat.rs @@ -12,7 +12,7 @@ pub use libc::{dev_t, mode_t}; #[cfg(not(target_os = "redox"))] use crate::fcntl::AtFlags; use crate::sys::time::{TimeSpec, TimeVal}; -use crate::{errno::Errno, NixPath, Result}; +use crate::{errno::Errno, IntoPathFd, NixPath, Result}; use std::mem; use std::os::unix::io::{AsFd, AsRawFd}; @@ -191,8 +191,8 @@ pub fn mknod( target_os = "haiku" )))] #[cfg_attr(docsrs, doc(cfg(all())))] -pub fn mknodat( - dirfd: &Fd, +pub fn mknodat( + dirfd: Fd, path: &P, kind: SFlag, perm: Mode, @@ -200,7 +200,7 @@ pub fn mknodat( ) -> Result<()> { let res = path.with_nix_path(|cstr| unsafe { libc::mknodat( - dirfd.as_fd().as_raw_fd(), + dirfd.as_raw_path_fd(), cstr.as_ptr(), kind.bits | perm.bits() as mode_t, dev, @@ -258,7 +258,7 @@ pub fn lstat(path: &P) -> Result { Ok(unsafe { dst.assume_init() }) } -pub fn fstat(fd: &Fd) -> Result { +pub fn fstat(fd: Fd) -> Result { let mut dst = mem::MaybeUninit::uninit(); let res = unsafe { libc::fstat(fd.as_fd().as_raw_fd(), dst.as_mut_ptr()) }; @@ -269,15 +269,15 @@ pub fn fstat(fd: &Fd) -> Result { #[cfg(not(target_os = "redox"))] #[cfg_attr(docsrs, doc(cfg(all())))] -pub fn fstatat( - dirfd: &Fd, +pub fn fstatat( + dirfd: Fd, pathname: &P, f: AtFlags, ) -> Result { let mut dst = mem::MaybeUninit::uninit(); let res = pathname.with_nix_path(|cstr| unsafe { libc::fstatat( - dirfd.as_fd().as_raw_fd(), + dirfd.as_raw_path_fd(), cstr.as_ptr(), dst.as_mut_ptr(), f.bits() as libc::c_int, @@ -294,7 +294,7 @@ pub fn fstatat( /// # References /// /// [fchmod(2)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/fchmod.html). -pub fn fchmod(fd: &Fd, mode: Mode) -> Result<()> { +pub fn fchmod(fd: Fd, mode: Mode) -> Result<()> { let res = unsafe { libc::fchmod(fd.as_fd().as_raw_fd(), mode.bits() as mode_t) }; @@ -326,8 +326,8 @@ pub enum FchmodatFlags { /// [fchmodat(2)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/fchmodat.html). #[cfg(not(target_os = "redox"))] #[cfg_attr(docsrs, doc(cfg(all())))] -pub fn fchmodat( - dirfd: &Fd, +pub fn fchmodat( + dirfd: Fd, path: &P, mode: Mode, flag: FchmodatFlags, @@ -338,7 +338,7 @@ pub fn fchmodat( }; let res = path.with_nix_path(|cstr| unsafe { libc::fchmodat( - dirfd.as_fd().as_raw_fd(), + dirfd.as_raw_path_fd(), cstr.as_ptr(), mode.bits() as mode_t, atflag.bits() as libc::c_int, @@ -410,7 +410,7 @@ pub fn lutimes( /// [futimens(2)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/futimens.html). #[inline] pub fn futimens( - fd: &Fd, + fd: Fd, atime: &TimeSpec, mtime: &TimeSpec, ) -> Result<()> { @@ -446,8 +446,8 @@ pub enum UtimensatFlags { /// [utimensat(2)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/utimens.html). #[cfg(not(target_os = "redox"))] #[cfg_attr(docsrs, doc(cfg(all())))] -pub fn utimensat( - dirfd: &Fd, +pub fn utimensat( + dirfd: Fd, path: &P, atime: &TimeSpec, mtime: &TimeSpec, @@ -460,7 +460,7 @@ pub fn utimensat( let times: [libc::timespec; 2] = [*atime.as_ref(), *mtime.as_ref()]; let res = path.with_nix_path(|cstr| unsafe { libc::utimensat( - dirfd.as_fd().as_raw_fd(), + dirfd.as_raw_path_fd(), cstr.as_ptr(), ×[0], atflag.bits() as libc::c_int, @@ -472,17 +472,13 @@ pub fn utimensat( #[cfg(not(target_os = "redox"))] #[cfg_attr(docsrs, doc(cfg(all())))] -pub fn mkdirat( - fd: &Fd, +pub fn mkdirat( + fd: Fd, path: &P, mode: Mode, ) -> Result<()> { let res = path.with_nix_path(|cstr| unsafe { - libc::mkdirat( - fd.as_fd().as_raw_fd(), - cstr.as_ptr(), - mode.bits() as mode_t, - ) + libc::mkdirat(fd.as_raw_path_fd(), cstr.as_ptr(), mode.bits() as mode_t) })?; Errno::result(res).map(drop) diff --git a/src/sys/statfs.rs b/src/sys/statfs.rs index 721d45cb21..5111df2e6e 100644 --- a/src/sys/statfs.rs +++ b/src/sys/statfs.rs @@ -740,7 +740,7 @@ pub fn statfs(path: &P) -> Result { /// # Arguments /// /// `fd` - File descriptor of any open file within the file system to describe -pub fn fstatfs(fd: &Fd) -> Result { +pub fn fstatfs(fd: Fd) -> Result { unsafe { let mut stat = mem::MaybeUninit::::uninit(); Errno::result(LIBC_FSTATFS(fd.as_fd().as_raw_fd(), stat.as_mut_ptr())) diff --git a/src/unistd.rs b/src/unistd.rs index 29a2cac67f..bbe57afe92 100644 --- a/src/unistd.rs +++ b/src/unistd.rs @@ -20,7 +20,7 @@ use crate::fcntl::{fcntl, FcntlArg::F_SETFD, FdFlag, OFlag}; use crate::sys::stat::FileFlag; #[cfg(feature = "fs")] use crate::sys::stat::Mode; -use crate::{Error, NixPath, Result}; +use crate::{Error, IntoPathFd, NixPath, Result}; #[cfg(not(target_os = "redox"))] use cfg_if::cfg_if; use libc::{ @@ -439,7 +439,7 @@ feature! { /// /// The two file descriptors do not share file descriptor flags (e.g. `OFlag::FD_CLOEXEC`). #[inline] -pub fn dup(oldfd: &Fd) -> Result { +pub fn dup(oldfd: Fd) -> Result { let res = unsafe { libc::dup(oldfd.as_fd().as_raw_fd()) }; Errno::result(res).map(|fd| unsafe { OwnedFd::from_raw_fd(fd) }) @@ -452,7 +452,7 @@ pub fn dup(oldfd: &Fd) -> Result { /// specified fd instead of allocating a new one. See the man pages for more /// detail on the exact behavior of this function. #[inline] -pub fn dup2(oldfd: &Fd, newfd: &mut OwnedFd) -> Result<()> { +pub fn dup2(oldfd: Fd, newfd: &mut OwnedFd) -> Result<()> { let res = unsafe { libc::dup2(oldfd.as_fd().as_raw_fd(), newfd.as_raw_fd()) }; @@ -465,7 +465,7 @@ pub fn dup2(oldfd: &Fd, newfd: &mut OwnedFd) -> Result<()> { /// This function behaves similar to `dup2()` but allows for flags to be /// specified. pub fn dup3( - oldfd: &Fd, + oldfd: Fd, newfd: &mut OwnedFd, flags: OFlag, ) -> Result<()> { @@ -474,7 +474,7 @@ pub fn dup3( #[inline] fn dup3_polyfill( - oldfd: &Fd, + oldfd: Fd, newfd: &mut OwnedFd, flags: OFlag, ) -> Result<()> { @@ -512,7 +512,7 @@ pub fn chdir(path: &P) -> Result<()> { /// pages for additional details on possible failure cases. #[inline] #[cfg(not(target_os = "fuchsia"))] -pub fn fchdir(dirfd: &Fd) -> Result<()> { +pub fn fchdir(dirfd: Fd) -> Result<()> { let res = unsafe { libc::fchdir(dirfd.as_fd().as_raw_fd()) }; Errno::result(res).map(drop) @@ -610,14 +610,14 @@ pub fn mkfifo(path: &P, mode: Mode) -> Result<()> { target_os = "android", target_os = "redox" )))] -pub fn mkfifoat( - dirfd: &Fd, +pub fn mkfifoat( + dirfd: Fd, path: &P, mode: Mode, ) -> Result<()> { let res = path.with_nix_path(|cstr| unsafe { libc::mkfifoat( - dirfd.as_fd().as_raw_fd(), + dirfd.as_raw_path_fd(), cstr.as_ptr(), mode.bits() as mode_t, ) @@ -636,16 +636,16 @@ pub fn mkfifoat( /// /// See also [symlinkat(2)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/symlinkat.html). #[cfg(not(target_os = "redox"))] -pub fn symlinkat( +pub fn symlinkat( path1: &P1, - dirfd: &Fd, + dirfd: Fd, path2: &P2, ) -> Result<()> { let res = path1.with_nix_path(|path1| { path2.with_nix_path(|path2| unsafe { libc::symlinkat( path1.as_ptr(), - dirfd.as_fd().as_raw_fd(), + dirfd.as_raw_path_fd(), path2.as_ptr(), ) }) @@ -770,7 +770,7 @@ pub fn chown( /// only if `Some` owner/group is provided. #[inline] pub fn fchown( - fd: &Fd, + fd: Fd, owner: Option, group: Option, ) -> Result<()> { @@ -808,8 +808,8 @@ pub enum FchownatFlags { /// /// [fchownat(2)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/fchownat.html). #[cfg(not(target_os = "redox"))] -pub fn fchownat( - dirfd: &Fd, +pub fn fchownat( + dirfd: Fd, path: &P, owner: Option, group: Option, @@ -822,7 +822,7 @@ pub fn fchownat( let res = path.with_nix_path(|cstr| unsafe { let (uid, gid) = chown_raw_ids(owner, group); libc::fchownat( - dirfd.as_fd().as_raw_fd(), + dirfd.as_raw_path_fd(), cstr.as_ptr(), uid, gid, @@ -947,7 +947,7 @@ pub fn execvpe, SE: AsRef>( ))] #[inline] pub fn fexecve, SE: AsRef>( - fd: &Fd, + fd: Fd, args: &[SA], env: &[SE], ) -> Result { @@ -971,8 +971,8 @@ pub fn fexecve, SE: AsRef>( /// is referenced as a file descriptor to the base directory plus a path. #[cfg(any(target_os = "android", target_os = "linux"))] #[inline] -pub fn execveat, SE: AsRef>( - dirfd: &Fd, +pub fn execveat, SE: AsRef>( + dirfd: Fd, pathname: &CStr, args: &[SA], env: &[SE], @@ -984,7 +984,7 @@ pub fn execveat, SE: AsRef>( unsafe { libc::syscall( libc::SYS_execveat, - dirfd.as_fd().as_raw_fd(), + dirfd.as_raw_path_fd(), pathname.as_ptr(), args_p.as_ptr(), env_p.as_ptr(), @@ -1125,7 +1125,7 @@ pub fn close(fd: Fd) -> Result<()> { /// Read from a raw file descriptor. /// /// See also [read(2)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/read.html) -pub fn read(fd: &Fd, buf: &mut [u8]) -> Result { +pub fn read(fd: Fd, buf: &mut [u8]) -> Result { let res = unsafe { libc::read( fd.as_fd().as_raw_fd(), @@ -1140,7 +1140,7 @@ pub fn read(fd: &Fd, buf: &mut [u8]) -> Result { /// Write to a raw file descriptor. /// /// See also [write(2)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/write.html) -pub fn write(fd: &Fd, buf: &[u8]) -> Result { +pub fn write(fd: Fd, buf: &[u8]) -> Result { let res = unsafe { libc::write( fd.as_fd().as_raw_fd(), @@ -1197,7 +1197,7 @@ pub enum Whence { /// Move the read/write file offset. /// /// See also [lseek(2)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/lseek.html) -pub fn lseek(fd: &Fd, offset: off_t, whence: Whence) -> Result { +pub fn lseek(fd: Fd, offset: off_t, whence: Whence) -> Result { let res = unsafe { libc::lseek(fd.as_fd().as_raw_fd(), offset, whence as i32) }; @@ -1206,7 +1206,7 @@ pub fn lseek(fd: &Fd, offset: off_t, whence: Whence) -> Result #[cfg(any(target_os = "linux", target_os = "android"))] pub fn lseek64( - fd: &Fd, + fd: Fd, offset: libc::off64_t, whence: Whence, ) -> Result { @@ -1296,12 +1296,12 @@ pub fn truncate(path: &P, len: off_t) -> Result<()> { /// /// See also /// [ftruncate(2)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/ftruncate.html) -pub fn ftruncate(fd: &Fd, len: off_t) -> Result<()> { +pub fn ftruncate(fd: Fd, len: off_t) -> Result<()> { Errno::result(unsafe { libc::ftruncate(fd.as_fd().as_raw_fd(), len) }) .map(drop) } -pub fn isatty(fd: &Fd) -> Result { +pub fn isatty(fd: Fd) -> Result { unsafe { // ENOTTY means `fd` is a valid file descriptor, but not a TTY, so // we return `Ok(false)` @@ -1337,10 +1337,10 @@ pub enum LinkatFlags { /// # References /// See also [linkat(2)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/linkat.html) #[cfg(not(target_os = "redox"))] // RedoxFS does not support symlinks yet -pub fn linkat( - olddirfd: &Fd1, +pub fn linkat( + olddirfd: Fd1, oldpath: &P1, - newdirfd: &Fd2, + newdirfd: Fd2, newpath: &P2, flag: LinkatFlags, ) -> Result<()> { @@ -1352,9 +1352,9 @@ pub fn linkat( let res = oldpath.with_nix_path(|oldcstr| { newpath.with_nix_path(|newcstr| unsafe { libc::linkat( - olddirfd.as_fd().as_raw_fd(), + olddirfd.as_raw_path_fd(), oldcstr.as_ptr(), - newdirfd.as_fd().as_raw_fd(), + newdirfd.as_raw_path_fd(), newcstr.as_ptr(), atflag.bits() as c_int, ) @@ -1390,8 +1390,8 @@ pub enum UnlinkatFlags { /// # References /// See also [unlinkat(2)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/unlinkat.html) #[cfg(not(target_os = "redox"))] -pub fn unlinkat( - dirfd: &Fd, +pub fn unlinkat( + dirfd: Fd, path: &P, flag: UnlinkatFlags, ) -> Result<()> { @@ -1401,7 +1401,7 @@ pub fn unlinkat( }; let res = path.with_nix_path(|cstr| unsafe { libc::unlinkat( - dirfd.as_fd().as_raw_fd(), + dirfd.as_raw_path_fd(), cstr.as_ptr(), atflag.bits() as c_int, ) @@ -1437,7 +1437,7 @@ pub fn sync() { /// /// See also [syncfs(2)](https://man7.org/linux/man-pages/man2/sync.2.html) #[cfg(target_os = "linux")] -pub fn syncfs(fd: &Fd) -> Result<()> { +pub fn syncfs(fd: Fd) -> Result<()> { let res = unsafe { libc::syncfs(fd.as_fd().as_raw_fd()) }; Errno::result(res).map(drop) @@ -1447,7 +1447,7 @@ pub fn syncfs(fd: &Fd) -> Result<()> { /// /// See also [fsync(2)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/fsync.html) #[inline] -pub fn fsync(fd: &Fd) -> Result<()> { +pub fn fsync(fd: Fd) -> Result<()> { let res = unsafe { libc::fsync(fd.as_fd().as_raw_fd()) }; Errno::result(res).map(drop) @@ -1469,7 +1469,7 @@ pub fn fsync(fd: &Fd) -> Result<()> { target_os = "solaris" ))] #[inline] -pub fn fdatasync(fd: &Fd) -> Result<()> { +pub fn fdatasync(fd: Fd) -> Result<()> { let res = unsafe { libc::fdatasync(fd.as_fd().as_raw_fd()) }; Errno::result(res).map(drop) @@ -3002,15 +3002,15 @@ pub fn access(path: &P, amode: AccessFlags) -> Result<()> { /// [faccessat(2)](http://pubs.opengroup.org/onlinepubs/9699919799/functions/faccessat.html) // redox: does not appear to support the *at family of syscalls. #[cfg(not(target_os = "redox"))] -pub fn faccessat( - dirfd: &Fd, +pub fn faccessat( + dirfd: Fd, path: &P, mode: AccessFlags, flags: AtFlags, ) -> Result<()> { let res = path.with_nix_path(|cstr| unsafe { libc::faccessat( - dirfd.as_fd().as_raw_fd(), + dirfd.as_raw_path_fd(), cstr.as_ptr(), mode.bits(), flags.bits(), diff --git a/test/test_fcntl.rs b/test/test_fcntl.rs index 354c50ec35..5b9c933772 100644 --- a/test/test_fcntl.rs +++ b/test/test_fcntl.rs @@ -340,7 +340,7 @@ mod linux_android { let tmp = NamedTempFile::new().unwrap(); - let statfs = nix::sys::statfs::fstatfs(&tmp).unwrap(); + let statfs = nix::sys::statfs::fstatfs(tmp.as_file()).unwrap(); if statfs.filesystem_type() == nix::sys::statfs::OVERLAYFS_SUPER_MAGIC { // OverlayFS is a union file system. It returns one inode value in // stat(2), but a different one shows up in /proc/locks. So we must @@ -380,7 +380,7 @@ mod linux_android { let tmp = NamedTempFile::new().unwrap(); - let statfs = nix::sys::statfs::fstatfs(&tmp).unwrap(); + let statfs = nix::sys::statfs::fstatfs(tmp.as_file()).unwrap(); if statfs.filesystem_type() == nix::sys::statfs::OVERLAYFS_SUPER_MAGIC { // OverlayFS is a union file system. It returns one inode value in // stat(2), but a different one shows up in /proc/locks. So we must diff --git a/test/test_stat.rs b/test/test_stat.rs index c30778f98f..574aed2a5f 100644 --- a/test/test_stat.rs +++ b/test/test_stat.rs @@ -14,7 +14,7 @@ use libc::mode_t; #[cfg(not(any(target_os = "netbsd", target_os = "redox")))] use libc::{S_IFLNK, S_IFMT}; #[cfg(not(target_os = "redox"))] -use nix::AT_FDCWD; +use nix::Cwd; #[cfg(not(target_os = "redox"))] use nix::errno::Errno; @@ -194,7 +194,7 @@ fn test_fchmodat() { let mut mode2 = Mode::empty(); mode2.insert(Mode::S_IROTH); - fchmodat(AT_FDCWD, filename, mode2, FchmodatFlags::FollowSymlink).unwrap(); + fchmodat(Cwd, filename, mode2, FchmodatFlags::FollowSymlink).unwrap(); let file_stat2 = stat(&fullpath).unwrap(); assert_eq!(file_stat2.st_mode as mode_t & 0o7777, mode2.bits()); @@ -305,7 +305,7 @@ fn test_utimensat() { chdir(tempdir.path()).unwrap(); utimensat( - AT_FDCWD, + Cwd, filename, &TimeSpec::seconds(500), &TimeSpec::seconds(800), diff --git a/test/test_unistd.rs b/test/test_unistd.rs index 006824bceb..cd21c26953 100644 --- a/test/test_unistd.rs +++ b/test/test_unistd.rs @@ -20,7 +20,7 @@ use nix::sys::wait::*; use nix::unistd::ForkResult::*; use nix::unistd::*; #[cfg(not(target_os = "redox"))] -use nix::AT_FDCWD; +use nix::Cwd; use std::env; #[cfg(not(any(target_os = "fuchsia", target_os = "redox")))] use std::ffi::CString; @@ -140,7 +140,7 @@ fn test_mkfifoat_none() { let tempdir = tempdir().unwrap(); let mkfifoat_fifo = tempdir.path().join("mkfifoat_fifo"); - mkfifoat(AT_FDCWD, &mkfifoat_fifo, Mode::S_IRUSR).unwrap(); + mkfifoat(Cwd, &mkfifoat_fifo, Mode::S_IRUSR).unwrap(); let stats = stat::stat(&mkfifoat_fifo).unwrap(); let typ = SFlag::from_bits_truncate(stats.st_mode); @@ -181,7 +181,7 @@ fn test_mkfifoat_directory_none() { let _m = crate::CWD_LOCK.read(); // mkfifoat should fail if a directory is given - mkfifoat(AT_FDCWD, &env::temp_dir(), Mode::S_IRUSR) + mkfifoat(Cwd, &env::temp_dir(), Mode::S_IRUSR) .expect_err("assertion failed"); } @@ -545,11 +545,10 @@ fn test_fchownat() { fchownat(&dirfd, "file", uid, gid, FchownatFlags::FollowSymlink).unwrap(); chdir(tempdir.path()).unwrap(); - fchownat(AT_FDCWD, "file", uid, gid, FchownatFlags::FollowSymlink).unwrap(); + fchownat(Cwd, "file", uid, gid, FchownatFlags::FollowSymlink).unwrap(); fs::remove_file(&path).unwrap(); - fchownat(AT_FDCWD, "file", uid, gid, FchownatFlags::FollowSymlink) - .unwrap_err(); + fchownat(Cwd, "file", uid, gid, FchownatFlags::FollowSymlink).unwrap_err(); } #[test] @@ -862,7 +861,7 @@ fn test_symlinkat() { let target = tempdir.path().join("a"); let linkpath = tempdir.path().join("b"); - symlinkat(&target, AT_FDCWD, &linkpath).unwrap(); + symlinkat(&target, Cwd, &linkpath).unwrap(); assert_eq!( readlink(&linkpath).unwrap().to_str().unwrap(), target.to_str().unwrap() @@ -932,7 +931,7 @@ fn test_linkat_olddirfd_none() { // Attempt hard link file using curent working directory as relative path for old file path chdir(tempdir_oldfile.path()).unwrap(); linkat( - AT_FDCWD, + Cwd, oldfilename, &dirfd, newfilename, @@ -967,7 +966,7 @@ fn test_linkat_newdirfd_none() { linkat( &dirfd, oldfilename, - AT_FDCWD, + Cwd, newfilename, LinkatFlags::SymlinkFollow, ) @@ -999,7 +998,7 @@ fn test_linkat_no_follow_symlink() { File::create(&oldfilepath).unwrap(); // Create symlink to file - symlinkat(&oldfilepath, AT_FDCWD, &symoldfilepath).unwrap(); + symlinkat(&oldfilepath, Cwd, &symoldfilepath).unwrap(); // Get file descriptor for base directory let dirfd = open(tempdir.path(), OFlag::empty(), Mode::empty()).unwrap(); @@ -1040,7 +1039,7 @@ fn test_linkat_follow_symlink() { File::create(&oldfilepath).unwrap(); // Create symlink to file - symlinkat(&oldfilepath, AT_FDCWD, &symoldfilepath).unwrap(); + symlinkat(&oldfilepath, Cwd, &symoldfilepath).unwrap(); // Get file descriptor for base directory let dirfd = open(tempdir.path(), OFlag::empty(), Mode::empty()).unwrap(); @@ -1285,7 +1284,7 @@ fn test_faccessat_none_not_existing() { let tempdir = tempdir().unwrap(); let dir = tempdir.path().join("does_not_exist.txt"); assert_eq!( - faccessat(AT_FDCWD, &dir, AccessFlags::F_OK, AtFlags::empty()) + faccessat(Cwd, &dir, AccessFlags::F_OK, AtFlags::empty()) .err() .unwrap(), Errno::ENOENT @@ -1315,7 +1314,7 @@ fn test_faccessat_none_file_exists() { let path = tempdir.path().join("does_exist.txt"); let _file = File::create(path.clone()).unwrap(); assert!(faccessat( - AT_FDCWD, + Cwd, &path, AccessFlags::R_OK | AccessFlags::W_OK, AtFlags::empty(),