From 86acc26cbcadfd572d6af93ba34467a3cdac8b4e Mon Sep 17 00:00:00 2001 From: Alan Somers Date: Sat, 24 Jul 2021 16:47:26 -0600 Subject: Constify many functions Constify most functions that can be constified. The exceptions are mostly accessors for structs that have no const constructor. --- src/sys/stat.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/sys/stat.rs') diff --git a/src/sys/stat.rs b/src/sys/stat.rs index 15451e78..ba212611 100644 --- a/src/sys/stat.rs +++ b/src/sys/stat.rs @@ -52,19 +52,19 @@ pub fn mknod(path: &P, kind: SFlag, perm: Mode, dev: dev_t) } #[cfg(target_os = "linux")] -pub fn major(dev: dev_t) -> u64 { +pub const fn major(dev: dev_t) -> u64 { ((dev >> 32) & 0xffff_f000) | ((dev >> 8) & 0x0000_0fff) } #[cfg(target_os = "linux")] -pub fn minor(dev: dev_t) -> u64 { +pub const fn minor(dev: dev_t) -> u64 { ((dev >> 12) & 0xffff_ff00) | ((dev ) & 0x0000_00ff) } #[cfg(target_os = "linux")] -pub fn makedev(major: u64, minor: u64) -> dev_t { +pub const fn makedev(major: u64, minor: u64) -> dev_t { ((major & 0xffff_f000) << 32) | ((major & 0x0000_0fff) << 8) | ((minor & 0xffff_ff00) << 12) | -- cgit v1.2.3 From 6e2158bc7126510890351d67400d53de9fa9629e Mon Sep 17 00:00:00 2001 From: Luca BRUNO Date: Thu, 22 Jul 2021 07:49:59 +0000 Subject: sys/stat: add a safe wrapper for mknodat(2) This introduces a new `mknodat` helper. Ref: https://pubs.opengroup.org/onlinepubs/9699919799/functions/mknod.html --- src/sys/stat.rs | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) (limited to 'src/sys/stat.rs') diff --git a/src/sys/stat.rs b/src/sys/stat.rs index 15451e78..c797576e 100644 --- a/src/sys/stat.rs +++ b/src/sys/stat.rs @@ -9,6 +9,7 @@ use std::os::unix::io::RawFd; use crate::sys::time::{TimeSpec, TimeVal}; libc_bitflags!( + /// "File type" flags for `mknod` and related functions. pub struct SFlag: mode_t { S_IFIFO; S_IFCHR; @@ -22,6 +23,7 @@ libc_bitflags!( ); libc_bitflags! { + /// "File mode / permissions" flags. pub struct Mode: mode_t { S_IRWXU; S_IRUSR; @@ -41,11 +43,26 @@ libc_bitflags! { } } +/// Create a special or ordinary file, by pathname. pub fn mknod(path: &P, kind: SFlag, perm: Mode, dev: dev_t) -> Result<()> { - let res = path.with_nix_path(|cstr| { - unsafe { - libc::mknod(cstr.as_ptr(), kind.bits | perm.bits() as mode_t, dev) - } + let res = path.with_nix_path(|cstr| unsafe { + libc::mknod(cstr.as_ptr(), kind.bits | perm.bits() as mode_t, dev) + })?; + + Errno::result(res).map(drop) +} + +/// Create a special or ordinary file, relative to a given directory. +#[cfg(not(any(target_os = "ios", target_os = "macos", target_os = "redox")))] +pub fn mknodat( + dirfd: RawFd, + path: &P, + kind: SFlag, + perm: Mode, + dev: dev_t, +) -> Result<()> { + let res = path.with_nix_path(|cstr| unsafe { + libc::mknodat(dirfd, cstr.as_ptr(), kind.bits | perm.bits() as mode_t, dev) })?; Errno::result(res).map(drop) -- cgit v1.2.3