summaryrefslogtreecommitdiff
path: root/src/unistd.rs
diff options
context:
space:
mode:
authorAlex Saveau <saveau.alexandre@gmail.com>2022-12-04 11:44:33 -0800
committerAlex Saveau <saveau.alexandre@gmail.com>2022-12-04 11:54:02 -0800
commit59f66d128b6664ff618e12c193ec2adc4a862f2f (patch)
tree177a6716eebf17604bfa2852e354b2649842a375 /src/unistd.rs
parentbedf83bf0b469cc82d10c82a62dd3726655ce3e0 (diff)
downloadnix-59f66d128b6664ff618e12c193ec2adc4a862f2f.zip
Move some pure formatting changes out of #1863
Signed-off-by: Alex Saveau <saveau.alexandre@gmail.com>
Diffstat (limited to 'src/unistd.rs')
-rw-r--r--src/unistd.rs230
1 files changed, 125 insertions, 105 deletions
diff --git a/src/unistd.rs b/src/unistd.rs
index ca07b34a..1bfeb7b9 100644
--- a/src/unistd.rs
+++ b/src/unistd.rs
@@ -585,8 +585,12 @@ pub fn mkfifo<P: ?Sized + NixPath>(path: &P, mode: Mode) -> Result<()> {
// mkfifoat is not implemented in OSX or android
#[inline]
#[cfg(not(any(
- target_os = "macos", target_os = "ios", target_os = "haiku",
- target_os = "android", target_os = "redox")))]
+ target_os = "macos",
+ target_os = "ios",
+ target_os = "haiku",
+ target_os = "android",
+ target_os = "redox"
+)))]
pub fn mkfifoat<P: ?Sized + NixPath>(dirfd: Option<RawFd>, path: &P, mode: Mode) -> Result<()> {
let res = path.with_nix_path(|cstr| unsafe {
libc::mkfifoat(at_rawfd(dirfd), cstr.as_ptr(), mode.bits() as mode_t)
@@ -695,13 +699,18 @@ feature! {
/// Computes the raw UID and GID values to pass to a `*chown` call.
// The cast is not unnecessary on all platforms.
#[allow(clippy::unnecessary_cast)]
-fn chown_raw_ids(owner: Option<Uid>, group: Option<Gid>) -> (libc::uid_t, libc::gid_t) {
+fn chown_raw_ids(
+ owner: Option<Uid>,
+ group: Option<Gid>,
+) -> (uid_t, gid_t) {
// According to the POSIX specification, -1 is used to indicate that owner and group
// are not to be changed. Since uid_t and gid_t are unsigned types, we have to wrap
// around to get -1.
- let uid = owner.map(Into::into)
+ let uid = owner
+ .map(Into::into)
.unwrap_or_else(|| (0 as uid_t).wrapping_sub(1));
- let gid = group.map(Into::into)
+ let gid = group
+ .map(Into::into)
.unwrap_or_else(|| (0 as gid_t).wrapping_sub(1));
(uid, gid)
}
@@ -714,7 +723,11 @@ fn chown_raw_ids(owner: Option<Uid>, group: Option<Gid>) -> (libc::uid_t, libc::
/// provided for that argument. Ownership change will be attempted for the path
/// only if `Some` owner/group is provided.
#[inline]
-pub fn chown<P: ?Sized + NixPath>(path: &P, owner: Option<Uid>, group: Option<Gid>) -> Result<()> {
+pub fn chown<P: ?Sized + NixPath>(
+ path: &P,
+ owner: Option<Uid>,
+ group: Option<Gid>,
+) -> Result<()> {
let res = path.with_nix_path(|cstr| {
let (uid, gid) = chown_raw_ids(owner, group);
unsafe { libc::chown(cstr.as_ptr(), uid, gid) }
@@ -773,11 +786,10 @@ pub fn fchownat<P: ?Sized + NixPath>(
group: Option<Gid>,
flag: FchownatFlags,
) -> Result<()> {
- let atflag =
- match flag {
- FchownatFlags::FollowSymlink => AtFlags::empty(),
- FchownatFlags::NoFollowSymlink => AtFlags::AT_SYMLINK_NOFOLLOW,
- };
+ let atflag = match flag {
+ FchownatFlags::FollowSymlink => AtFlags::empty(),
+ FchownatFlags::NoFollowSymlink => AtFlags::AT_SYMLINK_NOFOLLOW,
+ };
let res = path.with_nix_path(|cstr| unsafe {
let (uid, gid) = chown_raw_ids(owner, group);
libc::fchownat(at_rawfd(dirfd), cstr.as_ptr(), uid, gid,
@@ -808,14 +820,11 @@ fn to_exec_array<S: AsRef<CStr>>(args: &[S]) -> Vec<*const c_char> {
pub fn execv<S: AsRef<CStr>>(path: &CStr, argv: &[S]) -> Result<Infallible> {
let args_p = to_exec_array(argv);
- unsafe {
- libc::execv(path.as_ptr(), args_p.as_ptr())
- };
+ unsafe { libc::execv(path.as_ptr(), args_p.as_ptr()) };
Err(Errno::last())
}
-
/// Replace the current process image with a new one (see
/// [execve(2)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/exec.html)).
///
@@ -829,13 +838,15 @@ pub fn execv<S: AsRef<CStr>>(path: &CStr, argv: &[S]) -> Result<Infallible> {
/// in the `args` list is an argument to the new process. Each element in the
/// `env` list should be a string in the form "key=value".
#[inline]
-pub fn execve<SA: AsRef<CStr>, SE: AsRef<CStr>>(path: &CStr, args: &[SA], env: &[SE]) -> Result<Infallible> {
+pub fn execve<SA: AsRef<CStr>, SE: AsRef<CStr>>(
+ path: &CStr,
+ args: &[SA],
+ env: &[SE],
+) -> Result<Infallible> {
let args_p = to_exec_array(args);
let env_p = to_exec_array(env);
- unsafe {
- libc::execve(path.as_ptr(), args_p.as_ptr(), env_p.as_ptr())
- };
+ unsafe { libc::execve(path.as_ptr(), args_p.as_ptr(), env_p.as_ptr()) };
Err(Errno::last())
}
@@ -850,12 +861,13 @@ pub fn execve<SA: AsRef<CStr>, SE: AsRef<CStr>>(path: &CStr, args: &[SA], env: &
/// would not work if "bash" was specified for the path argument, but `execvp`
/// would assuming that a bash executable was on the system `PATH`.
#[inline]
-pub fn execvp<S: AsRef<CStr>>(filename: &CStr, args: &[S]) -> Result<Infallible> {
+pub fn execvp<S: AsRef<CStr>>(
+ filename: &CStr,
+ args: &[S],
+) -> Result<Infallible> {
let args_p = to_exec_array(args);
- unsafe {
- libc::execvp(filename.as_ptr(), args_p.as_ptr())
- };
+ unsafe { libc::execvp(filename.as_ptr(), args_p.as_ptr()) };
Err(Errno::last())
}
@@ -867,10 +879,12 @@ pub fn execvp<S: AsRef<CStr>>(filename: &CStr, args: &[S]) -> Result<Infallible>
/// This functions like a combination of `execvp(2)` and `execve(2)` to pass an
/// environment and have a search path. See these two for additional
/// information.
-#[cfg(any(target_os = "haiku",
- target_os = "linux",
- target_os = "openbsd"))]
-pub fn execvpe<SA: AsRef<CStr>, SE: AsRef<CStr>>(filename: &CStr, args: &[SA], env: &[SE]) -> Result<Infallible> {
+#[cfg(any(target_os = "haiku", target_os = "linux", target_os = "openbsd"))]
+pub fn execvpe<SA: AsRef<CStr>, SE: AsRef<CStr>>(
+ filename: &CStr,
+ args: &[SA],
+ env: &[SE],
+) -> Result<Infallible> {
let args_p = to_exec_array(args);
let env_p = to_exec_array(env);
@@ -891,10 +905,12 @@ pub fn execvpe<SA: AsRef<CStr>, SE: AsRef<CStr>>(filename: &CStr, args: &[SA], e
///
/// This function is similar to `execve`, except that the program to be executed
/// is referenced as a file descriptor instead of a path.
-#[cfg(any(target_os = "android",
- target_os = "linux",
- target_os = "dragonfly",
- target_os = "freebsd"))]
+#[cfg(any(
+ target_os = "android",
+ target_os = "linux",
+ target_os = "dragonfly",
+ target_os = "freebsd"
+))]
#[inline]
pub fn fexecve<SA: AsRef<CStr> ,SE: AsRef<CStr>>(fd: RawFd, args: &[SA], env: &[SE]) -> Result<Infallible> {
let args_p = to_exec_array(args);
@@ -957,14 +973,16 @@ pub fn execveat<SA: AsRef<CStr>,SE: AsRef<CStr>>(dirfd: RawFd, pathname: &CStr,
/// descriptors will remain identical after daemonizing.
/// * `noclose = false`: The process' stdin, stdout, and stderr will point to
/// `/dev/null` after daemonizing.
-#[cfg(any(target_os = "android",
- target_os = "dragonfly",
- target_os = "freebsd",
- target_os = "illumos",
- target_os = "linux",
- target_os = "netbsd",
- target_os = "openbsd",
- target_os = "solaris"))]
+#[cfg(any(
+ target_os = "android",
+ target_os = "dragonfly",
+ target_os = "freebsd",
+ target_os = "illumos",
+ target_os = "linux",
+ target_os = "netbsd",
+ target_os = "openbsd",
+ target_os = "solaris"
+))]
pub fn daemon(nochdir: bool, noclose: bool) -> Result<()> {
let res = unsafe { libc::daemon(nochdir as c_int, noclose as c_int) };
Errno::result(res).map(drop)
@@ -1106,23 +1124,27 @@ pub enum Whence {
/// Specify an offset relative to the next location in the file greater than or
/// equal to offset that contains some data. If offset points to
/// some data, then the file offset is set to offset.
- #[cfg(any(target_os = "dragonfly",
- target_os = "freebsd",
- target_os = "illumos",
- target_os = "linux",
- target_os = "solaris"))]
+ #[cfg(any(
+ target_os = "dragonfly",
+ target_os = "freebsd",
+ target_os = "illumos",
+ target_os = "linux",
+ target_os = "solaris"
+ ))]
SeekData = libc::SEEK_DATA,
/// Specify an offset relative to the next hole in the file greater than
/// or equal to offset. If offset points into the middle of a hole, then
/// the file offset should be set to offset. If there is no hole past offset,
/// then the file offset should be adjusted to the end of the file (i.e., there
/// is an implicit hole at the end of any file).
- #[cfg(any(target_os = "dragonfly",
- target_os = "freebsd",
- target_os = "illumos",
- target_os = "linux",
- target_os = "solaris"))]
- SeekHole = libc::SEEK_HOLE
+ #[cfg(any(
+ target_os = "dragonfly",
+ target_os = "freebsd",
+ target_os = "illumos",
+ target_os = "linux",
+ target_os = "solaris"
+ ))]
+ SeekHole = libc::SEEK_HOLE,
}
/// Move the read/write file offset.
@@ -1163,21 +1185,29 @@ feature! {
/// created:
///
/// - `O_CLOEXEC`: Set the close-on-exec flag for the new file descriptors.
-#[cfg_attr(target_os = "linux", doc = "- `O_DIRECT`: Create a pipe that performs I/O in \"packet\" mode.")]
-#[cfg_attr(target_os = "netbsd", doc = "- `O_NOSIGPIPE`: Return `EPIPE` instead of raising `SIGPIPE`.")]
+#[cfg_attr(
+ target_os = "linux",
+ doc = "- `O_DIRECT`: Create a pipe that performs I/O in \"packet\" mode."
+)]
+#[cfg_attr(
+ target_os = "netbsd",
+ doc = "- `O_NOSIGPIPE`: Return `EPIPE` instead of raising `SIGPIPE`."
+)]
/// - `O_NONBLOCK`: Set the non-blocking flag for the ends of the pipe.
///
/// See also [pipe(2)](https://man7.org/linux/man-pages/man2/pipe.2.html)
-#[cfg(any(target_os = "android",
- target_os = "dragonfly",
- target_os = "emscripten",
- target_os = "freebsd",
- target_os = "illumos",
- target_os = "linux",
- target_os = "redox",
- target_os = "netbsd",
- target_os = "openbsd",
- target_os = "solaris"))]
+#[cfg(any(
+ target_os = "android",
+ target_os = "dragonfly",
+ target_os = "emscripten",
+ target_os = "freebsd",
+ target_os = "illumos",
+ target_os = "linux",
+ target_os = "redox",
+ target_os = "netbsd",
+ target_os = "openbsd",
+ target_os = "solaris"
+))]
pub fn pipe2(flags: OFlag) -> Result<(RawFd, RawFd)> {
let mut fds = mem::MaybeUninit::<[c_int; 2]>::uninit();
@@ -1196,11 +1226,8 @@ pub fn pipe2(flags: OFlag) -> Result<(RawFd, RawFd)> {
/// [truncate(2)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/truncate.html)
#[cfg(not(any(target_os = "redox", target_os = "fuchsia")))]
pub fn truncate<P: ?Sized + NixPath>(path: &P, len: off_t) -> Result<()> {
- let res = path.with_nix_path(|cstr| {
- unsafe {
- libc::truncate(cstr.as_ptr(), len)
- }
- })?;
+ let res = path
+ .with_nix_path(|cstr| unsafe { libc::truncate(cstr.as_ptr(), len) })?;
Errno::result(res).map(drop)
}
@@ -1224,7 +1251,7 @@ pub fn isatty(fd: RawFd) -> Result<bool> {
Errno::ENOTTY => Ok(false),
err => Err(err),
}
- }
+ }
}
}
@@ -1280,16 +1307,12 @@ pub fn linkat<P: ?Sized + NixPath>(
Errno::result(res).map(drop)
}
-
/// Remove a directory entry
///
/// See also [unlink(2)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/unlink.html)
pub fn unlink<P: ?Sized + NixPath>(path: &P) -> Result<()> {
- let res = path.with_nix_path(|cstr| {
- unsafe {
- libc::unlink(cstr.as_ptr())
- }
- })?;
+ let res =
+ path.with_nix_path(|cstr| unsafe { libc::unlink(cstr.as_ptr()) })?;
Errno::result(res).map(drop)
}
@@ -1329,13 +1352,11 @@ pub fn unlinkat<P: ?Sized + NixPath>(
Errno::result(res).map(drop)
}
-
#[inline]
#[cfg(not(target_os = "fuchsia"))]
pub fn chroot<P: ?Sized + NixPath>(path: &P) -> Result<()> {
- let res = path.with_nix_path(|cstr| {
- unsafe { libc::chroot(cstr.as_ptr()) }
- })?;
+ let res =
+ path.with_nix_path(|cstr| unsafe { libc::chroot(cstr.as_ptr()) })?;
Errno::result(res).map(drop)
}
@@ -1379,15 +1400,17 @@ pub fn fsync(fd: RawFd) -> Result<()> {
///
/// See also
/// [fdatasync(2)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/fdatasync.html)
-#[cfg(any(target_os = "linux",
- target_os = "android",
- target_os = "emscripten",
- target_os = "freebsd",
- target_os = "fuchsia",
- target_os = "netbsd",
- target_os = "openbsd",
- target_os = "illumos",
- target_os = "solaris"))]
+#[cfg(any(
+ target_os = "linux",
+ target_os = "android",
+ target_os = "emscripten",
+ target_os = "freebsd",
+ target_os = "fuchsia",
+ target_os = "netbsd",
+ target_os = "openbsd",
+ target_os = "illumos",
+ target_os = "solaris"
+))]
#[inline]
pub fn fdatasync(fd: RawFd) -> Result<()> {
let res = unsafe { libc::fdatasync(fd) };
@@ -2739,28 +2762,29 @@ pub fn sysconf(var: SysconfVar) -> Result<Option<c_long>> {
}
}
-feature! {
-#![feature = "fs"]
-
#[cfg(any(target_os = "android", target_os = "linux"))]
+#[cfg(feature = "fs")]
mod pivot_root {
- use crate::{Result, NixPath};
use crate::errno::Errno;
+ use crate::{NixPath, Result};
pub fn pivot_root<P1: ?Sized + NixPath, P2: ?Sized + NixPath>(
- new_root: &P1, put_old: &P2) -> Result<()> {
+ new_root: &P1,
+ put_old: &P2,
+ ) -> Result<()> {
let res = new_root.with_nix_path(|new_root| {
- put_old.with_nix_path(|put_old| {
- unsafe {
- libc::syscall(libc::SYS_pivot_root, new_root.as_ptr(), put_old.as_ptr())
- }
+ put_old.with_nix_path(|put_old| unsafe {
+ libc::syscall(
+ libc::SYS_pivot_root,
+ new_root.as_ptr(),
+ put_old.as_ptr(),
+ )
})
})??;
Errno::result(res).map(drop)
}
}
-}
#[cfg(any(
target_os = "android",
@@ -2904,10 +2928,8 @@ feature! {
/// Checks the file named by `path` for accessibility according to the flags given by `amode`
/// See [access(2)](https://pubs.opengroup.org/onlinepubs/9699919799/functions/access.html)
pub fn access<P: ?Sized + NixPath>(path: &P, amode: AccessFlags) -> Result<()> {
- let res = path.with_nix_path(|cstr| {
- unsafe {
- libc::access(cstr.as_ptr(), amode.bits)
- }
+ let res = path.with_nix_path(|cstr| unsafe {
+ libc::access(cstr.as_ptr(), amode.bits)
})?;
Errno::result(res).map(drop)
}
@@ -2945,10 +2967,8 @@ pub fn faccessat<P: ?Sized + NixPath>(dirfd: Option<RawFd>, path: &P, mode: Acce
target_os = "dragonfly"
))]
pub fn eaccess<P: ?Sized + NixPath>(path: &P, mode: AccessFlags) -> Result<()> {
- let res = path.with_nix_path(|cstr| {
- unsafe {
- libc::eaccess(cstr.as_ptr(), mode.bits)
- }
+ let res = path.with_nix_path(|cstr| unsafe {
+ libc::eaccess(cstr.as_ptr(), mode.bits)
})?;
Errno::result(res).map(drop)
}