Skip to content
Draft
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
5 changes: 2 additions & 3 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 Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -749,3 +749,6 @@ strip = true
inherits = "release"
panic = "unwind"
debug = true

[patch.crates-io]
rustix = { git = "https://github.com/bytecodealliance/rustix.git", rev = "ffa6ee556f54ef646c5cfc67afc72703ae3a7382" }
2 changes: 1 addition & 1 deletion src/uu/mknod/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ doctest = false
[dependencies]
clap = { workspace = true }
fluent = { workspace = true }
nix = { workspace = true, features = ["fs"] }
rustix = { workspace = true, features = ["fs"] }
uucore = { workspace = true, features = ["fs", "mode"] }

[features]
Expand Down
42 changes: 17 additions & 25 deletions src/uu/mknod/src/mknod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

// spell-checker:ignore (ToDO) parsemode makedev sysmacros perror IFBLK IFCHR IFIFO sflag
// spell-checker:ignore (ToDO) parsemode makedev sysmacros perror mknodat

use clap::{Arg, ArgAction, Command, value_parser};
use nix::libc::{S_IRGRP, S_IROTH, S_IRUSR, S_IWGRP, S_IWOTH, S_IWUSR, mode_t};
use nix::sys::stat::{Mode, SFlag, dev_t, mknod as nix_mknod};
use rustix::fs::{CWD, Dev, Mode, mknodat};
use std::ffi::{OsStr, OsString};
use std::io::{self, Write as _};

Expand All @@ -17,8 +16,7 @@ use uucore::format_usage;
use uucore::fs::makedev;
use uucore::{show_warning, translate};

#[allow(clippy::unnecessary_cast)]
const MODE_RW_UGO: u32 = (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH) as u32;
const MODE_RW_UGO: u32 = 0o666;

mod options {
pub const MODE: &str = "mode";
Expand All @@ -29,19 +27,19 @@ mod options {
pub const CONTEXT: &str = "context";
}

#[derive(Clone, PartialEq)]
#[derive(Clone, Copy, PartialEq)]
enum FileType {
Block,
Character,
Fifo,
}

impl FileType {
fn as_sflag(&self) -> SFlag {
match self {
Self::Block => SFlag::S_IFBLK,
Self::Character => SFlag::S_IFCHR,
Self::Fifo => SFlag::S_IFIFO,
impl From<FileType> for rustix::fs::FileType {
fn from(file_type: FileType) -> Self {
match file_type {
FileType::Block => Self::BlockDevice,
FileType::Character => Self::CharacterDevice,
FileType::Fifo => Self::Fifo,
}
}
}
Expand All @@ -56,7 +54,7 @@ struct Config {
/// when false, the exact mode bits will be set
use_umask: bool,

dev: dev_t,
dev: Dev,

/// Set security context (SELinux/SMACK).
#[cfg(any(
Expand All @@ -70,6 +68,8 @@ struct Config {
}

fn mknod(file_name: &OsStr, config: Config) -> i32 {
let file_type = rustix::fs::FileType::from(config.file_type);

#[cfg(all(feature = "selinux", any(target_os = "android", target_os = "linux")))]
let selinux_enabled = uucore::selinux::is_selinux_enabled();
#[cfg(not(all(feature = "selinux", any(target_os = "android", target_os = "linux"))))]
Expand All @@ -86,7 +86,7 @@ fn mknod(file_name: &OsStr, config: Config) -> i32 {
// Label the node at creation, as GNU does; relabelling after leaves a window.
#[cfg(all(feature = "selinux", any(target_os = "android", target_os = "linux")))]
let _selinux_guard = if config.set_security_context && selinux_enabled {
let mode = config.file_type.as_sflag().bits() | config.mode.bits();
let mode = file_type.as_raw_mode() | config.mode.as_raw_mode();
match uucore::selinux::FsCreateContext::new(
std::path::Path::new(file_name),
Some(mode),
Expand All @@ -102,15 +102,7 @@ fn mknod(file_name: &OsStr, config: Config) -> i32 {
None
};

let create_node = || {
nix_mknod(
file_name,
config.file_type.as_sflag(),
config.mode,
config.dev,
)
.err()
};
let create_node = || mknodat(CWD, file_name, file_type, config.mode, config.dev).err();
let mknod_err = if config.use_umask {
create_node()
} else {
Expand Down Expand Up @@ -179,7 +171,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
mode
}
};
let mode = Mode::from_bits_truncate(mode_permissions as mode_t);
let mode = Mode::from_bits_truncate(mode_permissions as _);

let file_name = matches
.get_one::<OsString>("name")
Expand Down Expand Up @@ -216,7 +208,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {

let config = Config {
mode,
file_type: file_type.clone(),
file_type: *file_type,
use_umask,
dev,
#[cfg(any(
Expand Down
21 changes: 21 additions & 0 deletions tests/by-util/test_mknod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

// spell-checker:ignore nconfined

use rstest::rstest;
use std::os::unix::fs::PermissionsExt;

#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))]
Expand Down Expand Up @@ -36,6 +37,26 @@ fn test_mknod_invalid_arg() {
.stderr_contains("unexpected argument '--foo' found");
}

#[rstest]
#[case("f")]
#[case("d")]
#[case("l")]
#[case("s")]
#[case("?")]
#[case("regular")]
#[case("directory")]
#[case("symlink")]
#[case("socket")]
fn test_mknod_invalid_file_type(#[case] file_type: &str) {
let ts = TestScenario::new(util_name!());
ts.ucmd()
.args(&["node", file_type, "1", "2"])
.fails_with_code(1)
.no_stdout()
.stderr_contains("invalid device type");
assert!(ts.fixtures.plus("node").symlink_metadata().is_err());
}

#[test]
fn test_mknod_help() {
new_ucmd!()
Expand Down
Loading