mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-28 11:37:44 +00:00
Merge pull request #4976 from Skryptonyte/mv_fixhardlinktosymlink
mv: Fix for tests/mv/symlink-onto-hardlink-to-self.sh
This commit is contained in:
commit
468af7dc16
3 changed files with 36 additions and 5 deletions
|
@ -25,7 +25,7 @@ use std::path::{Path, PathBuf};
|
||||||
use uucore::backup_control::{self, BackupMode};
|
use uucore::backup_control::{self, BackupMode};
|
||||||
use uucore::display::Quotable;
|
use uucore::display::Quotable;
|
||||||
use uucore::error::{set_exit_code, FromIo, UError, UResult, USimpleError, UUsageError};
|
use uucore::error::{set_exit_code, FromIo, UError, UResult, USimpleError, UUsageError};
|
||||||
use uucore::fs::are_hardlinks_or_one_way_symlink_to_same_file;
|
use uucore::fs::{are_hardlinks_or_one_way_symlink_to_same_file, are_hardlinks_to_same_file};
|
||||||
use uucore::update_control::{self, UpdateMode};
|
use uucore::update_control::{self, UpdateMode};
|
||||||
use uucore::{format_usage, help_about, help_section, help_usage, prompt_yes, show};
|
use uucore::{format_usage, help_about, help_section, help_usage, prompt_yes, show};
|
||||||
|
|
||||||
|
@ -255,8 +255,10 @@ fn handle_two_paths(source: &Path, target: &Path, b: &Behavior) -> UResult<()> {
|
||||||
return Err(MvError::NoSuchFile(source.quote().to_string()).into());
|
return Err(MvError::NoSuchFile(source.quote().to_string()).into());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (source.eq(target) || are_hardlinks_or_one_way_symlink_to_same_file(source, target))
|
if (source.eq(target)
|
||||||
&& b.backup != BackupMode::SimpleBackup
|
|| are_hardlinks_to_same_file(source, target)
|
||||||
|
|| are_hardlinks_or_one_way_symlink_to_same_file(source, target))
|
||||||
|
&& b.backup == BackupMode::NoBackup
|
||||||
{
|
{
|
||||||
if source.eq(Path::new(".")) || source.ends_with("/.") || source.is_file() {
|
if source.eq(Path::new(".")) || source.ends_with("/.") || source.is_file() {
|
||||||
return Err(
|
return Err(
|
||||||
|
|
|
@ -635,12 +635,12 @@ pub fn are_hardlinks_to_same_file(_source: &Path, _target: &Path) -> bool {
|
||||||
/// * `bool` - Returns `true` if the paths are hard links to the same file, and `false` otherwise.
|
/// * `bool` - Returns `true` if the paths are hard links to the same file, and `false` otherwise.
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
pub fn are_hardlinks_to_same_file(source: &Path, target: &Path) -> bool {
|
pub fn are_hardlinks_to_same_file(source: &Path, target: &Path) -> bool {
|
||||||
let source_metadata = match fs::metadata(source) {
|
let source_metadata = match fs::symlink_metadata(source) {
|
||||||
Ok(metadata) => metadata,
|
Ok(metadata) => metadata,
|
||||||
Err(_) => return false,
|
Err(_) => return false,
|
||||||
};
|
};
|
||||||
|
|
||||||
let target_metadata = match fs::metadata(target) {
|
let target_metadata = match fs::symlink_metadata(target) {
|
||||||
Ok(metadata) => metadata,
|
Ok(metadata) => metadata,
|
||||||
Err(_) => return false,
|
Err(_) => return false,
|
||||||
};
|
};
|
||||||
|
|
|
@ -465,6 +465,35 @@ fn test_mv_same_symlink() {
|
||||||
.stderr_is(format!("mv: '{file_c}' and '{file_a}' are the same file\n",));
|
.stderr_is(format!("mv: '{file_c}' and '{file_a}' are the same file\n",));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
#[cfg(all(unix, not(target_os = "android")))]
|
||||||
|
fn test_mv_hardlink_to_symlink() {
|
||||||
|
let (at, mut ucmd) = at_and_ucmd!();
|
||||||
|
let file = "file";
|
||||||
|
let symlink_file = "symlink";
|
||||||
|
let hardlink_to_symlink_file = "hardlink_to_symlink";
|
||||||
|
|
||||||
|
at.touch(file);
|
||||||
|
at.symlink_file(file, symlink_file);
|
||||||
|
at.hard_link(symlink_file, hardlink_to_symlink_file);
|
||||||
|
|
||||||
|
ucmd.arg(symlink_file).arg(hardlink_to_symlink_file).fails();
|
||||||
|
|
||||||
|
let (at2, mut ucmd2) = at_and_ucmd!();
|
||||||
|
|
||||||
|
at2.touch(file);
|
||||||
|
at2.symlink_file(file, symlink_file);
|
||||||
|
at2.hard_link(symlink_file, hardlink_to_symlink_file);
|
||||||
|
|
||||||
|
ucmd2
|
||||||
|
.arg("--backup")
|
||||||
|
.arg(symlink_file)
|
||||||
|
.arg(hardlink_to_symlink_file)
|
||||||
|
.succeeds();
|
||||||
|
assert!(!at2.symlink_exists(symlink_file));
|
||||||
|
assert!(at2.symlink_exists(&format!("{hardlink_to_symlink_file}~")));
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(all(unix, not(target_os = "android")))]
|
#[cfg(all(unix, not(target_os = "android")))]
|
||||||
fn test_mv_same_hardlink_backup_simple() {
|
fn test_mv_same_hardlink_backup_simple() {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue