Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/uu/mkdir/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ clap = { workspace = true }
uucore = { workspace = true, features = ["fs", "mode", "fsxattr"] }
fluent = { workspace = true }

[target.'cfg(unix)'.dependencies]
nix = { workspace = true, features = ["fs"] }

[features]
selinux = ["uucore/selinux"]
smack = ["uucore/smack"]
Expand Down
20 changes: 8 additions & 12 deletions src/uu/mkdir/src/mkdir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use clap::builder::ValueParser;
use clap::parser::ValuesRef;
use clap::{Arg, ArgAction, ArgMatches, Command};
#[cfg(unix)]
use nix::sys::stat::{Mode, umask};
use std::ffi::OsString;
use std::io::{Write, stdout};
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -252,23 +254,20 @@ fn create_dir(path: &Path, is_parent: bool, config: &Config) -> UResult<()> {

/// RAII guard to restore umask on drop, ensuring cleanup even on panic.
#[cfg(unix)]
struct UmaskGuard(uucore::libc::mode_t);
struct UmaskGuard(Mode);

#[cfg(unix)]
impl UmaskGuard {
/// Set umask to the given value and return a guard that restores the original on drop.
fn set(new_mask: uucore::libc::mode_t) -> Self {
let old_mask = unsafe { uucore::libc::umask(new_mask) };
Self(old_mask)
/// Set umask to 0 and return a guard that restores the original on drop.
fn new() -> Self {
Self(umask(Mode::empty()))
}
}

#[cfg(unix)]
impl Drop for UmaskGuard {
fn drop(&mut self) {
unsafe {
uucore::libc::umask(self.0);
}
let _ = umask(self.0);
}
}

Expand All @@ -281,10 +280,7 @@ impl Drop for UmaskGuard {
fn create_dir_with_mode(path: &Path, mode: u32) -> std::io::Result<()> {
use std::os::unix::fs::DirBuilderExt;

// Temporarily set umask to 0 so the directory is created with the exact mode.
// The guard restores the original umask on drop, even if we panic.
let _guard = UmaskGuard::set(0);

let _guard = UmaskGuard::new();
std::fs::DirBuilder::new().mode(mode).create(path)
}

Expand Down
Loading