add mountpoint

This commit is contained in:
Sylvestre Ledru
2024-01-16 22:29:27 +01:00
parent 9737677a2d
commit 10811a35bd
4 changed files with 60 additions and 0 deletions

View 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"

View 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,
}
}