Repository navigation
mountpoint: add -d/--fs-devno and -x/--devno #726
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Harp404
wants to merge
1
commit into
uutils:main
Choose a base branch
from
Harp404:mountpoint-devno
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+158
−1
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,8 +3,8 @@ | |
| // For the full copyright and license information, please view the LICENSE | ||
| // file that was distributed with this source code. | ||
|
|
||
| use clap::Arg; | ||
| use clap::{crate_version, Command}; | ||
| use clap::{Arg, ArgAction}; | ||
| use std::env; | ||
| #[cfg(not(windows))] | ||
| use std::fs; | ||
|
|
@@ -23,6 +23,20 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> { | |
| let path = matches.get_one::<String>("path"); | ||
|
|
||
| if let Some(path) = path { | ||
| if matches.get_flag("devno") { | ||
| return print_block_devno(path); | ||
| } | ||
| if matches.get_flag("fs-devno") { | ||
| match fs_devno(path) { | ||
| Ok(Some(devno)) => println!("{devno}"), | ||
| Ok(None) => { | ||
| println!("{path} is not a mountpoint"); | ||
| set_exit_code(32); | ||
| } | ||
| Err(e) => return Err(USimpleError::new(1, format!("{path}: {}", strip_errno(&e)))), | ||
| } | ||
| return Ok(()); | ||
| } | ||
| match is_mountpoint(path) { | ||
| Ok(true) => println!("{path} is a mountpoint"), | ||
| Ok(false) => { | ||
|
|
@@ -56,6 +70,77 @@ fn is_mountpoint(path: &str) -> std::io::Result<bool> { | |
| .any(|mount_point| unescape_mountinfo(mount_point) == target.as_os_str().as_bytes())) | ||
| } | ||
|
|
||
| /// The `major:minor` of the filesystem mounted on `path`, or `None` when it is | ||
| /// not a mount point. Like util-linux, this is the device number of the | ||
| /// topmost (last) mountinfo entry for the path, which can differ from the | ||
| /// path's `st_dev`, for example for a bind-mounted file. | ||
| #[cfg(target_os = "linux")] | ||
| fn fs_devno(path: &str) -> std::io::Result<Option<String>> { | ||
| use std::os::unix::ffi::OsStrExt; | ||
|
|
||
| let target = fs::canonicalize(path)?; | ||
| let Ok(mountinfo) = fs::read("/proc/self/mountinfo") else { | ||
| return fs_devno_by_device(path); | ||
| }; | ||
| // the last entry for a mount point is the topmost mount | ||
| Ok(mountinfo.rsplit(|&b| b == b'\n').find_map(|line| { | ||
| let mut fields = line.split(|&b| b == b' '); | ||
| let devno = fields.nth(2)?; | ||
| let mount_point = fields.nth(1)?; | ||
| (unescape_mountinfo(mount_point) == target.as_os_str().as_bytes()) | ||
| .then(|| String::from_utf8_lossy(devno).into_owned()) | ||
| })) | ||
| } | ||
|
|
||
| #[cfg(all(not(windows), not(target_os = "linux")))] | ||
| fn fs_devno(path: &str) -> std::io::Result<Option<String>> { | ||
| fs_devno_by_device(path) | ||
| } | ||
|
|
||
| #[cfg(not(windows))] | ||
| fn fs_devno_by_device(path: &str) -> std::io::Result<Option<String>> { | ||
| if !is_mountpoint_by_device(path)? { | ||
| return Ok(None); | ||
| } | ||
| Ok(Some(format_devno(fs::metadata(path)?.dev()))) | ||
| } | ||
|
|
||
| #[cfg(windows)] | ||
| fn fs_devno(_path: &str) -> std::io::Result<Option<String>> { | ||
| Ok(None) | ||
| } | ||
|
|
||
| #[cfg(not(windows))] | ||
| fn format_devno(dev: u64) -> String { | ||
| // dev_t is narrower than u64 on some platforms, such as macOS | ||
| #[allow(clippy::unnecessary_cast)] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could we avoid the #[allow]? e.g. |
||
| let dev = dev as libc::dev_t; | ||
| format!("{}:{}", libc::major(dev), libc::minor(dev)) | ||
| } | ||
|
|
||
| /// Prints the `major:minor` of the block device `path`. | ||
| #[cfg(not(windows))] | ||
| fn print_block_devno(path: &str) -> UResult<()> { | ||
| use std::os::unix::fs::FileTypeExt; | ||
|
|
||
| let metadata = fs::metadata(path) | ||
| .map_err(|e| USimpleError::new(1, format!("{path}: {}", strip_errno(&e))))?; | ||
| if metadata.file_type().is_block_device() { | ||
| println!("{}", format_devno(metadata.rdev())); | ||
| } else { | ||
| uucore::show_error!("{path}: not a block device"); | ||
| set_exit_code(32); | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| #[cfg(windows)] | ||
| fn print_block_devno(path: &str) -> UResult<()> { | ||
| uucore::show_error!("{path}: not a block device"); | ||
| set_exit_code(32); | ||
| Ok(()) | ||
| } | ||
|
|
||
| /// mountinfo writes space, tab, newline and backslash as `\ooo` octal escapes. | ||
| #[cfg(target_os = "linux")] | ||
| fn unescape_mountinfo(field: &[u8]) -> Vec<u8> { | ||
|
|
@@ -108,6 +193,20 @@ pub fn uu_app() -> Command { | |
| .about(ABOUT) | ||
| .override_usage(format_usage(USAGE)) | ||
| .infer_long_args(true) | ||
| .arg( | ||
| Arg::new("fs-devno") | ||
| .short('d') | ||
| .long("fs-devno") | ||
| .action(ArgAction::SetTrue) | ||
| .help("Print the major/minor device number of the filesystem mounted on PATH"), | ||
| ) | ||
| .arg( | ||
| Arg::new("devno") | ||
| .short('x') | ||
| .long("devno") | ||
| .action(ArgAction::SetTrue) | ||
| .help("Print the major/minor device number of the block device PATH"), | ||
| ) | ||
| .arg( | ||
| Arg::new("path") | ||
| .value_name("PATH") | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is almost the same code as is_mountpoint (mountinfo scan + by-device fallback)
could is_mountpoint just be
fs_devno(path).map(|d| d.is_some()), no?