diff options
Diffstat (limited to 'src/fcntl.rs')
-rw-r--r-- | src/fcntl.rs | 126 |
1 files changed, 91 insertions, 35 deletions
diff --git a/src/fcntl.rs b/src/fcntl.rs index 65bbd4ca..1e24f603 100644 --- a/src/fcntl.rs +++ b/src/fcntl.rs @@ -193,9 +193,13 @@ feature! { // The conversion is not identical on all operating systems. #[allow(clippy::useless_conversion)] -pub fn open<P: ?Sized + NixPath>(path: &P, oflag: OFlag, mode: Mode) -> Result<RawFd> { - let fd = path.with_nix_path(|cstr| { - unsafe { libc::open(cstr.as_ptr(), oflag.bits(), mode.bits() as c_uint) } +pub fn open<P: ?Sized + NixPath>( + path: &P, + oflag: OFlag, + mode: Mode, +) -> Result<RawFd> { + let fd = path.with_nix_path(|cstr| unsafe { + libc::open(cstr.as_ptr(), oflag.bits(), mode.bits() as c_uint) })?; Errno::result(fd) @@ -210,8 +214,8 @@ pub fn openat<P: ?Sized + NixPath>( oflag: OFlag, mode: Mode, ) -> Result<RawFd> { - let fd = path.with_nix_path(|cstr| { - unsafe { libc::openat(dirfd, cstr.as_ptr(), oflag.bits(), mode.bits() as c_uint) } + let fd = path.with_nix_path(|cstr| unsafe { + libc::openat(dirfd, cstr.as_ptr(), oflag.bits(), mode.bits() as c_uint) })?; Errno::result(fd) } @@ -303,7 +307,10 @@ fn readlink_maybe_at<P: ?Sized + NixPath>( }) } -fn inner_readlink<P: ?Sized + NixPath>(dirfd: Option<RawFd>, path: &P) -> Result<OsString> { +fn inner_readlink<P: ?Sized + NixPath>( + dirfd: Option<RawFd>, + path: &P, +) -> Result<OsString> { let mut v = Vec::with_capacity(libc::PATH_MAX as usize); { @@ -387,7 +394,10 @@ pub fn readlink<P: ?Sized + NixPath>(path: &P) -> Result<OsString> { } #[cfg(not(target_os = "redox"))] -pub fn readlinkat<P: ?Sized + NixPath>(dirfd: RawFd, path: &P) -> Result<OsString> { +pub fn readlinkat<P: ?Sized + NixPath>( + dirfd: RawFd, + path: &P, +) -> Result<OsString> { inner_readlink(Some(dirfd), path) } @@ -450,9 +460,17 @@ pub enum FcntlArg<'a> { F_OFD_SETLKW(&'a libc::flock), #[cfg(any(target_os = "linux", target_os = "android"))] F_OFD_GETLK(&'a mut libc::flock), - #[cfg(any(target_os = "android", target_os = "linux", target_os = "freebsd"))] + #[cfg(any( + target_os = "android", + target_os = "linux", + target_os = "freebsd" + ))] F_ADD_SEALS(SealFlag), - #[cfg(any(target_os = "android", target_os = "linux", target_os = "freebsd"))] + #[cfg(any( + target_os = "android", + target_os = "linux", + target_os = "freebsd" + ))] F_GET_SEALS, #[cfg(any(target_os = "macos", target_os = "ios"))] F_FULLFSYNC, @@ -481,7 +499,9 @@ pub fn fcntl(fd: RawFd, arg: FcntlArg) -> Result<c_int> { let res = unsafe { match arg { F_DUPFD(rawfd) => libc::fcntl(fd, libc::F_DUPFD, rawfd), - F_DUPFD_CLOEXEC(rawfd) => libc::fcntl(fd, libc::F_DUPFD_CLOEXEC, rawfd), + F_DUPFD_CLOEXEC(rawfd) => { + libc::fcntl(fd, libc::F_DUPFD_CLOEXEC, rawfd) + } F_GETFD => libc::fcntl(fd, libc::F_GETFD), F_SETFD(flag) => libc::fcntl(fd, libc::F_SETFD, flag.bits()), F_GETFL => libc::fcntl(fd, libc::F_GETFL), @@ -498,9 +518,19 @@ pub fn fcntl(fd: RawFd, arg: FcntlArg) -> Result<c_int> { F_OFD_SETLKW(flock) => libc::fcntl(fd, libc::F_OFD_SETLKW, flock), #[cfg(any(target_os = "android", target_os = "linux"))] F_OFD_GETLK(flock) => libc::fcntl(fd, libc::F_OFD_GETLK, flock), - #[cfg(any(target_os = "android", target_os = "linux", target_os = "freebsd"))] - F_ADD_SEALS(flag) => libc::fcntl(fd, libc::F_ADD_SEALS, flag.bits()), - #[cfg(any(target_os = "android", target_os = "linux", target_os = "freebsd"))] + #[cfg(any( + target_os = "android", + target_os = "linux", + target_os = "freebsd" + ))] + F_ADD_SEALS(flag) => { + libc::fcntl(fd, libc::F_ADD_SEALS, flag.bits()) + } + #[cfg(any( + target_os = "android", + target_os = "linux", + target_os = "freebsd" + ))] F_GET_SEALS => libc::fcntl(fd, libc::F_GET_SEALS), #[cfg(any(target_os = "macos", target_os = "ios"))] F_FULLFSYNC => libc::fcntl(fd, libc::F_FULLFSYNC), @@ -535,8 +565,12 @@ pub fn flock(fd: RawFd, arg: FlockArg) -> Result<()> { LockShared => libc::flock(fd, libc::LOCK_SH), LockExclusive => libc::flock(fd, libc::LOCK_EX), Unlock => libc::flock(fd, libc::LOCK_UN), - LockSharedNonblock => libc::flock(fd, libc::LOCK_SH | libc::LOCK_NB), - LockExclusiveNonblock => libc::flock(fd, libc::LOCK_EX | libc::LOCK_NB), + LockSharedNonblock => { + libc::flock(fd, libc::LOCK_SH | libc::LOCK_NB) + } + LockExclusiveNonblock => { + libc::flock(fd, libc::LOCK_EX | libc::LOCK_NB) + } UnlockNonblock => libc::flock(fd, libc::LOCK_UN | libc::LOCK_NB), } }; @@ -632,12 +666,19 @@ 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) } #[cfg(any(target_os = "linux", target_os = "android"))] -pub fn tee(fd_in: RawFd, fd_out: RawFd, len: usize, flags: SpliceFFlags) -> Result<usize> { +pub fn tee( + fd_in: RawFd, + fd_out: RawFd, + len: usize, + flags: SpliceFFlags, +) -> Result<usize> { let ret = unsafe { libc::tee(fd_in, fd_out, len, flags.bits()) }; Errno::result(ret).map(|r| r as usize) } @@ -646,9 +687,8 @@ pub fn tee(fd_in: RawFd, fd_out: RawFd, len: usize, flags: SpliceFFlags) -> Resu pub fn vmsplice( fd: RawFd, iov: &[std::io::IoSlice<'_>], - flags: SpliceFFlags - ) -> Result<usize> -{ + flags: SpliceFFlags, +) -> Result<usize> { let ret = unsafe { libc::vmsplice( fd, @@ -778,14 +818,19 @@ impl SpacectlRange { /// ``` #[cfg(target_os = "freebsd")] pub fn fspacectl(fd: RawFd, range: SpacectlRange) -> Result<SpacectlRange> { - let mut rqsr = libc::spacectl_range{r_offset: range.0, r_len: range.1}; - let res = unsafe { libc::fspacectl( + let mut rqsr = libc::spacectl_range { + r_offset: range.0, + r_len: range.1, + }; + let res = unsafe { + libc::fspacectl( fd, libc::SPACECTL_DEALLOC, // Only one command is supported ATM &rqsr, - 0, // No flags are currently supported - &mut rqsr - )}; + 0, // No flags are currently supported + &mut rqsr, + ) + }; Errno::result(res).map(|_| SpacectlRange(rqsr.r_offset, rqsr.r_len)) } @@ -820,18 +865,25 @@ pub fn fspacectl(fd: RawFd, range: SpacectlRange) -> Result<SpacectlRange> { /// assert_eq!(buf, b"012\0\0\0\0\0\09abcdef"); /// ``` #[cfg(target_os = "freebsd")] -pub fn fspacectl_all(fd: RawFd, offset: libc::off_t, len: libc::off_t) - -> Result<()> -{ - let mut rqsr = libc::spacectl_range{r_offset: offset, r_len: len}; +pub fn fspacectl_all( + fd: RawFd, + offset: libc::off_t, + len: libc::off_t, +) -> Result<()> { + let mut rqsr = libc::spacectl_range { + r_offset: offset, + r_len: len, + }; while rqsr.r_len > 0 { - let res = unsafe { libc::fspacectl( + let res = unsafe { + libc::fspacectl( fd, libc::SPACECTL_DEALLOC, // Only one command is supported ATM &rqsr, - 0, // No flags are currently supported - &mut rqsr - )}; + 0, // No flags are currently supported + &mut rqsr, + ) + }; Errno::result(res)?; } Ok(()) @@ -848,8 +900,8 @@ pub fn fspacectl_all(fd: RawFd, offset: libc::off_t, len: libc::off_t) ))] mod posix_fadvise { use crate::errno::Errno; - use std::os::unix::io::RawFd; use crate::Result; + use std::os::unix::io::RawFd; #[cfg(feature = "fs")] libc_enum! { @@ -894,7 +946,11 @@ mod posix_fadvise { target_os = "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), |