add mountpoint
This commit is contained in:
parent
9737677a2d
commit
10811a35bd
5
Cargo.lock
generated
5
Cargo.lock
generated
@ -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"
|
||||
|
@ -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" }
|
||||
|
12
src/uu/mountpoint/Cargo.toml
Normal file
12
src/uu/mountpoint/Cargo.toml
Normal file
@ -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"
|
42
src/uu/mountpoint/src/mountpoint.rs
Normal file
42
src/uu/mountpoint/src/mountpoint.rs
Normal file
@ -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,
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user