32 lines
703 B
Rust
32 lines
703 B
Rust
#[cfg(unix)]
|
|
use libc::{mode_t, umask};
|
|
|
|
/// Unfortunately, novice unix users can sometimes be running
|
|
/// with an overly permissive umask so we take care to install
|
|
/// a more restrictive mask while we might be creating things
|
|
/// in the filesystem.
|
|
/// This struct locks down the umask for its lifetime, restoring
|
|
/// the prior umask when it is dropped.
|
|
pub struct UmaskSaver {
|
|
#[cfg(unix)]
|
|
mask: mode_t,
|
|
}
|
|
|
|
impl UmaskSaver {
|
|
pub fn new() -> Self {
|
|
Self {
|
|
#[cfg(unix)]
|
|
mask: unsafe { umask(0o077) },
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Drop for UmaskSaver {
|
|
fn drop(&mut self) {
|
|
#[cfg(unix)]
|
|
unsafe {
|
|
umask(self.mask);
|
|
}
|
|
}
|
|
}
|