Skip to content

Commit 2ede1ea

Browse files
mattsu2020sylvestre
authored andcommitted
Fix umask handling for non-Unix platforms
Replace unsafe libc::umask usage with nix crate for Unix platforms and add proper cfg guards for non-Unix platforms to ensure correct umask handling across all supported operating systems.
1 parent 8a58bf5 commit 2ede1ea

1 file changed

Lines changed: 18 additions & 18 deletions

File tree

src/uucore/src/lib/features/mode.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
88
// spell-checker:ignore (vars) fperm srwx
99

10+
#[cfg(not(unix))]
1011
use libc::umask;
1112

1213
pub fn parse_numeric(fperm: u32, mut mode: &str, considering_dir: bool) -> Result<u32, String> {
@@ -175,24 +176,23 @@ pub fn get_umask() -> u32 {
175176
// some other thread is affected.
176177
// On modern Linux kernels the current umask could instead be read
177178
// from /proc/self/status. But that's a lot of work.
178-
// SAFETY: umask always succeeds and doesn't operate on memory. Races are
179-
// possible but it can't violate Rust's guarantees.
180-
let mask = unsafe { umask(0) };
181-
unsafe { umask(mask) };
182-
#[cfg(all(
183-
not(target_os = "freebsd"),
184-
not(target_vendor = "apple"),
185-
not(target_os = "android"),
186-
not(target_os = "redox")
187-
))]
188-
return mask;
189-
#[cfg(any(
190-
target_os = "freebsd",
191-
target_vendor = "apple",
192-
target_os = "android",
193-
target_os = "redox"
194-
))]
195-
return mask as u32;
179+
#[cfg(unix)]
180+
{
181+
use nix::sys::stat::{Mode, umask};
182+
183+
let mask = umask(Mode::empty());
184+
let _ = umask(mask);
185+
return mask.bits() as u32;
186+
}
187+
188+
#[cfg(not(unix))]
189+
{
190+
// SAFETY: umask always succeeds and doesn't operate on memory. Races are
191+
// possible but it can't violate Rust's guarantees.
192+
let mask = unsafe { umask(0) };
193+
unsafe { umask(mask) };
194+
return mask as u32;
195+
}
196196
}
197197

198198
#[cfg(test)]

0 commit comments

Comments
 (0)