mirror of
https://github.com/uutils/coreutils
synced 2024-12-14 15:22:38 +00:00
uucore: add function which checks hardlink as well as directed symlink
This commit is contained in:
parent
60e797ea57
commit
ab42b1e599
1 changed files with 30 additions and 0 deletions
|
@ -648,6 +648,36 @@ pub fn are_hardlinks_to_same_file(source: &Path, target: &Path) -> bool {
|
|||
source_metadata.ino() == target_metadata.ino() && source_metadata.dev() == target_metadata.dev()
|
||||
}
|
||||
|
||||
#[cfg(not(unix))]
|
||||
pub fn are_hardlinks_or_one_way_symlink_to_same_file(_source: &Path, _target: &Path) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
/// Checks if either two paths are hard links to the same file or if the source path is a symbolic link which when fully resolved points to target path
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `source` - A reference to a `Path` representing the source path.
|
||||
/// * `target` - A reference to a `Path` representing the target path.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// * `bool` - Returns `true` if either of above conditions are true, and `false` otherwise.
|
||||
#[cfg(unix)]
|
||||
pub fn are_hardlinks_or_one_way_symlink_to_same_file(source: &Path, target: &Path) -> bool {
|
||||
let source_metadata = match fs::metadata(source) {
|
||||
Ok(metadata) => metadata,
|
||||
Err(_) => return false,
|
||||
};
|
||||
|
||||
let target_metadata = match fs::symlink_metadata(target) {
|
||||
Ok(metadata) => metadata,
|
||||
Err(_) => return false,
|
||||
};
|
||||
|
||||
source_metadata.ino() == target_metadata.ino() && source_metadata.dev() == target_metadata.dev()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
// Note this useful idiom: importing names from outer (for mod tests) scope.
|
||||
|
|
Loading…
Reference in a new issue