diff --git a/Cargo.lock b/Cargo.lock index 800c0af..253c08b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1311,6 +1311,7 @@ dependencies = [ "time", "unindent", "uu_lscpu", + "uu_mountpoint", "uu_pwdx", "uu_renice", "walkdir", @@ -1325,6 +1326,10 @@ dependencies = [ "sysinfo", ] +[[package]] +name = "uu_mountpoint" +version = "0.0.1" + [[package]] name = "uu_pwdx" version = "0.0.1" diff --git a/Cargo.toml b/Cargo.toml index 48ebf16..a9df23b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -147,6 +147,7 @@ zip = { workspace = true, optional = true } uu_pwdx = { optional = true, version = "0.0.1", package = "uu_pwdx", path = "src/uu/pwdx" } uu_lscpu = { optional = true, version = "0.0.1", package = "uu_lscpu", path = "src/uu/lscpu" } uu_renice = { optional = true, version = "0.0.1", package = "uu_renice", path = "src/uu/renice" } +uu_mountpoint = { optional = true, version = "0.0.1", package = "uu_mountpoint", path = "src/uu/mountpoint" } # this breaks clippy linting with: "tests/by-util/test_factor_benches.rs: No such file or directory (os error 2)" # factor_benches = { optional = true, version = "0.0.0", package = "uu_factor_benches", path = "tests/benches/factor" } diff --git a/src/uu/mountpoint/Cargo.toml b/src/uu/mountpoint/Cargo.toml new file mode 100644 index 0000000..bea7aab --- /dev/null +++ b/src/uu/mountpoint/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "uu_mountpoint" +version = "0.0.1" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] + +[[bin]] +name = "mountpoint" +path = "src/mountpoint.rs" diff --git a/src/uu/mountpoint/src/mountpoint.rs b/src/uu/mountpoint/src/mountpoint.rs new file mode 100644 index 0000000..589c53c --- /dev/null +++ b/src/uu/mountpoint/src/mountpoint.rs @@ -0,0 +1,42 @@ +// This file is part of the uutils util-linux package. +// +// For the full copyright and license information, please view the LICENSE +// file that was distributed with this source code. + +use std::env; +use std::fs; +use std::os::unix::fs::MetadataExt; +use std::process; + +fn main() { + let args: Vec<String> = env::args().collect(); + + if args.len() != 2 { + eprintln!("Usage: mountpoint <path>"); + process::exit(1); + } + + let path = &args[1]; + if is_mountpoint(path) { + println!("{} is a mountpoint", path); + } else { + println!("{} is not a mountpoint", path); + } +} + +fn is_mountpoint(path: &str) -> bool { + let metadata = match fs::metadata(path) { + Ok(metadata) => metadata, + Err(_) => return false, + }; + + let dev = metadata.dev(); + let inode = metadata.ino(); + + // Root inode (typically 2 in most Unix filesystems) indicates a mount point + inode == 2 + || match fs::metadata("..") { + Ok(parent_metadata) => parent_metadata.dev() != dev, + Err(_) => false, + } +}