Skip to content

Commit 6542209

Browse files
committed
Fix umask tests on non-Linux Unix platforms.
1 parent 8aed312 commit 6542209

File tree

2 files changed

+18
-19
lines changed

2 files changed

+18
-19
lines changed

cap-tempfile/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ features = [
3232
"Win32_Foundation",
3333
]
3434

35+
[dev-dependencies]
36+
tempfile = "3.23.0"
37+
3538
[features]
3639
default = []
3740
fs_utf8 = ["cap-std/fs_utf8", "camino"]

cap-tempfile/src/tempfile.rs

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -243,26 +243,22 @@ mod test {
243243
use super::*;
244244

245245
/// On Unix, calling `umask()` actually *mutates* the process global state.
246-
/// This uses Linux `/proc` to read the current value.
247-
#[cfg(any(target_os = "android", target_os = "linux"))]
246+
/// This uses a temporary file instead.
247+
#[cfg(unix)]
248248
fn get_process_umask() -> io::Result<u32> {
249-
use io::BufRead;
250-
let status = std::fs::File::open("/proc/self/status")?;
251-
let bufr = io::BufReader::new(status);
252-
for line in bufr.lines() {
253-
let line = line?;
254-
let l = if let Some(v) = line.split_once(':') {
255-
v
256-
} else {
257-
continue;
258-
};
259-
let (k, v) = l;
260-
if k != "Umask" {
261-
continue;
262-
}
263-
return Ok(u32::from_str_radix(v.trim(), 8).unwrap());
264-
}
265-
panic!("Could not determine process umask")
249+
use std::os::unix::fs::{OpenOptionsExt, MetadataExt};
250+
251+
let d = tempfile::tempdir().unwrap();
252+
let p = d.path().join("file");
253+
254+
let mut opts = std::fs::OpenOptions::new();
255+
opts.read(true);
256+
opts.write(true);
257+
opts.create_new(true);
258+
opts.mode(0o777);
259+
let f = opts.open(p).unwrap();
260+
let m = f.metadata().unwrap();
261+
Ok(!m.mode() & 0o777)
266262
}
267263

268264
/// Older Windows versions don't support removing open files

0 commit comments

Comments
 (0)