1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-09-16 03:36:18 +00:00

Realpath symlinks handling, solves issue #3669 (#3703)

This commit is contained in:
Niyaz Nigmatullin 2022-07-10 17:49:25 +03:00 committed by GitHub
parent 392ae87a9f
commit 9d285e953d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 243 additions and 218 deletions

View file

@ -1492,13 +1492,12 @@ fn test_copy_through_just_created_symlink() {
at.mkdir("a");
at.mkdir("b");
at.mkdir("c");
#[cfg(unix)]
fs::symlink("../t", at.plus("a/1")).unwrap();
#[cfg(target_os = "windows")]
symlink_file("../t", at.plus("a/1")).unwrap();
at.relative_symlink_file("../t", "a/1");
at.touch("b/1");
at.write("b/1", "hello");
if create_t {
at.touch("t");
at.write("t", "world");
}
ucmd.arg("--no-dereference")
.arg("a/1")
@ -1510,6 +1509,9 @@ fn test_copy_through_just_created_symlink() {
} else {
"cp: will not copy 'b/1' through just-created symlink 'c\\1'"
});
if create_t {
assert_eq!(at.read("a/1"), "world");
}
}
}
@ -1536,6 +1538,16 @@ fn test_copy_through_dangling_symlink_no_dereference() {
.no_stdout();
}
#[test]
fn test_copy_through_dangling_symlink_no_dereference_2() {
let (at, mut ucmd) = at_and_ucmd!();
at.touch("file");
at.symlink_file("nonexistent", "target");
ucmd.args(&["-P", "file", "target"])
.fails()
.stderr_only("cp: not writing through dangling symlink 'target'");
}
#[test]
#[cfg(unix)]
fn test_cp_archive_on_nonexistent_file() {
@ -1658,3 +1670,52 @@ fn test_cp_overriding_arguments() {
s.fixtures.remove("file2");
}
}
#[test]
fn test_copy_no_dereference_1() {
let (at, mut ucmd) = at_and_ucmd!();
at.mkdir("a");
at.mkdir("b");
at.touch("a/foo");
at.write("a/foo", "bar");
at.relative_symlink_file("../a/foo", "b/foo");
ucmd.args(&["-P", "a/foo", "b"]).fails();
}
#[test]
fn test_abuse_existing() {
let (at, mut ucmd) = at_and_ucmd!();
at.mkdir("a");
at.mkdir("b");
at.mkdir("c");
at.relative_symlink_file("../t", "a/1");
at.touch("b/1");
at.write("b/1", "hello");
at.relative_symlink_file("../t", "c/1");
at.touch("t");
at.write("t", "i");
ucmd.args(&["-dR", "a/1", "b/1", "c"])
.fails()
.stderr_contains(format!(
"will not copy 'b/1' through just-created symlink 'c{}1'",
if cfg!(windows) { "\\" } else { "/" }
));
assert_eq!(at.read("t"), "i");
}
#[test]
fn test_copy_same_symlink_no_dereference() {
let (at, mut ucmd) = at_and_ucmd!();
at.relative_symlink_file("t", "a");
at.relative_symlink_file("t", "b");
at.touch("t");
ucmd.args(&["-d", "a", "b"]).succeeds();
}
#[test]
fn test_copy_same_symlink_no_dereference_dangling() {
let (at, mut ucmd) = at_and_ucmd!();
at.relative_symlink_file("t", "a");
at.relative_symlink_file("t", "b");
ucmd.args(&["-d", "a", "b"]).succeeds();
}

View file

@ -1,6 +1,6 @@
use crate::common::util::*;
use std::path::Path;
use std::path::{Path, MAIN_SEPARATOR};
static GIBBERISH: &str = "supercalifragilisticexpialidocious";
@ -155,8 +155,8 @@ fn test_realpath_dangling() {
let (at, mut ucmd) = at_and_ucmd!();
at.symlink_file("nonexistent-file", "link");
ucmd.arg("link")
.fails()
.stderr_contains("realpath: link: No such file or directory");
.succeeds()
.stdout_contains(at.plus_as_string("nonexistent-file\n"));
}
#[test]
@ -166,8 +166,8 @@ fn test_realpath_loop() {
at.symlink_file("3", "2");
at.symlink_file("1", "3");
ucmd.arg("1")
.succeeds()
.stdout_only(at.plus_as_string("2\n"));
.fails()
.stderr_contains("Too many levels of symbolic links");
}
#[test]
@ -241,7 +241,6 @@ fn test_realpath_when_symlink_is_absolute_and_enoent() {
}
#[test]
#[ignore = "issue #3669"]
fn test_realpath_when_symlink_part_is_missing() {
let (at, mut ucmd) = at_and_ucmd!();
@ -254,10 +253,13 @@ fn test_realpath_when_symlink_part_is_missing() {
at.relative_symlink_file("../dir2/baz", "dir1/foo3");
at.symlink_file("dir3/bar", "dir1/foo4");
let expect1 = format!("dir2{}bar", MAIN_SEPARATOR);
let expect2 = format!("dir2{}baz", MAIN_SEPARATOR);
ucmd.args(&["dir1/foo1", "dir1/foo2", "dir1/foo3", "dir1/foo4"])
.run()
.stdout_contains(at.plus_as_string("dir2/bar") + "\n")
.stdout_contains(at.plus_as_string("dir2/baz") + "\n")
.stdout_contains(expect1 + "\n")
.stdout_contains(expect2 + "\n")
.stderr_contains("realpath: dir1/foo2: No such file or directory\n")
.stderr_contains("realpath: dir1/foo4: No such file or directory\n");
}

View file

@ -22,6 +22,8 @@ use std::io::{Read, Result, Write};
use std::os::unix::fs::{symlink as symlink_dir, symlink as symlink_file};
#[cfg(windows)]
use std::os::windows::fs::{symlink_dir, symlink_file};
#[cfg(windows)]
use std::path::MAIN_SEPARATOR;
use std::path::{Path, PathBuf};
use std::process::{Child, Command, Stdio};
use std::rc::Rc;
@ -702,11 +704,13 @@ impl AtPath {
}
pub fn relative_symlink_file(&self, original: &str, link: &str) {
#[cfg(windows)]
let original = original.replace('/', &MAIN_SEPARATOR.to_string());
log_info(
"symlink",
&format!("{},{}", original, &self.plus_as_string(link)),
&format!("{},{}", &original, &self.plus_as_string(link)),
);
symlink_file(original, &self.plus(link)).unwrap();
symlink_file(&original, &self.plus(link)).unwrap();
}
pub fn symlink_dir(&self, original: &str, link: &str) {
@ -722,11 +726,13 @@ impl AtPath {
}
pub fn relative_symlink_dir(&self, original: &str, link: &str) {
#[cfg(windows)]
let original = original.replace('/', &MAIN_SEPARATOR.to_string());
log_info(
"symlink",
&format!("{},{}", original, &self.plus_as_string(link)),
&format!("{},{}", &original, &self.plus_as_string(link)),
);
symlink_dir(original, &self.plus(link)).unwrap();
symlink_dir(&original, &self.plus(link)).unwrap();
}
pub fn is_symlink(&self, path: &str) -> bool {