mirror of
https://github.com/uutils/coreutils
synced 2024-09-06 16:03:44 +00:00
ln: make the tests/ln/hard-backup.sh test work
We haven't a great error message with hard link on the same file + Update the GNU error message to match ours
This commit is contained in:
parent
de71d01959
commit
050b5b0c9b
|
@ -54,6 +54,7 @@ enum LnError {
|
||||||
TargetIsDirectory(PathBuf),
|
TargetIsDirectory(PathBuf),
|
||||||
SomeLinksFailed,
|
SomeLinksFailed,
|
||||||
FailedToLink(String),
|
FailedToLink(String),
|
||||||
|
SameFile(PathBuf, PathBuf),
|
||||||
MissingDestination(PathBuf),
|
MissingDestination(PathBuf),
|
||||||
ExtraOperand(OsString),
|
ExtraOperand(OsString),
|
||||||
}
|
}
|
||||||
|
@ -63,6 +64,12 @@ impl Display for LnError {
|
||||||
match self {
|
match self {
|
||||||
Self::TargetIsDirectory(s) => write!(f, "target {} is not a directory", s.quote()),
|
Self::TargetIsDirectory(s) => write!(f, "target {} is not a directory", s.quote()),
|
||||||
Self::FailedToLink(e) => write!(f, "failed to link: {}", e),
|
Self::FailedToLink(e) => write!(f, "failed to link: {}", e),
|
||||||
|
Self::SameFile(e, e2) => write!(
|
||||||
|
f,
|
||||||
|
"'{}' and '{}' are the same file",
|
||||||
|
e2.display(),
|
||||||
|
e.display()
|
||||||
|
),
|
||||||
Self::SomeLinksFailed => write!(f, "some links failed to create"),
|
Self::SomeLinksFailed => write!(f, "some links failed to create"),
|
||||||
Self::MissingDestination(s) => {
|
Self::MissingDestination(s) => {
|
||||||
write!(f, "missing destination file operand after {}", s.quote())
|
write!(f, "missing destination file operand after {}", s.quote())
|
||||||
|
@ -85,6 +92,7 @@ impl UError for LnError {
|
||||||
Self::TargetIsDirectory(_)
|
Self::TargetIsDirectory(_)
|
||||||
| Self::SomeLinksFailed
|
| Self::SomeLinksFailed
|
||||||
| Self::FailedToLink(_)
|
| Self::FailedToLink(_)
|
||||||
|
| Self::SameFile(_, _)
|
||||||
| Self::MissingDestination(_)
|
| Self::MissingDestination(_)
|
||||||
| Self::ExtraOperand(_) => 1,
|
| Self::ExtraOperand(_) => 1,
|
||||||
}
|
}
|
||||||
|
@ -381,7 +389,7 @@ fn relative_path<'a>(src: &Path, dst: &Path) -> Result<Cow<'a, Path>> {
|
||||||
Ok(result.into())
|
Ok(result.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn link(src: &Path, dst: &Path, settings: &Settings) -> Result<()> {
|
fn link(src: &Path, dst: &Path, settings: &Settings) -> UResult<()> {
|
||||||
let mut backup_path = None;
|
let mut backup_path = None;
|
||||||
let source: Cow<'_, Path> = if settings.relative {
|
let source: Cow<'_, Path> = if settings.relative {
|
||||||
relative_path(src, dst)?
|
relative_path(src, dst)?
|
||||||
|
@ -408,6 +416,18 @@ fn link(src: &Path, dst: &Path, settings: &Settings) -> Result<()> {
|
||||||
BackupMode::NumberedBackup => Some(numbered_backup_path(dst)),
|
BackupMode::NumberedBackup => Some(numbered_backup_path(dst)),
|
||||||
BackupMode::ExistingBackup => Some(existing_backup_path(dst, &settings.suffix)),
|
BackupMode::ExistingBackup => Some(existing_backup_path(dst, &settings.suffix)),
|
||||||
};
|
};
|
||||||
|
if settings.backup == BackupMode::ExistingBackup && !settings.symbolic {
|
||||||
|
// when ln --backup f f, it should detect that it is the same file
|
||||||
|
let dst_abs = canonicalize(dst, MissingHandling::Normal, ResolveMode::Logical)?;
|
||||||
|
let source_abs = canonicalize(
|
||||||
|
source.clone(),
|
||||||
|
MissingHandling::Normal,
|
||||||
|
ResolveMode::Logical,
|
||||||
|
)?;
|
||||||
|
if dst_abs == source_abs {
|
||||||
|
return Err(LnError::SameFile(dst.to_path_buf(), source.to_path_buf()).into());
|
||||||
|
}
|
||||||
|
}
|
||||||
if let Some(ref p) = backup_path {
|
if let Some(ref p) = backup_path {
|
||||||
fs::rename(dst, p)?;
|
fs::rename(dst, p)?;
|
||||||
}
|
}
|
||||||
|
|
|
@ -615,3 +615,12 @@ fn test_relative_recursive() {
|
||||||
ucmd.args(&["-sr", "dir", "dir/recursive"]).succeeds();
|
ucmd.args(&["-sr", "dir", "dir/recursive"]).succeeds();
|
||||||
assert_eq!(at.resolve_link("dir/recursive"), ".");
|
assert_eq!(at.resolve_link("dir/recursive"), ".");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_backup_same_file() {
|
||||||
|
let (at, mut ucmd) = at_and_ucmd!();
|
||||||
|
at.touch("file1");
|
||||||
|
ucmd.args(&["--backup", "file1", "./file1"])
|
||||||
|
.fails()
|
||||||
|
.stderr_contains("'file1' and './file1' are the same file");
|
||||||
|
}
|
||||||
|
|
|
@ -158,3 +158,6 @@ sed -i -e "s~ sed -n \"1s/'\\\/'/'OPT'/p\" < err >> pat || framework_failure_~
|
||||||
# And change the default error code to 2
|
# And change the default error code to 2
|
||||||
# see issue #3331
|
# see issue #3331
|
||||||
sed -i -e "s/rcexp=1$/rcexp=2\n case \"\$prg\" in chcon|dir|runcon|vdir) return;; esac/" tests/misc/usage_vs_getopt.sh
|
sed -i -e "s/rcexp=1$/rcexp=2\n case \"\$prg\" in chcon|dir|runcon|vdir) return;; esac/" tests/misc/usage_vs_getopt.sh
|
||||||
|
|
||||||
|
# Update the GNU error message to match ours
|
||||||
|
sed -i -e "s/ln: 'f' and 'f' are the same file/ln: failed to link: 'f' and 'f' are the same file/g" tests/ln/hard-backup.sh
|
||||||
|
|
Loading…
Reference in a new issue