| 1 |
// This file is part of the uutils coreutils package. |
| 2 |
// |
| 3 |
// For the full copyright and license information, please view the LICENSE |
| 4 |
// file that was distributed with this source code. |
| 5 |
|
| 6 |
// spell-checker:ignore (flags) reflink (fs) tmpfs (linux) filefrag rlimit Rlim NOFILE clob btrfs neve ROOTDIR USERDIR outfile subvolume uufs xattrs ELOOP |
| 7 |
// spell-checker:ignore bdfl hlsl IRWXO IRWXG nconfined matchpathcon libselinux-devel prwx doesnotexist reftests subdirs mksocket srwx dstlink mcstransd |
| 8 |
#[cfg(unix)] |
| 9 |
use rstest::rstest; |
| 10 |
use uucore::display::Quotable; |
| 11 |
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))] |
| 12 |
use uucore::selinux::get_getfattr_output; |
| 13 |
use uutests::util::TestScenario; |
| 14 |
use uutests::{at_and_ucmd, new_ucmd, path_concat, util_name}; |
| 15 |
|
| 16 |
use std::fs::set_permissions; |
| 17 |
|
| 18 |
use std::io::Write; |
| 19 |
#[cfg(not(windows))] |
| 20 |
use std::os::unix::fs; |
| 21 |
|
| 22 |
#[cfg(unix)] |
| 23 |
use std::os::unix::fs::PermissionsExt; |
| 24 |
#[cfg(unix)] |
| 25 |
use std::os::unix::fs::{FileTypeExt, MetadataExt}; |
| 26 |
#[cfg(windows)] |
| 27 |
use std::os::windows::fs::symlink_file; |
| 28 |
use std::path::Path; |
| 29 |
#[cfg(target_os = "linux")] |
| 30 |
use std::path::PathBuf; |
| 31 |
|
| 32 |
#[cfg(target_os = "linux")] |
| 33 |
use std::ffi::OsString; |
| 34 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 35 |
use std::fs as std_fs; |
| 36 |
use std::thread::sleep; |
| 37 |
use std::time::Duration; |
| 38 |
|
| 39 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 40 |
#[cfg(feature = "truncate")] |
| 41 |
use uutests::util::PATH; |
| 42 |
|
| 43 |
static TEST_EXISTING_FILE: &str = "existing_file.txt"; |
| 44 |
static TEST_HELLO_WORLD_SOURCE: &str = "hello_world.txt"; |
| 45 |
static TEST_HELLO_WORLD_SOURCE_SYMLINK: &str = "hello_world.txt.link"; |
| 46 |
static TEST_HELLO_WORLD_DEST: &str = "copy_of_hello_world.txt"; |
| 47 |
static TEST_HELLO_WORLD_DEST_SYMLINK: &str = "copy_of_hello_world.txt.link"; |
| 48 |
static TEST_HOW_ARE_YOU_SOURCE: &str = "how_are_you.txt"; |
| 49 |
static TEST_HOW_ARE_YOU_DEST: &str = "hello_dir/how_are_you.txt"; |
| 50 |
static TEST_COPY_TO_FOLDER: &str = "hello_dir/"; |
| 51 |
static TEST_COPY_TO_FOLDER_FILE: &str = "hello_dir/hello_world.txt"; |
| 52 |
static TEST_COPY_FROM_FOLDER: &str = "hello_dir_with_file/"; |
| 53 |
static TEST_COPY_FROM_FOLDER_FILE: &str = "hello_dir_with_file/hello_world.txt"; |
| 54 |
static TEST_COPY_TO_FOLDER_NEW: &str = "hello_dir_new"; |
| 55 |
static TEST_COPY_TO_FOLDER_NEW_FILE: &str = "hello_dir_new/hello_world.txt"; |
| 56 |
#[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd"))] |
| 57 |
static TEST_MOUNT_COPY_FROM_FOLDER: &str = "dir_with_mount"; |
| 58 |
#[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd"))] |
| 59 |
static TEST_MOUNT_MOUNTPOINT: &str = "mount"; |
| 60 |
#[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd"))] |
| 61 |
static TEST_MOUNT_OTHER_FILESYSTEM_FILE: &str = "mount/DO_NOT_copy_me.txt"; |
| 62 |
static TEST_NONEXISTENT_FILE: &str = "nonexistent_file.txt"; |
| 63 |
#[cfg(all( |
| 64 |
unix, |
| 65 |
not(any(target_vendor = "apple", target_os = "android", target_os = "openbsd")) |
| 66 |
))] |
| 67 |
use uutests::util::compare_xattrs; |
| 68 |
|
| 69 |
/// Assert that mode, ownership, and permissions of two metadata objects match. |
| 70 |
#[cfg(all(not(windows), not(target_os = "freebsd"), not(target_os = "openbsd")))] |
| 71 |
macro_rules! assert_metadata_eq { |
| 72 |
($m1:expr, $m2:expr) => {{ |
| 73 |
assert_eq!($m1.mode(), $m2.mode(), "mode is different"); |
| 74 |
assert_eq!($m1.uid(), $m2.uid(), "uid is different"); |
| 75 |
assert_eq!($m1.atime(), $m2.atime(), "atime is different"); |
| 76 |
assert_eq!( |
| 77 |
$m1.atime_nsec(), |
| 78 |
$m2.atime_nsec(), |
| 79 |
"atime_nsec is different" |
| 80 |
); |
| 81 |
assert_eq!($m1.mtime(), $m2.mtime(), "mtime is different"); |
| 82 |
assert_eq!( |
| 83 |
$m1.mtime_nsec(), |
| 84 |
$m2.mtime_nsec(), |
| 85 |
"mtime_nsec is different" |
| 86 |
); |
| 87 |
}}; |
| 88 |
} |
| 89 |
|
| 90 |
#[test] |
| 91 |
#[cfg(target_os = "linux")] |
| 92 |
#[cfg_attr( |
| 93 |
wasi_runner, |
| 94 |
ignore = "WASI sandbox: host paths (/dev, /private) not visible" |
| 95 |
)] |
| 96 |
fn test_cp_stream_to_full() { |
| 97 |
let (_, mut ucmd) = at_and_ucmd!(); |
| 98 |
ucmd.arg("/dev/zero") |
| 99 |
.arg("/dev/full") |
| 100 |
.fails() |
| 101 |
.stderr_contains("No space"); |
| 102 |
} |
| 103 |
|
| 104 |
#[test] |
| 105 |
#[cfg(target_os = "linux")] |
| 106 |
fn test_cp_verbose_write_error_is_reported() { |
| 107 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 108 |
at.touch("source_file"); |
| 109 |
ucmd.arg("--verbose") |
| 110 |
.arg("source_file") |
| 111 |
.arg("dest_file") |
| 112 |
.set_stdout(std::fs::File::create("/dev/full").unwrap()) |
| 113 |
.fails() |
| 114 |
.stderr_is("cp: write error: No space left on device\n"); |
| 115 |
|
| 116 |
// The copy itself should still succeed even though reporting it on |
| 117 |
// stdout failed. |
| 118 |
assert!(at.file_exists("dest_file")); |
| 119 |
} |
| 120 |
|
| 121 |
#[test] |
| 122 |
fn test_cp_cp() { |
| 123 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 124 |
// Invoke our binary to make the copy. |
| 125 |
ucmd.arg(TEST_HELLO_WORLD_SOURCE) |
| 126 |
.arg(TEST_HELLO_WORLD_DEST) |
| 127 |
.succeeds(); |
| 128 |
|
| 129 |
// Check the content of the destination file that was copied. |
| 130 |
assert_eq!(at.read(TEST_HELLO_WORLD_DEST), "Hello, World!\n"); |
| 131 |
} |
| 132 |
|
| 133 |
#[test] |
| 134 |
fn test_cp_existing_target() { |
| 135 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 136 |
ucmd.arg(TEST_HELLO_WORLD_SOURCE) |
| 137 |
.arg(TEST_EXISTING_FILE) |
| 138 |
.succeeds(); |
| 139 |
|
| 140 |
// Check the content of the destination file |
| 141 |
assert_eq!(at.read(TEST_EXISTING_FILE), "Hello, World!\n"); |
| 142 |
|
| 143 |
// No backup should have been created |
| 144 |
assert!(!at.file_exists(format!("{TEST_EXISTING_FILE}~"))); |
| 145 |
} |
| 146 |
|
| 147 |
#[test] |
| 148 |
fn test_cp_duplicate_files() { |
| 149 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 150 |
ucmd.arg(TEST_HELLO_WORLD_SOURCE) |
| 151 |
.arg(TEST_HELLO_WORLD_SOURCE) |
| 152 |
.arg(TEST_COPY_TO_FOLDER) |
| 153 |
.succeeds() |
| 154 |
.stderr_contains(format!( |
| 155 |
"source file '{TEST_HELLO_WORLD_SOURCE}' specified more than once" |
| 156 |
)); |
| 157 |
assert_eq!(at.read(TEST_COPY_TO_FOLDER_FILE), "Hello, World!\n"); |
| 158 |
} |
| 159 |
|
| 160 |
#[test] |
| 161 |
fn test_cp_duplicate_folder() { |
| 162 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 163 |
ucmd.arg("-r") |
| 164 |
.arg(TEST_COPY_FROM_FOLDER) |
| 165 |
.arg(TEST_COPY_FROM_FOLDER) |
| 166 |
.arg(TEST_COPY_TO_FOLDER) |
| 167 |
.succeeds() |
| 168 |
.stderr_contains(format!( |
| 169 |
"source directory '{TEST_COPY_FROM_FOLDER}' specified more than once" |
| 170 |
)); |
| 171 |
assert!(at.dir_exists(format!("{TEST_COPY_TO_FOLDER}/{TEST_COPY_FROM_FOLDER}").as_str())); |
| 172 |
} |
| 173 |
|
| 174 |
#[test] |
| 175 |
fn test_cp_duplicate_directories_merge() { |
| 176 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 177 |
|
| 178 |
// Source directory 1 |
| 179 |
at.mkdir_all("src_dir/subdir"); |
| 180 |
at.write("src_dir/subdir/file1.txt", "content1"); |
| 181 |
at.write("src_dir/subdir/file2.txt", "content2"); |
| 182 |
|
| 183 |
// Source directory 2 |
| 184 |
at.mkdir_all("src_dir2/subdir"); |
| 185 |
at.write("src_dir2/subdir/file1.txt", "content3"); |
| 186 |
|
| 187 |
// Destination |
| 188 |
at.mkdir("dest"); |
| 189 |
|
| 190 |
// Perform merge copy |
| 191 |
ucmd.arg("-r") |
| 192 |
.arg("src_dir/subdir") |
| 193 |
.arg("src_dir2/subdir") |
| 194 |
.arg("dest") |
| 195 |
.succeeds(); |
| 196 |
|
| 197 |
// Verify directory exists |
| 198 |
assert!(at.dir_exists("dest/subdir")); |
| 199 |
|
| 200 |
// file1.txt should be overwritten by src_dir2/subdir/file1.txt |
| 201 |
assert!(at.file_exists("dest/subdir/file1.txt")); |
| 202 |
assert_eq!(at.read("dest/subdir/file1.txt"), "content3"); |
| 203 |
|
| 204 |
// file2.txt should remain from first copy |
| 205 |
assert!(at.file_exists("dest/subdir/file2.txt")); |
| 206 |
assert_eq!(at.read("dest/subdir/file2.txt"), "content2"); |
| 207 |
} |
| 208 |
|
| 209 |
#[test] |
| 210 |
fn test_cp_duplicate_files_normalized_path() { |
| 211 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 212 |
ucmd.arg(TEST_HELLO_WORLD_SOURCE) |
| 213 |
.arg(format!("./{TEST_HELLO_WORLD_SOURCE}")) |
| 214 |
.arg(TEST_COPY_TO_FOLDER) |
| 215 |
.succeeds() |
| 216 |
.stderr_contains(format!( |
| 217 |
"source file './{TEST_HELLO_WORLD_SOURCE}' specified more than once" |
| 218 |
)); |
| 219 |
assert_eq!(at.read(TEST_COPY_TO_FOLDER_FILE), "Hello, World!\n"); |
| 220 |
} |
| 221 |
|
| 222 |
#[test] |
| 223 |
fn test_cp_duplicate_files_with_plain_backup() { |
| 224 |
let (_, mut ucmd) = at_and_ucmd!(); |
| 225 |
ucmd.arg(TEST_HELLO_WORLD_SOURCE) |
| 226 |
.arg(TEST_HELLO_WORLD_SOURCE) |
| 227 |
.arg(TEST_COPY_TO_FOLDER) |
| 228 |
.arg("--backup") |
| 229 |
.fails() |
| 230 |
// cp would skip duplicate src check and fail when it tries to overwrite the "just-created" file. |
| 231 |
.stderr_contains( |
| 232 |
"will not overwrite just-created 'hello_dir/hello_world.txt' with 'hello_world.txt", |
| 233 |
); |
| 234 |
} |
| 235 |
|
| 236 |
#[test] |
| 237 |
fn test_cp_duplicate_files_with_numbered_backup() { |
| 238 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 239 |
// cp would skip duplicate src check and succeeds |
| 240 |
ucmd.arg(TEST_HELLO_WORLD_SOURCE) |
| 241 |
.arg(TEST_HELLO_WORLD_SOURCE) |
| 242 |
.arg(TEST_COPY_TO_FOLDER) |
| 243 |
.arg("--backup=numbered") |
| 244 |
.succeeds(); |
| 245 |
at.file_exists(TEST_COPY_TO_FOLDER_FILE); |
| 246 |
at.file_exists(format!("{TEST_COPY_TO_FOLDER}.~1~")); |
| 247 |
} |
| 248 |
|
| 249 |
#[test] |
| 250 |
fn test_cp_same_file() { |
| 251 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 252 |
let file = "a"; |
| 253 |
|
| 254 |
at.touch(file); |
| 255 |
|
| 256 |
ucmd.arg(file) |
| 257 |
.arg(file) |
| 258 |
.fails_with_code(1) |
| 259 |
.stderr_contains(format!("'{file}' and '{file}' are the same file")); |
| 260 |
} |
| 261 |
|
| 262 |
#[test] |
| 263 |
fn test_cp_multiple_files_target_is_file() { |
| 264 |
new_ucmd!() |
| 265 |
.arg(TEST_HELLO_WORLD_SOURCE) |
| 266 |
.arg(TEST_HELLO_WORLD_SOURCE) |
| 267 |
.arg(TEST_EXISTING_FILE) |
| 268 |
.fails() |
| 269 |
.stderr_contains("not a directory"); |
| 270 |
} |
| 271 |
|
| 272 |
#[test] |
| 273 |
fn test_cp_directory_not_recursive() { |
| 274 |
new_ucmd!() |
| 275 |
.arg(TEST_COPY_TO_FOLDER) |
| 276 |
.arg(TEST_HELLO_WORLD_DEST) |
| 277 |
.fails() |
| 278 |
.stderr_is(format!( |
| 279 |
"cp: -r not specified; omitting directory '{TEST_COPY_TO_FOLDER}'\n" |
| 280 |
)); |
| 281 |
} |
| 282 |
|
| 283 |
#[test] |
| 284 |
fn test_cp_multiple_files() { |
| 285 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 286 |
ucmd.arg(TEST_HELLO_WORLD_SOURCE) |
| 287 |
.arg(TEST_HOW_ARE_YOU_SOURCE) |
| 288 |
.arg(TEST_COPY_TO_FOLDER) |
| 289 |
.succeeds(); |
| 290 |
|
| 291 |
assert_eq!(at.read(TEST_COPY_TO_FOLDER_FILE), "Hello, World!\n"); |
| 292 |
assert_eq!(at.read(TEST_HOW_ARE_YOU_DEST), "How are you?\n"); |
| 293 |
} |
| 294 |
|
| 295 |
#[test] |
| 296 |
fn test_cp_multiple_files_with_nonexistent_file() { |
| 297 |
#[cfg(windows)] |
| 298 |
let error_msg = "The system cannot find the file specified"; |
| 299 |
#[cfg(not(windows))] |
| 300 |
let error_msg = format!("'{TEST_NONEXISTENT_FILE}': No such file or directory"); |
| 301 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 302 |
ucmd.arg(TEST_HELLO_WORLD_SOURCE) |
| 303 |
.arg(TEST_NONEXISTENT_FILE) |
| 304 |
.arg(TEST_HOW_ARE_YOU_SOURCE) |
| 305 |
.arg(TEST_COPY_TO_FOLDER) |
| 306 |
.fails() |
| 307 |
.stderr_contains(error_msg); |
| 308 |
|
| 309 |
assert_eq!(at.read(TEST_COPY_TO_FOLDER_FILE), "Hello, World!\n"); |
| 310 |
assert_eq!(at.read(TEST_HOW_ARE_YOU_DEST), "How are you?\n"); |
| 311 |
} |
| 312 |
|
| 313 |
#[test] |
| 314 |
#[cfg_attr( |
| 315 |
wasi_runner, |
| 316 |
ignore = "WASI: an empty path argument resolves to a directory instead of ENOENT" |
| 317 |
)] |
| 318 |
fn test_cp_multiple_files_with_empty_file_name() { |
| 319 |
#[cfg(windows)] |
| 320 |
let error_msg = "The system cannot find the path specified"; |
| 321 |
#[cfg(not(windows))] |
| 322 |
let error_msg = "'': No such file or directory"; |
| 323 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 324 |
ucmd.arg(TEST_HELLO_WORLD_SOURCE) |
| 325 |
.arg("") |
| 326 |
.arg(TEST_HOW_ARE_YOU_SOURCE) |
| 327 |
.arg(TEST_COPY_TO_FOLDER) |
| 328 |
.fails() |
| 329 |
.stderr_contains(error_msg); |
| 330 |
|
| 331 |
assert_eq!(at.read(TEST_COPY_TO_FOLDER_FILE), "Hello, World!\n"); |
| 332 |
assert_eq!(at.read(TEST_HOW_ARE_YOU_DEST), "How are you?\n"); |
| 333 |
} |
| 334 |
|
| 335 |
#[test] |
| 336 |
// FixME: for MacOS, this has intermittent failures; track repair progress at GH:uutils/coreutils/issues/1590 |
| 337 |
#[cfg(not(target_vendor = "apple"))] |
| 338 |
fn test_cp_recurse() { |
| 339 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 340 |
ucmd.arg("-r") |
| 341 |
.arg(TEST_COPY_FROM_FOLDER) |
| 342 |
.arg(TEST_COPY_TO_FOLDER_NEW) |
| 343 |
.succeeds(); |
| 344 |
|
| 345 |
// Check the content of the destination file that was copied. |
| 346 |
assert_eq!(at.read(TEST_COPY_TO_FOLDER_NEW_FILE), "Hello, World!\n"); |
| 347 |
} |
| 348 |
|
| 349 |
#[test] |
| 350 |
#[cfg(not(target_vendor = "apple"))] |
| 351 |
fn test_cp_recurse_several() { |
| 352 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 353 |
ucmd.arg("-r") |
| 354 |
.arg("-r") |
| 355 |
.arg(TEST_COPY_FROM_FOLDER) |
| 356 |
.arg(TEST_COPY_TO_FOLDER_NEW) |
| 357 |
.succeeds(); |
| 358 |
|
| 359 |
// Check the content of the destination file that was copied. |
| 360 |
assert_eq!(at.read(TEST_COPY_TO_FOLDER_NEW_FILE), "Hello, World!\n"); |
| 361 |
} |
| 362 |
|
| 363 |
#[test] |
| 364 |
fn test_cp_recurse_source_path_ends_with_slash_dot() { |
| 365 |
let source_dir = "source_dir"; |
| 366 |
let target_dir = "target_dir"; |
| 367 |
let file = "file"; |
| 368 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 369 |
|
| 370 |
at.mkdir(source_dir); |
| 371 |
at.touch(format!("{source_dir}/{file}")); |
| 372 |
|
| 373 |
ucmd.arg("-r") |
| 374 |
.arg(format!("{source_dir}/.")) |
| 375 |
.arg(target_dir) |
| 376 |
.succeeds() |
| 377 |
.no_output(); |
| 378 |
assert!(at.file_exists(format!("{target_dir}/{file}"))); |
| 379 |
} |
| 380 |
|
| 381 |
#[test] |
| 382 |
fn test_cp_with_dirs_t() { |
| 383 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 384 |
ucmd.arg("-t") |
| 385 |
.arg(TEST_COPY_TO_FOLDER) |
| 386 |
.arg(TEST_HELLO_WORLD_SOURCE) |
| 387 |
.succeeds(); |
| 388 |
assert_eq!(at.read(TEST_COPY_TO_FOLDER_FILE), "Hello, World!\n"); |
| 389 |
} |
| 390 |
|
| 391 |
#[test] |
| 392 |
// FixME: for MacOS, this has intermittent failures; track repair progress at GH:uutils/coreutils/issues/1590 |
| 393 |
#[cfg(not(target_vendor = "apple"))] |
| 394 |
fn test_cp_with_dirs() { |
| 395 |
let scene = TestScenario::new(util_name!()); |
| 396 |
let at = &scene.fixtures; |
| 397 |
|
| 398 |
scene |
| 399 |
.ucmd() |
| 400 |
.arg(TEST_HELLO_WORLD_SOURCE) |
| 401 |
.arg(TEST_COPY_TO_FOLDER) |
| 402 |
.succeeds(); |
| 403 |
assert_eq!(at.read(TEST_COPY_TO_FOLDER_FILE), "Hello, World!\n"); |
| 404 |
|
| 405 |
scene |
| 406 |
.ucmd() |
| 407 |
.arg(TEST_COPY_FROM_FOLDER_FILE) |
| 408 |
.arg(TEST_HELLO_WORLD_DEST) |
| 409 |
.succeeds(); |
| 410 |
assert_eq!(at.read(TEST_HELLO_WORLD_DEST), "Hello, World!\n"); |
| 411 |
} |
| 412 |
|
| 413 |
#[test] |
| 414 |
fn test_cp_arg_target_directory() { |
| 415 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 416 |
ucmd.arg(TEST_HELLO_WORLD_SOURCE) |
| 417 |
.arg("-t") |
| 418 |
.arg(TEST_COPY_TO_FOLDER) |
| 419 |
.succeeds(); |
| 420 |
|
| 421 |
assert_eq!(at.read(TEST_COPY_TO_FOLDER_FILE), "Hello, World!\n"); |
| 422 |
} |
| 423 |
|
| 424 |
#[test] |
| 425 |
fn test_cp_arg_no_target_directory() { |
| 426 |
new_ucmd!() |
| 427 |
.arg(TEST_HELLO_WORLD_SOURCE) |
| 428 |
.arg("-v") |
| 429 |
.arg("-T") |
| 430 |
.arg(TEST_COPY_TO_FOLDER) |
| 431 |
.fails() |
| 432 |
.stderr_contains("cannot overwrite directory"); |
| 433 |
} |
| 434 |
|
| 435 |
#[test] |
| 436 |
fn test_cp_arg_no_target_directory_with_recursive() { |
| 437 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 438 |
|
| 439 |
at.mkdir("dir"); |
| 440 |
at.mkdir("dir2"); |
| 441 |
at.touch("dir/a"); |
| 442 |
at.touch("dir/b"); |
| 443 |
|
| 444 |
ucmd.arg("-rT") |
| 445 |
.arg("dir") |
| 446 |
.arg("dir2") |
| 447 |
.succeeds() |
| 448 |
.no_output(); |
| 449 |
|
| 450 |
assert!(at.plus("dir2").join("a").exists()); |
| 451 |
assert!(at.plus("dir2").join("b").exists()); |
| 452 |
assert!(!at.plus("dir2").join("dir").exists()); |
| 453 |
} |
| 454 |
|
| 455 |
#[test] |
| 456 |
fn test_cp_arg_no_target_directory_with_recursive_target_does_not_exists() { |
| 457 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 458 |
|
| 459 |
at.mkdir("dir"); |
| 460 |
at.touch("dir/a"); |
| 461 |
at.touch("dir/b"); |
| 462 |
|
| 463 |
let target = "create_me"; |
| 464 |
assert!(!at.plus(target).exists()); |
| 465 |
|
| 466 |
ucmd.arg("-rT") |
| 467 |
.arg("dir") |
| 468 |
.arg(target) |
| 469 |
.succeeds() |
| 470 |
.no_output(); |
| 471 |
|
| 472 |
assert!(at.plus(target).join("a").exists()); |
| 473 |
assert!(at.plus(target).join("b").exists()); |
| 474 |
assert!(!at.plus(target).join("dir").exists()); |
| 475 |
} |
| 476 |
|
| 477 |
#[test] |
| 478 |
fn test_cp_target_directory_is_file() { |
| 479 |
new_ucmd!() |
| 480 |
.arg("-t") |
| 481 |
.arg(TEST_HOW_ARE_YOU_SOURCE) |
| 482 |
.arg(TEST_HELLO_WORLD_SOURCE) |
| 483 |
.fails() |
| 484 |
.stderr_contains(format!("'{TEST_HOW_ARE_YOU_SOURCE}' is not a directory")); |
| 485 |
} |
| 486 |
|
| 487 |
#[test] |
| 488 |
fn test_cp_arg_update_interactive_error() { |
| 489 |
new_ucmd!() |
| 490 |
.arg(TEST_HELLO_WORLD_SOURCE) |
| 491 |
.arg(TEST_HOW_ARE_YOU_SOURCE) |
| 492 |
.arg("-i") |
| 493 |
.fails() |
| 494 |
.no_stdout(); |
| 495 |
} |
| 496 |
|
| 497 |
#[test] |
| 498 |
fn test_cp_arg_update_none() { |
| 499 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 500 |
ucmd.arg(TEST_HELLO_WORLD_SOURCE) |
| 501 |
.arg(TEST_HOW_ARE_YOU_SOURCE) |
| 502 |
.arg("--update=none") |
| 503 |
.succeeds() |
| 504 |
.no_output(); |
| 505 |
assert_eq!(at.read(TEST_HOW_ARE_YOU_SOURCE), "How are you?\n"); |
| 506 |
} |
| 507 |
|
| 508 |
#[test] |
| 509 |
fn test_cp_arg_update_none_fail() { |
| 510 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 511 |
ucmd.arg(TEST_HELLO_WORLD_SOURCE) |
| 512 |
.arg(TEST_HOW_ARE_YOU_SOURCE) |
| 513 |
.arg("--update=none-fail") |
| 514 |
.fails() |
| 515 |
.stderr_contains(format!("not replacing '{TEST_HOW_ARE_YOU_SOURCE}'")); |
| 516 |
assert_eq!(at.read(TEST_HOW_ARE_YOU_SOURCE), "How are you?\n"); |
| 517 |
} |
| 518 |
|
| 519 |
#[test] |
| 520 |
fn test_cp_arg_update_all() { |
| 521 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 522 |
|
| 523 |
ucmd.arg(TEST_HELLO_WORLD_SOURCE) |
| 524 |
.arg(TEST_HOW_ARE_YOU_SOURCE) |
| 525 |
.arg("--update=all") |
| 526 |
.succeeds() |
| 527 |
.no_output(); |
| 528 |
|
| 529 |
assert_eq!( |
| 530 |
at.read(TEST_HOW_ARE_YOU_SOURCE), |
| 531 |
at.read(TEST_HELLO_WORLD_SOURCE) |
| 532 |
); |
| 533 |
} |
| 534 |
|
| 535 |
#[test] |
| 536 |
fn test_cp_arg_update_older_dest_not_older_than_src() { |
| 537 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 538 |
|
| 539 |
let old = "test_cp_arg_update_dest_not_older_file1"; |
| 540 |
let new = "test_cp_arg_update_dest_not_older_file2"; |
| 541 |
let old_content = "old content\n"; |
| 542 |
let new_content = "new content\n"; |
| 543 |
|
| 544 |
at.write(old, old_content); |
| 545 |
at.write(new, new_content); |
| 546 |
|
| 547 |
ucmd.arg(old) |
| 548 |
.arg(new) |
| 549 |
.arg("--update=older") |
| 550 |
.succeeds() |
| 551 |
.no_output(); |
| 552 |
|
| 553 |
assert_eq!(at.read(new), "new content\n"); |
| 554 |
} |
| 555 |
|
| 556 |
#[test] |
| 557 |
fn test_cp_arg_update_older_dest_not_older_than_src_no_verbose_output() { |
| 558 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 559 |
|
| 560 |
let old = "test_cp_arg_update_dest_not_older_file1"; |
| 561 |
let new = "test_cp_arg_update_dest_not_older_file2"; |
| 562 |
let old_content = "old content\n"; |
| 563 |
let new_content = "new content\n"; |
| 564 |
|
| 565 |
at.write(old, old_content); |
| 566 |
at.write(new, new_content); |
| 567 |
|
| 568 |
ucmd.arg(old) |
| 569 |
.arg(new) |
| 570 |
.arg("--verbose") |
| 571 |
.arg("--update=older") |
| 572 |
.succeeds() |
| 573 |
.no_output(); |
| 574 |
|
| 575 |
assert_eq!(at.read(new), "new content\n"); |
| 576 |
} |
| 577 |
|
| 578 |
#[test] |
| 579 |
fn test_cp_arg_update_older_dest_older_than_src() { |
| 580 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 581 |
|
| 582 |
let old = "test_cp_arg_update_dest_older_file1"; |
| 583 |
let new = "test_cp_arg_update_dest_older_file2"; |
| 584 |
let old_content = "old content\n"; |
| 585 |
let new_content = "new content\n"; |
| 586 |
|
| 587 |
let mut f = at.make_file(old); |
| 588 |
f.write_all(old_content.as_bytes()).unwrap(); |
| 589 |
f.set_modified(std::time::UNIX_EPOCH).unwrap(); |
| 590 |
|
| 591 |
at.write(new, new_content); |
| 592 |
|
| 593 |
ucmd.arg(new) |
| 594 |
.arg(old) |
| 595 |
.arg("--update=older") |
| 596 |
.succeeds() |
| 597 |
.no_output(); |
| 598 |
|
| 599 |
assert_eq!(at.read(old), "new content\n"); |
| 600 |
} |
| 601 |
|
| 602 |
#[test] |
| 603 |
fn test_cp_arg_update_older_dest_older_than_src_with_verbose_output() { |
| 604 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 605 |
|
| 606 |
let old = "test_cp_arg_update_dest_older_file1"; |
| 607 |
let new = "test_cp_arg_update_dest_older_file2"; |
| 608 |
let old_content = "old content\n"; |
| 609 |
let new_content = "new content\n"; |
| 610 |
|
| 611 |
let mut f = at.make_file(old); |
| 612 |
f.write_all(old_content.as_bytes()).unwrap(); |
| 613 |
f.set_modified(std::time::UNIX_EPOCH).unwrap(); |
| 614 |
|
| 615 |
at.write(new, new_content); |
| 616 |
|
| 617 |
ucmd.arg(new) |
| 618 |
.arg(old) |
| 619 |
.arg("--verbose") |
| 620 |
.arg("--update=older") |
| 621 |
.succeeds() |
| 622 |
.no_stderr() |
| 623 |
.stdout_is(format!("'{new}' -> '{old}'\n")); |
| 624 |
|
| 625 |
assert_eq!(at.read(old), "new content\n"); |
| 626 |
} |
| 627 |
|
| 628 |
#[test] |
| 629 |
fn test_cp_arg_update_short_no_overwrite() { |
| 630 |
// same as --update=older |
| 631 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 632 |
|
| 633 |
let old = "test_cp_arg_update_short_no_overwrite_file1"; |
| 634 |
let new = "test_cp_arg_update_short_no_overwrite_file2"; |
| 635 |
let old_content = "old content\n"; |
| 636 |
let new_content = "new content\n"; |
| 637 |
|
| 638 |
let mut f = at.make_file(old); |
| 639 |
f.write_all(old_content.as_bytes()).unwrap(); |
| 640 |
f.set_modified(std::time::UNIX_EPOCH).unwrap(); |
| 641 |
|
| 642 |
at.write(new, new_content); |
| 643 |
|
| 644 |
ucmd.arg(old).arg(new).arg("-u").succeeds().no_output(); |
| 645 |
|
| 646 |
assert_eq!(at.read(new), "new content\n"); |
| 647 |
} |
| 648 |
|
| 649 |
#[test] |
| 650 |
fn test_cp_arg_update_short_overwrite() { |
| 651 |
// same as --update=older |
| 652 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 653 |
|
| 654 |
let old = "test_cp_arg_update_short_overwrite_file1"; |
| 655 |
let new = "test_cp_arg_update_short_overwrite_file2"; |
| 656 |
let old_content = "old content\n"; |
| 657 |
let new_content = "new content\n"; |
| 658 |
|
| 659 |
let mut f = at.make_file(old); |
| 660 |
f.write_all(old_content.as_bytes()).unwrap(); |
| 661 |
f.set_modified(std::time::UNIX_EPOCH).unwrap(); |
| 662 |
|
| 663 |
at.write(new, new_content); |
| 664 |
|
| 665 |
ucmd.arg(new).arg(old).arg("-u").succeeds().no_output(); |
| 666 |
|
| 667 |
assert_eq!(at.read(old), "new content\n"); |
| 668 |
} |
| 669 |
|
| 670 |
#[test] |
| 671 |
fn test_cp_arg_update_none_then_all() { |
| 672 |
// take last if multiple update args are supplied, |
| 673 |
// update=all wins in this case |
| 674 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 675 |
|
| 676 |
let old = "test_cp_arg_update_none_then_all_file1"; |
| 677 |
let new = "test_cp_arg_update_none_then_all_file2"; |
| 678 |
let old_content = "old content\n"; |
| 679 |
let new_content = "new content\n"; |
| 680 |
|
| 681 |
let mut f = at.make_file(old); |
| 682 |
f.write_all(old_content.as_bytes()).unwrap(); |
| 683 |
f.set_modified(std::time::UNIX_EPOCH).unwrap(); |
| 684 |
|
| 685 |
at.write(new, new_content); |
| 686 |
|
| 687 |
ucmd.arg(old) |
| 688 |
.arg(new) |
| 689 |
.arg("--update=none") |
| 690 |
.arg("--update=all") |
| 691 |
.succeeds() |
| 692 |
.no_output(); |
| 693 |
|
| 694 |
assert_eq!(at.read(new), "old content\n"); |
| 695 |
} |
| 696 |
|
| 697 |
#[test] |
| 698 |
fn test_cp_arg_update_all_then_none() { |
| 699 |
// take last if multiple update args are supplied, |
| 700 |
// update=none wins in this case |
| 701 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 702 |
|
| 703 |
let old = "test_cp_arg_update_all_then_none_file1"; |
| 704 |
let new = "test_cp_arg_update_all_then_none_file2"; |
| 705 |
let old_content = "old content\n"; |
| 706 |
let new_content = "new content\n"; |
| 707 |
|
| 708 |
let mut f = at.make_file(old); |
| 709 |
f.write_all(old_content.as_bytes()).unwrap(); |
| 710 |
f.set_modified(std::time::UNIX_EPOCH).unwrap(); |
| 711 |
|
| 712 |
at.write(new, new_content); |
| 713 |
|
| 714 |
ucmd.arg(old) |
| 715 |
.arg(new) |
| 716 |
.arg("--update=all") |
| 717 |
.arg("--update=none") |
| 718 |
.succeeds() |
| 719 |
.no_output(); |
| 720 |
|
| 721 |
assert_eq!(at.read(new), "new content\n"); |
| 722 |
} |
| 723 |
|
| 724 |
#[test] |
| 725 |
fn test_cp_arg_interactive() { |
| 726 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 727 |
at.touch("a"); |
| 728 |
at.touch("b"); |
| 729 |
ucmd.args(&["-i", "a", "b"]) |
| 730 |
.pipe_in("N\n") |
| 731 |
.fails() |
| 732 |
.no_stdout() |
| 733 |
.stderr_is("cp: overwrite 'b'? "); |
| 734 |
} |
| 735 |
|
| 736 |
#[test] |
| 737 |
#[cfg(not(any(target_os = "android", target_os = "freebsd", target_os = "openbsd")))] |
| 738 |
fn test_cp_arg_interactive_update_overwrite_newer() { |
| 739 |
// -u -i won't show the prompt to validate the override or not |
| 740 |
// Therefore, the error code will be 0 |
| 741 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 742 |
at.touch("a"); |
| 743 |
at.touch("b"); |
| 744 |
ucmd.args(&["-i", "-u", "a", "b"]) |
| 745 |
.pipe_in("") |
| 746 |
.succeeds() |
| 747 |
.no_stdout(); |
| 748 |
// Make extra sure that closing stdin behaves identically to piping-in nothing. |
| 749 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 750 |
at.touch("a"); |
| 751 |
at.touch("b"); |
| 752 |
ucmd.args(&["-i", "-u", "a", "b"]).succeeds().no_stdout(); |
| 753 |
} |
| 754 |
|
| 755 |
#[test] |
| 756 |
#[cfg(not(any(target_os = "android", target_os = "freebsd")))] |
| 757 |
fn test_cp_arg_interactive_update_overwrite_older() { |
| 758 |
// -u -i *WILL* show the prompt to validate the override. |
| 759 |
// Therefore, the error code depends on the prompt response. |
| 760 |
// Option N |
| 761 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 762 |
at.touch("b"); |
| 763 |
sleep(Duration::from_millis(100)); |
| 764 |
at.touch("a"); |
| 765 |
ucmd.args(&["-i", "-u", "a", "b"]) |
| 766 |
.pipe_in("N\n") |
| 767 |
.fails_with_code(1) |
| 768 |
.no_stdout() |
| 769 |
.stderr_is("cp: overwrite 'b'? "); |
| 770 |
|
| 771 |
// Option Y |
| 772 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 773 |
at.touch("b"); |
| 774 |
sleep(Duration::from_millis(100)); |
| 775 |
at.touch("a"); |
| 776 |
ucmd.args(&["-i", "-u", "a", "b"]) |
| 777 |
.pipe_in("Y\n") |
| 778 |
.succeeds() |
| 779 |
.no_stdout(); |
| 780 |
} |
| 781 |
|
| 782 |
#[test] |
| 783 |
#[cfg(not(target_os = "android"))] |
| 784 |
fn test_cp_arg_interactive_verbose() { |
| 785 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 786 |
at.touch("a"); |
| 787 |
at.touch("b"); |
| 788 |
ucmd.args(&["-vi", "a", "b"]) |
| 789 |
.pipe_in("N\n") |
| 790 |
.fails() |
| 791 |
.stderr_is("cp: overwrite 'b'? ") |
| 792 |
.no_stdout(); |
| 793 |
} |
| 794 |
|
| 795 |
#[test] |
| 796 |
#[cfg(not(target_os = "android"))] |
| 797 |
fn test_cp_arg_interactive_verbose_clobber() { |
| 798 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 799 |
at.touch("a"); |
| 800 |
at.touch("b"); |
| 801 |
ucmd.args(&["-vin", "--debug", "a", "b"]) |
| 802 |
.succeeds() |
| 803 |
.stdout_contains("skipped 'b'"); |
| 804 |
} |
| 805 |
|
| 806 |
#[test] |
| 807 |
#[cfg(unix)] |
| 808 |
#[cfg_attr( |
| 809 |
wasi_runner, |
| 810 |
ignore = "WASI: no chmod syscall, so required mode/ownership preservation always fails" |
| 811 |
)] |
| 812 |
fn test_cp_f_i_verbose_non_writeable_destination_y() { |
| 813 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 814 |
|
| 815 |
at.touch("a"); |
| 816 |
at.touch("b"); |
| 817 |
|
| 818 |
// Non-writeable file |
| 819 |
at.set_mode("b", 0o0000); |
| 820 |
|
| 821 |
ucmd.args(&["-f", "-i", "--verbose", "a", "b"]) |
| 822 |
.pipe_in("y") |
| 823 |
.succeeds() |
| 824 |
.stderr_is("cp: replace 'b', overriding mode 0000 (---------)? ") |
| 825 |
.stdout_is("removed 'b'\n'a' -> 'b'\n"); |
| 826 |
} |
| 827 |
|
| 828 |
#[test] |
| 829 |
#[cfg(unix)] |
| 830 |
#[cfg_attr( |
| 831 |
wasi_runner, |
| 832 |
ignore = "WASI: no chmod syscall, so required mode/ownership preservation always fails" |
| 833 |
)] |
| 834 |
fn test_cp_f_i_verbose_non_writeable_destination_empty() { |
| 835 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 836 |
|
| 837 |
at.touch("a"); |
| 838 |
at.touch("b"); |
| 839 |
|
| 840 |
// Non-writeable file |
| 841 |
at.set_mode("b", 0o0000); |
| 842 |
|
| 843 |
ucmd.args(&["-f", "-i", "--verbose", "a", "b"]) |
| 844 |
.pipe_in("") |
| 845 |
.fails() |
| 846 |
.stderr_only("cp: replace 'b', overriding mode 0000 (---------)? "); |
| 847 |
} |
| 848 |
|
| 849 |
#[test] |
| 850 |
#[cfg(target_os = "linux")] |
| 851 |
fn test_cp_arg_link() { |
| 852 |
use std::os::linux::fs::MetadataExt; |
| 853 |
|
| 854 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 855 |
ucmd.arg(TEST_HELLO_WORLD_SOURCE) |
| 856 |
.arg("--link") |
| 857 |
.arg(TEST_HELLO_WORLD_DEST) |
| 858 |
.succeeds(); |
| 859 |
|
| 860 |
assert_eq!(at.metadata(TEST_HELLO_WORLD_SOURCE).st_nlink(), 2); |
| 861 |
} |
| 862 |
|
| 863 |
#[test] |
| 864 |
#[cfg(target_os = "linux")] |
| 865 |
#[cfg_attr( |
| 866 |
wasi_runner, |
| 867 |
ignore = "WASI: same-file hard link detection not supported" |
| 868 |
)] |
| 869 |
fn test_cp_arg_link_with_dest_hardlink_to_source() { |
| 870 |
use std::os::linux::fs::MetadataExt; |
| 871 |
|
| 872 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 873 |
let file = "file"; |
| 874 |
let hardlink = "hardlink"; |
| 875 |
|
| 876 |
at.touch(file); |
| 877 |
at.hard_link(file, hardlink); |
| 878 |
|
| 879 |
ucmd.args(&["--link", file, hardlink]).succeeds(); |
| 880 |
|
| 881 |
assert_eq!(at.metadata(file).st_nlink(), 2); |
| 882 |
assert!(at.file_exists(file)); |
| 883 |
assert!(at.file_exists(hardlink)); |
| 884 |
} |
| 885 |
|
| 886 |
#[test] |
| 887 |
#[cfg(target_os = "linux")] |
| 888 |
#[cfg_attr( |
| 889 |
wasi_runner, |
| 890 |
ignore = "WASI: same-file hard link detection not supported" |
| 891 |
)] |
| 892 |
fn test_cp_arg_link_with_same_file() { |
| 893 |
use std::os::linux::fs::MetadataExt; |
| 894 |
|
| 895 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 896 |
let file = "file"; |
| 897 |
|
| 898 |
at.touch(file); |
| 899 |
|
| 900 |
ucmd.args(&["--link", file, file]).succeeds(); |
| 901 |
|
| 902 |
assert_eq!(at.metadata(file).st_nlink(), 1); |
| 903 |
assert!(at.file_exists(file)); |
| 904 |
} |
| 905 |
|
| 906 |
#[test] |
| 907 |
#[cfg(target_os = "linux")] |
| 908 |
fn test_cp_verbose_preserved_link_to_dir() { |
| 909 |
use std::os::linux::fs::MetadataExt; |
| 910 |
|
| 911 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 912 |
let file = "file"; |
| 913 |
let hardlink = "hardlink"; |
| 914 |
let dir = "dir"; |
| 915 |
let dst_file = "dir/file"; |
| 916 |
let dst_hardlink = "dir/hardlink"; |
| 917 |
|
| 918 |
at.touch(file); |
| 919 |
at.hard_link(file, hardlink); |
| 920 |
at.mkdir(dir); |
| 921 |
|
| 922 |
ucmd.args(&["-d", "--verbose", file, hardlink, dir]) |
| 923 |
.succeeds() |
| 924 |
.stdout_is("'file' -> 'dir/file'\n'hardlink' -> 'dir/hardlink'\n"); |
| 925 |
|
| 926 |
assert!(at.file_exists(dst_file)); |
| 927 |
assert!(at.file_exists(dst_hardlink)); |
| 928 |
assert_eq!(at.metadata(dst_file).st_nlink(), 2); |
| 929 |
assert_eq!(at.metadata(dst_hardlink).st_nlink(), 2); |
| 930 |
assert_eq!( |
| 931 |
at.metadata(dst_file).st_ino(), |
| 932 |
at.metadata(dst_hardlink).st_ino() |
| 933 |
); |
| 934 |
} |
| 935 |
|
| 936 |
#[test] |
| 937 |
fn test_cp_arg_symlink() { |
| 938 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 939 |
ucmd.arg(TEST_HELLO_WORLD_SOURCE) |
| 940 |
.arg("--symbolic-link") |
| 941 |
.arg(TEST_HELLO_WORLD_DEST) |
| 942 |
.succeeds(); |
| 943 |
|
| 944 |
assert!(at.is_symlink(TEST_HELLO_WORLD_DEST)); |
| 945 |
assert_eq!( |
| 946 |
std::fs::read_link(at.plus(TEST_HELLO_WORLD_DEST)).unwrap(), |
| 947 |
Path::new(TEST_HELLO_WORLD_SOURCE) |
| 948 |
); |
| 949 |
assert_eq!(at.read(TEST_HELLO_WORLD_DEST), "Hello, World!\n"); |
| 950 |
} |
| 951 |
|
| 952 |
// Recursively copying a tree that contains a symlink must not run chmod through |
| 953 |
// the destination symlink cp just created. chmod() follows symlinks, so doing so |
| 954 |
// would change the mode of the link target, which can live outside the copied |
| 955 |
// tree. GNU cp leaves the target untouched. |
| 956 |
#[test] |
| 957 |
#[cfg(unix)] |
| 958 |
#[cfg_attr( |
| 959 |
wasi_runner, |
| 960 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 961 |
)] |
| 962 |
fn test_cp_recursive_symlink_preserves_target_mode() { |
| 963 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 964 |
|
| 965 |
at.mkdir("target_dir"); |
| 966 |
at.touch("target_dir/file.txt"); |
| 967 |
at.set_mode("target_dir/file.txt", 0o600); |
| 968 |
|
| 969 |
at.mkdir("src"); |
| 970 |
at.symlink_file("target_dir/file.txt", "src/link"); |
| 971 |
|
| 972 |
ucmd.arg("-r").arg("src").arg("dst").succeeds(); |
| 973 |
|
| 974 |
assert!(at.is_symlink("dst/link")); |
| 975 |
assert_eq!( |
| 976 |
at.metadata("target_dir/file.txt").permissions().mode() & 0o777, |
| 977 |
0o600 |
| 978 |
); |
| 979 |
} |
| 980 |
|
| 981 |
// With --remove-destination onto a symlink, cp removes the link and creates a |
| 982 |
// fresh regular file, so the final chmod must not be skipped based on the |
| 983 |
// destination's pre-copy symlink state. GNU cp gives the new file the source |
| 984 |
// mode masked by the umask (664 & ~022 = 644 here); skipping the chmod leaves |
| 985 |
// it at the restrictive 0o600 creation mode (or at the raw cloned source mode |
| 986 |
// on filesystems that copy via clonefile). |
| 987 |
#[test] |
| 988 |
#[cfg(unix)] |
| 989 |
fn test_cp_remove_destination_symlink_applies_mode() { |
| 990 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 991 |
|
| 992 |
at.touch("src"); |
| 993 |
at.set_mode("src", 0o664); |
| 994 |
at.touch("target"); |
| 995 |
at.symlink_file("target", "dst"); |
| 996 |
|
| 997 |
ucmd.umask(0o022) |
| 998 |
.arg("--remove-destination") |
| 999 |
.arg("src") |
| 1000 |
.arg("dst") |
| 1001 |
.succeeds(); |
| 1002 |
|
| 1003 |
assert!(!at.is_symlink("dst")); |
| 1004 |
assert_eq!(at.metadata("dst").permissions().mode() & 0o777, 0o644); |
| 1005 |
} |
| 1006 |
|
| 1007 |
// A umask that strips the owner write bit (0o333 here) makes the freshly |
| 1008 |
// created destination read-only. The copy must keep using the fd from the |
| 1009 |
// creating open instead of re-opening the path for writing, which fails |
| 1010 |
// with EACCES and leaves an empty file behind (LP: #2164777). |
| 1011 |
#[test] |
| 1012 |
#[cfg(unix)] |
| 1013 |
fn test_cp_umask_stripping_owner_write_bit() { |
| 1014 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1015 |
|
| 1016 |
at.write("input.txt", "copied through the first fd\n"); |
| 1017 |
at.set_mode("input.txt", 0o664); |
| 1018 |
|
| 1019 |
ucmd.umask(0o333) |
| 1020 |
.arg("input.txt") |
| 1021 |
.arg("output.txt") |
| 1022 |
.succeeds() |
| 1023 |
.no_output(); |
| 1024 |
|
| 1025 |
assert_eq!(at.read("output.txt"), "copied through the first fd\n"); |
| 1026 |
assert_eq!( |
| 1027 |
at.metadata("output.txt").permissions().mode() & 0o777, |
| 1028 |
0o444 |
| 1029 |
); |
| 1030 |
} |
| 1031 |
|
| 1032 |
// Same umask scenario without a clone attempt: on reflink-capable |
| 1033 |
// filesystems the test above succeeds via FICLONE and never reaches the |
| 1034 |
// fallback, so pin the plain and sparse copy paths explicitly. |
| 1035 |
#[test] |
| 1036 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 1037 |
#[cfg_attr(wasi_runner, ignore = "WASI: --sparse is only supported on linux")] |
| 1038 |
fn test_cp_umask_stripping_owner_write_bit_reflink_never() { |
| 1039 |
for sparse in ["--sparse=auto", "--sparse=always"] { |
| 1040 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1041 |
|
| 1042 |
at.write("input.txt", "written while still writable\n"); |
| 1043 |
at.set_mode("input.txt", 0o664); |
| 1044 |
|
| 1045 |
ucmd.umask(0o333) |
| 1046 |
.args(&["--reflink=never", sparse, "input.txt", "output.txt"]) |
| 1047 |
.succeeds() |
| 1048 |
.no_output(); |
| 1049 |
|
| 1050 |
assert_eq!(at.read("output.txt"), "written while still writable\n"); |
| 1051 |
assert_eq!( |
| 1052 |
at.metadata("output.txt").permissions().mode() & 0o777, |
| 1053 |
0o444 |
| 1054 |
); |
| 1055 |
} |
| 1056 |
} |
| 1057 |
|
| 1058 |
// When --reflink=always fails, GNU cp removes a destination it created |
| 1059 |
// itself but keeps a pre-existing (truncated) one. Only observable on |
| 1060 |
// filesystems without clone support; when the clone succeeds there is |
| 1061 |
// nothing to clean up. |
| 1062 |
#[test] |
| 1063 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 1064 |
#[cfg_attr( |
| 1065 |
wasi_runner, |
| 1066 |
ignore = "WASI: --reflink is only supported on linux and macOS" |
| 1067 |
)] |
| 1068 |
fn test_cp_reflink_always_failure_dest_cleanup() { |
| 1069 |
let scene = TestScenario::new(util_name!()); |
| 1070 |
let at = &scene.fixtures; |
| 1071 |
|
| 1072 |
at.write("payload.txt", "reflink me\n"); |
| 1073 |
let fresh = scene |
| 1074 |
.ucmd() |
| 1075 |
.args(&["--reflink=always", "payload.txt", "fresh.txt"]) |
| 1076 |
.run(); |
| 1077 |
if fresh.succeeded() { |
| 1078 |
return; // clone worked, the failure path is unreachable here |
| 1079 |
} |
| 1080 |
assert!(!at.file_exists("fresh.txt")); |
| 1081 |
|
| 1082 |
at.write("kept.txt", "previous contents\n"); |
| 1083 |
scene |
| 1084 |
.ucmd() |
| 1085 |
.args(&["--reflink=always", "payload.txt", "kept.txt"]) |
| 1086 |
.fails(); |
| 1087 |
assert!(at.file_exists("kept.txt")); |
| 1088 |
assert_eq!(at.read("kept.txt"), ""); |
| 1089 |
} |
| 1090 |
|
| 1091 |
#[test] |
| 1092 |
#[cfg(target_os = "linux")] |
| 1093 |
#[cfg_attr( |
| 1094 |
wasi_runner, |
| 1095 |
ignore = "WASI sandbox: host paths (/dev, /private) not visible" |
| 1096 |
)] |
| 1097 |
fn test_cp_reflink_always_failure() { |
| 1098 |
let scene = TestScenario::new(util_name!()); |
| 1099 |
scene |
| 1100 |
.ucmd() |
| 1101 |
.args(&["--reflink=always", "/dev/null", "/dev/full"]) |
| 1102 |
.fails() |
| 1103 |
.no_stdout() |
| 1104 |
.stderr_contains("Invalid argument"); |
| 1105 |
scene |
| 1106 |
.ucmd() |
| 1107 |
.args(&["--reflink=always", "/dev/null", "target"]) |
| 1108 |
.fails() |
| 1109 |
.no_stdout() |
| 1110 |
.stderr_contains("ross-device link"); // cover both of glibc and musl |
| 1111 |
} |
| 1112 |
|
| 1113 |
#[test] |
| 1114 |
fn test_cp_arg_no_clobber() { |
| 1115 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1116 |
ucmd.arg(TEST_HELLO_WORLD_SOURCE) |
| 1117 |
.arg(TEST_HOW_ARE_YOU_SOURCE) |
| 1118 |
.arg("--no-clobber") |
| 1119 |
.arg("--debug") |
| 1120 |
.succeeds() |
| 1121 |
.stdout_contains("skipped 'how_are_you.txt'"); |
| 1122 |
|
| 1123 |
assert_eq!(at.read(TEST_HOW_ARE_YOU_SOURCE), "How are you?\n"); |
| 1124 |
} |
| 1125 |
|
| 1126 |
#[test] |
| 1127 |
fn test_cp_arg_no_clobber_inferred_arg() { |
| 1128 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1129 |
ucmd.arg(TEST_HELLO_WORLD_SOURCE) |
| 1130 |
.arg(TEST_HOW_ARE_YOU_SOURCE) |
| 1131 |
.arg("--no-clob") |
| 1132 |
.arg("--debug") |
| 1133 |
.succeeds() |
| 1134 |
.stdout_contains("skipped 'how_are_you.txt'"); |
| 1135 |
|
| 1136 |
assert_eq!(at.read(TEST_HOW_ARE_YOU_SOURCE), "How are you?\n"); |
| 1137 |
} |
| 1138 |
|
| 1139 |
#[test] |
| 1140 |
fn test_cp_arg_no_clobber_twice() { |
| 1141 |
let scene = TestScenario::new(util_name!()); |
| 1142 |
let at = &scene.fixtures; |
| 1143 |
|
| 1144 |
at.touch(TEST_HELLO_WORLD_SOURCE); |
| 1145 |
scene |
| 1146 |
.ucmd() |
| 1147 |
.arg("--no-clobber") |
| 1148 |
.arg(TEST_HELLO_WORLD_SOURCE) |
| 1149 |
.arg(TEST_HELLO_WORLD_DEST) |
| 1150 |
.arg("--debug") |
| 1151 |
.succeeds() |
| 1152 |
.no_stderr(); |
| 1153 |
|
| 1154 |
assert_eq!(at.read(TEST_HELLO_WORLD_SOURCE), ""); |
| 1155 |
|
| 1156 |
at.append(TEST_HELLO_WORLD_SOURCE, "some-content"); |
| 1157 |
scene |
| 1158 |
.ucmd() |
| 1159 |
.arg("--no-clobber") |
| 1160 |
.arg(TEST_HELLO_WORLD_SOURCE) |
| 1161 |
.arg(TEST_HELLO_WORLD_DEST) |
| 1162 |
.arg("--debug") |
| 1163 |
.succeeds() |
| 1164 |
.stdout_contains(format!("skipped '{TEST_HELLO_WORLD_DEST}'")); |
| 1165 |
|
| 1166 |
assert_eq!(at.read(TEST_HELLO_WORLD_SOURCE), "some-content"); |
| 1167 |
// Should be empty as the "no-clobber" should keep |
| 1168 |
// the previous version |
| 1169 |
assert_eq!(at.read(TEST_HELLO_WORLD_DEST), ""); |
| 1170 |
} |
| 1171 |
|
| 1172 |
#[test] |
| 1173 |
fn test_cp_arg_force() { |
| 1174 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1175 |
|
| 1176 |
// create dest without write permissions |
| 1177 |
let mut permissions = at |
| 1178 |
.make_file(TEST_HELLO_WORLD_DEST) |
| 1179 |
.metadata() |
| 1180 |
.unwrap() |
| 1181 |
.permissions(); |
| 1182 |
permissions.set_readonly(true); |
| 1183 |
set_permissions(at.plus(TEST_HELLO_WORLD_DEST), permissions).unwrap(); |
| 1184 |
|
| 1185 |
ucmd.arg(TEST_HELLO_WORLD_SOURCE) |
| 1186 |
.arg("--force") |
| 1187 |
.arg(TEST_HELLO_WORLD_DEST) |
| 1188 |
.succeeds(); |
| 1189 |
|
| 1190 |
assert_eq!(at.read(TEST_HELLO_WORLD_DEST), "Hello, World!\n"); |
| 1191 |
} |
| 1192 |
|
| 1193 |
/// TODO: write a better test that differentiates --remove-destination |
| 1194 |
/// from --force. Also this test currently doesn't work on |
| 1195 |
/// Windows. This test originally checked file timestamps, which |
| 1196 |
/// proved to be unreliable per target / CI platform |
| 1197 |
#[test] |
| 1198 |
#[cfg(not(windows))] |
| 1199 |
fn test_cp_arg_remove_destination() { |
| 1200 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1201 |
|
| 1202 |
// create dest without write permissions |
| 1203 |
let mut permissions = at |
| 1204 |
.make_file(TEST_HELLO_WORLD_DEST) |
| 1205 |
.metadata() |
| 1206 |
.unwrap() |
| 1207 |
.permissions(); |
| 1208 |
permissions.set_readonly(true); |
| 1209 |
set_permissions(at.plus(TEST_HELLO_WORLD_DEST), permissions).unwrap(); |
| 1210 |
|
| 1211 |
ucmd.arg(TEST_HELLO_WORLD_SOURCE) |
| 1212 |
.arg("--remove-destination") |
| 1213 |
.arg(TEST_HELLO_WORLD_DEST) |
| 1214 |
.succeeds(); |
| 1215 |
|
| 1216 |
assert_eq!(at.read(TEST_HELLO_WORLD_DEST), "Hello, World!\n"); |
| 1217 |
} |
| 1218 |
|
| 1219 |
#[test] |
| 1220 |
fn test_cp_arg_backup() { |
| 1221 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1222 |
|
| 1223 |
ucmd.arg(TEST_HELLO_WORLD_SOURCE) |
| 1224 |
.arg(TEST_HOW_ARE_YOU_SOURCE) |
| 1225 |
.arg("-b") |
| 1226 |
.succeeds(); |
| 1227 |
|
| 1228 |
assert_eq!(at.read(TEST_HOW_ARE_YOU_SOURCE), "Hello, World!\n"); |
| 1229 |
assert_eq!( |
| 1230 |
at.read(&format!("{TEST_HOW_ARE_YOU_SOURCE}~")), |
| 1231 |
"How are you?\n" |
| 1232 |
); |
| 1233 |
} |
| 1234 |
|
| 1235 |
#[test] |
| 1236 |
#[cfg_attr( |
| 1237 |
wasi_runner, |
| 1238 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 1239 |
)] |
| 1240 |
fn test_cp_arg_backup_with_dest_a_symlink() { |
| 1241 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1242 |
let source = "source"; |
| 1243 |
let source_content = "content"; |
| 1244 |
let symlink = "symlink"; |
| 1245 |
let original = "original"; |
| 1246 |
let backup = "symlink~"; |
| 1247 |
|
| 1248 |
at.write(source, source_content); |
| 1249 |
at.write(original, "original"); |
| 1250 |
at.symlink_file(original, symlink); |
| 1251 |
|
| 1252 |
ucmd.arg("-b").arg(source).arg(symlink).succeeds(); |
| 1253 |
|
| 1254 |
assert!(!at.symlink_exists(symlink)); |
| 1255 |
assert_eq!(source_content, at.read(symlink)); |
| 1256 |
assert!(at.symlink_exists(backup)); |
| 1257 |
assert_eq!(original, at.resolve_link(backup)); |
| 1258 |
} |
| 1259 |
|
| 1260 |
#[test] |
| 1261 |
#[cfg_attr( |
| 1262 |
wasi_runner, |
| 1263 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 1264 |
)] |
| 1265 |
fn test_cp_arg_backup_with_dest_a_symlink_to_source() { |
| 1266 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1267 |
let source = "source"; |
| 1268 |
let source_content = "content"; |
| 1269 |
let symlink = "symlink"; |
| 1270 |
let backup = "symlink~"; |
| 1271 |
|
| 1272 |
at.write(source, source_content); |
| 1273 |
at.symlink_file(source, symlink); |
| 1274 |
|
| 1275 |
ucmd.arg("-b").arg(source).arg(symlink).succeeds(); |
| 1276 |
|
| 1277 |
assert!(!at.symlink_exists(symlink)); |
| 1278 |
assert_eq!(source_content, at.read(symlink)); |
| 1279 |
assert!(at.symlink_exists(backup)); |
| 1280 |
assert_eq!(source, at.resolve_link(backup)); |
| 1281 |
} |
| 1282 |
|
| 1283 |
#[test] |
| 1284 |
fn test_cp_arg_backup_with_other_args() { |
| 1285 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1286 |
|
| 1287 |
ucmd.arg(TEST_HELLO_WORLD_SOURCE) |
| 1288 |
.arg(TEST_HOW_ARE_YOU_SOURCE) |
| 1289 |
.arg("-vbL") |
| 1290 |
.succeeds(); |
| 1291 |
|
| 1292 |
assert_eq!(at.read(TEST_HOW_ARE_YOU_SOURCE), "Hello, World!\n"); |
| 1293 |
assert_eq!( |
| 1294 |
at.read(&format!("{TEST_HOW_ARE_YOU_SOURCE}~")), |
| 1295 |
"How are you?\n" |
| 1296 |
); |
| 1297 |
} |
| 1298 |
|
| 1299 |
#[test] |
| 1300 |
fn test_cp_arg_backup_arg_first() { |
| 1301 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1302 |
|
| 1303 |
ucmd.arg("--backup") |
| 1304 |
.arg(TEST_HELLO_WORLD_SOURCE) |
| 1305 |
.arg(TEST_HOW_ARE_YOU_SOURCE) |
| 1306 |
.succeeds(); |
| 1307 |
|
| 1308 |
assert_eq!(at.read(TEST_HOW_ARE_YOU_SOURCE), "Hello, World!\n"); |
| 1309 |
assert_eq!( |
| 1310 |
at.read(&format!("{TEST_HOW_ARE_YOU_SOURCE}~")), |
| 1311 |
"How are you?\n" |
| 1312 |
); |
| 1313 |
} |
| 1314 |
|
| 1315 |
#[test] |
| 1316 |
fn test_cp_arg_suffix() { |
| 1317 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1318 |
|
| 1319 |
ucmd.arg(TEST_HELLO_WORLD_SOURCE) |
| 1320 |
.arg("-b") |
| 1321 |
.arg("--suffix") |
| 1322 |
.arg(".bak") |
| 1323 |
.arg(TEST_HOW_ARE_YOU_SOURCE) |
| 1324 |
.succeeds(); |
| 1325 |
|
| 1326 |
assert_eq!(at.read(TEST_HOW_ARE_YOU_SOURCE), "Hello, World!\n"); |
| 1327 |
assert_eq!( |
| 1328 |
at.read(&format!("{TEST_HOW_ARE_YOU_SOURCE}.bak")), |
| 1329 |
"How are you?\n" |
| 1330 |
); |
| 1331 |
} |
| 1332 |
|
| 1333 |
#[test] |
| 1334 |
fn test_cp_arg_suffix_without_backup_option() { |
| 1335 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1336 |
|
| 1337 |
ucmd.arg(TEST_HELLO_WORLD_SOURCE) |
| 1338 |
.arg("--suffix") |
| 1339 |
.arg(".bak") |
| 1340 |
.arg(TEST_HOW_ARE_YOU_SOURCE) |
| 1341 |
.succeeds(); |
| 1342 |
|
| 1343 |
assert_eq!(at.read(TEST_HOW_ARE_YOU_SOURCE), "Hello, World!\n"); |
| 1344 |
assert_eq!( |
| 1345 |
at.read(&format!("{TEST_HOW_ARE_YOU_SOURCE}.bak")), |
| 1346 |
"How are you?\n" |
| 1347 |
); |
| 1348 |
} |
| 1349 |
|
| 1350 |
#[test] |
| 1351 |
fn test_cp_empty_backup_suffix_uses_default() { |
| 1352 |
for backup_arg in ["--backup=nil", "--backup"] { |
| 1353 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1354 |
|
| 1355 |
ucmd.arg(backup_arg) |
| 1356 |
.arg("--suffix=") |
| 1357 |
.arg(TEST_HELLO_WORLD_SOURCE) |
| 1358 |
.arg(TEST_HOW_ARE_YOU_SOURCE) |
| 1359 |
.succeeds() |
| 1360 |
.no_stderr(); |
| 1361 |
|
| 1362 |
assert_eq!(at.read(TEST_HOW_ARE_YOU_SOURCE), "Hello, World!\n"); |
| 1363 |
assert_eq!( |
| 1364 |
at.read(&format!("{TEST_HOW_ARE_YOU_SOURCE}~")), |
| 1365 |
"How are you?\n" |
| 1366 |
); |
| 1367 |
} |
| 1368 |
} |
| 1369 |
|
| 1370 |
#[test] |
| 1371 |
fn test_cp_arg_suffix_hyphen_value() { |
| 1372 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1373 |
|
| 1374 |
ucmd.arg(TEST_HELLO_WORLD_SOURCE) |
| 1375 |
.arg("-b") |
| 1376 |
.arg("--suffix") |
| 1377 |
.arg("-v") |
| 1378 |
.arg(TEST_HOW_ARE_YOU_SOURCE) |
| 1379 |
.succeeds(); |
| 1380 |
|
| 1381 |
assert_eq!(at.read(TEST_HOW_ARE_YOU_SOURCE), "Hello, World!\n"); |
| 1382 |
assert_eq!( |
| 1383 |
at.read(&format!("{TEST_HOW_ARE_YOU_SOURCE}-v")), |
| 1384 |
"How are you?\n" |
| 1385 |
); |
| 1386 |
} |
| 1387 |
|
| 1388 |
#[test] |
| 1389 |
fn test_cp_custom_backup_suffix_via_env() { |
| 1390 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1391 |
let suffix = "super-suffix-of-the-century"; |
| 1392 |
|
| 1393 |
ucmd.arg("-b") |
| 1394 |
.env("SIMPLE_BACKUP_SUFFIX", suffix) |
| 1395 |
.arg(TEST_HELLO_WORLD_SOURCE) |
| 1396 |
.arg(TEST_HOW_ARE_YOU_SOURCE) |
| 1397 |
.succeeds() |
| 1398 |
.no_stderr(); |
| 1399 |
|
| 1400 |
assert_eq!(at.read(TEST_HOW_ARE_YOU_SOURCE), "Hello, World!\n"); |
| 1401 |
assert_eq!( |
| 1402 |
at.read(&format!("{TEST_HOW_ARE_YOU_SOURCE}{suffix}")), |
| 1403 |
"How are you?\n" |
| 1404 |
); |
| 1405 |
} |
| 1406 |
|
| 1407 |
#[test] |
| 1408 |
fn test_cp_backup_numbered_with_t() { |
| 1409 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1410 |
|
| 1411 |
ucmd.arg("--backup=t") |
| 1412 |
.arg(TEST_HELLO_WORLD_SOURCE) |
| 1413 |
.arg(TEST_HOW_ARE_YOU_SOURCE) |
| 1414 |
.succeeds() |
| 1415 |
.no_stderr(); |
| 1416 |
|
| 1417 |
assert_eq!(at.read(TEST_HOW_ARE_YOU_SOURCE), "Hello, World!\n"); |
| 1418 |
assert_eq!( |
| 1419 |
at.read(&format!("{TEST_HOW_ARE_YOU_SOURCE}.~1~")), |
| 1420 |
"How are you?\n" |
| 1421 |
); |
| 1422 |
} |
| 1423 |
|
| 1424 |
#[test] |
| 1425 |
fn test_cp_backup_numbered() { |
| 1426 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1427 |
|
| 1428 |
ucmd.arg("--backup=numbered") |
| 1429 |
.arg(TEST_HELLO_WORLD_SOURCE) |
| 1430 |
.arg(TEST_HOW_ARE_YOU_SOURCE) |
| 1431 |
.succeeds() |
| 1432 |
.no_stderr(); |
| 1433 |
|
| 1434 |
assert_eq!(at.read(TEST_HOW_ARE_YOU_SOURCE), "Hello, World!\n"); |
| 1435 |
assert_eq!( |
| 1436 |
at.read(&format!("{TEST_HOW_ARE_YOU_SOURCE}.~1~")), |
| 1437 |
"How are you?\n" |
| 1438 |
); |
| 1439 |
} |
| 1440 |
|
| 1441 |
#[test] |
| 1442 |
fn test_cp_backup_existing() { |
| 1443 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1444 |
|
| 1445 |
ucmd.arg("--backup=existing") |
| 1446 |
.arg(TEST_HELLO_WORLD_SOURCE) |
| 1447 |
.arg(TEST_HOW_ARE_YOU_SOURCE) |
| 1448 |
.succeeds() |
| 1449 |
.no_stderr(); |
| 1450 |
|
| 1451 |
assert_eq!(at.read(TEST_HOW_ARE_YOU_SOURCE), "Hello, World!\n"); |
| 1452 |
assert_eq!( |
| 1453 |
at.read(&format!("{TEST_HOW_ARE_YOU_SOURCE}~")), |
| 1454 |
"How are you?\n" |
| 1455 |
); |
| 1456 |
} |
| 1457 |
|
| 1458 |
#[test] |
| 1459 |
#[cfg(unix)] |
| 1460 |
fn test_cp_backup_existing_target_is_fifo() { |
| 1461 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1462 |
at.mkfifo(&format!("{TEST_HOW_ARE_YOU_SOURCE}~")); |
| 1463 |
|
| 1464 |
ucmd.arg("--backup=simple") |
| 1465 |
.arg(TEST_HELLO_WORLD_SOURCE) |
| 1466 |
.arg(TEST_HOW_ARE_YOU_SOURCE) |
| 1467 |
.timeout(Duration::from_secs(10)) |
| 1468 |
.succeeds() |
| 1469 |
.no_stderr(); |
| 1470 |
|
| 1471 |
assert_eq!(at.read(TEST_HOW_ARE_YOU_SOURCE), "Hello, World!\n"); |
| 1472 |
assert!(!at.is_fifo(&format!("{TEST_HOW_ARE_YOU_SOURCE}~"))); |
| 1473 |
assert_eq!( |
| 1474 |
at.read(&format!("{TEST_HOW_ARE_YOU_SOURCE}~")), |
| 1475 |
"How are you?\n" |
| 1476 |
); |
| 1477 |
} |
| 1478 |
|
| 1479 |
#[test] |
| 1480 |
fn test_cp_backup_nil() { |
| 1481 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1482 |
|
| 1483 |
ucmd.arg("--backup=nil") |
| 1484 |
.arg(TEST_HELLO_WORLD_SOURCE) |
| 1485 |
.arg(TEST_HOW_ARE_YOU_SOURCE) |
| 1486 |
.succeeds() |
| 1487 |
.no_stderr(); |
| 1488 |
|
| 1489 |
assert_eq!(at.read(TEST_HOW_ARE_YOU_SOURCE), "Hello, World!\n"); |
| 1490 |
assert_eq!( |
| 1491 |
at.read(&format!("{TEST_HOW_ARE_YOU_SOURCE}~")), |
| 1492 |
"How are you?\n" |
| 1493 |
); |
| 1494 |
} |
| 1495 |
|
| 1496 |
#[test] |
| 1497 |
fn test_cp_numbered_if_existing_backup_existing() { |
| 1498 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1499 |
let existing_backup = &format!("{TEST_HOW_ARE_YOU_SOURCE}.~1~"); |
| 1500 |
at.touch(existing_backup); |
| 1501 |
|
| 1502 |
ucmd.arg("--backup=existing") |
| 1503 |
.arg(TEST_HELLO_WORLD_SOURCE) |
| 1504 |
.arg(TEST_HOW_ARE_YOU_SOURCE) |
| 1505 |
.succeeds() |
| 1506 |
.no_stderr(); |
| 1507 |
|
| 1508 |
assert!(at.file_exists(TEST_HOW_ARE_YOU_SOURCE)); |
| 1509 |
assert!(at.file_exists(existing_backup)); |
| 1510 |
assert_eq!( |
| 1511 |
at.read(&format!("{TEST_HOW_ARE_YOU_SOURCE}.~2~")), |
| 1512 |
"How are you?\n" |
| 1513 |
); |
| 1514 |
} |
| 1515 |
|
| 1516 |
#[test] |
| 1517 |
fn test_cp_numbered_if_existing_backup_nil() { |
| 1518 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1519 |
let existing_backup = &format!("{TEST_HOW_ARE_YOU_SOURCE}.~1~"); |
| 1520 |
|
| 1521 |
at.touch(existing_backup); |
| 1522 |
ucmd.arg("--backup=nil") |
| 1523 |
.arg(TEST_HELLO_WORLD_SOURCE) |
| 1524 |
.arg(TEST_HOW_ARE_YOU_SOURCE) |
| 1525 |
.succeeds() |
| 1526 |
.no_stderr(); |
| 1527 |
|
| 1528 |
assert!(at.file_exists(TEST_HOW_ARE_YOU_SOURCE)); |
| 1529 |
assert!(at.file_exists(existing_backup)); |
| 1530 |
assert_eq!( |
| 1531 |
at.read(&format!("{TEST_HOW_ARE_YOU_SOURCE}.~2~")), |
| 1532 |
"How are you?\n" |
| 1533 |
); |
| 1534 |
} |
| 1535 |
|
| 1536 |
#[test] |
| 1537 |
fn test_cp_backup_simple() { |
| 1538 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1539 |
|
| 1540 |
ucmd.arg("--backup=simple") |
| 1541 |
.arg(TEST_HELLO_WORLD_SOURCE) |
| 1542 |
.arg(TEST_HOW_ARE_YOU_SOURCE) |
| 1543 |
.succeeds() |
| 1544 |
.no_stderr(); |
| 1545 |
|
| 1546 |
assert_eq!(at.read(TEST_HOW_ARE_YOU_SOURCE), "Hello, World!\n"); |
| 1547 |
assert_eq!( |
| 1548 |
at.read(&format!("{TEST_HOW_ARE_YOU_SOURCE}~")), |
| 1549 |
"How are you?\n" |
| 1550 |
); |
| 1551 |
} |
| 1552 |
|
| 1553 |
#[test] |
| 1554 |
fn test_cp_backup_simple_protect_source() { |
| 1555 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1556 |
let source = format!("{TEST_HELLO_WORLD_SOURCE}~"); |
| 1557 |
at.touch(&source); |
| 1558 |
ucmd.arg("--backup=simple") |
| 1559 |
.arg(&source) |
| 1560 |
.arg(TEST_HELLO_WORLD_SOURCE) |
| 1561 |
.fails() |
| 1562 |
.stderr_only(format!( |
| 1563 |
"cp: backing up '{TEST_HELLO_WORLD_SOURCE}' might destroy source; '{source}' not copied\n", |
| 1564 |
)); |
| 1565 |
|
| 1566 |
assert_eq!(at.read(TEST_HELLO_WORLD_SOURCE), "Hello, World!\n"); |
| 1567 |
assert_eq!(at.read(&source), ""); |
| 1568 |
} |
| 1569 |
|
| 1570 |
#[test] |
| 1571 |
// Android denies hard links on the filesystem backing the test directory, so |
| 1572 |
// the setup cannot be built there; see the mv analogue. |
| 1573 |
#[cfg(not(target_os = "android"))] |
| 1574 |
fn test_cp_backup_simple_allows_hardlink_under_another_name() { |
| 1575 |
// `other` shares an inode with the backup path but is not named after it, |
| 1576 |
// so the backup rename cannot clobber it. GNU copies this happily. |
| 1577 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1578 |
let backup = format!("{TEST_HELLO_WORLD_SOURCE}~"); |
| 1579 |
at.write(&backup, "backup content"); |
| 1580 |
at.hard_link(&backup, "other"); |
| 1581 |
|
| 1582 |
ucmd.arg("--backup=simple") |
| 1583 |
.arg("other") |
| 1584 |
.arg(TEST_HELLO_WORLD_SOURCE) |
| 1585 |
.succeeds() |
| 1586 |
.no_stderr(); |
| 1587 |
|
| 1588 |
assert_eq!(at.read(TEST_HELLO_WORLD_SOURCE), "backup content"); |
| 1589 |
} |
| 1590 |
|
| 1591 |
#[test] |
| 1592 |
fn test_cp_backup_simple_protect_source_regardless_of_spelling() { |
| 1593 |
// The guard compares files, not the strings naming them. |
| 1594 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1595 |
let source = format!("{TEST_HELLO_WORLD_SOURCE}~"); |
| 1596 |
at.write(&source, "source content"); |
| 1597 |
|
| 1598 |
ucmd.arg("--backup=simple") |
| 1599 |
.arg(format!("./{source}")) |
| 1600 |
.arg(TEST_HELLO_WORLD_SOURCE) |
| 1601 |
.fails() |
| 1602 |
.stderr_contains("might destroy source"); |
| 1603 |
|
| 1604 |
assert_eq!(at.read(&source), "source content"); |
| 1605 |
} |
| 1606 |
|
| 1607 |
#[test] |
| 1608 |
fn test_cp_backup_numbered_allows_source_named_like_backup() { |
| 1609 |
// A numbered backup never reuses an existing name, so nothing is at risk. |
| 1610 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1611 |
let source = format!("{TEST_HELLO_WORLD_SOURCE}~"); |
| 1612 |
at.write(&source, "source content"); |
| 1613 |
|
| 1614 |
ucmd.arg("--backup=numbered") |
| 1615 |
.arg(&source) |
| 1616 |
.arg(TEST_HELLO_WORLD_SOURCE) |
| 1617 |
.succeeds(); |
| 1618 |
|
| 1619 |
assert_eq!(at.read(&source), "source content"); |
| 1620 |
assert_eq!(at.read(TEST_HELLO_WORLD_SOURCE), "source content"); |
| 1621 |
} |
| 1622 |
|
| 1623 |
#[test] |
| 1624 |
fn test_cp_backup_never() { |
| 1625 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1626 |
|
| 1627 |
ucmd.arg("--backup=never") |
| 1628 |
.arg(TEST_HELLO_WORLD_SOURCE) |
| 1629 |
.arg(TEST_HOW_ARE_YOU_SOURCE) |
| 1630 |
.succeeds() |
| 1631 |
.no_stderr(); |
| 1632 |
|
| 1633 |
assert_eq!(at.read(TEST_HOW_ARE_YOU_SOURCE), "Hello, World!\n"); |
| 1634 |
assert_eq!( |
| 1635 |
at.read(&format!("{TEST_HOW_ARE_YOU_SOURCE}~")), |
| 1636 |
"How are you?\n" |
| 1637 |
); |
| 1638 |
} |
| 1639 |
|
| 1640 |
#[test] |
| 1641 |
fn test_cp_backup_none() { |
| 1642 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1643 |
|
| 1644 |
ucmd.arg("--backup=none") |
| 1645 |
.arg(TEST_HELLO_WORLD_SOURCE) |
| 1646 |
.arg(TEST_HOW_ARE_YOU_SOURCE) |
| 1647 |
.succeeds() |
| 1648 |
.no_stderr(); |
| 1649 |
|
| 1650 |
assert_eq!(at.read(TEST_HOW_ARE_YOU_SOURCE), "Hello, World!\n"); |
| 1651 |
assert!(!at.file_exists(format!("{TEST_HOW_ARE_YOU_SOURCE}~"))); |
| 1652 |
} |
| 1653 |
|
| 1654 |
#[test] |
| 1655 |
fn test_cp_backup_off() { |
| 1656 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1657 |
|
| 1658 |
ucmd.arg("--backup=off") |
| 1659 |
.arg(TEST_HELLO_WORLD_SOURCE) |
| 1660 |
.arg(TEST_HOW_ARE_YOU_SOURCE) |
| 1661 |
.succeeds() |
| 1662 |
.no_stderr(); |
| 1663 |
|
| 1664 |
assert_eq!(at.read(TEST_HOW_ARE_YOU_SOURCE), "Hello, World!\n"); |
| 1665 |
assert!(!at.file_exists(format!("{TEST_HOW_ARE_YOU_SOURCE}~"))); |
| 1666 |
} |
| 1667 |
|
| 1668 |
#[test] |
| 1669 |
fn test_cp_backup_no_clobber_conflicting_options() { |
| 1670 |
new_ucmd!() |
| 1671 |
.arg("--backup") |
| 1672 |
.arg("--no-clobber") |
| 1673 |
.arg(TEST_HELLO_WORLD_SOURCE) |
| 1674 |
.arg(TEST_HOW_ARE_YOU_SOURCE) |
| 1675 |
.fails() |
| 1676 |
.usage_error("options --backup and --no-clobber are mutually exclusive"); |
| 1677 |
} |
| 1678 |
|
| 1679 |
#[test] |
| 1680 |
fn test_cp_deref_conflicting_options() { |
| 1681 |
new_ucmd!() |
| 1682 |
.arg("-LP") |
| 1683 |
.arg(TEST_COPY_TO_FOLDER) |
| 1684 |
.arg(TEST_HELLO_WORLD_SOURCE) |
| 1685 |
.fails(); |
| 1686 |
} |
| 1687 |
|
| 1688 |
#[test] |
| 1689 |
fn test_cp_deref() { |
| 1690 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1691 |
|
| 1692 |
#[cfg(not(windows))] |
| 1693 |
let _r = fs::symlink( |
| 1694 |
TEST_HELLO_WORLD_SOURCE, |
| 1695 |
at.subdir.join(TEST_HELLO_WORLD_SOURCE_SYMLINK), |
| 1696 |
); |
| 1697 |
#[cfg(windows)] |
| 1698 |
let _r = symlink_file( |
| 1699 |
TEST_HELLO_WORLD_SOURCE, |
| 1700 |
at.subdir.join(TEST_HELLO_WORLD_SOURCE_SYMLINK), |
| 1701 |
); |
| 1702 |
//using -L option |
| 1703 |
ucmd.arg("-L") |
| 1704 |
.arg(TEST_HELLO_WORLD_SOURCE) |
| 1705 |
.arg(TEST_HELLO_WORLD_SOURCE_SYMLINK) |
| 1706 |
.arg(TEST_COPY_TO_FOLDER) |
| 1707 |
.succeeds(); |
| 1708 |
|
| 1709 |
let path_to_new_symlink = at |
| 1710 |
.subdir |
| 1711 |
.join(TEST_COPY_TO_FOLDER) |
| 1712 |
.join(TEST_HELLO_WORLD_SOURCE_SYMLINK); |
| 1713 |
// unlike -P/--no-deref, we expect a file, not a link |
| 1714 |
assert!( |
| 1715 |
at.file_exists( |
| 1716 |
path_to_new_symlink |
| 1717 |
.clone() |
| 1718 |
.into_os_string() |
| 1719 |
.into_string() |
| 1720 |
.unwrap() |
| 1721 |
) |
| 1722 |
); |
| 1723 |
// Check the content of the destination file that was copied. |
| 1724 |
assert_eq!(at.read(TEST_COPY_TO_FOLDER_FILE), "Hello, World!\n"); |
| 1725 |
let path_to_check = path_to_new_symlink.to_str().unwrap(); |
| 1726 |
assert_eq!(at.read(path_to_check), "Hello, World!\n"); |
| 1727 |
} |
| 1728 |
#[test] |
| 1729 |
fn test_cp_no_deref() { |
| 1730 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1731 |
|
| 1732 |
#[cfg(not(windows))] |
| 1733 |
let _r = fs::symlink( |
| 1734 |
TEST_HELLO_WORLD_SOURCE, |
| 1735 |
at.subdir.join(TEST_HELLO_WORLD_SOURCE_SYMLINK), |
| 1736 |
); |
| 1737 |
#[cfg(windows)] |
| 1738 |
let _r = symlink_file( |
| 1739 |
TEST_HELLO_WORLD_SOURCE, |
| 1740 |
at.subdir.join(TEST_HELLO_WORLD_SOURCE_SYMLINK), |
| 1741 |
); |
| 1742 |
//using -P option |
| 1743 |
ucmd.arg("-P") |
| 1744 |
.arg(TEST_HELLO_WORLD_SOURCE) |
| 1745 |
.arg(TEST_HELLO_WORLD_SOURCE_SYMLINK) |
| 1746 |
.arg(TEST_COPY_TO_FOLDER) |
| 1747 |
.succeeds(); |
| 1748 |
|
| 1749 |
let path_to_new_symlink = at |
| 1750 |
.subdir |
| 1751 |
.join(TEST_COPY_TO_FOLDER) |
| 1752 |
.join(TEST_HELLO_WORLD_SOURCE_SYMLINK); |
| 1753 |
assert!( |
| 1754 |
at.is_symlink( |
| 1755 |
&path_to_new_symlink |
| 1756 |
.clone() |
| 1757 |
.into_os_string() |
| 1758 |
.into_string() |
| 1759 |
.unwrap() |
| 1760 |
) |
| 1761 |
); |
| 1762 |
// Check the content of the destination file that was copied. |
| 1763 |
assert_eq!(at.read(TEST_COPY_TO_FOLDER_FILE), "Hello, World!\n"); |
| 1764 |
let path_to_check = path_to_new_symlink.to_str().unwrap(); |
| 1765 |
assert_eq!(at.read(path_to_check), "Hello, World!\n"); |
| 1766 |
} |
| 1767 |
|
| 1768 |
#[test] |
| 1769 |
fn test_cp_no_deref_link_onto_link() { |
| 1770 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1771 |
|
| 1772 |
at.copy(TEST_HELLO_WORLD_SOURCE, TEST_HELLO_WORLD_DEST); |
| 1773 |
|
| 1774 |
#[cfg(not(windows))] |
| 1775 |
let _r = fs::symlink( |
| 1776 |
TEST_HELLO_WORLD_SOURCE, |
| 1777 |
at.subdir.join(TEST_HELLO_WORLD_SOURCE_SYMLINK), |
| 1778 |
); |
| 1779 |
#[cfg(windows)] |
| 1780 |
let _r = symlink_file( |
| 1781 |
TEST_HELLO_WORLD_SOURCE, |
| 1782 |
at.subdir.join(TEST_HELLO_WORLD_SOURCE_SYMLINK), |
| 1783 |
); |
| 1784 |
|
| 1785 |
#[cfg(not(windows))] |
| 1786 |
let _r = fs::symlink( |
| 1787 |
TEST_HELLO_WORLD_DEST, |
| 1788 |
at.subdir.join(TEST_HELLO_WORLD_DEST_SYMLINK), |
| 1789 |
); |
| 1790 |
#[cfg(windows)] |
| 1791 |
let _r = symlink_file( |
| 1792 |
TEST_HELLO_WORLD_DEST, |
| 1793 |
at.subdir.join(TEST_HELLO_WORLD_DEST_SYMLINK), |
| 1794 |
); |
| 1795 |
|
| 1796 |
ucmd.arg("-P") |
| 1797 |
.arg(TEST_HELLO_WORLD_SOURCE_SYMLINK) |
| 1798 |
.arg(TEST_HELLO_WORLD_DEST_SYMLINK) |
| 1799 |
.succeeds(); |
| 1800 |
|
| 1801 |
// Ensure that the target of the destination was not modified. |
| 1802 |
assert!( |
| 1803 |
!at.symlink_metadata(TEST_HELLO_WORLD_DEST) |
| 1804 |
.file_type() |
| 1805 |
.is_symlink() |
| 1806 |
); |
| 1807 |
assert!( |
| 1808 |
at.symlink_metadata(TEST_HELLO_WORLD_DEST_SYMLINK) |
| 1809 |
.file_type() |
| 1810 |
.is_symlink() |
| 1811 |
); |
| 1812 |
assert_eq!(at.read(TEST_HELLO_WORLD_DEST_SYMLINK), "Hello, World!\n"); |
| 1813 |
} |
| 1814 |
|
| 1815 |
#[test] |
| 1816 |
fn test_cp_strip_trailing_slashes() { |
| 1817 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1818 |
|
| 1819 |
//using --strip-trailing-slashes option |
| 1820 |
ucmd.arg("--strip-trailing-slashes") |
| 1821 |
.arg(format!("{TEST_HELLO_WORLD_SOURCE}/")) |
| 1822 |
.arg(TEST_HELLO_WORLD_DEST) |
| 1823 |
.succeeds(); |
| 1824 |
|
| 1825 |
// Check the content of the destination file that was copied. |
| 1826 |
assert_eq!(at.read(TEST_HELLO_WORLD_DEST), "Hello, World!\n"); |
| 1827 |
} |
| 1828 |
|
| 1829 |
#[test] |
| 1830 |
fn test_cp_parents() { |
| 1831 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1832 |
|
| 1833 |
ucmd.arg("--parents") |
| 1834 |
.arg(TEST_COPY_FROM_FOLDER_FILE) |
| 1835 |
.arg(TEST_COPY_TO_FOLDER) |
| 1836 |
.succeeds(); |
| 1837 |
|
| 1838 |
assert_eq!( |
| 1839 |
at.read(&format!( |
| 1840 |
"{TEST_COPY_TO_FOLDER}/{TEST_COPY_FROM_FOLDER_FILE}" |
| 1841 |
)), |
| 1842 |
"Hello, World!\n" |
| 1843 |
); |
| 1844 |
} |
| 1845 |
|
| 1846 |
#[test] |
| 1847 |
fn test_cp_parents_multiple_files() { |
| 1848 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1849 |
|
| 1850 |
ucmd.arg("--parents") |
| 1851 |
.arg(TEST_COPY_FROM_FOLDER_FILE) |
| 1852 |
.arg(TEST_HOW_ARE_YOU_SOURCE) |
| 1853 |
.arg(TEST_COPY_TO_FOLDER) |
| 1854 |
.succeeds(); |
| 1855 |
|
| 1856 |
assert_eq!( |
| 1857 |
at.read(&format!( |
| 1858 |
"{TEST_COPY_TO_FOLDER}/{TEST_COPY_FROM_FOLDER_FILE}" |
| 1859 |
)), |
| 1860 |
"Hello, World!\n" |
| 1861 |
); |
| 1862 |
assert_eq!( |
| 1863 |
at.read(&format!("{TEST_COPY_TO_FOLDER}/{TEST_HOW_ARE_YOU_SOURCE}")), |
| 1864 |
"How are you?\n" |
| 1865 |
); |
| 1866 |
} |
| 1867 |
|
| 1868 |
#[test] |
| 1869 |
fn test_cp_parents_dest_not_directory() { |
| 1870 |
new_ucmd!() |
| 1871 |
.arg("--parents") |
| 1872 |
.arg(TEST_COPY_FROM_FOLDER_FILE) |
| 1873 |
.arg(TEST_HELLO_WORLD_DEST) |
| 1874 |
.fails() |
| 1875 |
.stderr_contains("with --parents, the destination must be a directory"); |
| 1876 |
} |
| 1877 |
|
| 1878 |
#[test] |
| 1879 |
#[cfg(not(target_os = "openbsd"))] |
| 1880 |
#[cfg_attr( |
| 1881 |
wasi_runner, |
| 1882 |
ignore = "WASI: no chmod syscall, so required mode/ownership preservation always fails" |
| 1883 |
)] |
| 1884 |
fn test_cp_parents_with_permissions_copy_file() { |
| 1885 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1886 |
|
| 1887 |
let dir = "dir"; |
| 1888 |
let file = "p1/p2/file"; |
| 1889 |
|
| 1890 |
at.mkdir(dir); |
| 1891 |
at.mkdir_all("p1/p2"); |
| 1892 |
at.touch(file); |
| 1893 |
|
| 1894 |
#[cfg(unix)] |
| 1895 |
{ |
| 1896 |
let p1_mode = 0o0777; |
| 1897 |
let p2_mode = 0o0711; |
| 1898 |
let file_mode = 0o0702; |
| 1899 |
|
| 1900 |
at.set_mode("p1", p1_mode); |
| 1901 |
at.set_mode("p1/p2", p2_mode); |
| 1902 |
at.set_mode(file, file_mode); |
| 1903 |
} |
| 1904 |
|
| 1905 |
ucmd.arg("-p") |
| 1906 |
.arg("--parents") |
| 1907 |
.arg(file) |
| 1908 |
.arg(dir) |
| 1909 |
.succeeds(); |
| 1910 |
|
| 1911 |
#[cfg(all(unix, not(target_os = "freebsd"), not(target_os = "openbsd")))] |
| 1912 |
{ |
| 1913 |
let p1_metadata = at.metadata("p1"); |
| 1914 |
let p2_metadata = at.metadata("p1/p2"); |
| 1915 |
let file_metadata = at.metadata(file); |
| 1916 |
|
| 1917 |
assert_metadata_eq!(p1_metadata, at.metadata("dir/p1")); |
| 1918 |
assert_metadata_eq!(p2_metadata, at.metadata("dir/p1/p2")); |
| 1919 |
assert_metadata_eq!(file_metadata, at.metadata("dir/p1/p2/file")); |
| 1920 |
} |
| 1921 |
} |
| 1922 |
|
| 1923 |
#[test] |
| 1924 |
#[cfg(not(target_os = "openbsd"))] |
| 1925 |
#[cfg_attr( |
| 1926 |
wasi_runner, |
| 1927 |
ignore = "WASI: no chmod syscall, so required mode/ownership preservation always fails" |
| 1928 |
)] |
| 1929 |
fn test_cp_parents_with_permissions_copy_dir() { |
| 1930 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1931 |
|
| 1932 |
let dir1 = "dir"; |
| 1933 |
let dir2 = "p1/p2"; |
| 1934 |
let file = "p1/p2/file"; |
| 1935 |
|
| 1936 |
at.mkdir(dir1); |
| 1937 |
at.mkdir_all(dir2); |
| 1938 |
at.touch(file); |
| 1939 |
|
| 1940 |
#[cfg(unix)] |
| 1941 |
{ |
| 1942 |
let p1_mode = 0o0777; |
| 1943 |
let p2_mode = 0o0711; |
| 1944 |
let file_mode = 0o0702; |
| 1945 |
|
| 1946 |
at.set_mode("p1", p1_mode); |
| 1947 |
at.set_mode("p1/p2", p2_mode); |
| 1948 |
at.set_mode(file, file_mode); |
| 1949 |
} |
| 1950 |
|
| 1951 |
ucmd.arg("-p") |
| 1952 |
.arg("--parents") |
| 1953 |
.arg("-r") |
| 1954 |
.arg(dir2) |
| 1955 |
.arg(dir1) |
| 1956 |
.succeeds(); |
| 1957 |
|
| 1958 |
#[cfg(all(unix, not(target_os = "freebsd"), not(target_os = "openbsd")))] |
| 1959 |
{ |
| 1960 |
let p1_metadata = at.metadata("p1"); |
| 1961 |
let p2_metadata = at.metadata("p1/p2"); |
| 1962 |
let file_metadata = at.metadata(file); |
| 1963 |
|
| 1964 |
assert_metadata_eq!(p1_metadata, at.metadata("dir/p1")); |
| 1965 |
assert_metadata_eq!(p2_metadata, at.metadata("dir/p1/p2")); |
| 1966 |
assert_metadata_eq!(file_metadata, at.metadata("dir/p1/p2/file")); |
| 1967 |
} |
| 1968 |
} |
| 1969 |
|
| 1970 |
#[test] |
| 1971 |
#[cfg(unix)] |
| 1972 |
#[cfg_attr( |
| 1973 |
wasi_runner, |
| 1974 |
ignore = "WASI sandbox: host paths (/dev, /private) not visible" |
| 1975 |
)] |
| 1976 |
fn test_cp_writable_special_file_permissions() { |
| 1977 |
new_ucmd!().arg("/dev/null").arg("/dev/zero").succeeds(); |
| 1978 |
} |
| 1979 |
|
| 1980 |
#[test] |
| 1981 |
#[cfg(unix)] |
| 1982 |
#[cfg_attr( |
| 1983 |
wasi_runner, |
| 1984 |
ignore = "WASI sandbox: host paths (/dev, /private) not visible" |
| 1985 |
)] |
| 1986 |
fn test_cp_issue_1665() { |
| 1987 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 1988 |
ucmd.arg("/dev/null").arg("foo").succeeds(); |
| 1989 |
assert!(at.file_exists("foo")); |
| 1990 |
assert_eq!(at.read("foo"), ""); |
| 1991 |
} |
| 1992 |
|
| 1993 |
#[test] |
| 1994 |
#[cfg(not(target_os = "openbsd"))] |
| 1995 |
#[cfg_attr( |
| 1996 |
wasi_runner, |
| 1997 |
ignore = "WASI: no chmod syscall, so required mode/ownership preservation always fails" |
| 1998 |
)] |
| 1999 |
fn test_cp_preserve_no_args() { |
| 2000 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 2001 |
let src_file = "a"; |
| 2002 |
let dst_file = "b"; |
| 2003 |
|
| 2004 |
// Prepare the source file |
| 2005 |
at.touch(src_file); |
| 2006 |
#[cfg(unix)] |
| 2007 |
at.set_mode(src_file, 0o0500); |
| 2008 |
|
| 2009 |
// Copy |
| 2010 |
ucmd.arg(src_file) |
| 2011 |
.arg(dst_file) |
| 2012 |
.arg("--preserve") |
| 2013 |
.succeeds(); |
| 2014 |
|
| 2015 |
#[cfg(all(unix, not(target_os = "freebsd"), not(target_os = "openbsd")))] |
| 2016 |
{ |
| 2017 |
// Assert that the mode, ownership, and timestamps are preserved |
| 2018 |
// NOTICE: the ownership is not modified on the src file, because that requires root permissions |
| 2019 |
let metadata_src = at.metadata(src_file); |
| 2020 |
let metadata_dst = at.metadata(dst_file); |
| 2021 |
assert_metadata_eq!(metadata_src, metadata_dst); |
| 2022 |
} |
| 2023 |
} |
| 2024 |
|
| 2025 |
#[test] |
| 2026 |
#[cfg(not(target_os = "openbsd"))] |
| 2027 |
#[cfg_attr( |
| 2028 |
wasi_runner, |
| 2029 |
ignore = "WASI: no chmod syscall, so required mode/ownership preservation always fails" |
| 2030 |
)] |
| 2031 |
fn test_cp_preserve_no_args_before_opts() { |
| 2032 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 2033 |
let src_file = "a"; |
| 2034 |
let dst_file = "b"; |
| 2035 |
|
| 2036 |
// Prepare the source file |
| 2037 |
at.touch(src_file); |
| 2038 |
#[cfg(unix)] |
| 2039 |
at.set_mode(src_file, 0o0500); |
| 2040 |
|
| 2041 |
// Copy |
| 2042 |
ucmd.arg("--preserve") |
| 2043 |
.arg(src_file) |
| 2044 |
.arg(dst_file) |
| 2045 |
.succeeds(); |
| 2046 |
|
| 2047 |
#[cfg(all(unix, not(target_os = "freebsd"), not(target_os = "openbsd")))] |
| 2048 |
{ |
| 2049 |
// Assert that the mode, ownership, and timestamps are preserved |
| 2050 |
// NOTICE: the ownership is not modified on the src file, because that requires root permissions |
| 2051 |
let metadata_src = at.metadata(src_file); |
| 2052 |
let metadata_dst = at.metadata(dst_file); |
| 2053 |
assert_metadata_eq!(metadata_src, metadata_dst); |
| 2054 |
} |
| 2055 |
} |
| 2056 |
|
| 2057 |
#[test] |
| 2058 |
#[cfg_attr( |
| 2059 |
wasi_runner, |
| 2060 |
ignore = "WASI: no chmod syscall, so required mode/ownership preservation always fails" |
| 2061 |
)] |
| 2062 |
fn test_cp_preserve_all() { |
| 2063 |
for argument in ["--preserve=all", "--preserve=al"] { |
| 2064 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 2065 |
let src_file = "a"; |
| 2066 |
let dst_file = "b"; |
| 2067 |
|
| 2068 |
// Prepare the source file |
| 2069 |
at.touch(src_file); |
| 2070 |
#[cfg(unix)] |
| 2071 |
at.set_mode(src_file, 0o0500); |
| 2072 |
|
| 2073 |
// TODO: create a destination that does not allow copying of xattr and context |
| 2074 |
// Copy |
| 2075 |
ucmd.arg(src_file).arg(dst_file).arg(argument).succeeds(); |
| 2076 |
|
| 2077 |
#[cfg(all(unix, not(target_os = "freebsd"), not(target_os = "openbsd")))] |
| 2078 |
{ |
| 2079 |
// Assert that the mode, ownership, and timestamps are preserved |
| 2080 |
// NOTICE: the ownership is not modified on the src file, because that requires root permissions |
| 2081 |
let metadata_src = at.metadata(src_file); |
| 2082 |
let metadata_dst = at.metadata(dst_file); |
| 2083 |
assert_metadata_eq!(metadata_src, metadata_dst); |
| 2084 |
} |
| 2085 |
} |
| 2086 |
} |
| 2087 |
|
| 2088 |
// GNU `cp -p` preserves mode, ownership, and timestamps but NOT xattrs. |
| 2089 |
// xattr preservation requires explicit `--preserve=xattr` or `-a`. See #9704. |
| 2090 |
#[test] |
| 2091 |
#[cfg(all( |
| 2092 |
unix, |
| 2093 |
not(any(target_vendor = "apple", target_os = "android", target_os = "openbsd")) |
| 2094 |
))] |
| 2095 |
fn test_cp_p_does_not_preserve_xattr_by_default() { |
| 2096 |
use std::process::Command; |
| 2097 |
|
| 2098 |
let scene = TestScenario::new(util_name!()); |
| 2099 |
let at = &scene.fixtures; |
| 2100 |
at.touch("src"); |
| 2101 |
|
| 2102 |
let xattr_key = "user.test_preserve_p"; |
| 2103 |
let setfattr = Command::new("setfattr") |
| 2104 |
.args(["-n", xattr_key, "-v", "v", &at.plus_as_string("src")]) |
| 2105 |
.status(); |
| 2106 |
match setfattr { |
| 2107 |
Ok(s) if s.success() => {} |
| 2108 |
_ => { |
| 2109 |
println!("test skipped: setfattr not available / filesystem rejects xattrs"); |
| 2110 |
return; |
| 2111 |
} |
| 2112 |
} |
| 2113 |
|
| 2114 |
scene |
| 2115 |
.ucmd() |
| 2116 |
.args(&["-p", &at.plus_as_string("src"), &at.plus_as_string("dst")]) |
| 2117 |
.succeeds(); |
| 2118 |
|
| 2119 |
let out = Command::new("getfattr") |
| 2120 |
.args(["--only-values", "-n", xattr_key, &at.plus_as_string("dst")]) |
| 2121 |
.output() |
| 2122 |
.expect("getfattr failed"); |
| 2123 |
assert!( |
| 2124 |
!out.status.success(), |
| 2125 |
"cp -p should not preserve xattrs by default, but '{xattr_key}' was copied" |
| 2126 |
); |
| 2127 |
} |
| 2128 |
|
| 2129 |
#[test] |
| 2130 |
#[cfg(all(unix, not(any(target_os = "android", target_os = "openbsd"))))] |
| 2131 |
fn test_cp_preserve_xattr() { |
| 2132 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 2133 |
let src_file = "a"; |
| 2134 |
let dst_file = "b"; |
| 2135 |
|
| 2136 |
// Prepare the source file |
| 2137 |
at.touch(src_file); |
| 2138 |
#[cfg(unix)] |
| 2139 |
at.set_mode(src_file, 0o0500); |
| 2140 |
|
| 2141 |
// Sleep so that the time stats are different |
| 2142 |
sleep(Duration::from_secs(1)); |
| 2143 |
|
| 2144 |
// TODO: create a destination that does not allow copying of xattr and context |
| 2145 |
// Copy |
| 2146 |
ucmd.arg(src_file) |
| 2147 |
.arg(dst_file) |
| 2148 |
.arg("--preserve=xattr") |
| 2149 |
.succeeds(); |
| 2150 |
|
| 2151 |
// FIXME: macos copy keeps the original mtime |
| 2152 |
#[cfg(not(any(target_vendor = "apple", target_os = "freebsd")))] |
| 2153 |
{ |
| 2154 |
// Assert that the mode, ownership, and timestamps are *NOT* preserved |
| 2155 |
// NOTICE: the ownership is not modified on the src file, because that requires root permissions |
| 2156 |
let metadata_src = at.metadata(src_file); |
| 2157 |
let metadata_dst = at.metadata(dst_file); |
| 2158 |
assert_ne!(metadata_src.mtime(), metadata_dst.mtime()); |
| 2159 |
// TODO: verify access time as well. It shouldn't change, however, it does change in this test. |
| 2160 |
} |
| 2161 |
} |
| 2162 |
|
| 2163 |
#[test] |
| 2164 |
#[cfg(all( |
| 2165 |
not(feature = "selinux"), |
| 2166 |
any(target_os = "linux", target_os = "android") |
| 2167 |
))] |
| 2168 |
fn test_cp_preserve_all_context_fails_on_non_selinux() { |
| 2169 |
new_ucmd!() |
| 2170 |
.arg(TEST_COPY_FROM_FOLDER_FILE) |
| 2171 |
.arg(TEST_HELLO_WORLD_DEST) |
| 2172 |
.arg("--preserve=all,context") |
| 2173 |
.fails(); |
| 2174 |
} |
| 2175 |
|
| 2176 |
#[test] |
| 2177 |
fn test_cp_preserve_link_parses() { |
| 2178 |
// TODO: Also check whether --preserve=link did the right thing! |
| 2179 |
for argument in [ |
| 2180 |
"--preserve=links", |
| 2181 |
"--preserve=link", |
| 2182 |
"--preserve=li", |
| 2183 |
"--preserve=l", |
| 2184 |
] { |
| 2185 |
new_ucmd!() |
| 2186 |
.arg(argument) |
| 2187 |
.arg(TEST_COPY_FROM_FOLDER_FILE) |
| 2188 |
.arg(TEST_HELLO_WORLD_DEST) |
| 2189 |
.succeeds() |
| 2190 |
.no_output(); |
| 2191 |
} |
| 2192 |
} |
| 2193 |
|
| 2194 |
#[test] |
| 2195 |
fn test_cp_preserve_invalid_rejected() { |
| 2196 |
new_ucmd!() |
| 2197 |
.arg("--preserve=invalid-value") |
| 2198 |
.arg(TEST_COPY_FROM_FOLDER_FILE) |
| 2199 |
.arg(TEST_HELLO_WORLD_DEST) |
| 2200 |
.fails_with_code(1) |
| 2201 |
.no_stdout(); |
| 2202 |
} |
| 2203 |
|
| 2204 |
#[test] |
| 2205 |
#[cfg(target_os = "android")] |
| 2206 |
#[ignore = "disabled until fixed"] // FIXME: the test looks to .succeed on android |
| 2207 |
fn test_cp_preserve_xattr_fails_on_android() { |
| 2208 |
// Because of the SELinux extended attributes used on Android, trying to copy extended |
| 2209 |
// attributes has to fail in this case, since we specify `--preserve=xattr` and this puts it |
| 2210 |
// into the required attributes |
| 2211 |
new_ucmd!() |
| 2212 |
.arg(TEST_COPY_FROM_FOLDER_FILE) |
| 2213 |
.arg(TEST_HELLO_WORLD_DEST) |
| 2214 |
.arg("--preserve=xattr") |
| 2215 |
.fails(); |
| 2216 |
} |
| 2217 |
|
| 2218 |
#[test] |
| 2219 |
// android platform will causing stderr = cp: Permission denied (os error 13) |
| 2220 |
#[cfg(not(target_os = "android"))] |
| 2221 |
fn test_cp_preserve_links_case_1() { |
| 2222 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 2223 |
|
| 2224 |
at.touch("a"); |
| 2225 |
at.hard_link("a", "b"); |
| 2226 |
at.mkdir("c"); |
| 2227 |
|
| 2228 |
ucmd.arg("-d").arg("a").arg("b").arg("c").succeeds(); |
| 2229 |
|
| 2230 |
assert!(at.dir_exists("c")); |
| 2231 |
assert!(at.plus("c").join("a").exists()); |
| 2232 |
assert!(at.plus("c").join("b").exists()); |
| 2233 |
|
| 2234 |
#[cfg(unix)] |
| 2235 |
{ |
| 2236 |
let metadata_a = std::fs::metadata(at.subdir.join("c").join("a")).unwrap(); |
| 2237 |
let metadata_b = std::fs::metadata(at.subdir.join("c").join("b")).unwrap(); |
| 2238 |
|
| 2239 |
assert_eq!(metadata_a.ino(), metadata_b.ino()); |
| 2240 |
} |
| 2241 |
} |
| 2242 |
|
| 2243 |
#[test] |
| 2244 |
// android platform will causing stderr = cp: Permission denied (os error 13) |
| 2245 |
#[cfg(not(target_os = "android"))] |
| 2246 |
#[cfg_attr( |
| 2247 |
wasi_runner, |
| 2248 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 2249 |
)] |
| 2250 |
fn test_cp_preserve_links_case_2() { |
| 2251 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 2252 |
|
| 2253 |
at.touch("a"); |
| 2254 |
at.symlink_file("a", "b"); |
| 2255 |
at.mkdir("c"); |
| 2256 |
|
| 2257 |
ucmd.arg("--preserve=links") |
| 2258 |
.arg("-R") |
| 2259 |
.arg("-H") |
| 2260 |
.arg("a") |
| 2261 |
.arg("b") |
| 2262 |
.arg("c") |
| 2263 |
.succeeds(); |
| 2264 |
|
| 2265 |
assert!(at.dir_exists("c")); |
| 2266 |
assert!(at.plus("c").join("a").exists()); |
| 2267 |
assert!(at.plus("c").join("b").exists()); |
| 2268 |
|
| 2269 |
#[cfg(unix)] |
| 2270 |
{ |
| 2271 |
let metadata_a = std::fs::metadata(at.subdir.join("c").join("a")).unwrap(); |
| 2272 |
let metadata_b = std::fs::metadata(at.subdir.join("c").join("b")).unwrap(); |
| 2273 |
|
| 2274 |
assert_eq!(metadata_a.ino(), metadata_b.ino()); |
| 2275 |
} |
| 2276 |
} |
| 2277 |
|
| 2278 |
#[test] |
| 2279 |
// android platform will causing stderr = cp: Permission denied (os error 13) |
| 2280 |
#[cfg(not(target_os = "android"))] |
| 2281 |
#[cfg_attr( |
| 2282 |
wasi_runner, |
| 2283 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 2284 |
)] |
| 2285 |
fn test_cp_preserve_links_case_3() { |
| 2286 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 2287 |
|
| 2288 |
at.mkdir("d"); |
| 2289 |
at.touch("d/a"); |
| 2290 |
at.symlink_file("d/a", "d/b"); |
| 2291 |
|
| 2292 |
ucmd.arg("--preserve=links") |
| 2293 |
.arg("-R") |
| 2294 |
.arg("-L") |
| 2295 |
.arg("d") |
| 2296 |
.arg("c") |
| 2297 |
.succeeds(); |
| 2298 |
|
| 2299 |
assert!(at.dir_exists("c")); |
| 2300 |
assert!(at.plus("c").join("a").exists()); |
| 2301 |
assert!(at.plus("c").join("b").exists()); |
| 2302 |
|
| 2303 |
#[cfg(unix)] |
| 2304 |
{ |
| 2305 |
let metadata_a = std::fs::metadata(at.subdir.join("c").join("a")).unwrap(); |
| 2306 |
let metadata_b = std::fs::metadata(at.subdir.join("c").join("b")).unwrap(); |
| 2307 |
|
| 2308 |
assert_eq!(metadata_a.ino(), metadata_b.ino()); |
| 2309 |
} |
| 2310 |
} |
| 2311 |
|
| 2312 |
#[test] |
| 2313 |
// android platform will causing stderr = cp: Permission denied (os error 13) |
| 2314 |
#[cfg(not(target_os = "android"))] |
| 2315 |
fn test_cp_preserve_links_case_4() { |
| 2316 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 2317 |
|
| 2318 |
at.mkdir("d"); |
| 2319 |
at.touch("d/a"); |
| 2320 |
at.hard_link("d/a", "d/b"); |
| 2321 |
|
| 2322 |
ucmd.arg("--preserve=links") |
| 2323 |
.arg("-R") |
| 2324 |
.arg("-L") |
| 2325 |
.arg("d") |
| 2326 |
.arg("c") |
| 2327 |
.succeeds(); |
| 2328 |
|
| 2329 |
assert!(at.dir_exists("c")); |
| 2330 |
assert!(at.plus("c").join("a").exists()); |
| 2331 |
assert!(at.plus("c").join("b").exists()); |
| 2332 |
|
| 2333 |
#[cfg(unix)] |
| 2334 |
{ |
| 2335 |
let metadata_a = std::fs::metadata(at.subdir.join("c").join("a")).unwrap(); |
| 2336 |
let metadata_b = std::fs::metadata(at.subdir.join("c").join("b")).unwrap(); |
| 2337 |
|
| 2338 |
assert_eq!(metadata_a.ino(), metadata_b.ino()); |
| 2339 |
} |
| 2340 |
} |
| 2341 |
|
| 2342 |
#[test] |
| 2343 |
// android platform will causing stderr = cp: Permission denied (os error 13) |
| 2344 |
#[cfg(not(target_os = "android"))] |
| 2345 |
fn test_cp_preserve_links_case_5() { |
| 2346 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 2347 |
|
| 2348 |
at.mkdir("d"); |
| 2349 |
at.touch("d/a"); |
| 2350 |
at.hard_link("d/a", "d/b"); |
| 2351 |
|
| 2352 |
ucmd.arg("-dR") |
| 2353 |
.arg("--no-preserve=links") |
| 2354 |
.arg("d") |
| 2355 |
.arg("c") |
| 2356 |
.succeeds(); |
| 2357 |
|
| 2358 |
assert!(at.dir_exists("c")); |
| 2359 |
assert!(at.plus("c").join("a").exists()); |
| 2360 |
assert!(at.plus("c").join("b").exists()); |
| 2361 |
|
| 2362 |
#[cfg(unix)] |
| 2363 |
{ |
| 2364 |
let metadata_a = std::fs::metadata(at.subdir.join("c").join("a")).unwrap(); |
| 2365 |
let metadata_b = std::fs::metadata(at.subdir.join("c").join("b")).unwrap(); |
| 2366 |
|
| 2367 |
assert_ne!(metadata_a.ino(), metadata_b.ino()); |
| 2368 |
} |
| 2369 |
} |
| 2370 |
|
| 2371 |
#[test] |
| 2372 |
// android platform will causing stderr = cp: Permission denied (os error 13) |
| 2373 |
#[cfg(not(target_os = "android"))] |
| 2374 |
fn test_cp_preserve_links_case_6() { |
| 2375 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 2376 |
|
| 2377 |
at.touch("a"); |
| 2378 |
at.hard_link("a", "b"); |
| 2379 |
at.mkdir("c"); |
| 2380 |
|
| 2381 |
ucmd.arg("-d").arg("a").arg("b").arg("c").succeeds(); |
| 2382 |
|
| 2383 |
assert!(at.dir_exists("c")); |
| 2384 |
assert!(at.plus("c").join("a").exists()); |
| 2385 |
assert!(at.plus("c").join("b").exists()); |
| 2386 |
|
| 2387 |
#[cfg(unix)] |
| 2388 |
{ |
| 2389 |
let metadata_a = std::fs::metadata(at.subdir.join("c").join("a")).unwrap(); |
| 2390 |
let metadata_b = std::fs::metadata(at.subdir.join("c").join("b")).unwrap(); |
| 2391 |
|
| 2392 |
assert_eq!(metadata_a.ino(), metadata_b.ino()); |
| 2393 |
} |
| 2394 |
} |
| 2395 |
|
| 2396 |
#[test] |
| 2397 |
// android platform will causing stderr = cp: Permission denied (os error 13) |
| 2398 |
#[cfg(not(target_os = "android"))] |
| 2399 |
fn test_cp_preserve_links_case_7() { |
| 2400 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 2401 |
|
| 2402 |
at.mkdir("src"); |
| 2403 |
at.touch("src/f"); |
| 2404 |
at.hard_link("src/f", "src/g"); |
| 2405 |
|
| 2406 |
at.mkdir("dest"); |
| 2407 |
at.touch("dest/g"); |
| 2408 |
|
| 2409 |
ucmd.arg("-n") |
| 2410 |
.arg("--preserve=links") |
| 2411 |
.arg("--debug") |
| 2412 |
.arg("src/f") |
| 2413 |
.arg("src/g") |
| 2414 |
.arg("dest") |
| 2415 |
.succeeds() |
| 2416 |
.stdout_contains("skipped"); |
| 2417 |
|
| 2418 |
assert!(at.dir_exists("dest")); |
| 2419 |
assert!(at.plus("dest").join("f").exists()); |
| 2420 |
assert!(at.plus("dest").join("g").exists()); |
| 2421 |
} |
| 2422 |
|
| 2423 |
#[test] |
| 2424 |
#[cfg(unix)] |
| 2425 |
fn test_cp_no_preserve_mode() { |
| 2426 |
use uucore::fs as uufs; |
| 2427 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 2428 |
|
| 2429 |
at.touch("a"); |
| 2430 |
at.set_mode("a", 0o731); |
| 2431 |
|
| 2432 |
ucmd.arg("-a") |
| 2433 |
.arg("--no-preserve=mode") |
| 2434 |
.arg("a") |
| 2435 |
.arg("b") |
| 2436 |
.umask(0o077) |
| 2437 |
.succeeds(); |
| 2438 |
|
| 2439 |
assert!(at.file_exists("b")); |
| 2440 |
|
| 2441 |
let metadata_b = std::fs::metadata(at.subdir.join("b")).unwrap(); |
| 2442 |
let permission_b = uufs::display_permissions(&metadata_b, false); |
| 2443 |
assert_eq!(permission_b, "rw-------".to_string()); |
| 2444 |
} |
| 2445 |
|
| 2446 |
#[test] |
| 2447 |
// For now, disable the test on Windows. Symlinks aren't well support on Windows. |
| 2448 |
// It works on Unix for now and it works locally when run from a powershell |
| 2449 |
#[cfg(not(windows))] |
| 2450 |
#[cfg_attr( |
| 2451 |
wasi_runner, |
| 2452 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 2453 |
)] |
| 2454 |
fn test_cp_deref_folder_to_folder() { |
| 2455 |
let scene = TestScenario::new(util_name!()); |
| 2456 |
let at = &scene.fixtures; |
| 2457 |
|
| 2458 |
let path_to_new_symlink = at.plus(TEST_COPY_FROM_FOLDER); |
| 2459 |
|
| 2460 |
at.symlink_file( |
| 2461 |
&path_to_new_symlink |
| 2462 |
.join(TEST_HELLO_WORLD_SOURCE) |
| 2463 |
.to_string_lossy(), |
| 2464 |
&path_to_new_symlink |
| 2465 |
.join(TEST_HELLO_WORLD_SOURCE_SYMLINK) |
| 2466 |
.to_string_lossy(), |
| 2467 |
); |
| 2468 |
|
| 2469 |
//using -P -R option |
| 2470 |
scene |
| 2471 |
.ucmd() |
| 2472 |
.arg("-L") |
| 2473 |
.arg("-R") |
| 2474 |
.arg("-v") |
| 2475 |
.arg(TEST_COPY_FROM_FOLDER) |
| 2476 |
.arg(TEST_COPY_TO_FOLDER_NEW) |
| 2477 |
.succeeds(); |
| 2478 |
|
| 2479 |
#[cfg(not(windows))] |
| 2480 |
{ |
| 2481 |
let scene2 = TestScenario::new("ls"); |
| 2482 |
let result = scene2.cmd("ls").arg("-al").arg(path_to_new_symlink).run(); |
| 2483 |
println!("ls source {}", result.stdout_str()); |
| 2484 |
|
| 2485 |
let path_to_new_symlink = at.subdir.join(TEST_COPY_TO_FOLDER_NEW); |
| 2486 |
|
| 2487 |
let result = scene2.cmd("ls").arg("-al").arg(path_to_new_symlink).run(); |
| 2488 |
println!("ls dest {}", result.stdout_str()); |
| 2489 |
} |
| 2490 |
|
| 2491 |
#[cfg(windows)] |
| 2492 |
{ |
| 2493 |
// No action as this test is disabled but kept in case we want to |
| 2494 |
// try to make it work in the future. |
| 2495 |
let a = Command::new("cmd").args(&["/C", "dir"]).output(); |
| 2496 |
println!("output {a:#?}"); |
| 2497 |
|
| 2498 |
let a = Command::new("cmd") |
| 2499 |
.args(&["/C", "dir", &at.as_string()]) |
| 2500 |
.output(); |
| 2501 |
println!("output {a:#?}"); |
| 2502 |
|
| 2503 |
let a = Command::new("cmd") |
| 2504 |
.args(&["/C", "dir", path_to_new_symlink.to_str().unwrap()]) |
| 2505 |
.output(); |
| 2506 |
println!("output {a:#?}"); |
| 2507 |
|
| 2508 |
let path_to_new_symlink = at.subdir.join(TEST_COPY_FROM_FOLDER); |
| 2509 |
|
| 2510 |
let a = Command::new("cmd") |
| 2511 |
.args(&["/C", "dir", path_to_new_symlink.to_str().unwrap()]) |
| 2512 |
.output(); |
| 2513 |
println!("output {a:#?}"); |
| 2514 |
|
| 2515 |
let path_to_new_symlink = at.subdir.join(TEST_COPY_TO_FOLDER_NEW); |
| 2516 |
|
| 2517 |
let a = Command::new("cmd") |
| 2518 |
.args(&["/C", "dir", path_to_new_symlink.to_str().unwrap()]) |
| 2519 |
.output(); |
| 2520 |
println!("output {a:#?}"); |
| 2521 |
} |
| 2522 |
|
| 2523 |
let path_to_new_symlink = at |
| 2524 |
.subdir |
| 2525 |
.join(TEST_COPY_TO_FOLDER_NEW) |
| 2526 |
.join(TEST_HELLO_WORLD_SOURCE_SYMLINK); |
| 2527 |
assert!( |
| 2528 |
at.file_exists( |
| 2529 |
path_to_new_symlink |
| 2530 |
.clone() |
| 2531 |
.into_os_string() |
| 2532 |
.into_string() |
| 2533 |
.unwrap() |
| 2534 |
) |
| 2535 |
); |
| 2536 |
|
| 2537 |
let path_to_new = at.subdir.join(TEST_COPY_TO_FOLDER_NEW_FILE); |
| 2538 |
|
| 2539 |
// Check the content of the destination file that was copied. |
| 2540 |
let path_to_check = path_to_new.to_str().unwrap(); |
| 2541 |
assert_eq!(at.read(path_to_check), "Hello, World!\n"); |
| 2542 |
|
| 2543 |
// Check the content of the symlink |
| 2544 |
let path_to_check = path_to_new_symlink.to_str().unwrap(); |
| 2545 |
assert_eq!(at.read(path_to_check), "Hello, World!\n"); |
| 2546 |
} |
| 2547 |
|
| 2548 |
#[test] |
| 2549 |
// For now, disable the test on Windows. Symlinks aren't well support on Windows. |
| 2550 |
// It works on Unix for now and it works locally when run from a powershell |
| 2551 |
#[cfg(not(windows))] |
| 2552 |
#[cfg_attr( |
| 2553 |
wasi_runner, |
| 2554 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 2555 |
)] |
| 2556 |
fn test_cp_no_deref_folder_to_folder() { |
| 2557 |
let scene = TestScenario::new(util_name!()); |
| 2558 |
let at = &scene.fixtures; |
| 2559 |
|
| 2560 |
let path_to_new_symlink = at.plus(TEST_COPY_FROM_FOLDER); |
| 2561 |
|
| 2562 |
at.symlink_file( |
| 2563 |
&path_to_new_symlink |
| 2564 |
.join(TEST_HELLO_WORLD_SOURCE) |
| 2565 |
.to_string_lossy(), |
| 2566 |
&path_to_new_symlink |
| 2567 |
.join(TEST_HELLO_WORLD_SOURCE_SYMLINK) |
| 2568 |
.to_string_lossy(), |
| 2569 |
); |
| 2570 |
|
| 2571 |
//using -P -R option |
| 2572 |
scene |
| 2573 |
.ucmd() |
| 2574 |
.arg("-P") |
| 2575 |
.arg("-R") |
| 2576 |
.arg("-v") |
| 2577 |
.arg(TEST_COPY_FROM_FOLDER) |
| 2578 |
.arg(TEST_COPY_TO_FOLDER_NEW) |
| 2579 |
.succeeds(); |
| 2580 |
|
| 2581 |
#[cfg(not(windows))] |
| 2582 |
{ |
| 2583 |
let scene2 = TestScenario::new("ls"); |
| 2584 |
let result = scene2.cmd("ls").arg("-al").arg(path_to_new_symlink).run(); |
| 2585 |
println!("ls source {}", result.stdout_str()); |
| 2586 |
|
| 2587 |
let path_to_new_symlink = at.subdir.join(TEST_COPY_TO_FOLDER_NEW); |
| 2588 |
|
| 2589 |
let result = scene2.cmd("ls").arg("-al").arg(path_to_new_symlink).run(); |
| 2590 |
println!("ls dest {}", result.stdout_str()); |
| 2591 |
} |
| 2592 |
|
| 2593 |
#[cfg(windows)] |
| 2594 |
{ |
| 2595 |
// No action as this test is disabled but kept in case we want to |
| 2596 |
// try to make it work in the future. |
| 2597 |
let a = Command::new("cmd").args(&["/C", "dir"]).output(); |
| 2598 |
println!("output {a:#?}"); |
| 2599 |
|
| 2600 |
let a = Command::new("cmd") |
| 2601 |
.args(&["/C", "dir", &at.as_string()]) |
| 2602 |
.output(); |
| 2603 |
println!("output {a:#?}"); |
| 2604 |
|
| 2605 |
let a = Command::new("cmd") |
| 2606 |
.args(&["/C", "dir", path_to_new_symlink.to_str().unwrap()]) |
| 2607 |
.output(); |
| 2608 |
println!("output {a:#?}"); |
| 2609 |
|
| 2610 |
let path_to_new_symlink = at.subdir.join(TEST_COPY_FROM_FOLDER); |
| 2611 |
|
| 2612 |
let a = Command::new("cmd") |
| 2613 |
.args(&["/C", "dir", path_to_new_symlink.to_str().unwrap()]) |
| 2614 |
.output(); |
| 2615 |
println!("output {a:#?}"); |
| 2616 |
|
| 2617 |
let path_to_new_symlink = at.subdir.join(TEST_COPY_TO_FOLDER_NEW); |
| 2618 |
|
| 2619 |
let a = Command::new("cmd") |
| 2620 |
.args(&["/C", "dir", path_to_new_symlink.to_str().unwrap()]) |
| 2621 |
.output(); |
| 2622 |
println!("output {a:#?}"); |
| 2623 |
} |
| 2624 |
|
| 2625 |
let path_to_new_symlink = at |
| 2626 |
.subdir |
| 2627 |
.join(TEST_COPY_TO_FOLDER_NEW) |
| 2628 |
.join(TEST_HELLO_WORLD_SOURCE_SYMLINK); |
| 2629 |
assert!( |
| 2630 |
at.is_symlink( |
| 2631 |
&path_to_new_symlink |
| 2632 |
.clone() |
| 2633 |
.into_os_string() |
| 2634 |
.into_string() |
| 2635 |
.unwrap() |
| 2636 |
) |
| 2637 |
); |
| 2638 |
|
| 2639 |
let path_to_new = at.subdir.join(TEST_COPY_TO_FOLDER_NEW_FILE); |
| 2640 |
|
| 2641 |
// Check the content of the destination file that was copied. |
| 2642 |
let path_to_check = path_to_new.to_str().unwrap(); |
| 2643 |
assert_eq!(at.read(path_to_check), "Hello, World!\n"); |
| 2644 |
|
| 2645 |
// Check the content of the symlink |
| 2646 |
let path_to_check = path_to_new_symlink.to_str().unwrap(); |
| 2647 |
assert_eq!(at.read(path_to_check), "Hello, World!\n"); |
| 2648 |
} |
| 2649 |
|
| 2650 |
#[test] |
| 2651 |
#[cfg(target_os = "linux")] |
| 2652 |
#[cfg_attr( |
| 2653 |
wasi_runner, |
| 2654 |
ignore = "WASI: ACL/xattr syscalls are not implemented, so --archive's attribute preservation fails" |
| 2655 |
)] |
| 2656 |
fn test_cp_archive() { |
| 2657 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 2658 |
let previous = std::time::SystemTime::now() - Duration::from_secs(3600); |
| 2659 |
// set the file creation/modification an hour ago |
| 2660 |
let file = std::fs::OpenOptions::new() |
| 2661 |
.write(true) |
| 2662 |
.open(at.plus(TEST_HELLO_WORLD_SOURCE)) |
| 2663 |
.unwrap(); |
| 2664 |
file.set_times( |
| 2665 |
std_fs::FileTimes::new() |
| 2666 |
.set_accessed(previous) |
| 2667 |
.set_modified(previous), |
| 2668 |
) |
| 2669 |
.unwrap(); |
| 2670 |
ucmd.arg(TEST_HELLO_WORLD_SOURCE) |
| 2671 |
.arg("--archive") |
| 2672 |
.arg(TEST_HOW_ARE_YOU_SOURCE) |
| 2673 |
.succeeds(); |
| 2674 |
|
| 2675 |
assert_eq!(at.read(TEST_HOW_ARE_YOU_SOURCE), "Hello, World!\n"); |
| 2676 |
|
| 2677 |
let metadata = std_fs::metadata(at.subdir.join(TEST_HELLO_WORLD_SOURCE)).unwrap(); |
| 2678 |
let creation = metadata.modified().unwrap(); |
| 2679 |
|
| 2680 |
let metadata2 = std_fs::metadata(at.subdir.join(TEST_HOW_ARE_YOU_SOURCE)).unwrap(); |
| 2681 |
let creation2 = metadata2.modified().unwrap(); |
| 2682 |
|
| 2683 |
let scene2 = TestScenario::new("ls"); |
| 2684 |
let result = scene2.cmd("ls").arg("-al").arg(at.subdir).succeeds(); |
| 2685 |
|
| 2686 |
println!("ls dest {}", result.stdout_str()); |
| 2687 |
assert_eq!(creation, creation2); |
| 2688 |
} |
| 2689 |
|
| 2690 |
#[test] |
| 2691 |
#[cfg(all(unix, not(target_os = "android")))] |
| 2692 |
#[cfg_attr( |
| 2693 |
wasi_runner, |
| 2694 |
ignore = "WASI: no chmod syscall, so required mode/ownership preservation always fails" |
| 2695 |
)] |
| 2696 |
fn test_cp_archive_recursive() { |
| 2697 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 2698 |
|
| 2699 |
// creates |
| 2700 |
// dir/1 |
| 2701 |
// dir/1.link => dir/1 |
| 2702 |
// dir/2 |
| 2703 |
// dir/2.link => dir/2 |
| 2704 |
|
| 2705 |
let file_1 = at.subdir.join(TEST_COPY_TO_FOLDER).join("1"); |
| 2706 |
let file_1_link = at.subdir.join(TEST_COPY_TO_FOLDER).join("1.link"); |
| 2707 |
let file_2 = at.subdir.join(TEST_COPY_TO_FOLDER).join("2"); |
| 2708 |
let file_2_link = at.subdir.join(TEST_COPY_TO_FOLDER).join("2.link"); |
| 2709 |
|
| 2710 |
at.touch(file_1); |
| 2711 |
at.touch(file_2); |
| 2712 |
|
| 2713 |
at.symlink_file("1", &file_1_link.to_string_lossy()); |
| 2714 |
at.symlink_file("2", &file_2_link.to_string_lossy()); |
| 2715 |
|
| 2716 |
ucmd.arg("--archive") |
| 2717 |
.arg(TEST_COPY_TO_FOLDER) |
| 2718 |
.arg(TEST_COPY_TO_FOLDER_NEW) |
| 2719 |
.succeeds(); |
| 2720 |
|
| 2721 |
let scene2 = TestScenario::new("ls"); |
| 2722 |
let result = scene2 |
| 2723 |
.cmd("ls") |
| 2724 |
.arg("-al") |
| 2725 |
.arg(at.subdir.join(TEST_COPY_TO_FOLDER)) |
| 2726 |
.run(); |
| 2727 |
|
| 2728 |
println!("ls dest {}", result.stdout_str()); |
| 2729 |
|
| 2730 |
let result = scene2 |
| 2731 |
.cmd("ls") |
| 2732 |
.arg("-al") |
| 2733 |
.arg(at.subdir.join(TEST_COPY_TO_FOLDER_NEW)) |
| 2734 |
.run(); |
| 2735 |
|
| 2736 |
println!("ls dest {}", result.stdout_str()); |
| 2737 |
assert!(at.file_exists(at.subdir.join(TEST_COPY_TO_FOLDER_NEW).join("1"))); |
| 2738 |
assert!(at.file_exists(at.subdir.join(TEST_COPY_TO_FOLDER_NEW).join("2"))); |
| 2739 |
|
| 2740 |
assert!( |
| 2741 |
at.is_symlink( |
| 2742 |
&at.subdir |
| 2743 |
.join(TEST_COPY_TO_FOLDER_NEW) |
| 2744 |
.join("1.link") |
| 2745 |
.to_string_lossy() |
| 2746 |
) |
| 2747 |
); |
| 2748 |
assert!( |
| 2749 |
at.is_symlink( |
| 2750 |
&at.subdir |
| 2751 |
.join(TEST_COPY_TO_FOLDER_NEW) |
| 2752 |
.join("2.link") |
| 2753 |
.to_string_lossy() |
| 2754 |
) |
| 2755 |
); |
| 2756 |
} |
| 2757 |
|
| 2758 |
#[test] |
| 2759 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 2760 |
fn test_cp_preserve_timestamps() { |
| 2761 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 2762 |
let previous = std::time::SystemTime::now() - Duration::from_secs(3600); |
| 2763 |
// set the file creation/modification an hour ago |
| 2764 |
let file = std::fs::OpenOptions::new() |
| 2765 |
.write(true) |
| 2766 |
.open(at.plus(TEST_HELLO_WORLD_SOURCE)) |
| 2767 |
.unwrap(); |
| 2768 |
file.set_times( |
| 2769 |
std_fs::FileTimes::new() |
| 2770 |
.set_accessed(previous) |
| 2771 |
.set_modified(previous), |
| 2772 |
) |
| 2773 |
.unwrap(); |
| 2774 |
ucmd.arg(TEST_HELLO_WORLD_SOURCE) |
| 2775 |
.arg("--preserve=timestamps") |
| 2776 |
.arg(TEST_HOW_ARE_YOU_SOURCE) |
| 2777 |
.succeeds(); |
| 2778 |
|
| 2779 |
assert_eq!(at.read(TEST_HOW_ARE_YOU_SOURCE), "Hello, World!\n"); |
| 2780 |
|
| 2781 |
let metadata = std_fs::metadata(at.subdir.join(TEST_HELLO_WORLD_SOURCE)).unwrap(); |
| 2782 |
let creation = metadata.modified().unwrap(); |
| 2783 |
|
| 2784 |
let metadata2 = std_fs::metadata(at.subdir.join(TEST_HOW_ARE_YOU_SOURCE)).unwrap(); |
| 2785 |
let creation2 = metadata2.modified().unwrap(); |
| 2786 |
|
| 2787 |
let scene2 = TestScenario::new("ls"); |
| 2788 |
let result = scene2.cmd("ls").arg("-al").arg(at.subdir).run(); |
| 2789 |
|
| 2790 |
println!("ls dest {}", result.stdout_str()); |
| 2791 |
assert_eq!(creation, creation2); |
| 2792 |
} |
| 2793 |
|
| 2794 |
#[test] |
| 2795 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 2796 |
fn test_cp_no_preserve_timestamps() { |
| 2797 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 2798 |
let previous = std::time::SystemTime::now() - Duration::from_secs(3600); |
| 2799 |
// set the file creation/modification an hour ago |
| 2800 |
let file = std::fs::OpenOptions::new() |
| 2801 |
.write(true) |
| 2802 |
.open(at.plus(TEST_HELLO_WORLD_SOURCE)) |
| 2803 |
.unwrap(); |
| 2804 |
file.set_times( |
| 2805 |
std_fs::FileTimes::new() |
| 2806 |
.set_accessed(previous) |
| 2807 |
.set_modified(previous), |
| 2808 |
) |
| 2809 |
.unwrap(); |
| 2810 |
sleep(Duration::from_millis(100)); |
| 2811 |
|
| 2812 |
ucmd.arg(TEST_HELLO_WORLD_SOURCE) |
| 2813 |
.arg("--no-preserve=timestamps") |
| 2814 |
.arg(TEST_HOW_ARE_YOU_SOURCE) |
| 2815 |
.succeeds(); |
| 2816 |
|
| 2817 |
assert_eq!(at.read(TEST_HOW_ARE_YOU_SOURCE), "Hello, World!\n"); |
| 2818 |
|
| 2819 |
let metadata = std_fs::metadata(at.subdir.join(TEST_HELLO_WORLD_SOURCE)).unwrap(); |
| 2820 |
let creation = metadata.modified().unwrap(); |
| 2821 |
|
| 2822 |
let metadata2 = std_fs::metadata(at.subdir.join(TEST_HOW_ARE_YOU_SOURCE)).unwrap(); |
| 2823 |
let creation2 = metadata2.modified().unwrap(); |
| 2824 |
|
| 2825 |
let scene2 = TestScenario::new("ls"); |
| 2826 |
let result = scene2.cmd("ls").arg("-al").arg(at.subdir).run(); |
| 2827 |
|
| 2828 |
println!("ls dest {}", result.stdout_str()); |
| 2829 |
println!("creation {creation:?} / {creation2:?}"); |
| 2830 |
|
| 2831 |
assert_ne!(creation, creation2); |
| 2832 |
let res = creation |
| 2833 |
.elapsed() |
| 2834 |
.unwrap() |
| 2835 |
.checked_sub(creation2.elapsed().unwrap()) |
| 2836 |
.unwrap(); |
| 2837 |
// Some margins with time check |
| 2838 |
assert!(res.as_secs() > 3595); |
| 2839 |
assert!(res.as_secs() < 3605); |
| 2840 |
} |
| 2841 |
|
| 2842 |
// `filetime::FileTime::from_last_{access,modification}_time` panics on WASI |
| 2843 |
// (the crate has no WASI-specific backend). `cp --preserve=timestamps` must |
| 2844 |
// instead read timestamps via `Metadata::accessed`/`modified` there. See |
| 2845 |
// `source_times` in src/uu/cp/src/cp.rs. |
| 2846 |
#[test] |
| 2847 |
#[cfg(wasi_runner)] |
| 2848 |
fn test_cp_preserve_timestamps_wasi() { |
| 2849 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 2850 |
let previous = std::time::SystemTime::now() - Duration::from_secs(3600); |
| 2851 |
// set the file creation/modification an hour ago |
| 2852 |
let file = std::fs::OpenOptions::new() |
| 2853 |
.write(true) |
| 2854 |
.open(at.plus(TEST_HELLO_WORLD_SOURCE)) |
| 2855 |
.unwrap(); |
| 2856 |
file.set_times( |
| 2857 |
std::fs::FileTimes::new() |
| 2858 |
.set_accessed(previous) |
| 2859 |
.set_modified(previous), |
| 2860 |
) |
| 2861 |
.unwrap(); |
| 2862 |
|
| 2863 |
ucmd.arg(TEST_HELLO_WORLD_SOURCE) |
| 2864 |
.arg("--preserve=timestamps") |
| 2865 |
.arg(TEST_HOW_ARE_YOU_SOURCE) |
| 2866 |
.succeeds(); |
| 2867 |
|
| 2868 |
assert_eq!(at.read(TEST_HOW_ARE_YOU_SOURCE), "Hello, World!\n"); |
| 2869 |
|
| 2870 |
let src_mtime = std::fs::metadata(at.subdir.join(TEST_HELLO_WORLD_SOURCE)) |
| 2871 |
.unwrap() |
| 2872 |
.modified() |
| 2873 |
.unwrap(); |
| 2874 |
let dst_mtime = std::fs::metadata(at.subdir.join(TEST_HOW_ARE_YOU_SOURCE)) |
| 2875 |
.unwrap() |
| 2876 |
.modified() |
| 2877 |
.unwrap(); |
| 2878 |
assert_eq!(src_mtime, dst_mtime); |
| 2879 |
} |
| 2880 |
|
| 2881 |
// WASI has no chmod syscall at all, so `fs::set_permissions` always fails |
| 2882 |
// with ENOSYS there. A plain recursive copy tries to match a freshly |
| 2883 |
// created destination directory's mode to the source's as a best-effort |
| 2884 |
// (not explicitly requested) preservation; `cp` must treat ENOSYS the same |
| 2885 |
// as EOPNOTSUPP and skip it silently instead of erroring out. See |
| 2886 |
// `is_enotsup_error` in src/uu/cp/src/cp.rs. |
| 2887 |
#[test] |
| 2888 |
#[cfg(wasi_runner)] |
| 2889 |
fn test_cp_preserve_mode_optional_wasi() { |
| 2890 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 2891 |
at.mkdir("src"); |
| 2892 |
at.touch("src/file"); |
| 2893 |
|
| 2894 |
ucmd.args(&["-r", "src", "dst"]).succeeds().no_stderr(); |
| 2895 |
|
| 2896 |
assert!(at.dir_exists("dst")); |
| 2897 |
assert!(at.file_exists("dst/file")); |
| 2898 |
} |
| 2899 |
|
| 2900 |
#[test] |
| 2901 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 2902 |
#[cfg_attr( |
| 2903 |
wasi_runner, |
| 2904 |
ignore = "WASI sandbox: host paths (/dev, /private) not visible" |
| 2905 |
)] |
| 2906 |
fn test_cp_target_file_dev_null() { |
| 2907 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 2908 |
let file1 = "/dev/null"; |
| 2909 |
let file2 = "test_cp_target_file_file_i2"; |
| 2910 |
|
| 2911 |
at.touch(file2); |
| 2912 |
ucmd.arg(file1).arg(file2).succeeds().no_stderr(); |
| 2913 |
|
| 2914 |
assert!(at.file_exists(file2)); |
| 2915 |
} |
| 2916 |
|
| 2917 |
#[test] |
| 2918 |
#[cfg(any(target_os = "linux", target_os = "android", target_os = "freebsd"))] |
| 2919 |
fn test_cp_one_file_system() { |
| 2920 |
use uutests::util::AtPath; |
| 2921 |
use walkdir::WalkDir; |
| 2922 |
|
| 2923 |
let mut scene = TestScenario::new(util_name!()); |
| 2924 |
let at = &scene.fixtures; |
| 2925 |
|
| 2926 |
// Test must be run as root (or with `sudo -E`) |
| 2927 |
if scene.cmd("whoami").run().stdout_str() != "root\n" { |
| 2928 |
return; |
| 2929 |
} |
| 2930 |
|
| 2931 |
let at_src = AtPath::new(&at.plus(TEST_MOUNT_COPY_FROM_FOLDER)); |
| 2932 |
let at_dst = AtPath::new(&at.plus(TEST_COPY_TO_FOLDER_NEW)); |
| 2933 |
|
| 2934 |
// Prepare the mount |
| 2935 |
at_src.mkdir(TEST_MOUNT_MOUNTPOINT); |
| 2936 |
let mountpoint_path = &at_src.plus_as_string(TEST_MOUNT_MOUNTPOINT); |
| 2937 |
|
| 2938 |
scene |
| 2939 |
.mount_temp_fs(mountpoint_path) |
| 2940 |
.expect("mounting tmpfs failed"); |
| 2941 |
|
| 2942 |
at_src.touch(TEST_MOUNT_OTHER_FILESYSTEM_FILE); |
| 2943 |
|
| 2944 |
// Begin testing -x flag |
| 2945 |
scene |
| 2946 |
.ucmd() |
| 2947 |
.arg("-rx") |
| 2948 |
.arg(TEST_MOUNT_COPY_FROM_FOLDER) |
| 2949 |
.arg(TEST_COPY_TO_FOLDER_NEW) |
| 2950 |
.succeeds(); |
| 2951 |
|
| 2952 |
// Ditch the mount before the asserts |
| 2953 |
scene.umount_temp_fs(); |
| 2954 |
|
| 2955 |
assert!(!at_dst.file_exists(TEST_MOUNT_OTHER_FILESYSTEM_FILE)); |
| 2956 |
// Check if the other files were copied from the source folder hierarchy |
| 2957 |
for entry in WalkDir::new(at_src.as_string()) { |
| 2958 |
let entry = entry.unwrap(); |
| 2959 |
let relative_src = entry |
| 2960 |
.path() |
| 2961 |
.strip_prefix(at_src.as_string()) |
| 2962 |
.unwrap() |
| 2963 |
.to_str() |
| 2964 |
.unwrap(); |
| 2965 |
|
| 2966 |
let ft = entry.file_type(); |
| 2967 |
match (ft.is_dir(), ft.is_file(), ft.is_symlink()) { |
| 2968 |
(true, _, _) => assert!(at_dst.dir_exists(relative_src)), |
| 2969 |
(_, true, _) => assert!(at_dst.file_exists(relative_src)), |
| 2970 |
(_, _, _) => panic!(), |
| 2971 |
} |
| 2972 |
} |
| 2973 |
} |
| 2974 |
|
| 2975 |
#[test] |
| 2976 |
#[cfg(any(target_vendor = "apple", target_os = "linux", target_os = "android"))] |
| 2977 |
fn test_cp_reflink_always() { |
| 2978 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 2979 |
let result = ucmd |
| 2980 |
.arg("--reflink=always") |
| 2981 |
.arg(TEST_HELLO_WORLD_SOURCE) |
| 2982 |
.arg(TEST_EXISTING_FILE) |
| 2983 |
.run(); |
| 2984 |
|
| 2985 |
if result.succeeded() { |
| 2986 |
// Check the content of the destination file |
| 2987 |
assert_eq!(at.read(TEST_EXISTING_FILE), "Hello, World!\n"); |
| 2988 |
} else { |
| 2989 |
// Older Linux versions do not support cloning. |
| 2990 |
} |
| 2991 |
} |
| 2992 |
|
| 2993 |
#[test] |
| 2994 |
#[cfg(any(target_vendor = "apple", target_os = "linux", target_os = "android"))] |
| 2995 |
fn test_cp_reflink_auto() { |
| 2996 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 2997 |
ucmd.arg("--reflink=auto") |
| 2998 |
.arg(TEST_HELLO_WORLD_SOURCE) |
| 2999 |
.arg(TEST_EXISTING_FILE) |
| 3000 |
.succeeds(); |
| 3001 |
|
| 3002 |
// Check the content of the destination file |
| 3003 |
assert_eq!(at.read(TEST_EXISTING_FILE), "Hello, World!\n"); |
| 3004 |
} |
| 3005 |
|
| 3006 |
#[test] |
| 3007 |
#[cfg(any(target_vendor = "apple", target_os = "linux", target_os = "android"))] |
| 3008 |
fn test_cp_reflink_none() { |
| 3009 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 3010 |
let result = ucmd |
| 3011 |
.arg("--reflink") |
| 3012 |
.arg(TEST_HELLO_WORLD_SOURCE) |
| 3013 |
.arg(TEST_EXISTING_FILE) |
| 3014 |
.run(); |
| 3015 |
|
| 3016 |
if result.succeeded() { |
| 3017 |
// Check the content of the destination file |
| 3018 |
assert_eq!(at.read(TEST_EXISTING_FILE), "Hello, World!\n"); |
| 3019 |
} else { |
| 3020 |
// Older Linux versions do not support cloning. |
| 3021 |
} |
| 3022 |
} |
| 3023 |
|
| 3024 |
#[test] |
| 3025 |
#[cfg(any(target_vendor = "apple", target_os = "linux", target_os = "android"))] |
| 3026 |
fn test_cp_reflink_never() { |
| 3027 |
for argument in ["--reflink=never", "--reflink=neve", "--reflink=n"] { |
| 3028 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 3029 |
ucmd.arg(argument) |
| 3030 |
.arg(TEST_HELLO_WORLD_SOURCE) |
| 3031 |
.arg(TEST_EXISTING_FILE) |
| 3032 |
.succeeds(); |
| 3033 |
|
| 3034 |
// Check the content of the destination file |
| 3035 |
assert_eq!(at.read(TEST_EXISTING_FILE), "Hello, World!\n"); |
| 3036 |
// TODO: make Btrfs image without sudo dynamically to support GitHub runner |
| 3037 |
#[cfg(target_os = "linux")] |
| 3038 |
let might_reflink = std::process::Command::new("filefrag") |
| 3039 |
.arg("-v") |
| 3040 |
.arg(TEST_EXISTING_FILE) |
| 3041 |
.output() |
| 3042 |
.map(|o| o.stdout.windows(6).any(|w| w == b"shared")) |
| 3043 |
.unwrap(); |
| 3044 |
#[cfg(target_os = "linux")] |
| 3045 |
assert!(!might_reflink, "--reflink=never did not work"); |
| 3046 |
} |
| 3047 |
} |
| 3048 |
|
| 3049 |
// Regression test for #14052: on macOS, `cp` used to call clonefile(2) unconditionally, so |
| 3050 |
// `--reflink=never` still cloned — and clonefile copies the source's metadata, including mtime. |
| 3051 |
// A real (non-clone) copy stamps the destination with its own mtime, so after `--reflink=never` |
| 3052 |
// the destination must NOT inherit the source's (old) mtime. |
| 3053 |
#[test] |
| 3054 |
#[cfg(target_vendor = "apple")] |
| 3055 |
fn test_cp_reflink_never_does_not_clonefile() { |
| 3056 |
use filetime::FileTime; // todo: replace with std |
| 3057 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 3058 |
at.write("src", "reflink never contents"); |
| 3059 |
// Stamp the source with a mtime well in the past; a clonefile would copy it verbatim. |
| 3060 |
let past = FileTime::from_unix_time(1_000_000_000, 0); // 2001-09-09 |
| 3061 |
filetime::set_file_times(at.plus("src"), past, past).unwrap(); |
| 3062 |
|
| 3063 |
ucmd.arg("--reflink=never").arg("src").arg("dst").succeeds(); |
| 3064 |
|
| 3065 |
assert_eq!(at.read("dst"), "reflink never contents"); |
| 3066 |
let src_mtime = FileTime::from_last_modification_time(&at.metadata("src")); |
| 3067 |
let dst_mtime = FileTime::from_last_modification_time(&at.metadata("dst")); |
| 3068 |
assert_ne!( |
| 3069 |
dst_mtime, src_mtime, |
| 3070 |
"--reflink=never must copy (not clonefile), so dst must not inherit the source's mtime" |
| 3071 |
); |
| 3072 |
} |
| 3073 |
|
| 3074 |
#[test] |
| 3075 |
#[cfg(any(target_vendor = "apple", target_os = "linux", target_os = "android"))] |
| 3076 |
fn test_cp_reflink_bad() { |
| 3077 |
let (_, mut ucmd) = at_and_ucmd!(); |
| 3078 |
let _result = ucmd |
| 3079 |
.arg("--reflink=bad") |
| 3080 |
.arg(TEST_HELLO_WORLD_SOURCE) |
| 3081 |
.arg(TEST_EXISTING_FILE) |
| 3082 |
.fails() |
| 3083 |
.stderr_contains("error: invalid value 'bad' for '--reflink[=<WHEN>]'"); |
| 3084 |
} |
| 3085 |
|
| 3086 |
#[test] |
| 3087 |
fn test_cp_conflicting_update() { |
| 3088 |
new_ucmd!() |
| 3089 |
.arg("-b") |
| 3090 |
.arg("--update=none") |
| 3091 |
.arg("a") |
| 3092 |
.arg("b") |
| 3093 |
.fails() |
| 3094 |
.stderr_contains("--backup is mutually exclusive with -n or --update=none-fail"); |
| 3095 |
} |
| 3096 |
|
| 3097 |
#[test] |
| 3098 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 3099 |
fn test_cp_reflink_insufficient_permission() { |
| 3100 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 3101 |
|
| 3102 |
at.make_file("unreadable") |
| 3103 |
.set_permissions(PermissionsExt::from_mode(0o000)) |
| 3104 |
.unwrap(); |
| 3105 |
|
| 3106 |
ucmd.arg("-r") |
| 3107 |
.arg("--reflink=auto") |
| 3108 |
.arg("unreadable") |
| 3109 |
.arg(TEST_EXISTING_FILE) |
| 3110 |
.fails() |
| 3111 |
.stderr_only("cp: 'unreadable' -> 'existing_file.txt': Permission denied\n"); |
| 3112 |
} |
| 3113 |
|
| 3114 |
#[cfg(target_os = "linux")] |
| 3115 |
#[test] |
| 3116 |
fn test_closes_file_descriptors() { |
| 3117 |
use rlimit::Resource; |
| 3118 |
|
| 3119 |
let pid = std::process::id(); |
| 3120 |
let fd_path = format!("/proc/{pid}/fd"); |
| 3121 |
|
| 3122 |
// The test suite runs in parallel, we have pipe, sockets |
| 3123 |
// opened by other tests. |
| 3124 |
// So, we take in account the various fd to increase the limit |
| 3125 |
let number_file_already_opened: u64 = std::fs::read_dir(fd_path) |
| 3126 |
.unwrap() |
| 3127 |
.count() |
| 3128 |
.try_into() |
| 3129 |
.unwrap(); |
| 3130 |
let limit_fd: u64 = number_file_already_opened + 9; |
| 3131 |
|
| 3132 |
new_ucmd!() |
| 3133 |
.arg("-r") |
| 3134 |
.arg("--reflink=auto") |
| 3135 |
.arg("dir_with_10_files/") |
| 3136 |
.arg("dir_with_10_files_new/") |
| 3137 |
.limit(Resource::NOFILE, limit_fd, limit_fd) |
| 3138 |
.succeeds(); |
| 3139 |
} |
| 3140 |
|
| 3141 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 3142 |
#[test] |
| 3143 |
#[cfg_attr(wasi_runner, ignore = "WASI: --sparse is only supported on linux")] |
| 3144 |
fn test_cp_sparse_never_empty() { |
| 3145 |
const BUFFER_SIZE: usize = 4096 * 4; |
| 3146 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 3147 |
|
| 3148 |
let buf: [u8; BUFFER_SIZE] = [0; BUFFER_SIZE]; |
| 3149 |
|
| 3150 |
at.make_file("src_file1"); |
| 3151 |
at.write_bytes("src_file1", &buf); |
| 3152 |
|
| 3153 |
ucmd.args(&["--sparse=never", "src_file1", "dst_file_non_sparse"]) |
| 3154 |
.succeeds(); |
| 3155 |
assert_eq!(at.read_bytes("dst_file_non_sparse"), buf); |
| 3156 |
assert_eq!( |
| 3157 |
at.metadata("dst_file_non_sparse").blocks() * 512, |
| 3158 |
buf.len() as u64 |
| 3159 |
); |
| 3160 |
} |
| 3161 |
|
| 3162 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 3163 |
#[test] |
| 3164 |
#[cfg_attr(wasi_runner, ignore = "WASI: --sparse is only supported on linux")] |
| 3165 |
fn test_cp_sparse_always_empty() { |
| 3166 |
const BUFFER_SIZE: usize = 4096 * 4; |
| 3167 |
for argument in ["--sparse=always", "--sparse=alway", "--sparse=al"] { |
| 3168 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 3169 |
|
| 3170 |
let buf: [u8; BUFFER_SIZE] = [0; BUFFER_SIZE]; |
| 3171 |
|
| 3172 |
at.make_file("src_file1"); |
| 3173 |
at.write_bytes("src_file1", &buf); |
| 3174 |
|
| 3175 |
ucmd.args(&[argument, "src_file1", "dst_file_sparse"]) |
| 3176 |
.succeeds(); |
| 3177 |
|
| 3178 |
assert_eq!(at.read_bytes("dst_file_sparse"), buf); |
| 3179 |
assert_eq!(at.metadata("dst_file_sparse").blocks(), 0); |
| 3180 |
} |
| 3181 |
} |
| 3182 |
|
| 3183 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 3184 |
#[test] |
| 3185 |
#[cfg_attr(wasi_runner, ignore = "WASI: --sparse is only supported on linux")] |
| 3186 |
fn test_cp_sparse_always_non_empty() { |
| 3187 |
const BUFFER_SIZE: usize = 4096 * 16 + 3; |
| 3188 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 3189 |
|
| 3190 |
let mut buf = vec![0; BUFFER_SIZE].into_boxed_slice(); |
| 3191 |
let blocks_to_touch = [buf.len() / 3, 2 * (buf.len() / 3)]; |
| 3192 |
|
| 3193 |
for i in blocks_to_touch { |
| 3194 |
buf[i] = b'x'; |
| 3195 |
} |
| 3196 |
|
| 3197 |
at.make_file("src_file1"); |
| 3198 |
at.write_bytes("src_file1", &buf); |
| 3199 |
|
| 3200 |
ucmd.args(&["--sparse=always", "src_file1", "dst_file_sparse"]) |
| 3201 |
.succeeds(); |
| 3202 |
|
| 3203 |
let touched_block_count = |
| 3204 |
blocks_to_touch.len() as u64 * at.metadata("dst_file_sparse").blksize() / 512; |
| 3205 |
|
| 3206 |
assert_eq!(at.read_bytes("dst_file_sparse").into_boxed_slice(), buf); |
| 3207 |
assert_eq!(at.metadata("dst_file_sparse").blocks(), touched_block_count); |
| 3208 |
} |
| 3209 |
|
| 3210 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 3211 |
#[test] |
| 3212 |
fn test_cp_sparse_invalid_option() { |
| 3213 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 3214 |
|
| 3215 |
at.make_file("src_file1"); |
| 3216 |
|
| 3217 |
ucmd.args(&["--sparse=invalid", "src_file1", "dst_file"]) |
| 3218 |
.fails(); |
| 3219 |
} |
| 3220 |
|
| 3221 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 3222 |
#[test] |
| 3223 |
fn test_cp_sparse_always_reflink_always() { |
| 3224 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 3225 |
|
| 3226 |
at.make_file("src_file1"); |
| 3227 |
|
| 3228 |
ucmd.args(&[ |
| 3229 |
"--sparse=always", |
| 3230 |
"--reflink=always", |
| 3231 |
"src_file1", |
| 3232 |
"dst_file", |
| 3233 |
]) |
| 3234 |
.fails(); |
| 3235 |
} |
| 3236 |
|
| 3237 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 3238 |
#[test] |
| 3239 |
fn test_cp_sparse_never_reflink_always() { |
| 3240 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 3241 |
|
| 3242 |
at.make_file("src_file1"); |
| 3243 |
|
| 3244 |
ucmd.args(&[ |
| 3245 |
"--sparse=never", |
| 3246 |
"--reflink=always", |
| 3247 |
"src_file1", |
| 3248 |
"dst_file", |
| 3249 |
]) |
| 3250 |
.fails(); |
| 3251 |
} |
| 3252 |
|
| 3253 |
// Regression test for https://github.com/uutils/coreutils/issues/12186 |
| 3254 |
// `cp --sparse=always` should be supported on Windows (matching GNU), not |
| 3255 |
// rejected with "--sparse is only supported on linux". |
| 3256 |
#[cfg(windows)] |
| 3257 |
#[test] |
| 3258 |
fn test_cp_sparse_always_windows() { |
| 3259 |
use std::os::windows::fs::MetadataExt; |
| 3260 |
const BUFFER_SIZE: usize = 4096 * 16 + 3; |
| 3261 |
const FILE_ATTRIBUTE_SPARSE_FILE: u32 = 0x0000_0200; |
| 3262 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 3263 |
|
| 3264 |
// A file with long runs of zeros: a candidate for sparse copying. |
| 3265 |
let mut buf = vec![0; BUFFER_SIZE].into_boxed_slice(); |
| 3266 |
let bytes_to_touch = [buf.len() / 3, 2 * (buf.len() / 3)]; |
| 3267 |
for i in bytes_to_touch { |
| 3268 |
buf[i] = b'x'; |
| 3269 |
} |
| 3270 |
|
| 3271 |
at.make_file("src_file1"); |
| 3272 |
at.write_bytes("src_file1", &buf); |
| 3273 |
|
| 3274 |
ucmd.args(&["--sparse=always", "src_file1", "dst_file_sparse"]) |
| 3275 |
.succeeds(); |
| 3276 |
|
| 3277 |
// The copy must be byte-for-byte identical to the source... |
| 3278 |
assert_eq!(at.read_bytes("dst_file_sparse").into_boxed_slice(), buf); |
| 3279 |
|
| 3280 |
// ...and the destination must actually be flagged sparse (proving the |
| 3281 |
// FSCTL_SET_SPARSE path ran rather than a plain copy). The temp dir used by |
| 3282 |
// the test harness is on NTFS, which supports sparse files. |
| 3283 |
assert_ne!( |
| 3284 |
at.metadata("dst_file_sparse").file_attributes() & FILE_ATTRIBUTE_SPARSE_FILE, |
| 3285 |
0, |
| 3286 |
"destination should have the sparse file attribute set" |
| 3287 |
); |
| 3288 |
} |
| 3289 |
|
| 3290 |
// Companion to the regression above: `cp --sparse=never` must also be accepted |
| 3291 |
// on Windows and produce a faithful, non-sparse copy (it was rejected by the |
| 3292 |
// same "--sparse is only supported on linux" error before #12186). |
| 3293 |
#[cfg(windows)] |
| 3294 |
#[test] |
| 3295 |
fn test_cp_sparse_never_windows() { |
| 3296 |
use std::os::windows::fs::MetadataExt; |
| 3297 |
const BUFFER_SIZE: usize = 4096 * 4; |
| 3298 |
const FILE_ATTRIBUTE_SPARSE_FILE: u32 = 0x0000_0200; |
| 3299 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 3300 |
|
| 3301 |
let buf: [u8; BUFFER_SIZE] = [0; BUFFER_SIZE]; |
| 3302 |
at.make_file("src_file1"); |
| 3303 |
at.write_bytes("src_file1", &buf); |
| 3304 |
|
| 3305 |
ucmd.args(&["--sparse=never", "src_file1", "dst_file_non_sparse"]) |
| 3306 |
.succeeds(); |
| 3307 |
|
| 3308 |
assert_eq!(at.read_bytes("dst_file_non_sparse"), buf); |
| 3309 |
|
| 3310 |
assert_eq!( |
| 3311 |
at.metadata("dst_file_non_sparse").file_attributes() & FILE_ATTRIBUTE_SPARSE_FILE, |
| 3312 |
0, |
| 3313 |
"destination must not be sparse with --sparse=never" |
| 3314 |
); |
| 3315 |
} |
| 3316 |
|
| 3317 |
// `--sparse=auto` (the default) must preserve an already-sparse source on Windows, |
| 3318 |
// matching GNU. Before this fix the default mode did a plain copy that dropped the |
| 3319 |
// source's holes. A non-sparse source must still copy plainly (no new holes). |
| 3320 |
#[cfg(windows)] |
| 3321 |
#[test] |
| 3322 |
fn test_cp_sparse_auto_preserves_sparse_source_windows() { |
| 3323 |
use std::os::windows::fs::MetadataExt; |
| 3324 |
const FILE_ATTRIBUTE_SPARSE_FILE: u32 = 0x0000_0200; |
| 3325 |
const BUFFER_SIZE: usize = 1024 * 1024; |
| 3326 |
|
| 3327 |
let scene = TestScenario::new(util_name!()); |
| 3328 |
let at = &scene.fixtures; |
| 3329 |
|
| 3330 |
// A 1 MiB file with data only near the start and end: a clear sparse candidate. |
| 3331 |
let mut buf = vec![0u8; BUFFER_SIZE].into_boxed_slice(); |
| 3332 |
buf[1024] = b'x'; |
| 3333 |
buf[BUFFER_SIZE - 2048] = b'y'; |
| 3334 |
at.make_file("src"); |
| 3335 |
at.write_bytes("src", &buf); |
| 3336 |
|
| 3337 |
// Seed a genuinely sparse source via the already-supported --sparse=always. |
| 3338 |
scene |
| 3339 |
.ucmd() |
| 3340 |
.args(&["--sparse=always", "src", "sparse_src"]) |
| 3341 |
.succeeds(); |
| 3342 |
assert_ne!( |
| 3343 |
at.metadata("sparse_src").file_attributes() & FILE_ATTRIBUTE_SPARSE_FILE, |
| 3344 |
0, |
| 3345 |
"precondition: seeded source should be sparse" |
| 3346 |
); |
| 3347 |
|
| 3348 |
// Default mode is --sparse=auto: an already-sparse source must stay sparse. |
| 3349 |
scene.ucmd().args(&["sparse_src", "auto_dst"]).succeeds(); |
| 3350 |
assert_eq!( |
| 3351 |
at.read_bytes("auto_dst").into_boxed_slice(), |
| 3352 |
buf, |
| 3353 |
"auto copy must be byte-for-byte identical to the source" |
| 3354 |
); |
| 3355 |
assert_ne!( |
| 3356 |
at.metadata("auto_dst").file_attributes() & FILE_ATTRIBUTE_SPARSE_FILE, |
| 3357 |
0, |
| 3358 |
"--sparse=auto should preserve an already-sparse source" |
| 3359 |
); |
| 3360 |
|
| 3361 |
// A non-sparse source copied with the default mode must NOT become sparse. |
| 3362 |
at.make_file("plain_src"); |
| 3363 |
at.write_bytes("plain_src", &buf); |
| 3364 |
scene.ucmd().args(&["plain_src", "plain_dst"]).succeeds(); |
| 3365 |
assert_eq!( |
| 3366 |
at.metadata("plain_dst").file_attributes() & FILE_ATTRIBUTE_SPARSE_FILE, |
| 3367 |
0, |
| 3368 |
"--sparse=auto must not make a non-sparse source sparse" |
| 3369 |
); |
| 3370 |
} |
| 3371 |
|
| 3372 |
// `--reflink=auto` means "clone if possible, else plain copy" (GNU), so on |
| 3373 |
// Windows — which has no reflink support — it must fall back to a plain copy |
| 3374 |
// rather than fail. |
| 3375 |
#[cfg(windows)] |
| 3376 |
#[test] |
| 3377 |
fn test_cp_reflink_auto_windows() { |
| 3378 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 3379 |
at.write("src", "hello"); |
| 3380 |
|
| 3381 |
ucmd.args(&["--reflink=auto", "src", "dst"]).succeeds(); |
| 3382 |
|
| 3383 |
assert_eq!(at.read("dst"), "hello"); |
| 3384 |
} |
| 3385 |
|
| 3386 |
// `--reflink=always` (and bare `--reflink`, which defaults to `always`) must |
| 3387 |
// still fail on Windows: cloning was explicitly required but is unsupported. |
| 3388 |
#[cfg(windows)] |
| 3389 |
#[test] |
| 3390 |
fn test_cp_reflink_always_windows() { |
| 3391 |
for argument in ["--reflink=always", "--reflink"] { |
| 3392 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 3393 |
at.write("src", "hello"); |
| 3394 |
|
| 3395 |
ucmd.args(&[argument, "src", "dst"]).fails(); |
| 3396 |
} |
| 3397 |
} |
| 3398 |
|
| 3399 |
// `--reflink=auto` means "clone if possible, else plain copy" (GNU); WASI has |
| 3400 |
// no reflink support, so `copy_on_write` (src/uu/cp/src/platform/other.rs) |
| 3401 |
// must fall back to a plain copy rather than fail. |
| 3402 |
#[cfg(wasi_runner)] |
| 3403 |
#[test] |
| 3404 |
fn test_cp_reflink_auto_wasi() { |
| 3405 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 3406 |
at.write("src", "hello"); |
| 3407 |
|
| 3408 |
ucmd.args(&["--reflink=auto", "src", "dst"]).succeeds(); |
| 3409 |
|
| 3410 |
assert_eq!(at.read("dst"), "hello"); |
| 3411 |
} |
| 3412 |
|
| 3413 |
// `--reflink=always` (and bare `--reflink`, which defaults to `always`) must |
| 3414 |
// still fail on WASI: cloning was explicitly required but is unsupported. |
| 3415 |
#[cfg(wasi_runner)] |
| 3416 |
#[test] |
| 3417 |
fn test_cp_reflink_always_wasi() { |
| 3418 |
for argument in ["--reflink=always", "--reflink"] { |
| 3419 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 3420 |
at.write("src", "hello"); |
| 3421 |
|
| 3422 |
ucmd.args(&[argument, "src", "dst"]).fails(); |
| 3423 |
} |
| 3424 |
} |
| 3425 |
|
| 3426 |
// `--debug` must report the sparse detection that actually happened: `zeros` |
| 3427 |
// when the sparse copy ran (the test temp dir is NTFS, which supports it). |
| 3428 |
#[cfg(windows)] |
| 3429 |
#[test] |
| 3430 |
fn test_cp_debug_sparse_always_windows() { |
| 3431 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 3432 |
at.write("a", "hello"); |
| 3433 |
|
| 3434 |
ucmd.args(&["--debug", "--sparse=always", "a", "b"]) |
| 3435 |
.succeeds() |
| 3436 |
.stdout_contains( |
| 3437 |
"copy offload: unsupported, reflink: unsupported, sparse detection: zeros", |
| 3438 |
); |
| 3439 |
} |
| 3440 |
|
| 3441 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 3442 |
#[cfg(feature = "truncate")] |
| 3443 |
#[test] |
| 3444 |
#[cfg_attr( |
| 3445 |
wasi_runner, |
| 3446 |
ignore = "WASI: --reflink is only supported on linux and macOS" |
| 3447 |
)] |
| 3448 |
fn test_cp_reflink_always_override() { |
| 3449 |
const DISK: &str = "disk.img"; |
| 3450 |
const ROOTDIR: &str = "disk_root/"; |
| 3451 |
const USERDIR: &str = "dir/"; |
| 3452 |
const MOUNTPOINT: &str = "mountpoint/"; |
| 3453 |
let scene = TestScenario::new(util_name!()); |
| 3454 |
|
| 3455 |
let src1_path: &str = &[MOUNTPOINT, USERDIR, "src1"].concat(); |
| 3456 |
let src2_path: &str = &[MOUNTPOINT, USERDIR, "src2"].concat(); |
| 3457 |
let dst_path: &str = &[MOUNTPOINT, USERDIR, "dst"].concat(); |
| 3458 |
|
| 3459 |
scene.fixtures.mkdir(ROOTDIR); |
| 3460 |
scene.fixtures.mkdir([ROOTDIR, USERDIR].concat()); |
| 3461 |
|
| 3462 |
// Setup: |
| 3463 |
// Because neither `mkfs.btrfs` not btrfs `mount` options allow us to have a mountpoint owned |
| 3464 |
// by a non-root user, we want the following directory structure: |
| 3465 |
// |
| 3466 |
// uid | path |
| 3467 |
// --------------------------- |
| 3468 |
// user | . |
| 3469 |
// root | └── mountpoint |
| 3470 |
// user | └── dir |
| 3471 |
// user | ├── src1 |
| 3472 |
// user | └── src2 |
| 3473 |
|
| 3474 |
scene |
| 3475 |
.ccmd("truncate") |
| 3476 |
.args(&["-s", "128M", DISK]) |
| 3477 |
.succeeds(); |
| 3478 |
|
| 3479 |
if !scene |
| 3480 |
.cmd("env") |
| 3481 |
.env("PATH", PATH) |
| 3482 |
.args(&["mkfs.btrfs", "--rootdir", ROOTDIR, DISK]) |
| 3483 |
.run() |
| 3484 |
.succeeded() |
| 3485 |
{ |
| 3486 |
print!("Test skipped; couldn't make btrfs disk image"); |
| 3487 |
return; |
| 3488 |
} |
| 3489 |
|
| 3490 |
scene.fixtures.mkdir(MOUNTPOINT); |
| 3491 |
|
| 3492 |
let mount = scene |
| 3493 |
.cmd("sudo") |
| 3494 |
.env("PATH", PATH) |
| 3495 |
.args(&["-E", "--non-interactive", "mount", DISK, MOUNTPOINT]) |
| 3496 |
.run(); |
| 3497 |
|
| 3498 |
if !mount.succeeded() { |
| 3499 |
print!("Test skipped; requires root user"); |
| 3500 |
return; |
| 3501 |
} |
| 3502 |
|
| 3503 |
scene.fixtures.make_file(src1_path); |
| 3504 |
scene.fixtures.write_bytes(src1_path, &[0x64; 8192]); |
| 3505 |
|
| 3506 |
scene.fixtures.make_file(src2_path); |
| 3507 |
scene.fixtures.write(src2_path, "other data"); |
| 3508 |
|
| 3509 |
scene |
| 3510 |
.ucmd() |
| 3511 |
.args(&["--reflink=always", src1_path, dst_path]) |
| 3512 |
.succeeds(); |
| 3513 |
|
| 3514 |
scene |
| 3515 |
.ucmd() |
| 3516 |
.args(&["--reflink=always", src2_path, dst_path]) |
| 3517 |
.succeeds(); |
| 3518 |
|
| 3519 |
scene |
| 3520 |
.cmd("sudo") |
| 3521 |
.env("PATH", PATH) |
| 3522 |
.args(&["-E", "--non-interactive", "umount", MOUNTPOINT]) |
| 3523 |
.succeeds(); |
| 3524 |
} |
| 3525 |
|
| 3526 |
#[test] |
| 3527 |
#[cfg_attr( |
| 3528 |
wasi_runner, |
| 3529 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 3530 |
)] |
| 3531 |
fn test_copy_dir_symlink() { |
| 3532 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 3533 |
at.mkdir("dir"); |
| 3534 |
at.symlink_dir("dir", "dir-link"); |
| 3535 |
ucmd.args(&["-r", "dir-link", "copy"]).succeeds(); |
| 3536 |
assert_eq!(at.resolve_link("copy"), "dir"); |
| 3537 |
} |
| 3538 |
|
| 3539 |
#[test] |
| 3540 |
#[cfg(not(target_os = "freebsd"))] // FIXME: fix this test for FreeBSD |
| 3541 |
#[cfg(feature = "ln")] |
| 3542 |
#[cfg_attr( |
| 3543 |
wasi_runner, |
| 3544 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 3545 |
)] |
| 3546 |
fn test_copy_dir_with_symlinks() { |
| 3547 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 3548 |
at.mkdir("dir"); |
| 3549 |
at.make_file("dir/file"); |
| 3550 |
|
| 3551 |
TestScenario::new("ln") |
| 3552 |
.ucmd() |
| 3553 |
.arg("-sr") |
| 3554 |
.arg(at.subdir.join("dir/file")) |
| 3555 |
.arg(at.subdir.join("dir/file-link")) |
| 3556 |
.succeeds(); |
| 3557 |
|
| 3558 |
ucmd.args(&["-r", "dir", "copy"]).succeeds(); |
| 3559 |
assert_eq!(at.resolve_link("copy/file-link"), "file"); |
| 3560 |
} |
| 3561 |
|
| 3562 |
#[test] |
| 3563 |
#[cfg(not(windows))] |
| 3564 |
#[cfg_attr( |
| 3565 |
wasi_runner, |
| 3566 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 3567 |
)] |
| 3568 |
fn test_copy_symlink_force() { |
| 3569 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 3570 |
at.touch("file"); |
| 3571 |
at.symlink_file("file", "file-link"); |
| 3572 |
at.touch("copy"); |
| 3573 |
|
| 3574 |
ucmd.args(&["file-link", "copy", "-f", "--no-dereference"]) |
| 3575 |
.succeeds(); |
| 3576 |
assert_eq!(at.resolve_link("copy"), "file"); |
| 3577 |
} |
| 3578 |
|
| 3579 |
#[test] |
| 3580 |
#[cfg(unix)] |
| 3581 |
#[cfg(not(target_os = "openbsd"))] |
| 3582 |
#[cfg_attr( |
| 3583 |
wasi_runner, |
| 3584 |
ignore = "WASI: no chmod syscall, so required mode/ownership preservation always fails" |
| 3585 |
)] |
| 3586 |
fn test_no_preserve_mode() { |
| 3587 |
use std::os::unix::prelude::MetadataExt; |
| 3588 |
|
| 3589 |
const PERMS_ALL: u32 = if cfg!(target_os = "freebsd") { |
| 3590 |
// Only the superuser can set the sticky bit on a file. |
| 3591 |
0o6777 |
| 3592 |
} else { |
| 3593 |
0o7777 |
| 3594 |
}; |
| 3595 |
|
| 3596 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 3597 |
at.touch("file"); |
| 3598 |
set_permissions(at.plus("file"), PermissionsExt::from_mode(PERMS_ALL)).unwrap(); |
| 3599 |
let umask: u16 = 0o022; |
| 3600 |
ucmd.arg("file") |
| 3601 |
.arg("dest") |
| 3602 |
.umask(libc::mode_t::from(umask)) |
| 3603 |
.succeeds() |
| 3604 |
.no_output(); |
| 3605 |
// remove sticky bit, setuid and setgid bit; apply umask |
| 3606 |
let expected_perms = PERMS_ALL & !0o7000 & u32::from(!umask); |
| 3607 |
assert_eq!( |
| 3608 |
at.plus("dest").metadata().unwrap().mode() & 0o7777, |
| 3609 |
expected_perms |
| 3610 |
); |
| 3611 |
} |
| 3612 |
|
| 3613 |
#[test] |
| 3614 |
#[cfg(unix)] |
| 3615 |
#[cfg(not(target_os = "openbsd"))] |
| 3616 |
#[cfg_attr( |
| 3617 |
wasi_runner, |
| 3618 |
ignore = "WASI: no chmod syscall, so required mode/ownership preservation always fails" |
| 3619 |
)] |
| 3620 |
fn test_preserve_mode() { |
| 3621 |
use std::os::unix::prelude::MetadataExt; |
| 3622 |
|
| 3623 |
const PERMS_ALL: u32 = if cfg!(target_os = "freebsd") { |
| 3624 |
// Only the superuser can set the sticky bit on a file. |
| 3625 |
0o6777 |
| 3626 |
} else { |
| 3627 |
0o7777 |
| 3628 |
}; |
| 3629 |
|
| 3630 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 3631 |
at.touch("file"); |
| 3632 |
set_permissions(at.plus("file"), PermissionsExt::from_mode(PERMS_ALL)).unwrap(); |
| 3633 |
ucmd.arg("file") |
| 3634 |
.arg("dest") |
| 3635 |
.arg("-p") |
| 3636 |
.succeeds() |
| 3637 |
.no_output(); |
| 3638 |
assert_eq!( |
| 3639 |
at.plus("dest").metadata().unwrap().mode() & 0o7777, |
| 3640 |
PERMS_ALL |
| 3641 |
); |
| 3642 |
} |
| 3643 |
|
| 3644 |
#[test] |
| 3645 |
fn test_canonicalize_symlink() { |
| 3646 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 3647 |
at.mkdir("dir"); |
| 3648 |
at.touch("dir/file"); |
| 3649 |
at.relative_symlink_file("../dir/file", "dir/file-ln"); |
| 3650 |
ucmd.arg("dir/file-ln").arg(".").succeeds().no_output(); |
| 3651 |
} |
| 3652 |
|
| 3653 |
#[test] |
| 3654 |
fn test_copy_through_just_created_symlink() { |
| 3655 |
for create_t in [true, false] { |
| 3656 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 3657 |
at.mkdir("a"); |
| 3658 |
at.mkdir("b"); |
| 3659 |
at.mkdir("c"); |
| 3660 |
at.relative_symlink_file("../t", "a/1"); |
| 3661 |
at.write("b/1", "hello"); |
| 3662 |
if create_t { |
| 3663 |
at.write("t", "world"); |
| 3664 |
} |
| 3665 |
ucmd.arg("--no-dereference") |
| 3666 |
.arg("a/1") |
| 3667 |
.arg("b/1") |
| 3668 |
.arg("c") |
| 3669 |
.fails() |
| 3670 |
.stderr_only(if cfg!(not(windows)) { |
| 3671 |
"cp: will not copy 'b/1' through just-created symlink 'c/1'\n" |
| 3672 |
} else { |
| 3673 |
"cp: will not copy 'b/1' through just-created symlink 'c\\1'\n" |
| 3674 |
}); |
| 3675 |
if create_t { |
| 3676 |
assert_eq!(at.read("a/1"), "world"); |
| 3677 |
} |
| 3678 |
} |
| 3679 |
} |
| 3680 |
|
| 3681 |
#[test] |
| 3682 |
fn test_copy_through_dangling_symlink() { |
| 3683 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 3684 |
at.touch("file"); |
| 3685 |
at.symlink_file("nonexistent", "target"); |
| 3686 |
ucmd.arg("file") |
| 3687 |
.arg("target") |
| 3688 |
.fails() |
| 3689 |
.stderr_only("cp: not writing through dangling symlink 'target'\n"); |
| 3690 |
} |
| 3691 |
|
| 3692 |
#[test] |
| 3693 |
#[cfg_attr( |
| 3694 |
wasi_runner, |
| 3695 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 3696 |
)] |
| 3697 |
fn test_copy_through_dangling_symlink_posixly_correct() { |
| 3698 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 3699 |
at.touch("file"); |
| 3700 |
at.write("file", "content"); |
| 3701 |
at.symlink_file("nonexistent", "target"); |
| 3702 |
ucmd.arg("file") |
| 3703 |
.arg("target") |
| 3704 |
.env("POSIXLY_CORRECT", "1") |
| 3705 |
.succeeds(); |
| 3706 |
assert!(at.file_exists("nonexistent")); |
| 3707 |
let contents = at.read("nonexistent"); |
| 3708 |
assert_eq!(contents, "content"); |
| 3709 |
} |
| 3710 |
|
| 3711 |
#[test] |
| 3712 |
#[cfg_attr( |
| 3713 |
wasi_runner, |
| 3714 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 3715 |
)] |
| 3716 |
fn test_copy_through_dangling_symlink_no_dereference() { |
| 3717 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 3718 |
at.symlink_file("no-such-file", "dangle"); |
| 3719 |
ucmd.arg("-P") |
| 3720 |
.arg("dangle") |
| 3721 |
.arg("d2") |
| 3722 |
.succeeds() |
| 3723 |
.no_output(); |
| 3724 |
} |
| 3725 |
|
| 3726 |
#[test] |
| 3727 |
#[cfg_attr( |
| 3728 |
wasi_runner, |
| 3729 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 3730 |
)] |
| 3731 |
fn test_cp_symlink_overwrite_detection() { |
| 3732 |
let ts = TestScenario::new(util_name!()); |
| 3733 |
let at = &ts.fixtures; |
| 3734 |
|
| 3735 |
at.mkdir("good"); |
| 3736 |
at.mkdir("tmp"); |
| 3737 |
at.write("README", "file1"); |
| 3738 |
at.write("good/README", "file2"); |
| 3739 |
|
| 3740 |
at.symlink_file("tmp/foo", "tmp/README"); |
| 3741 |
at.touch("tmp/foo"); |
| 3742 |
|
| 3743 |
ts.ucmd() |
| 3744 |
.arg("README") |
| 3745 |
.arg("good/README") |
| 3746 |
.arg("tmp") |
| 3747 |
.fails() |
| 3748 |
.stderr_only(if cfg!(windows) { |
| 3749 |
"cp: will not copy 'good/README' through just-created symlink 'tmp\\README'\n" |
| 3750 |
} else { |
| 3751 |
"cp: will not copy 'good/README' through just-created symlink 'tmp/README'\n" |
| 3752 |
}); |
| 3753 |
let contents = at.read("tmp/foo"); |
| 3754 |
// None of the files seem to be copied in macos |
| 3755 |
if cfg!(not(target_vendor = "apple")) { |
| 3756 |
assert_eq!(contents, "file1"); |
| 3757 |
} |
| 3758 |
} |
| 3759 |
|
| 3760 |
#[test] |
| 3761 |
fn test_cp_dangling_symlink_inside_directory() { |
| 3762 |
let ts = TestScenario::new(util_name!()); |
| 3763 |
let at = &ts.fixtures; |
| 3764 |
|
| 3765 |
at.mkdir("good"); |
| 3766 |
at.mkdir("tmp"); |
| 3767 |
at.write("README", "file1"); |
| 3768 |
at.write("good/README", "file2"); |
| 3769 |
|
| 3770 |
at.symlink_file("foo", "tmp/README"); |
| 3771 |
|
| 3772 |
ts.ucmd() |
| 3773 |
.arg("README") |
| 3774 |
.arg("good/README") |
| 3775 |
.arg("tmp") |
| 3776 |
.fails() |
| 3777 |
.stderr_only( if cfg!(windows) { |
| 3778 |
"cp: not writing through dangling symlink 'tmp\\README'\ncp: not writing through dangling symlink 'tmp\\README'\n" |
| 3779 |
} else { |
| 3780 |
"cp: not writing through dangling symlink 'tmp/README'\ncp: not writing through dangling symlink 'tmp/README'\n" |
| 3781 |
} ); |
| 3782 |
} |
| 3783 |
|
| 3784 |
/// Test for copying a dangling symbolic link and its permissions. |
| 3785 |
#[cfg(not(any(target_os = "freebsd", target_os = "openbsd")))] // FIXME: fix this test for FreeBSD/OpenBSD |
| 3786 |
#[test] |
| 3787 |
#[cfg_attr( |
| 3788 |
wasi_runner, |
| 3789 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 3790 |
)] |
| 3791 |
fn test_copy_through_dangling_symlink_no_dereference_permissions() { |
| 3792 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 3793 |
// target name link name |
| 3794 |
at.symlink_file("no-such-file", "dangle"); |
| 3795 |
// to check if access time and modification time didn't change |
| 3796 |
sleep(Duration::from_millis(100)); |
| 3797 |
// don't dereference the link |
| 3798 |
// | copy permissions, too |
| 3799 |
// | | from the link |
| 3800 |
// | | | to new file d2 |
| 3801 |
// | | | | |
| 3802 |
// V V V V |
| 3803 |
ucmd.args(&["-P", "-p", "dangle", "d2"]) |
| 3804 |
.succeeds() |
| 3805 |
.no_output(); |
| 3806 |
assert!(at.symlink_exists("d2"), "symlink wasn't created"); |
| 3807 |
|
| 3808 |
// `-p` means `--preserve=mode,ownership,timestamps` |
| 3809 |
#[cfg(all(unix, not(target_os = "freebsd"), not(target_os = "openbsd")))] |
| 3810 |
{ |
| 3811 |
let metadata1 = at.symlink_metadata("dangle"); |
| 3812 |
let metadata2 = at.symlink_metadata("d2"); |
| 3813 |
assert_metadata_eq!(metadata1, metadata2); |
| 3814 |
} |
| 3815 |
} |
| 3816 |
|
| 3817 |
#[test] |
| 3818 |
fn test_copy_through_dangling_symlink_no_dereference_2() { |
| 3819 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 3820 |
at.touch("file"); |
| 3821 |
at.symlink_file("nonexistent", "target"); |
| 3822 |
ucmd.args(&["-P", "file", "target"]) |
| 3823 |
.fails() |
| 3824 |
.stderr_only("cp: not writing through dangling symlink 'target'\n"); |
| 3825 |
} |
| 3826 |
|
| 3827 |
/// Test that copy through a dangling symbolic link fails, even with --force. |
| 3828 |
#[test] |
| 3829 |
#[cfg(not(windows))] |
| 3830 |
fn test_copy_through_dangling_symlink_force() { |
| 3831 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 3832 |
at.touch("src"); |
| 3833 |
at.symlink_file("no-such-file", "dest"); |
| 3834 |
ucmd.args(&["--force", "src", "dest"]) |
| 3835 |
.fails() |
| 3836 |
.stderr_only("cp: not writing through dangling symlink 'dest'\n"); |
| 3837 |
assert!(!at.file_exists("dest")); |
| 3838 |
} |
| 3839 |
|
| 3840 |
#[test] |
| 3841 |
#[cfg(unix)] |
| 3842 |
fn test_cp_archive_on_nonexistent_file() { |
| 3843 |
new_ucmd!() |
| 3844 |
.arg("-a") |
| 3845 |
.arg(TEST_NONEXISTENT_FILE) |
| 3846 |
.arg(TEST_EXISTING_FILE) |
| 3847 |
.fails() |
| 3848 |
.stderr_contains("cannot stat 'nonexistent_file.txt'") |
| 3849 |
.stderr_contains("No such file or directory"); |
| 3850 |
} |
| 3851 |
|
| 3852 |
#[test] |
| 3853 |
#[cfg(not(target_os = "android"))] |
| 3854 |
fn test_cp_link_backup() { |
| 3855 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 3856 |
at.touch("file2"); |
| 3857 |
ucmd.arg("-l") |
| 3858 |
.arg("-b") |
| 3859 |
.arg(TEST_HELLO_WORLD_SOURCE) |
| 3860 |
.arg("file2") |
| 3861 |
.succeeds(); |
| 3862 |
|
| 3863 |
assert!(at.file_exists("file2~")); |
| 3864 |
assert_eq!(at.read("file2"), "Hello, World!\n"); |
| 3865 |
} |
| 3866 |
|
| 3867 |
/// `--attributes-only` must still give the destination the source's type. |
| 3868 |
/// It used to create a regular file for any source, which both got the type |
| 3869 |
/// wrong and made the later xattr copy `open()` the source FIFO -- blocking |
| 3870 |
/// forever, since nothing ever writes to it. GNU creates the FIFO and returns. |
| 3871 |
#[test] |
| 3872 |
#[cfg(unix)] |
| 3873 |
#[cfg_attr(wasi_runner, ignore = "WASI: no FIFO/mkfifo support")] |
| 3874 |
fn test_cp_attributes_only_fifo_keeps_type_and_returns() { |
| 3875 |
for recursive in ["-a", "-R"] { |
| 3876 |
let scene = TestScenario::new(util_name!()); |
| 3877 |
let at = &scene.fixtures; |
| 3878 |
at.mkfifo("fifo"); |
| 3879 |
|
| 3880 |
scene |
| 3881 |
.ucmd() |
| 3882 |
.args(&[recursive, "--attributes-only", "fifo", "copy"]) |
| 3883 |
.succeeds() |
| 3884 |
.no_stderr(); |
| 3885 |
|
| 3886 |
assert!( |
| 3887 |
at.is_fifo("copy"), |
| 3888 |
"--attributes-only {recursive} did not create a FIFO" |
| 3889 |
); |
| 3890 |
} |
| 3891 |
} |
| 3892 |
|
| 3893 |
#[test] |
| 3894 |
#[cfg(unix)] |
| 3895 |
#[cfg_attr(wasi_runner, ignore = "WASI: no FIFO/mkfifo support")] |
| 3896 |
fn test_cp_fifo() { |
| 3897 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 3898 |
at.mkfifo("fifo"); |
| 3899 |
// Also test that permissions are preserved |
| 3900 |
at.set_mode("fifo", 0o731); |
| 3901 |
ucmd.arg("--preserve=mode") |
| 3902 |
.arg("-r") |
| 3903 |
.arg("fifo") |
| 3904 |
.arg("fifo2") |
| 3905 |
.succeeds() |
| 3906 |
.no_output(); |
| 3907 |
assert!(at.is_fifo("fifo2")); |
| 3908 |
|
| 3909 |
let metadata = std::fs::metadata(at.subdir.join("fifo2")).unwrap(); |
| 3910 |
let permission = uucore::fs::display_permissions(&metadata, true); |
| 3911 |
assert_eq!(permission, "prwx-wx--x".to_string()); |
| 3912 |
} |
| 3913 |
|
| 3914 |
#[test] |
| 3915 |
#[cfg(unix)] |
| 3916 |
#[cfg_attr(wasi_runner, ignore = "WASI: no FIFO/mkfifo support")] |
| 3917 |
fn test_cp_fifo_preserve_timestamps() { |
| 3918 |
// Preserving timestamps must not open the FIFO: opening a FIFO with no |
| 3919 |
// writer blocks forever. If this regresses, the test hangs instead of |
| 3920 |
// completing. See the `-a`/`--preserve=timestamps` path in copy_attributes. |
| 3921 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 3922 |
at.mkfifo("pipe"); |
| 3923 |
at.set_mode("pipe", 0o624); |
| 3924 |
ucmd.arg("--preserve=timestamps") |
| 3925 |
.arg("-r") |
| 3926 |
.arg("pipe") |
| 3927 |
.arg("pipe_dup") |
| 3928 |
.succeeds() |
| 3929 |
.no_output(); |
| 3930 |
assert!(at.is_fifo("pipe_dup")); |
| 3931 |
} |
| 3932 |
|
| 3933 |
#[rstest] |
| 3934 |
#[case::recursive("-R")] |
| 3935 |
#[case::archive("-a")] |
| 3936 |
#[cfg(unix)] |
| 3937 |
fn test_cp_recursive_char_device(#[case] flag: &str) { |
| 3938 |
use nix::sys::stat::{Mode, SFlag, mknod as nix_mknod}; |
| 3939 |
use std::os::unix::fs::{FileTypeExt, MetadataExt, PermissionsExt}; |
| 3940 |
use uutests::util::run_ucmd_as_root; |
| 3941 |
let ts = TestScenario::new(util_name!()); |
| 3942 |
let at = &ts.fixtures; |
| 3943 |
let mode = Mode::S_IRUSR | Mode::S_IWUSR | Mode::S_IRGRP; |
| 3944 |
if nix_mknod( |
| 3945 |
at.plus("null").as_path(), |
| 3946 |
SFlag::S_IFCHR, |
| 3947 |
mode, |
| 3948 |
uucore::fs::makedev(1, 3), |
| 3949 |
) |
| 3950 |
.is_err() |
| 3951 |
{ |
| 3952 |
print!("Test skipped; creating a char device node requires CAP_MKNOD"); |
| 3953 |
return; |
| 3954 |
} |
| 3955 |
if let Ok(result) = run_ucmd_as_root(&ts, &[flag, "null", "null2"]) { |
| 3956 |
result.success(); |
| 3957 |
let null2_metadata = at.plus("null2").metadata().unwrap(); |
| 3958 |
assert!(null2_metadata.file_type().is_char_device()); |
| 3959 |
assert_eq!( |
| 3960 |
null2_metadata.rdev(), |
| 3961 |
at.plus("null").metadata().unwrap().rdev() |
| 3962 |
); |
| 3963 |
assert_eq!( |
| 3964 |
null2_metadata.permissions().mode() & 0o777, |
| 3965 |
mode.bits() as _ |
| 3966 |
); |
| 3967 |
} else { |
| 3968 |
print!("Test skipped; copying a char device node requires CAP_MKNOD"); |
| 3969 |
} |
| 3970 |
} |
| 3971 |
|
| 3972 |
#[rstest] |
| 3973 |
#[case::recursive("-R")] |
| 3974 |
#[case::archive("-a")] |
| 3975 |
#[cfg(unix)] |
| 3976 |
#[cfg_attr( |
| 3977 |
wasi_runner, |
| 3978 |
ignore = "WASI sandbox: host paths (/dev, /private) not visible" |
| 3979 |
)] |
| 3980 |
fn test_cp_recursive_char_device_no_permission(#[case] flag: &str) { |
| 3981 |
new_ucmd!() |
| 3982 |
.args(&[flag, "/dev/null", "null2"]) |
| 3983 |
.fails() |
| 3984 |
.stderr_is("cp: cannot create special file 'null2': Operation not permitted\n"); |
| 3985 |
} |
| 3986 |
|
| 3987 |
#[test] |
| 3988 |
#[cfg(unix)] |
| 3989 |
#[cfg_attr( |
| 3990 |
wasi_runner, |
| 3991 |
ignore = "WASI sandbox: host paths (/dev, /private) not visible" |
| 3992 |
)] |
| 3993 |
fn test_cp_recursive_char_device_copy_contents() { |
| 3994 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 3995 |
ucmd.args(&["-R", "--copy-contents", "/dev/null", "null2"]) |
| 3996 |
.succeeds() |
| 3997 |
.no_stderr(); |
| 3998 |
assert!(at.plus("null2").metadata().unwrap().file_type().is_file()); |
| 3999 |
assert_eq!(at.read("null2"), ""); |
| 4000 |
} |
| 4001 |
|
| 4002 |
#[test] |
| 4003 |
#[cfg(unix)] |
| 4004 |
#[cfg_attr( |
| 4005 |
wasi_runner, |
| 4006 |
ignore = "WASI sandbox: host paths (/dev, /private) not visible" |
| 4007 |
)] |
| 4008 |
fn test_cp_char_device() { |
| 4009 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 4010 |
ucmd.args(&["/dev/null", "null2"]).succeeds().no_stderr(); |
| 4011 |
assert!(at.plus("null2").metadata().unwrap().file_type().is_file()); |
| 4012 |
assert_eq!(at.read("null2"), ""); |
| 4013 |
} |
| 4014 |
|
| 4015 |
#[rstest] |
| 4016 |
#[case::recursive("-R")] |
| 4017 |
#[case::archive("-a")] |
| 4018 |
#[cfg(unix)] |
| 4019 |
fn test_cp_recursive_block_device(#[case] flag: &str) { |
| 4020 |
use nix::sys::stat::{Mode, SFlag, mknod as nix_mknod}; |
| 4021 |
use std::os::unix::fs::{FileTypeExt, MetadataExt}; |
| 4022 |
use uutests::util::run_ucmd_as_root; |
| 4023 |
let ts = TestScenario::new(util_name!()); |
| 4024 |
let at = &ts.fixtures; |
| 4025 |
if nix_mknod( |
| 4026 |
at.plus("sda").as_path(), |
| 4027 |
SFlag::S_IFBLK, |
| 4028 |
Mode::S_IRUSR | Mode::S_IWUSR, |
| 4029 |
uucore::fs::makedev(8, 0), |
| 4030 |
) |
| 4031 |
.is_err() |
| 4032 |
{ |
| 4033 |
print!("Test skipped; creating a block device node requires CAP_MKNOD"); |
| 4034 |
return; |
| 4035 |
} |
| 4036 |
if let Ok(result) = run_ucmd_as_root(&ts, &[flag, "sda", "sda2"]) { |
| 4037 |
result.success(); |
| 4038 |
let sda2_metadata = at.plus("sda2").metadata().unwrap(); |
| 4039 |
assert!(sda2_metadata.file_type().is_block_device()); |
| 4040 |
assert_eq!( |
| 4041 |
sda2_metadata.rdev(), |
| 4042 |
at.plus("sda").metadata().unwrap().rdev() |
| 4043 |
); |
| 4044 |
} else { |
| 4045 |
print!("Test skipped; copying a block device node requires CAP_MKNOD"); |
| 4046 |
} |
| 4047 |
} |
| 4048 |
|
| 4049 |
#[test] |
| 4050 |
#[cfg(unix)] |
| 4051 |
fn test_cp_block_device_no_permission() { |
| 4052 |
use nix::sys::stat::{Mode, SFlag, mknod as nix_mknod}; |
| 4053 |
let ts = TestScenario::new(util_name!()); |
| 4054 |
let at = &ts.fixtures; |
| 4055 |
if nix_mknod( |
| 4056 |
at.plus("sda").as_path(), |
| 4057 |
SFlag::S_IFBLK, |
| 4058 |
Mode::S_IRUSR | Mode::S_IWUSR, |
| 4059 |
uucore::fs::makedev(8, 0), |
| 4060 |
) |
| 4061 |
.is_err() |
| 4062 |
{ |
| 4063 |
print!("Test skipped; creating a block device node requires CAP_MKNOD"); |
| 4064 |
return; |
| 4065 |
} |
| 4066 |
ts.ucmd() |
| 4067 |
.args(&["-R", "sda", "sda2"]) |
| 4068 |
.fails() |
| 4069 |
.stderr_is("cp: cannot create special file 'sda2': Operation not permitted\n"); |
| 4070 |
} |
| 4071 |
|
| 4072 |
#[test] |
| 4073 |
#[cfg(unix)] |
| 4074 |
#[cfg_attr(wasi_runner, ignore = "WASI: no Unix domain socket support")] |
| 4075 |
fn test_cp_socket() { |
| 4076 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 4077 |
at.mksocket("socket"); |
| 4078 |
// Also test that permissions are preserved |
| 4079 |
at.set_mode("socket", 0o731); |
| 4080 |
ucmd.arg("--preserve=mode") |
| 4081 |
.arg("-r") |
| 4082 |
.arg("socket") |
| 4083 |
.arg("socket2") |
| 4084 |
.succeeds() |
| 4085 |
.no_output(); |
| 4086 |
|
| 4087 |
let metadata = std::fs::metadata(at.subdir.join("socket2")).unwrap(); |
| 4088 |
let permission = uucore::fs::display_permissions(&metadata, true); |
| 4089 |
assert!(metadata.file_type().is_socket()); |
| 4090 |
assert_eq!(permission, "srwx-wx--x".to_string()); |
| 4091 |
} |
| 4092 |
|
| 4093 |
#[cfg(all(unix, not(target_vendor = "apple")))] |
| 4094 |
fn find_other_group(current: u32) -> Option<u32> { |
| 4095 |
// Get the first group that doesn't match current |
| 4096 |
rustix::process::getgroups().ok()?.iter().find_map(|group| { |
| 4097 |
let gid = group.as_raw(); |
| 4098 |
(gid != current).then_some(gid) |
| 4099 |
}) |
| 4100 |
} |
| 4101 |
|
| 4102 |
#[cfg(target_vendor = "apple")] |
| 4103 |
fn find_other_group(_current: u32) -> Option<u32> { |
| 4104 |
None |
| 4105 |
} |
| 4106 |
|
| 4107 |
#[test] |
| 4108 |
#[cfg(unix)] |
| 4109 |
#[cfg_attr( |
| 4110 |
wasi_runner, |
| 4111 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 4112 |
)] |
| 4113 |
fn test_cp_r_symlink() { |
| 4114 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 4115 |
// Specifically test copying a link in a subdirectory, as the internal path |
| 4116 |
// is slightly different. |
| 4117 |
at.mkdir("tmp"); |
| 4118 |
// Create a symlink to a non-existent file to make sure |
| 4119 |
// we don't try to resolve it. |
| 4120 |
at.symlink_file("doesnotexist", "tmp/symlink"); |
| 4121 |
let symlink = at.subdir.join("tmp").join("symlink"); |
| 4122 |
|
| 4123 |
// If we can find such a group, change the owner to a non-default to test |
| 4124 |
// that (group) ownership is preserved. |
| 4125 |
let metadata = std::fs::symlink_metadata(&symlink).unwrap(); |
| 4126 |
let other_gid = find_other_group(metadata.gid()); |
| 4127 |
if let Some(gid) = other_gid { |
| 4128 |
uucore::perms::wrap_chown( |
| 4129 |
&symlink, |
| 4130 |
&metadata, |
| 4131 |
None, |
| 4132 |
Some(gid), |
| 4133 |
false, |
| 4134 |
uucore::perms::Verbosity::default(), |
| 4135 |
) |
| 4136 |
.expect("Cannot chgrp symlink."); |
| 4137 |
} else { |
| 4138 |
println!("Cannot find a second group to chgrp to."); |
| 4139 |
} |
| 4140 |
|
| 4141 |
// Use -r to make sure we copy the symlink itself |
| 4142 |
// --preserve will include ownership |
| 4143 |
ucmd.arg("--preserve") |
| 4144 |
.arg("-r") |
| 4145 |
.arg("tmp") |
| 4146 |
.arg("tmp2") |
| 4147 |
.succeeds() |
| 4148 |
.no_output(); |
| 4149 |
|
| 4150 |
// Is symlink2 still a symlink, and does it point at the same place? |
| 4151 |
assert!(at.is_symlink("tmp2/symlink")); |
| 4152 |
let symlink2 = at.subdir.join("tmp2/symlink"); |
| 4153 |
assert_eq!( |
| 4154 |
std::fs::read_link(&symlink).unwrap(), |
| 4155 |
std::fs::read_link(&symlink2).unwrap(), |
| 4156 |
); |
| 4157 |
|
| 4158 |
// If we found a suitable group, is the group correct after the copy. |
| 4159 |
if let Some(gid) = other_gid { |
| 4160 |
let metadata2 = std::fs::symlink_metadata(&symlink2).unwrap(); |
| 4161 |
assert_eq!(metadata2.gid(), gid); |
| 4162 |
} |
| 4163 |
} |
| 4164 |
|
| 4165 |
#[test] |
| 4166 |
fn test_dir_recursive_copy() { |
| 4167 |
let scene = TestScenario::new(util_name!()); |
| 4168 |
let at = &scene.fixtures; |
| 4169 |
|
| 4170 |
at.mkdir_all("parent1/child"); |
| 4171 |
at.mkdir_all("parent2/child1/child2/child3"); |
| 4172 |
|
| 4173 |
// case-1: copy parent1 -> parent1: should fail |
| 4174 |
scene |
| 4175 |
.ucmd() |
| 4176 |
.arg("-R") |
| 4177 |
.arg("parent1") |
| 4178 |
.arg("parent1") |
| 4179 |
.fails() |
| 4180 |
.stderr_contains("cannot copy a directory"); |
| 4181 |
// case-2: copy parent1 -> parent1/child should fail |
| 4182 |
scene |
| 4183 |
.ucmd() |
| 4184 |
.arg("-R") |
| 4185 |
.arg("parent1") |
| 4186 |
.arg("parent1/child") |
| 4187 |
.fails() |
| 4188 |
.stderr_contains("cannot copy a directory"); |
| 4189 |
// case-3: copy parent1/child -> parent2 should pass |
| 4190 |
scene |
| 4191 |
.ucmd() |
| 4192 |
.arg("-R") |
| 4193 |
.arg("parent1/child") |
| 4194 |
.arg("parent2") |
| 4195 |
.succeeds(); |
| 4196 |
// case-4: copy parent2/child1/ -> parent2/child1/child2/child3 |
| 4197 |
scene |
| 4198 |
.ucmd() |
| 4199 |
.arg("-R") |
| 4200 |
.arg("parent2/child1/") |
| 4201 |
.arg("parent2/child1/child2/child3") |
| 4202 |
.fails() |
| 4203 |
.stderr_contains("cannot copy a directory"); |
| 4204 |
} |
| 4205 |
|
| 4206 |
#[test] |
| 4207 |
fn test_cp_dir_vs_file() { |
| 4208 |
new_ucmd!() |
| 4209 |
.arg("-R") |
| 4210 |
.arg(TEST_COPY_FROM_FOLDER) |
| 4211 |
.arg(TEST_EXISTING_FILE) |
| 4212 |
.fails() |
| 4213 |
.stderr_only("cp: cannot overwrite non-directory with directory\n"); |
| 4214 |
} |
| 4215 |
|
| 4216 |
#[test] |
| 4217 |
fn test_cp_overriding_arguments() { |
| 4218 |
let s = TestScenario::new(util_name!()); |
| 4219 |
s.fixtures.touch("file1"); |
| 4220 |
for (arg1, arg2) in [ |
| 4221 |
#[cfg(not(windows))] |
| 4222 |
("--remove-destination", "--force"), |
| 4223 |
#[cfg(not(windows))] |
| 4224 |
("--force", "--remove-destination"), |
| 4225 |
("--interactive", "--no-clobber"), |
| 4226 |
("--link", "--symbolic-link"), |
| 4227 |
#[cfg(not(target_os = "android"))] |
| 4228 |
("--symbolic-link", "--link"), |
| 4229 |
("--dereference", "--no-dereference"), |
| 4230 |
("--no-dereference", "--dereference"), |
| 4231 |
] { |
| 4232 |
s.ucmd() |
| 4233 |
.arg(arg1) |
| 4234 |
.arg(arg2) |
| 4235 |
.arg("file1") |
| 4236 |
.arg("file2") |
| 4237 |
.succeeds(); |
| 4238 |
s.fixtures.remove("file2"); |
| 4239 |
} |
| 4240 |
} |
| 4241 |
|
| 4242 |
#[test] |
| 4243 |
fn test_copy_no_dereference_1() { |
| 4244 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 4245 |
at.mkdir("a"); |
| 4246 |
at.mkdir("b"); |
| 4247 |
at.write("a/foo", "bar"); |
| 4248 |
at.relative_symlink_file("../a/foo", "b/foo"); |
| 4249 |
ucmd.args(&["-P", "a/foo", "b"]).fails(); |
| 4250 |
} |
| 4251 |
|
| 4252 |
#[test] |
| 4253 |
fn test_abuse_existing() { |
| 4254 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 4255 |
at.mkdir("a"); |
| 4256 |
at.mkdir("b"); |
| 4257 |
at.mkdir("c"); |
| 4258 |
at.relative_symlink_file("../t", "a/1"); |
| 4259 |
at.write("b/1", "hello"); |
| 4260 |
at.relative_symlink_file("../t", "c/1"); |
| 4261 |
at.write("t", "i"); |
| 4262 |
ucmd.args(&["-dR", "a/1", "b/1", "c"]) |
| 4263 |
.fails() |
| 4264 |
.stderr_contains(format!( |
| 4265 |
"will not copy 'b/1' through just-created symlink 'c{}1'", |
| 4266 |
if cfg!(windows) { "\\" } else { "/" } |
| 4267 |
)); |
| 4268 |
assert_eq!(at.read("t"), "i"); |
| 4269 |
} |
| 4270 |
|
| 4271 |
#[test] |
| 4272 |
fn test_copy_same_symlink_no_dereference() { |
| 4273 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 4274 |
at.relative_symlink_file("t", "a"); |
| 4275 |
at.relative_symlink_file("t", "b"); |
| 4276 |
at.touch("t"); |
| 4277 |
ucmd.args(&["-d", "a", "b"]).succeeds(); |
| 4278 |
} |
| 4279 |
|
| 4280 |
#[test] |
| 4281 |
fn test_copy_same_symlink_no_dereference_dangling() { |
| 4282 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 4283 |
at.relative_symlink_file("t", "a"); |
| 4284 |
at.relative_symlink_file("t", "b"); |
| 4285 |
ucmd.args(&["-d", "a", "b"]).succeeds(); |
| 4286 |
} |
| 4287 |
|
| 4288 |
// TODO: enable for Android, when #3477 solved |
| 4289 |
#[cfg(not(any(windows, target_os = "android", target_os = "openbsd")))] |
| 4290 |
#[test] |
| 4291 |
#[cfg_attr( |
| 4292 |
wasi_runner, |
| 4293 |
ignore = "WASI: no chmod syscall, so required mode/ownership preservation always fails" |
| 4294 |
)] |
| 4295 |
fn test_cp_parents_2_dirs() { |
| 4296 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 4297 |
at.mkdir_all("a/b/c"); |
| 4298 |
at.mkdir("d"); |
| 4299 |
ucmd.args(&["-a", "--parents", "a/b/c", "d"]) |
| 4300 |
.succeeds() |
| 4301 |
.no_output(); |
| 4302 |
assert!(at.dir_exists("d/a/b/c")); |
| 4303 |
} |
| 4304 |
|
| 4305 |
#[test] |
| 4306 |
fn test_cp_parents_2() { |
| 4307 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 4308 |
at.mkdir_all("a/b"); |
| 4309 |
at.touch("a/b/c"); |
| 4310 |
at.mkdir("d"); |
| 4311 |
#[cfg(not(windows))] |
| 4312 |
let expected_stdout = "a -> d/a\na/b -> d/a/b\n'a/b/c' -> 'd/a/b/c'\n"; |
| 4313 |
#[cfg(windows)] |
| 4314 |
let expected_stdout = "a -> d\\a\na/b -> d\\a/b\n'a/b/c' -> 'd\\a/b/c'\n"; |
| 4315 |
ucmd.args(&["--verbose", "--parents", "a/b/c", "d"]) |
| 4316 |
.succeeds() |
| 4317 |
.stdout_only(expected_stdout); |
| 4318 |
assert!(at.file_exists("d/a/b/c")); |
| 4319 |
} |
| 4320 |
|
| 4321 |
#[test] |
| 4322 |
fn test_cp_parents_2_link() { |
| 4323 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 4324 |
at.mkdir_all("a/b"); |
| 4325 |
at.touch("a/b/c"); |
| 4326 |
at.mkdir("d"); |
| 4327 |
at.relative_symlink_file("b", "a/link"); |
| 4328 |
#[cfg(not(windows))] |
| 4329 |
let expected_stdout = "a -> d/a\na/link -> d/a/link\n'a/link/c' -> 'd/a/link/c'\n"; |
| 4330 |
#[cfg(windows)] |
| 4331 |
let expected_stdout = "a -> d\\a\na/link -> d\\a/link\n'a/link/c' -> 'd\\a/link/c'\n"; |
| 4332 |
ucmd.args(&["--verbose", "--parents", "a/link/c", "d"]) |
| 4333 |
.succeeds() |
| 4334 |
.stdout_only(expected_stdout); |
| 4335 |
assert!(at.dir_exists("d/a/link")); |
| 4336 |
assert!(!at.symlink_exists("d/a/link")); |
| 4337 |
assert!(at.file_exists("d/a/link/c")); |
| 4338 |
} |
| 4339 |
|
| 4340 |
#[test] |
| 4341 |
fn test_cp_parents_2_dir() { |
| 4342 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 4343 |
at.mkdir_all("a/b/c"); |
| 4344 |
at.mkdir("d"); |
| 4345 |
#[cfg(not(windows))] |
| 4346 |
let expected_stdout = "a -> d/a\na/b -> d/a/b\n'a/b/c' -> 'd/a/b/c'\n"; |
| 4347 |
#[cfg(windows)] |
| 4348 |
let expected_stdout = "a -> d\\a\na/b -> d\\a/b\n'a/b/c' -> 'd\\a/b\\c'\n"; |
| 4349 |
ucmd.args(&["--verbose", "-r", "--parents", "a/b/c", "d"]) |
| 4350 |
.succeeds() |
| 4351 |
.stdout_only(expected_stdout); |
| 4352 |
assert!(at.dir_exists("d/a/b/c")); |
| 4353 |
} |
| 4354 |
|
| 4355 |
#[test] |
| 4356 |
fn test_cp_parents_2_deep_dir() { |
| 4357 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 4358 |
at.mkdir_all("a/b/c"); |
| 4359 |
at.mkdir_all("d/e"); |
| 4360 |
#[cfg(not(windows))] |
| 4361 |
let expected_stdout = "a -> d/e/a\na/b -> d/e/a/b\n'a/b/c' -> 'd/e/a/b/c'\n"; |
| 4362 |
#[cfg(windows)] |
| 4363 |
let expected_stdout = "a -> d/e\\a\na/b -> d/e\\a/b\n'a/b/c' -> 'd/e\\a/b\\c'\n"; |
| 4364 |
ucmd.args(&["--verbose", "-r", "--parents", "a/b/c", "d/e"]) |
| 4365 |
.succeeds() |
| 4366 |
.stdout_only(expected_stdout); |
| 4367 |
assert!(at.dir_exists("d/e/a/b/c")); |
| 4368 |
} |
| 4369 |
|
| 4370 |
#[cfg(not(windows))] |
| 4371 |
#[test] |
| 4372 |
fn test_cp_parents_recursive_source_ending_in_parent_dir() { |
| 4373 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 4374 |
at.mkdir_all("src/sub"); |
| 4375 |
at.write("src/sub/f", "x\n"); |
| 4376 |
at.mkdir("d"); |
| 4377 |
|
| 4378 |
ucmd.args(&["--parents", "-r", "src/sub/..", "d"]) |
| 4379 |
.fails() |
| 4380 |
.stderr_contains("cannot create directory 'd/src/sub/..'") |
| 4381 |
.stderr_contains("File exists"); |
| 4382 |
|
| 4383 |
assert!(at.dir_exists("d/src/sub")); |
| 4384 |
assert!(!at.file_exists("d/src/sub/f")); |
| 4385 |
} |
| 4386 |
|
| 4387 |
#[test] |
| 4388 |
fn test_cp_copy_symlink_contents_recursive() { |
| 4389 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 4390 |
at.mkdir("src-dir"); |
| 4391 |
at.mkdir("dest-dir"); |
| 4392 |
at.write("f", "f"); |
| 4393 |
at.relative_symlink_file("f", "slink"); |
| 4394 |
at.relative_symlink_file("no-file", &path_concat!("src-dir", "slink")); |
| 4395 |
ucmd.args(&["-H", "-R", "slink", "src-dir", "dest-dir"]) |
| 4396 |
.succeeds(); |
| 4397 |
assert!(at.dir_exists("src-dir")); |
| 4398 |
assert!(at.dir_exists("dest-dir")); |
| 4399 |
assert!(at.dir_exists(&path_concat!("dest-dir", "src-dir"))); |
| 4400 |
let regular_file = path_concat!("dest-dir", "slink"); |
| 4401 |
assert!(!at.symlink_exists(®ular_file) && at.file_exists(®ular_file)); |
| 4402 |
assert_eq!(at.read(®ular_file), "f"); |
| 4403 |
} |
| 4404 |
|
| 4405 |
#[test] |
| 4406 |
fn test_cp_mode_symlink() { |
| 4407 |
for from in ["file", "slink", "slink2"] { |
| 4408 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 4409 |
at.write("file", "f"); |
| 4410 |
at.relative_symlink_file("file", "slink"); |
| 4411 |
at.relative_symlink_file("slink", "slink2"); |
| 4412 |
ucmd.args(&["-s", "-L", from, "z"]).succeeds(); |
| 4413 |
assert!(at.symlink_exists("z")); |
| 4414 |
assert_eq!(at.read_symlink("z"), from); |
| 4415 |
} |
| 4416 |
} |
| 4417 |
|
| 4418 |
// Android doesn't allow creating hard links |
| 4419 |
#[cfg(not(target_os = "android"))] |
| 4420 |
#[test] |
| 4421 |
fn test_cp_mode_hardlink() { |
| 4422 |
for from in ["file", "slink", "slink2"] { |
| 4423 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 4424 |
at.write("file", "f"); |
| 4425 |
at.relative_symlink_file("file", "slink"); |
| 4426 |
at.relative_symlink_file("slink", "slink2"); |
| 4427 |
ucmd.args(&["--link", "-L", from, "z"]).succeeds(); |
| 4428 |
assert!(at.file_exists("z") && !at.symlink_exists("z")); |
| 4429 |
assert_eq!(at.read("z"), "f"); |
| 4430 |
// checking that it's the same hard link |
| 4431 |
at.append("z", "g"); |
| 4432 |
assert_eq!(at.read("file"), "fg"); |
| 4433 |
} |
| 4434 |
} |
| 4435 |
|
| 4436 |
// Android doesn't allow creating hard links |
| 4437 |
#[cfg(not(target_os = "android"))] |
| 4438 |
#[test] |
| 4439 |
fn test_cp_mode_hardlink_no_dereference() { |
| 4440 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 4441 |
at.write("file", "f"); |
| 4442 |
at.relative_symlink_file("file", "slink"); |
| 4443 |
at.relative_symlink_file("slink", "slink2"); |
| 4444 |
ucmd.args(&["--link", "-P", "slink2", "z"]).succeeds(); |
| 4445 |
assert!(at.symlink_exists("z")); |
| 4446 |
assert_eq!(at.read_symlink("z"), "slink"); |
| 4447 |
} |
| 4448 |
|
| 4449 |
#[cfg(not(any(windows, target_os = "android")))] |
| 4450 |
#[test] |
| 4451 |
#[cfg_attr( |
| 4452 |
wasi_runner, |
| 4453 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 4454 |
)] |
| 4455 |
fn test_remove_destination_with_destination_being_a_hardlink_to_source() { |
| 4456 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 4457 |
let file = "file"; |
| 4458 |
let hardlink = "hardlink"; |
| 4459 |
|
| 4460 |
at.touch(file); |
| 4461 |
at.hard_link(file, hardlink); |
| 4462 |
|
| 4463 |
ucmd.args(&["--remove-destination", file, hardlink]) |
| 4464 |
.succeeds(); |
| 4465 |
|
| 4466 |
assert_eq!("", at.resolve_link(hardlink)); |
| 4467 |
assert!(at.file_exists(file)); |
| 4468 |
assert!(at.file_exists(hardlink)); |
| 4469 |
} |
| 4470 |
|
| 4471 |
#[test] |
| 4472 |
fn test_remove_destination_with_destination_being_relative_path_of_source() { |
| 4473 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 4474 |
let source = "a"; |
| 4475 |
let dest = "./a"; |
| 4476 |
at.write(source, "hello"); |
| 4477 |
|
| 4478 |
ucmd.args(&["--remove-destination", source, dest]) |
| 4479 |
.fails() |
| 4480 |
.stderr_contains("are the same file"); |
| 4481 |
|
| 4482 |
assert!(at.file_exists(source)); |
| 4483 |
assert_eq!(at.read(source), "hello"); |
| 4484 |
} |
| 4485 |
|
| 4486 |
#[test] |
| 4487 |
fn test_remove_destination_with_destination_being_a_symlink_to_source() { |
| 4488 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 4489 |
let file = "file"; |
| 4490 |
let symlink = "symlink"; |
| 4491 |
|
| 4492 |
at.touch(file); |
| 4493 |
at.symlink_file(file, symlink); |
| 4494 |
|
| 4495 |
ucmd.args(&["--remove-destination", file, symlink]) |
| 4496 |
.succeeds(); |
| 4497 |
assert!(!at.symlink_exists(symlink)); |
| 4498 |
assert!(at.file_exists(file)); |
| 4499 |
assert!(at.file_exists(symlink)); |
| 4500 |
} |
| 4501 |
|
| 4502 |
#[test] |
| 4503 |
fn test_remove_destination_symbolic_link_loop() { |
| 4504 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 4505 |
at.symlink_file("loop", "loop"); |
| 4506 |
at.plus("loop"); |
| 4507 |
at.touch("f"); |
| 4508 |
ucmd.args(&["--remove-destination", "f", "loop"]) |
| 4509 |
.succeeds() |
| 4510 |
.no_output(); |
| 4511 |
assert!(at.file_exists("loop")); |
| 4512 |
} |
| 4513 |
|
| 4514 |
#[test] |
| 4515 |
#[cfg(not(windows))] |
| 4516 |
#[cfg_attr( |
| 4517 |
wasi_runner, |
| 4518 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 4519 |
)] |
| 4520 |
fn test_cp_symbolic_link_loop() { |
| 4521 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 4522 |
at.symlink_file("loop", "loop"); |
| 4523 |
at.plus("loop"); |
| 4524 |
at.touch("f"); |
| 4525 |
ucmd.args(&["-f", "f", "loop"]).succeeds().no_output(); |
| 4526 |
assert!(at.file_exists("loop")); |
| 4527 |
} |
| 4528 |
|
| 4529 |
/// Test that copying a directory to itself is disallowed. |
| 4530 |
#[test] |
| 4531 |
fn test_copy_directory_to_itself_disallowed() { |
| 4532 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 4533 |
at.mkdir("d"); |
| 4534 |
#[cfg(not(windows))] |
| 4535 |
let expected = "cp: cannot copy a directory, 'd', into itself, 'd/d'\n"; |
| 4536 |
#[cfg(windows)] |
| 4537 |
let expected = "cp: cannot copy a directory, 'd', into itself, 'd\\d'\n"; |
| 4538 |
ucmd.args(&["-R", "d", "d"]).fails().stderr_only(expected); |
| 4539 |
} |
| 4540 |
|
| 4541 |
/// Test that copying a nested directory to itself is disallowed. |
| 4542 |
#[test] |
| 4543 |
fn test_copy_nested_directory_to_itself_disallowed() { |
| 4544 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 4545 |
at.mkdir("a"); |
| 4546 |
at.mkdir("a/b"); |
| 4547 |
at.mkdir("a/b/c"); |
| 4548 |
#[cfg(not(windows))] |
| 4549 |
let expected = "cp: cannot copy a directory, 'a/b', into itself, 'a/b/c/b'\n"; |
| 4550 |
#[cfg(windows)] |
| 4551 |
let expected = "cp: cannot copy a directory, 'a/b', into itself, 'a/b/c\\b'\n"; |
| 4552 |
ucmd.args(&["-R", "a/b", "a/b/c"]) |
| 4553 |
.fails() |
| 4554 |
.stderr_only(expected); |
| 4555 |
} |
| 4556 |
|
| 4557 |
/// Test for preserving permissions when copying a directory. |
| 4558 |
#[cfg(all(not(windows), not(target_os = "freebsd"), not(target_os = "openbsd")))] |
| 4559 |
#[test] |
| 4560 |
#[cfg_attr( |
| 4561 |
wasi_runner, |
| 4562 |
ignore = "WASI: no chmod syscall, so required mode/ownership preservation always fails" |
| 4563 |
)] |
| 4564 |
fn test_copy_dir_preserve_permissions() { |
| 4565 |
// Create a directory that has some non-default permissions. |
| 4566 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 4567 |
at.mkdir("d1"); |
| 4568 |
at.set_mode("d1", 0o0500); |
| 4569 |
|
| 4570 |
// Copy the directory, preserving those permissions. |
| 4571 |
// |
| 4572 |
// preserve permissions (mode, ownership, timestamps) |
| 4573 |
// | copy directories recursively |
| 4574 |
// | | from this source directory |
| 4575 |
// | | | to this destination |
| 4576 |
// | | | | |
| 4577 |
// V V V V |
| 4578 |
ucmd.args(&["-p", "-R", "d1", "d2"]).succeeds().no_output(); |
| 4579 |
assert!(at.dir_exists("d2")); |
| 4580 |
|
| 4581 |
// Assert that the permissions are preserved. |
| 4582 |
let metadata1 = at.metadata("d1"); |
| 4583 |
let metadata2 = at.metadata("d2"); |
| 4584 |
assert_metadata_eq!(metadata1, metadata2); |
| 4585 |
} |
| 4586 |
|
| 4587 |
/// cp should preserve attributes of subdirectories when copying recursively. |
| 4588 |
#[cfg(all(not(windows), not(target_os = "freebsd"), not(target_os = "openbsd")))] |
| 4589 |
#[test] |
| 4590 |
#[cfg_attr( |
| 4591 |
wasi_runner, |
| 4592 |
ignore = "WASI: no chmod syscall, so required mode/ownership preservation always fails" |
| 4593 |
)] |
| 4594 |
fn test_copy_dir_preserve_subdir_permissions() { |
| 4595 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 4596 |
at.mkdir("a1"); |
| 4597 |
at.mkdir("a1/a2"); |
| 4598 |
// Use different permissions for a better test |
| 4599 |
at.set_mode("a1/a2", 0o0555); |
| 4600 |
at.set_mode("a1", 0o0777); |
| 4601 |
|
| 4602 |
ucmd.args(&["-p", "-r", "a1", "b1"]).succeeds().no_output(); |
| 4603 |
|
| 4604 |
// Make sure everything is preserved |
| 4605 |
assert!(at.dir_exists("b1")); |
| 4606 |
assert!(at.dir_exists("b1/a2")); |
| 4607 |
assert_metadata_eq!(at.metadata("a1"), at.metadata("b1")); |
| 4608 |
assert_metadata_eq!(at.metadata("a1/a2"), at.metadata("b1/a2")); |
| 4609 |
} |
| 4610 |
|
| 4611 |
/// cp should successfully copy a read-only source directory containing files. |
| 4612 |
/// Regression test: build_dir previously created the destination with the source's |
| 4613 |
/// read-only mode, causing EPERM when copying files into it. |
| 4614 |
#[cfg(all(not(windows), not(target_os = "freebsd"), not(target_os = "openbsd")))] |
| 4615 |
#[test] |
| 4616 |
#[cfg_attr( |
| 4617 |
wasi_runner, |
| 4618 |
ignore = "WASI: no chmod syscall, so required mode/ownership preservation always fails" |
| 4619 |
)] |
| 4620 |
fn test_copy_dir_preserve_readonly_source_with_files() { |
| 4621 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 4622 |
at.mkdir("src"); |
| 4623 |
at.write("src/file.txt", "hello"); |
| 4624 |
at.set_mode("src", 0o0555); |
| 4625 |
|
| 4626 |
ucmd.args(&["-p", "-r", "src", "dest"]) |
| 4627 |
.succeeds() |
| 4628 |
.no_output(); |
| 4629 |
|
| 4630 |
assert!(at.dir_exists("dest")); |
| 4631 |
assert_eq!(at.read("dest/file.txt"), "hello"); |
| 4632 |
assert_metadata_eq!(at.metadata("src"), at.metadata("dest")); |
| 4633 |
} |
| 4634 |
|
| 4635 |
/// Test for preserving permissions when copying a directory, even in |
| 4636 |
/// the face of an inaccessible file in that directory. |
| 4637 |
#[cfg(all(not(windows), not(target_os = "freebsd"), not(target_os = "openbsd")))] |
| 4638 |
#[test] |
| 4639 |
#[cfg_attr( |
| 4640 |
wasi_runner, |
| 4641 |
ignore = "WASI: no chmod syscall, so required mode/ownership preservation always fails" |
| 4642 |
)] |
| 4643 |
fn test_copy_dir_preserve_permissions_inaccessible_file() { |
| 4644 |
// Create a directory that has some non-default permissions and |
| 4645 |
// contains an inaccessible file. |
| 4646 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 4647 |
at.mkdir("d1"); |
| 4648 |
at.touch("d1/f"); |
| 4649 |
at.set_mode("d1/f", 0); |
| 4650 |
at.set_mode("d1", 0o0500); |
| 4651 |
|
| 4652 |
// Copy the directory, preserving those permissions. There should |
| 4653 |
// be an error message that the file `d1/f` is inaccessible. |
| 4654 |
// |
| 4655 |
// preserve permissions (mode, ownership, timestamps) |
| 4656 |
// | copy directories recursively |
| 4657 |
// | | from this source directory |
| 4658 |
// | | | to this destination |
| 4659 |
// | | | | |
| 4660 |
// V V V V |
| 4661 |
ucmd.args(&["-p", "-R", "d1", "d2"]) |
| 4662 |
.fails_with_code(1) |
| 4663 |
.stderr_only("cp: cannot open 'd1/f' for reading: permission denied\n"); |
| 4664 |
assert!(at.dir_exists("d2")); |
| 4665 |
assert!(!at.file_exists("d2/f")); |
| 4666 |
|
| 4667 |
// Assert that the permissions are preserved. |
| 4668 |
let metadata1 = at.metadata("d1"); |
| 4669 |
let metadata2 = at.metadata("d2"); |
| 4670 |
assert_metadata_eq!(metadata1, metadata2); |
| 4671 |
} |
| 4672 |
|
| 4673 |
/// Test that copying file to itself with backup fails. |
| 4674 |
#[test] |
| 4675 |
fn test_same_file_backup() { |
| 4676 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 4677 |
at.touch("f"); |
| 4678 |
ucmd.args(&["--backup", "f", "f"]) |
| 4679 |
.fails() |
| 4680 |
.stderr_only("cp: 'f' and 'f' are the same file\n"); |
| 4681 |
assert!(!at.file_exists("f~")); |
| 4682 |
} |
| 4683 |
|
| 4684 |
/// Test that copying file to itself with forced backup succeeds. |
| 4685 |
#[cfg(not(windows))] |
| 4686 |
#[test] |
| 4687 |
fn test_same_file_force() { |
| 4688 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 4689 |
at.touch("f"); |
| 4690 |
ucmd.args(&["--force", "f", "f"]) |
| 4691 |
.fails() |
| 4692 |
.stderr_only("cp: 'f' and 'f' are the same file\n"); |
| 4693 |
assert!(!at.file_exists("f~")); |
| 4694 |
} |
| 4695 |
|
| 4696 |
/// Test that copying file to itself with forced backup succeeds. |
| 4697 |
#[cfg(not(windows))] |
| 4698 |
#[test] |
| 4699 |
#[cfg_attr( |
| 4700 |
wasi_runner, |
| 4701 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 4702 |
)] |
| 4703 |
fn test_same_file_force_backup() { |
| 4704 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 4705 |
at.touch("f"); |
| 4706 |
ucmd.args(&["--force", "--backup", "f", "f"]) |
| 4707 |
.succeeds() |
| 4708 |
.no_output(); |
| 4709 |
assert!(at.file_exists("f~")); |
| 4710 |
} |
| 4711 |
|
| 4712 |
/// Test for copying the contents of a FIFO as opposed to the FIFO object itself. |
| 4713 |
#[cfg(unix)] |
| 4714 |
#[test] |
| 4715 |
#[cfg_attr(wasi_runner, ignore = "WASI: no FIFO/mkfifo support")] |
| 4716 |
fn test_copy_contents_fifo() { |
| 4717 |
let scenario = TestScenario::new(util_name!()); |
| 4718 |
let at = &scenario.fixtures; |
| 4719 |
|
| 4720 |
// Start the `cp` process, reading the contents of `fifo` and |
| 4721 |
// writing to regular file `outfile`. |
| 4722 |
at.mkfifo("fifo"); |
| 4723 |
let mut ucmd = scenario.ucmd(); |
| 4724 |
let child = ucmd |
| 4725 |
.args(&["--copy-contents", "fifo", "outfile"]) |
| 4726 |
.run_no_wait(); |
| 4727 |
|
| 4728 |
// Write some bytes to the `fifo`. We expect these bytes to get |
| 4729 |
// copied through to `outfile`. |
| 4730 |
std::fs::write(at.plus("fifo"), "foo").unwrap(); |
| 4731 |
|
| 4732 |
// At this point the child process should have terminated |
| 4733 |
// successfully with no output. The `outfile` should have the |
| 4734 |
// contents of `fifo` copied into it. |
| 4735 |
child.wait().unwrap().no_output().success(); |
| 4736 |
assert_eq!(at.read("outfile"), "foo"); |
| 4737 |
} |
| 4738 |
|
| 4739 |
#[cfg(target_os = "linux")] |
| 4740 |
#[test] |
| 4741 |
#[cfg_attr(wasi_runner, ignore = "WASI: --sparse is only supported on linux")] |
| 4742 |
fn test_reflink_never_sparse_always() { |
| 4743 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 4744 |
|
| 4745 |
// Create a file and make it a large sparse file. |
| 4746 |
// |
| 4747 |
// On common Linux filesystems, setting the length to one megabyte |
| 4748 |
// should cause the file to become a sparse file, but it depends |
| 4749 |
// on the system. |
| 4750 |
std::fs::File::create(at.plus("src")) |
| 4751 |
.unwrap() |
| 4752 |
.set_len(1024 * 1024) |
| 4753 |
.unwrap(); |
| 4754 |
|
| 4755 |
ucmd.args(&["--reflink=never", "--sparse=always", "src", "dest"]) |
| 4756 |
.succeeds() |
| 4757 |
.no_output(); |
| 4758 |
at.file_exists("dest"); |
| 4759 |
|
| 4760 |
let src_metadata = std::fs::metadata(at.plus("src")).unwrap(); |
| 4761 |
let dest_metadata = std::fs::metadata(at.plus("dest")).unwrap(); |
| 4762 |
assert_eq!(src_metadata.blocks(), dest_metadata.blocks()); |
| 4763 |
assert_eq!(dest_metadata.len(), 1024 * 1024); |
| 4764 |
} |
| 4765 |
|
| 4766 |
/// Test for preserving attributes of a hard link in a directory. |
| 4767 |
#[test] |
| 4768 |
#[cfg(not(any(target_os = "android", target_os = "openbsd")))] |
| 4769 |
#[cfg_attr( |
| 4770 |
wasi_runner, |
| 4771 |
ignore = "WASI: no chmod syscall, so required mode/ownership preservation always fails" |
| 4772 |
)] |
| 4773 |
fn test_preserve_hardlink_attributes_in_directory() { |
| 4774 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 4775 |
|
| 4776 |
// The source directory tree. |
| 4777 |
at.mkdir("src"); |
| 4778 |
at.touch("src/f"); |
| 4779 |
at.hard_link("src/f", "src/link"); |
| 4780 |
|
| 4781 |
// The destination directory tree. |
| 4782 |
// |
| 4783 |
// The file `f` already exists, but the `link` does not. |
| 4784 |
at.mkdir_all("dest/src"); |
| 4785 |
at.touch("dest/src/f"); |
| 4786 |
|
| 4787 |
ucmd.args(&["-a", "src", "dest"]).succeeds().no_output(); |
| 4788 |
|
| 4789 |
// The hard link should now appear in the destination directory tree. |
| 4790 |
// |
| 4791 |
// A hard link should have the same inode as the target file. |
| 4792 |
at.file_exists("dest/src/link"); |
| 4793 |
#[cfg(all(unix, not(target_os = "freebsd"), not(target_os = "openbsd")))] |
| 4794 |
assert_eq!( |
| 4795 |
at.metadata("dest/src/f").ino(), |
| 4796 |
at.metadata("dest/src/link").ino() |
| 4797 |
); |
| 4798 |
} |
| 4799 |
|
| 4800 |
#[test] |
| 4801 |
#[cfg(not(any(windows, target_os = "android")))] |
| 4802 |
fn test_hard_link_file() { |
| 4803 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 4804 |
at.touch("src"); |
| 4805 |
at.touch("dest"); |
| 4806 |
ucmd.args(&["-f", "--link", "src", "dest"]) |
| 4807 |
.succeeds() |
| 4808 |
.no_output(); |
| 4809 |
#[cfg(all(unix, not(target_os = "freebsd"), not(target_os = "openbsd")))] |
| 4810 |
assert_eq!(at.metadata("src").ino(), at.metadata("dest").ino()); |
| 4811 |
} |
| 4812 |
|
| 4813 |
#[test] |
| 4814 |
#[cfg(not(windows))] |
| 4815 |
fn test_symbolic_link_file() { |
| 4816 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 4817 |
at.touch("src"); |
| 4818 |
at.touch("dest"); |
| 4819 |
ucmd.args(&["-f", "--symbolic-link", "src", "dest"]) |
| 4820 |
.succeeds() |
| 4821 |
.no_output(); |
| 4822 |
assert_eq!( |
| 4823 |
std::fs::read_link(at.plus("dest")).unwrap(), |
| 4824 |
Path::new("src") |
| 4825 |
); |
| 4826 |
} |
| 4827 |
|
| 4828 |
#[test] |
| 4829 |
#[cfg_attr( |
| 4830 |
wasi_runner, |
| 4831 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 4832 |
)] |
| 4833 |
fn test_src_base_dot() { |
| 4834 |
let ts = TestScenario::new(util_name!()); |
| 4835 |
let at = &ts.fixtures; |
| 4836 |
at.mkdir("x"); |
| 4837 |
at.mkdir("y"); |
| 4838 |
ts.ucmd() |
| 4839 |
.current_dir(at.plus("y")) |
| 4840 |
.args(&["--verbose", "-r", "../x/.", "."]) |
| 4841 |
.succeeds() |
| 4842 |
.no_output(); |
| 4843 |
assert!(!at.dir_exists("y/x")); |
| 4844 |
} |
| 4845 |
|
| 4846 |
#[cfg(target_os = "linux")] |
| 4847 |
fn non_utf8_name(suffix: &str) -> OsString { |
| 4848 |
use std::os::unix::ffi::OsStringExt; |
| 4849 |
let mut name = OsString::from_vec(vec![0xff, 0xff]); |
| 4850 |
name.push(suffix); |
| 4851 |
name |
| 4852 |
} |
| 4853 |
|
| 4854 |
#[cfg(target_os = "linux")] |
| 4855 |
#[test] |
| 4856 |
#[cfg_attr( |
| 4857 |
wasi_runner, |
| 4858 |
ignore = "WASI: requires valid UTF-8 paths, so the non-UTF-8 error is expected" |
| 4859 |
)] |
| 4860 |
fn test_non_utf8_src() { |
| 4861 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 4862 |
let src = non_utf8_name("src"); |
| 4863 |
std::fs::File::create(at.plus(&src)).unwrap(); |
| 4864 |
ucmd.args(&[src, "dest".into()]).succeeds().no_output(); |
| 4865 |
assert!(at.file_exists("dest")); |
| 4866 |
} |
| 4867 |
|
| 4868 |
#[cfg(target_os = "linux")] |
| 4869 |
#[test] |
| 4870 |
#[cfg_attr( |
| 4871 |
wasi_runner, |
| 4872 |
ignore = "WASI: requires valid UTF-8 paths, so the non-UTF-8 error is expected" |
| 4873 |
)] |
| 4874 |
fn test_non_utf8_dest() { |
| 4875 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 4876 |
let dest = non_utf8_name("dest"); |
| 4877 |
ucmd.args(&[TEST_HELLO_WORLD_SOURCE.as_ref(), &*dest]) |
| 4878 |
.succeeds() |
| 4879 |
.no_output(); |
| 4880 |
assert!(at.file_exists(dest)); |
| 4881 |
} |
| 4882 |
|
| 4883 |
#[cfg(target_os = "linux")] |
| 4884 |
#[test] |
| 4885 |
#[cfg_attr( |
| 4886 |
wasi_runner, |
| 4887 |
ignore = "WASI: requires valid UTF-8 paths, so the non-UTF-8 error is expected" |
| 4888 |
)] |
| 4889 |
fn test_non_utf8_target() { |
| 4890 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 4891 |
let dest = non_utf8_name("dest"); |
| 4892 |
at.mkdir(&dest); |
| 4893 |
ucmd.args(&["-t".as_ref(), &*dest, TEST_HELLO_WORLD_SOURCE.as_ref()]) |
| 4894 |
.succeeds() |
| 4895 |
.no_output(); |
| 4896 |
let mut copied_file = PathBuf::from(dest); |
| 4897 |
copied_file.push(TEST_HELLO_WORLD_SOURCE); |
| 4898 |
assert!(at.file_exists(copied_file)); |
| 4899 |
} |
| 4900 |
|
| 4901 |
#[test] |
| 4902 |
#[cfg(not(windows))] |
| 4903 |
#[cfg_attr( |
| 4904 |
wasi_runner, |
| 4905 |
ignore = "WASI: no chmod syscall, so required mode/ownership preservation always fails" |
| 4906 |
)] |
| 4907 |
fn test_cp_archive_on_directory_ending_dot() { |
| 4908 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 4909 |
at.mkdir("dir1"); |
| 4910 |
at.mkdir("dir2"); |
| 4911 |
at.touch("dir1/file"); |
| 4912 |
ucmd.args(&["-a", "dir1/.", "dir2"]).succeeds(); |
| 4913 |
assert!(at.file_exists("dir2/file")); |
| 4914 |
} |
| 4915 |
|
| 4916 |
#[test] |
| 4917 |
#[cfg(any(windows, target_vendor = "apple", target_os = "linux"))] |
| 4918 |
#[cfg_attr( |
| 4919 |
wasi_runner, |
| 4920 |
ignore = "WASI: --debug reports 'unsupported' instead of 'unknown' for this platform" |
| 4921 |
)] |
| 4922 |
fn test_cp_debug_default() { |
| 4923 |
#[cfg(target_vendor = "apple")] |
| 4924 |
let expected = "copy offload: unknown, reflink: unsupported, sparse detection: unsupported"; |
| 4925 |
#[cfg(target_os = "linux")] |
| 4926 |
let expected = "copy offload: unknown, reflink: unsupported, sparse detection: no"; |
| 4927 |
#[cfg(windows)] |
| 4928 |
let expected = "copy offload: unsupported, reflink: unsupported, sparse detection: unsupported"; |
| 4929 |
|
| 4930 |
let ts = TestScenario::new(util_name!()); |
| 4931 |
let at = &ts.fixtures; |
| 4932 |
at.touch("a"); |
| 4933 |
|
| 4934 |
ts.ucmd() |
| 4935 |
.arg("--debug") |
| 4936 |
.arg("a") |
| 4937 |
.arg("b") |
| 4938 |
.succeeds() |
| 4939 |
.stdout_contains(expected); |
| 4940 |
} |
| 4941 |
|
| 4942 |
#[test] |
| 4943 |
#[cfg(any(windows, target_vendor = "apple", target_os = "linux"))] |
| 4944 |
#[cfg_attr( |
| 4945 |
wasi_runner, |
| 4946 |
ignore = "WASI: --debug reports 'unsupported' instead of 'unknown' for this platform" |
| 4947 |
)] |
| 4948 |
fn test_cp_debug_multiple_default() { |
| 4949 |
let ts = TestScenario::new(util_name!()); |
| 4950 |
let at = &ts.fixtures; |
| 4951 |
let dir = "dir"; |
| 4952 |
at.touch("a"); |
| 4953 |
at.touch("b"); |
| 4954 |
at.mkdir(dir); |
| 4955 |
|
| 4956 |
let result = ts |
| 4957 |
.ucmd() |
| 4958 |
.arg("--debug") |
| 4959 |
.arg("a") |
| 4960 |
.arg("b") |
| 4961 |
.arg(dir) |
| 4962 |
.succeeds(); |
| 4963 |
|
| 4964 |
#[cfg(target_vendor = "apple")] |
| 4965 |
let expected = "copy offload: unknown, reflink: unsupported, sparse detection: unsupported"; |
| 4966 |
#[cfg(target_os = "linux")] |
| 4967 |
let expected = "copy offload: unknown, reflink: unsupported, sparse detection: no"; |
| 4968 |
#[cfg(windows)] |
| 4969 |
let expected = "copy offload: unsupported, reflink: unsupported, sparse detection: unsupported"; |
| 4970 |
|
| 4971 |
// two files, two occurrences |
| 4972 |
assert_eq!(result.stdout_str().matches(expected).count(), 2); |
| 4973 |
} |
| 4974 |
|
| 4975 |
#[test] |
| 4976 |
#[cfg(target_os = "linux")] |
| 4977 |
#[cfg_attr(wasi_runner, ignore = "WASI: --sparse is only supported on linux")] |
| 4978 |
fn test_cp_debug_sparse_reflink() { |
| 4979 |
let ts = TestScenario::new(util_name!()); |
| 4980 |
let at = &ts.fixtures; |
| 4981 |
at.touch("a"); |
| 4982 |
|
| 4983 |
ts.ucmd() |
| 4984 |
.arg("--debug") |
| 4985 |
.arg("--sparse=always") |
| 4986 |
.arg("--reflink=never") |
| 4987 |
.arg("a") |
| 4988 |
.arg("b") |
| 4989 |
.succeeds() |
| 4990 |
.stdout_contains("copy offload: avoided, reflink: no, sparse detection: zeros"); |
| 4991 |
} |
| 4992 |
|
| 4993 |
#[test] |
| 4994 |
fn test_cp_debug_no_update() { |
| 4995 |
let ts = TestScenario::new(util_name!()); |
| 4996 |
let at = &ts.fixtures; |
| 4997 |
at.touch("a"); |
| 4998 |
at.touch("b"); |
| 4999 |
ts.ucmd() |
| 5000 |
.arg("--debug") |
| 5001 |
.arg("--update=none") |
| 5002 |
.arg("a") |
| 5003 |
.arg("b") |
| 5004 |
.succeeds() |
| 5005 |
.stdout_contains("skipped 'b'"); |
| 5006 |
} |
| 5007 |
|
| 5008 |
#[test] |
| 5009 |
#[cfg(target_os = "linux")] |
| 5010 |
#[cfg_attr(wasi_runner, ignore = "WASI: --sparse is only supported on linux")] |
| 5011 |
fn test_cp_debug_sparse_always() { |
| 5012 |
let ts = TestScenario::new(util_name!()); |
| 5013 |
let at = &ts.fixtures; |
| 5014 |
at.touch("a"); |
| 5015 |
|
| 5016 |
ts.ucmd() |
| 5017 |
.arg("--debug") |
| 5018 |
.arg("--sparse=always") |
| 5019 |
.arg("a") |
| 5020 |
.arg("b") |
| 5021 |
.succeeds() |
| 5022 |
.stdout_contains("copy offload: avoided, reflink: unsupported, sparse detection: zeros"); |
| 5023 |
} |
| 5024 |
|
| 5025 |
#[test] |
| 5026 |
#[cfg(target_os = "linux")] |
| 5027 |
#[cfg_attr(wasi_runner, ignore = "WASI: --sparse is only supported on linux")] |
| 5028 |
fn test_cp_debug_sparse_never() { |
| 5029 |
let ts = TestScenario::new(util_name!()); |
| 5030 |
let at = &ts.fixtures; |
| 5031 |
at.touch("a"); |
| 5032 |
|
| 5033 |
ts.ucmd() |
| 5034 |
.arg("--debug") |
| 5035 |
.arg("--sparse=never") |
| 5036 |
.arg("a") |
| 5037 |
.arg("b") |
| 5038 |
.succeeds() |
| 5039 |
.stdout_contains("copy offload: avoided, reflink: no, sparse detection: no"); |
| 5040 |
} |
| 5041 |
|
| 5042 |
#[test] |
| 5043 |
#[cfg_attr( |
| 5044 |
wasi_runner, |
| 5045 |
ignore = "WASI: --debug reports 'unsupported' instead of 'unknown' for this platform" |
| 5046 |
)] |
| 5047 |
fn test_cp_debug_sparse_auto() { |
| 5048 |
let ts = TestScenario::new(util_name!()); |
| 5049 |
let at = &ts.fixtures; |
| 5050 |
at.touch("a"); |
| 5051 |
|
| 5052 |
#[cfg(not(any(target_vendor = "apple", target_os = "linux")))] |
| 5053 |
ts.ucmd() |
| 5054 |
.arg("--debug") |
| 5055 |
.arg("--sparse=auto") |
| 5056 |
.arg("a") |
| 5057 |
.arg("b") |
| 5058 |
.succeeds(); |
| 5059 |
|
| 5060 |
#[cfg(any(target_vendor = "apple", target_os = "linux"))] |
| 5061 |
{ |
| 5062 |
#[cfg(target_vendor = "apple")] |
| 5063 |
let expected = "copy offload: unknown, reflink: unsupported, sparse detection: unsupported"; |
| 5064 |
#[cfg(target_os = "linux")] |
| 5065 |
let expected = "copy offload: unknown, reflink: unsupported, sparse detection: no"; |
| 5066 |
|
| 5067 |
ts.ucmd() |
| 5068 |
.arg("--debug") |
| 5069 |
.arg("--sparse=auto") |
| 5070 |
.arg("a") |
| 5071 |
.arg("b") |
| 5072 |
.succeeds() |
| 5073 |
.stdout_contains(expected); |
| 5074 |
} |
| 5075 |
} |
| 5076 |
|
| 5077 |
#[test] |
| 5078 |
#[cfg(any(target_vendor = "apple", target_os = "linux"))] |
| 5079 |
#[cfg_attr( |
| 5080 |
wasi_runner, |
| 5081 |
ignore = "WASI: --debug reports 'unsupported' instead of 'unknown' for this platform" |
| 5082 |
)] |
| 5083 |
fn test_cp_debug_reflink_auto() { |
| 5084 |
#[cfg(target_vendor = "apple")] |
| 5085 |
let expected = "copy offload: unknown, reflink: unsupported, sparse detection: unsupported"; |
| 5086 |
#[cfg(target_os = "linux")] |
| 5087 |
let expected = "copy offload: unknown, reflink: unsupported, sparse detection: no"; |
| 5088 |
|
| 5089 |
let ts = TestScenario::new(util_name!()); |
| 5090 |
let at = &ts.fixtures; |
| 5091 |
at.touch("a"); |
| 5092 |
|
| 5093 |
ts.ucmd() |
| 5094 |
.arg("--debug") |
| 5095 |
.arg("--reflink=auto") |
| 5096 |
.arg("a") |
| 5097 |
.arg("b") |
| 5098 |
.succeeds() |
| 5099 |
.stdout_contains(expected); |
| 5100 |
} |
| 5101 |
|
| 5102 |
#[test] |
| 5103 |
#[cfg(target_os = "linux")] |
| 5104 |
#[cfg_attr(wasi_runner, ignore = "WASI: --sparse is only supported on linux")] |
| 5105 |
fn test_cp_debug_sparse_always_reflink_auto() { |
| 5106 |
let ts = TestScenario::new(util_name!()); |
| 5107 |
let at = &ts.fixtures; |
| 5108 |
at.touch("a"); |
| 5109 |
ts.ucmd() |
| 5110 |
.arg("--debug") |
| 5111 |
.arg("--sparse=always") |
| 5112 |
.arg("--reflink=auto") |
| 5113 |
.arg("a") |
| 5114 |
.arg("b") |
| 5115 |
.succeeds() |
| 5116 |
.stdout_contains("copy offload: avoided, reflink: unsupported, sparse detection: zeros"); |
| 5117 |
} |
| 5118 |
|
| 5119 |
#[test] |
| 5120 |
fn test_cp_only_source_no_target() { |
| 5121 |
let ts = TestScenario::new(util_name!()); |
| 5122 |
let at = &ts.fixtures; |
| 5123 |
at.touch("a"); |
| 5124 |
ts.ucmd().arg("a").fails().stderr_contains(format!( |
| 5125 |
"missing destination file operand after {}", |
| 5126 |
"a".quote() |
| 5127 |
)); |
| 5128 |
} |
| 5129 |
|
| 5130 |
#[test] |
| 5131 |
fn test_cp_dest_no_permissions() { |
| 5132 |
let ts = TestScenario::new(util_name!()); |
| 5133 |
let at = &ts.fixtures; |
| 5134 |
|
| 5135 |
at.touch("valid.txt"); |
| 5136 |
at.touch("invalid_perms.txt"); |
| 5137 |
at.set_readonly("invalid_perms.txt"); |
| 5138 |
|
| 5139 |
ts.ucmd() |
| 5140 |
.args(&["valid.txt", "invalid_perms.txt"]) |
| 5141 |
.fails() |
| 5142 |
.stderr_contains("invalid_perms.txt") |
| 5143 |
.stderr_contains("denied"); |
| 5144 |
} |
| 5145 |
|
| 5146 |
/// Test readonly destination behavior with reflink options |
| 5147 |
#[cfg(any(target_vendor = "apple", target_os = "linux"))] |
| 5148 |
#[test] |
| 5149 |
#[cfg_attr( |
| 5150 |
wasi_runner, |
| 5151 |
ignore = "WASI: --reflink is only supported on linux and macOS" |
| 5152 |
)] |
| 5153 |
fn test_cp_readonly_dest_with_reflink() { |
| 5154 |
let ts = TestScenario::new(util_name!()); |
| 5155 |
let at = &ts.fixtures; |
| 5156 |
|
| 5157 |
at.write("source.txt", "source content"); |
| 5158 |
at.write("readonly_dest_auto.txt", "original content"); |
| 5159 |
at.write("readonly_dest_always.txt", "original content"); |
| 5160 |
at.set_readonly("readonly_dest_auto.txt"); |
| 5161 |
at.set_readonly("readonly_dest_always.txt"); |
| 5162 |
|
| 5163 |
// Test reflink=auto |
| 5164 |
ts.ucmd() |
| 5165 |
.args(&["--reflink=auto", "source.txt", "readonly_dest_auto.txt"]) |
| 5166 |
.fails() |
| 5167 |
.stderr_contains("readonly_dest_auto.txt"); |
| 5168 |
|
| 5169 |
// Test reflink=always |
| 5170 |
ts.ucmd() |
| 5171 |
.args(&["--reflink=always", "source.txt", "readonly_dest_always.txt"]) |
| 5172 |
.fails() |
| 5173 |
.stderr_contains("readonly_dest_always.txt"); |
| 5174 |
|
| 5175 |
assert_eq!(at.read("readonly_dest_auto.txt"), "original content"); |
| 5176 |
assert_eq!(at.read("readonly_dest_always.txt"), "original content"); |
| 5177 |
} |
| 5178 |
|
| 5179 |
/// Test readonly destination behavior in recursive directory copy |
| 5180 |
#[test] |
| 5181 |
fn test_cp_readonly_dest_recursive() { |
| 5182 |
let ts = TestScenario::new(util_name!()); |
| 5183 |
let at = &ts.fixtures; |
| 5184 |
|
| 5185 |
at.mkdir("source_dir"); |
| 5186 |
at.mkdir("dest_dir"); |
| 5187 |
at.write("source_dir/file.txt", "source content"); |
| 5188 |
at.write("dest_dir/file.txt", "original content"); |
| 5189 |
at.set_readonly("dest_dir/file.txt"); |
| 5190 |
|
| 5191 |
ts.ucmd().args(&["-r", "source_dir", "dest_dir"]).succeeds(); |
| 5192 |
|
| 5193 |
assert_eq!(at.read("dest_dir/file.txt"), "original content"); |
| 5194 |
} |
| 5195 |
|
| 5196 |
/// Test copying to readonly file when another file exists |
| 5197 |
#[test] |
| 5198 |
fn test_cp_readonly_dest_with_existing_file() { |
| 5199 |
let ts = TestScenario::new(util_name!()); |
| 5200 |
let at = &ts.fixtures; |
| 5201 |
|
| 5202 |
at.write("source.txt", "source content"); |
| 5203 |
at.write("readonly_dest.txt", "original content"); |
| 5204 |
at.write("other_file.txt", "other content"); |
| 5205 |
at.set_readonly("readonly_dest.txt"); |
| 5206 |
|
| 5207 |
ts.ucmd() |
| 5208 |
.args(&["source.txt", "readonly_dest.txt"]) |
| 5209 |
.fails() |
| 5210 |
.stderr_contains("readonly_dest.txt") |
| 5211 |
.stderr_contains("denied"); |
| 5212 |
|
| 5213 |
assert_eq!(at.read("readonly_dest.txt"), "original content"); |
| 5214 |
assert_eq!(at.read("other_file.txt"), "other content"); |
| 5215 |
} |
| 5216 |
|
| 5217 |
/// Test readonly source file (should work fine) |
| 5218 |
#[test] |
| 5219 |
fn test_cp_readonly_source() { |
| 5220 |
let ts = TestScenario::new(util_name!()); |
| 5221 |
let at = &ts.fixtures; |
| 5222 |
|
| 5223 |
at.write("readonly_source.txt", "source content"); |
| 5224 |
at.write("dest.txt", "dest content"); |
| 5225 |
at.set_readonly("readonly_source.txt"); |
| 5226 |
|
| 5227 |
ts.ucmd() |
| 5228 |
.args(&["readonly_source.txt", "dest.txt"]) |
| 5229 |
.succeeds(); |
| 5230 |
|
| 5231 |
assert_eq!(at.read("dest.txt"), "source content"); |
| 5232 |
} |
| 5233 |
|
| 5234 |
/// Test readonly source and destination (should fail) |
| 5235 |
#[test] |
| 5236 |
fn test_cp_readonly_source_and_dest() { |
| 5237 |
let ts = TestScenario::new(util_name!()); |
| 5238 |
let at = &ts.fixtures; |
| 5239 |
|
| 5240 |
at.write("readonly_source.txt", "source content"); |
| 5241 |
at.write("readonly_dest.txt", "original content"); |
| 5242 |
at.set_readonly("readonly_source.txt"); |
| 5243 |
at.set_readonly("readonly_dest.txt"); |
| 5244 |
|
| 5245 |
ts.ucmd() |
| 5246 |
.args(&["readonly_source.txt", "readonly_dest.txt"]) |
| 5247 |
.fails() |
| 5248 |
.stderr_contains("readonly_dest.txt") |
| 5249 |
.stderr_contains("denied"); |
| 5250 |
|
| 5251 |
assert_eq!(at.read("readonly_dest.txt"), "original content"); |
| 5252 |
} |
| 5253 |
|
| 5254 |
#[test] |
| 5255 |
#[cfg(all(unix, not(target_os = "freebsd"), not(target_os = "openbsd")))] |
| 5256 |
fn test_cp_attributes_only() { |
| 5257 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 5258 |
let a = "file_a"; |
| 5259 |
let b = "file_b"; |
| 5260 |
let mode_a = 0o0500; |
| 5261 |
let mode_b = 0o0777; |
| 5262 |
|
| 5263 |
at.write(a, "a"); |
| 5264 |
at.write(b, "b"); |
| 5265 |
at.set_mode(a, mode_a); |
| 5266 |
at.set_mode(b, mode_b); |
| 5267 |
|
| 5268 |
let mode_a = at.metadata(a).mode(); |
| 5269 |
let mode_b = at.metadata(b).mode(); |
| 5270 |
|
| 5271 |
// --attributes-only doesn't do anything without other attribute preservation flags |
| 5272 |
ucmd.arg("--attributes-only") |
| 5273 |
.arg(a) |
| 5274 |
.arg(b) |
| 5275 |
.succeeds() |
| 5276 |
.no_output(); |
| 5277 |
|
| 5278 |
assert_eq!("a", at.read(a)); |
| 5279 |
assert_eq!("b", at.read(b)); |
| 5280 |
assert_eq!(mode_a, at.metadata(a).mode()); |
| 5281 |
assert_eq!(mode_b, at.metadata(b).mode()); |
| 5282 |
} |
| 5283 |
|
| 5284 |
#[test] |
| 5285 |
#[cfg(unix)] |
| 5286 |
fn test_cp_attributes_only_dest_open_error() { |
| 5287 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 5288 |
at.write("s.txt", "hi"); |
| 5289 |
ucmd.args(&["--attributes-only", "s.txt", "/dev/null/n.txt"]) |
| 5290 |
.fails_with_code(1) |
| 5291 |
.stderr_contains("cp: cannot create regular file '/dev/null/n.txt'"); |
| 5292 |
} |
| 5293 |
|
| 5294 |
#[test] |
| 5295 |
#[cfg(unix)] |
| 5296 |
#[cfg_attr( |
| 5297 |
wasi_runner, |
| 5298 |
ignore = "WASI sandbox: host paths (/dev, /private) not visible" |
| 5299 |
)] |
| 5300 |
fn test_cp_cannot_create_regular_file() { |
| 5301 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 5302 |
at.write("source.txt", "hello"); |
| 5303 |
ucmd.arg("source.txt") |
| 5304 |
.arg("/dev/null/n.txt") |
| 5305 |
.fails_with_code(1) |
| 5306 |
.stderr_contains("cp: cannot create regular file '/dev/null/n.txt'"); |
| 5307 |
} |
| 5308 |
|
| 5309 |
#[test] |
| 5310 |
#[cfg(unix)] |
| 5311 |
#[cfg_attr( |
| 5312 |
wasi_runner, |
| 5313 |
ignore = "WASI sandbox: host paths (/dev, /private) not visible" |
| 5314 |
)] |
| 5315 |
fn test_cp_cannot_create_regular_file_attributes_only() { |
| 5316 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 5317 |
at.write("source.txt", "hello"); |
| 5318 |
ucmd.args(&["--attributes-only", "source.txt", "/dev/null/n.txt"]) |
| 5319 |
.fails_with_code(1) |
| 5320 |
.stderr_only("cp: cannot create regular file '/dev/null/n.txt': Not a directory\n"); |
| 5321 |
} |
| 5322 |
|
| 5323 |
#[test] |
| 5324 |
fn test_cp_attributes_only_same_file() { |
| 5325 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 5326 |
let file = "a"; |
| 5327 |
|
| 5328 |
at.touch(file); |
| 5329 |
|
| 5330 |
ucmd.arg("--attributes-only") |
| 5331 |
.arg(file) |
| 5332 |
.arg(file) |
| 5333 |
.fails_with_code(1) |
| 5334 |
.stderr_contains(format!("'{file}' and '{file}' are the same file")); |
| 5335 |
} |
| 5336 |
|
| 5337 |
#[test] |
| 5338 |
fn test_cp_attributes_only_same_file_dot_path() { |
| 5339 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 5340 |
let file = "a"; |
| 5341 |
|
| 5342 |
at.touch(file); |
| 5343 |
|
| 5344 |
ucmd.arg("--attributes-only") |
| 5345 |
.arg(file) |
| 5346 |
.arg("./a") |
| 5347 |
.fails_with_code(1) |
| 5348 |
.stderr_contains(format!("'{file}' and './{file}' are the same file")); |
| 5349 |
} |
| 5350 |
|
| 5351 |
#[test] |
| 5352 |
fn test_cp_seen_file() { |
| 5353 |
let ts = TestScenario::new(util_name!()); |
| 5354 |
let at = &ts.fixtures; |
| 5355 |
|
| 5356 |
at.mkdir("a"); |
| 5357 |
at.mkdir("b"); |
| 5358 |
at.mkdir("c"); |
| 5359 |
at.write("a/f", "a"); |
| 5360 |
at.write("b/f", "b"); |
| 5361 |
|
| 5362 |
let result = ts.ucmd().arg("a/f").arg("b/f").arg("c").fails(); |
| 5363 |
#[cfg(not(unix))] |
| 5364 |
assert!( |
| 5365 |
result |
| 5366 |
.stderr_str() |
| 5367 |
.contains("will not overwrite just-created 'c\\f' with 'b/f'") |
| 5368 |
); |
| 5369 |
#[cfg(unix)] |
| 5370 |
assert!( |
| 5371 |
result |
| 5372 |
.stderr_str() |
| 5373 |
.contains("will not overwrite just-created 'c/f' with 'b/f'") |
| 5374 |
); |
| 5375 |
|
| 5376 |
assert!(at.plus("c").join("f").exists()); |
| 5377 |
|
| 5378 |
ts.ucmd() |
| 5379 |
.arg("--backup=numbered") |
| 5380 |
.arg("a/f") |
| 5381 |
.arg("b/f") |
| 5382 |
.arg("c") |
| 5383 |
.succeeds(); |
| 5384 |
assert!(at.plus("c").join("f").exists()); |
| 5385 |
assert!(at.plus("c").join("f.~1~").exists()); |
| 5386 |
} |
| 5387 |
|
| 5388 |
#[test] |
| 5389 |
fn test_cp_path_ends_with_terminator() { |
| 5390 |
let ts = TestScenario::new(util_name!()); |
| 5391 |
let at = &ts.fixtures; |
| 5392 |
at.mkdir("a"); |
| 5393 |
ts.ucmd().arg("-r").arg("-T").arg("a").arg("e/").succeeds(); |
| 5394 |
} |
| 5395 |
|
| 5396 |
#[test] |
| 5397 |
fn test_cp_no_such() { |
| 5398 |
let ts = TestScenario::new(util_name!()); |
| 5399 |
let at = &ts.fixtures; |
| 5400 |
at.touch("b"); |
| 5401 |
ts.ucmd() |
| 5402 |
.arg("b") |
| 5403 |
.arg("no-such/") |
| 5404 |
.fails() |
| 5405 |
.stderr_is("cp: 'no-such/' is not a directory\n"); |
| 5406 |
} |
| 5407 |
|
| 5408 |
#[cfg(all( |
| 5409 |
unix, |
| 5410 |
not(any(target_vendor = "apple", target_os = "android", target_os = "openbsd")) |
| 5411 |
))] |
| 5412 |
#[test] |
| 5413 |
#[cfg_attr( |
| 5414 |
wasi_runner, |
| 5415 |
ignore = "WASI sandbox: absolute host tmpdir path is outside the mapped guest root, so cp cannot stat it" |
| 5416 |
)] |
| 5417 |
fn test_acl_preserve() { |
| 5418 |
use std::process::Command; |
| 5419 |
|
| 5420 |
let scene = TestScenario::new(util_name!()); |
| 5421 |
let at = &scene.fixtures; |
| 5422 |
let path1 = "a"; |
| 5423 |
let path2 = "b"; |
| 5424 |
let file = "a/file"; |
| 5425 |
let file_target = "b/file"; |
| 5426 |
at.mkdir(path1); |
| 5427 |
at.mkdir(path2); |
| 5428 |
at.touch(file); |
| 5429 |
|
| 5430 |
let path1 = at.plus_as_string(path1); |
| 5431 |
// calling the command directly. xattr requires some dev packages to be installed |
| 5432 |
// and it adds a complex dependency just for a test |
| 5433 |
match Command::new("setfacl") |
| 5434 |
.args(["-m", "group::rwx", &path1]) |
| 5435 |
.status() |
| 5436 |
.map(|status| status.code()) |
| 5437 |
{ |
| 5438 |
Ok(Some(0)) => {} |
| 5439 |
Ok(_) => { |
| 5440 |
println!("test skipped: setfacl failed"); |
| 5441 |
return; |
| 5442 |
} |
| 5443 |
Err(e) => { |
| 5444 |
println!("test skipped: setfacl failed with {e}"); |
| 5445 |
return; |
| 5446 |
} |
| 5447 |
} |
| 5448 |
|
| 5449 |
let path = at.plus_as_string(file); |
| 5450 |
scene.ucmd().args(&["-p", &path, path2]).succeeds(); |
| 5451 |
|
| 5452 |
assert!(compare_xattrs(&file, &file_target)); |
| 5453 |
} |
| 5454 |
|
| 5455 |
#[test] |
| 5456 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 5457 |
#[cfg_attr(wasi_runner, ignore = "WASI: --sparse is only supported on linux")] |
| 5458 |
fn test_cp_debug_reflink_never_with_hole() { |
| 5459 |
let ts = TestScenario::new(util_name!()); |
| 5460 |
let at = &ts.fixtures; |
| 5461 |
at.write("a", "hello"); |
| 5462 |
let f = std::fs::OpenOptions::new() |
| 5463 |
.write(true) |
| 5464 |
.open(at.plus("a")) |
| 5465 |
.unwrap(); |
| 5466 |
f.set_len(10000).unwrap(); |
| 5467 |
|
| 5468 |
ts.ucmd() |
| 5469 |
.arg("--debug") |
| 5470 |
.arg("--reflink=never") |
| 5471 |
.arg("a") |
| 5472 |
.arg("b") |
| 5473 |
.succeeds() |
| 5474 |
.stdout_contains("copy offload: avoided, reflink: no, sparse detection: SEEK_HOLE"); |
| 5475 |
|
| 5476 |
let src_file_metadata = std::fs::metadata(at.plus("a")).unwrap(); |
| 5477 |
let dst_file_metadata = std::fs::metadata(at.plus("b")).unwrap(); |
| 5478 |
assert_eq!(src_file_metadata.blocks(), dst_file_metadata.blocks()); |
| 5479 |
} |
| 5480 |
|
| 5481 |
#[test] |
| 5482 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 5483 |
#[cfg_attr(wasi_runner, ignore = "WASI: --sparse is only supported on linux")] |
| 5484 |
fn test_cp_debug_reflink_never_empty_file_with_hole() { |
| 5485 |
let ts = TestScenario::new(util_name!()); |
| 5486 |
let at = &ts.fixtures; |
| 5487 |
at.touch("a"); |
| 5488 |
let f = std::fs::OpenOptions::new() |
| 5489 |
.write(true) |
| 5490 |
.open(at.plus("a")) |
| 5491 |
.unwrap(); |
| 5492 |
f.set_len(10000).unwrap(); |
| 5493 |
|
| 5494 |
ts.ucmd() |
| 5495 |
.arg("--debug") |
| 5496 |
.arg("--reflink=never") |
| 5497 |
.arg("a") |
| 5498 |
.arg("b") |
| 5499 |
.succeeds() |
| 5500 |
.stdout_contains("copy offload: unknown, reflink: no, sparse detection: SEEK_HOLE"); |
| 5501 |
|
| 5502 |
let src_file_metadata = std::fs::metadata(at.plus("a")).unwrap(); |
| 5503 |
let dst_file_metadata = std::fs::metadata(at.plus("b")).unwrap(); |
| 5504 |
assert_eq!(src_file_metadata.blocks(), dst_file_metadata.blocks()); |
| 5505 |
} |
| 5506 |
|
| 5507 |
#[test] |
| 5508 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 5509 |
#[cfg_attr(wasi_runner, ignore = "WASI: --sparse is only supported on linux")] |
| 5510 |
fn test_cp_debug_default_with_hole() { |
| 5511 |
let ts = TestScenario::new(util_name!()); |
| 5512 |
let at = &ts.fixtures; |
| 5513 |
at.touch("a"); |
| 5514 |
let f = std::fs::OpenOptions::new() |
| 5515 |
.write(true) |
| 5516 |
.open(at.plus("a")) |
| 5517 |
.unwrap(); |
| 5518 |
f.set_len(10000).unwrap(); |
| 5519 |
|
| 5520 |
at.append_bytes("a", "hello".as_bytes()); |
| 5521 |
|
| 5522 |
ts.ucmd() |
| 5523 |
.arg("--debug") |
| 5524 |
.arg("a") |
| 5525 |
.arg("b") |
| 5526 |
.succeeds() |
| 5527 |
.stdout_contains("copy offload: yes, reflink: unsupported, sparse detection: SEEK_HOLE"); |
| 5528 |
|
| 5529 |
let src_file_metadata = std::fs::metadata(at.plus("a")).unwrap(); |
| 5530 |
let dst_file_metadata = std::fs::metadata(at.plus("b")).unwrap(); |
| 5531 |
assert_eq!(src_file_metadata.blocks(), dst_file_metadata.blocks()); |
| 5532 |
} |
| 5533 |
|
| 5534 |
#[test] |
| 5535 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 5536 |
#[cfg_attr(wasi_runner, ignore = "WASI: --sparse is only supported on linux")] |
| 5537 |
fn test_cp_debug_default_less_than_512_bytes() { |
| 5538 |
let ts = TestScenario::new(util_name!()); |
| 5539 |
|
| 5540 |
let at = &ts.fixtures; |
| 5541 |
at.write_bytes("a", "hello".as_bytes()); |
| 5542 |
let f = std::fs::OpenOptions::new() |
| 5543 |
.write(true) |
| 5544 |
.open(at.plus("a")) |
| 5545 |
.unwrap(); |
| 5546 |
f.set_len(400).unwrap(); |
| 5547 |
|
| 5548 |
ts.ucmd() |
| 5549 |
.arg("--debug") |
| 5550 |
.arg("--reflink=auto") |
| 5551 |
.arg("--sparse=auto") |
| 5552 |
.arg("a") |
| 5553 |
.arg("b") |
| 5554 |
.succeeds() |
| 5555 |
.stdout_contains("copy offload: yes, reflink: unsupported, sparse detection: no"); |
| 5556 |
} |
| 5557 |
|
| 5558 |
#[test] |
| 5559 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 5560 |
#[cfg_attr(wasi_runner, ignore = "WASI: --sparse is only supported on linux")] |
| 5561 |
fn test_cp_debug_default_without_hole() { |
| 5562 |
let ts = TestScenario::new(util_name!()); |
| 5563 |
|
| 5564 |
let at = &ts.fixtures; |
| 5565 |
at.write_bytes("a", "hello".as_bytes()); |
| 5566 |
|
| 5567 |
let filler_bytes = [0_u8; 10000]; |
| 5568 |
|
| 5569 |
at.append_bytes("a", &filler_bytes); |
| 5570 |
|
| 5571 |
ts.ucmd() |
| 5572 |
.arg("--debug") |
| 5573 |
.arg("a") |
| 5574 |
.arg("b") |
| 5575 |
.succeeds() |
| 5576 |
.stdout_contains("copy offload: yes, reflink: unsupported, sparse detection: no"); |
| 5577 |
} |
| 5578 |
|
| 5579 |
#[test] |
| 5580 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 5581 |
#[cfg_attr(wasi_runner, ignore = "WASI: --sparse is only supported on linux")] |
| 5582 |
fn test_cp_debug_default_empty_file_with_hole() { |
| 5583 |
let ts = TestScenario::new(util_name!()); |
| 5584 |
let at = &ts.fixtures; |
| 5585 |
at.touch("a"); |
| 5586 |
let f = std::fs::OpenOptions::new() |
| 5587 |
.write(true) |
| 5588 |
.open(at.plus("a")) |
| 5589 |
.unwrap(); |
| 5590 |
f.set_len(10000).unwrap(); |
| 5591 |
|
| 5592 |
ts.ucmd() |
| 5593 |
.arg("--debug") |
| 5594 |
.arg("a") |
| 5595 |
.arg("b") |
| 5596 |
.succeeds() |
| 5597 |
.stdout_contains( |
| 5598 |
"copy offload: unknown, reflink: unsupported, sparse detection: SEEK_HOLE", |
| 5599 |
); |
| 5600 |
|
| 5601 |
let src_file_metadata = std::fs::metadata(at.plus("a")).unwrap(); |
| 5602 |
let dst_file_metadata = std::fs::metadata(at.plus("b")).unwrap(); |
| 5603 |
assert_eq!(src_file_metadata.blocks(), dst_file_metadata.blocks()); |
| 5604 |
} |
| 5605 |
|
| 5606 |
#[test] |
| 5607 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 5608 |
#[cfg_attr(wasi_runner, ignore = "WASI: --sparse is only supported on linux")] |
| 5609 |
fn test_cp_debug_reflink_never_sparse_always_with_hole() { |
| 5610 |
let ts = TestScenario::new(util_name!()); |
| 5611 |
let at = &ts.fixtures; |
| 5612 |
at.write("a", "hello"); |
| 5613 |
let f = std::fs::OpenOptions::new() |
| 5614 |
.write(true) |
| 5615 |
.open(at.plus("a")) |
| 5616 |
.unwrap(); |
| 5617 |
f.set_len(10000).unwrap(); |
| 5618 |
|
| 5619 |
ts.ucmd() |
| 5620 |
.arg("--debug") |
| 5621 |
.arg("--reflink=never") |
| 5622 |
.arg("--sparse=always") |
| 5623 |
.arg("a") |
| 5624 |
.arg("b") |
| 5625 |
.succeeds() |
| 5626 |
.stdout_contains("copy offload: avoided, reflink: no, sparse detection: SEEK_HOLE + zeros"); |
| 5627 |
|
| 5628 |
let src_file_metadata = std::fs::metadata(at.plus("a")).unwrap(); |
| 5629 |
let dst_file_metadata = std::fs::metadata(at.plus("b")).unwrap(); |
| 5630 |
assert_eq!(src_file_metadata.blocks(), dst_file_metadata.blocks()); |
| 5631 |
} |
| 5632 |
|
| 5633 |
#[test] |
| 5634 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 5635 |
#[cfg_attr(wasi_runner, ignore = "WASI: --sparse is only supported on linux")] |
| 5636 |
fn test_cp_debug_reflink_never_sparse_always_without_hole() { |
| 5637 |
let ts = TestScenario::new(util_name!()); |
| 5638 |
let empty_bytes = [0_u8; 10000]; |
| 5639 |
let at = &ts.fixtures; |
| 5640 |
at.write("a", "hello"); |
| 5641 |
at.append_bytes("a", &empty_bytes); |
| 5642 |
|
| 5643 |
ts.ucmd() |
| 5644 |
.arg("--debug") |
| 5645 |
.arg("--reflink=never") |
| 5646 |
.arg("--sparse=always") |
| 5647 |
.arg("a") |
| 5648 |
.arg("b") |
| 5649 |
.succeeds() |
| 5650 |
.stdout_contains("copy offload: avoided, reflink: no, sparse detection: zeros"); |
| 5651 |
|
| 5652 |
let dst_file_metadata = std::fs::metadata(at.plus("b")).unwrap(); |
| 5653 |
assert_eq!( |
| 5654 |
dst_file_metadata.blocks(), |
| 5655 |
dst_file_metadata.blksize() / 512 |
| 5656 |
); |
| 5657 |
} |
| 5658 |
|
| 5659 |
#[test] |
| 5660 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 5661 |
#[cfg_attr(wasi_runner, ignore = "WASI: --sparse is only supported on linux")] |
| 5662 |
fn test_cp_debug_reflink_never_sparse_always_empty_file_with_hole() { |
| 5663 |
let ts = TestScenario::new(util_name!()); |
| 5664 |
let at = &ts.fixtures; |
| 5665 |
at.touch("a"); |
| 5666 |
let f = std::fs::OpenOptions::new() |
| 5667 |
.write(true) |
| 5668 |
.open(at.plus("a")) |
| 5669 |
.unwrap(); |
| 5670 |
f.set_len(10000).unwrap(); |
| 5671 |
|
| 5672 |
ts.ucmd() |
| 5673 |
.arg("--debug") |
| 5674 |
.arg("--reflink=never") |
| 5675 |
.arg("--sparse=always") |
| 5676 |
.arg("a") |
| 5677 |
.arg("b") |
| 5678 |
.succeeds() |
| 5679 |
.stdout_contains("copy offload: unknown, reflink: no, sparse detection: SEEK_HOLE"); |
| 5680 |
} |
| 5681 |
|
| 5682 |
#[test] |
| 5683 |
#[cfg(target_os = "linux")] |
| 5684 |
#[cfg_attr( |
| 5685 |
wasi_runner, |
| 5686 |
ignore = "WASI: /sys and /proc virtual files are not exposed to the sandbox" |
| 5687 |
)] |
| 5688 |
fn test_cp_default_virtual_file() { |
| 5689 |
// This file has existed at least since 2008, so we assume that it is present on "all" Linux kernels. |
| 5690 |
// https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-profiling |
| 5691 |
|
| 5692 |
use std::os::unix::prelude::MetadataExt; |
| 5693 |
let ts = TestScenario::new(util_name!()); |
| 5694 |
|
| 5695 |
// in case the kernel was not built with profiling support, e.g. WSL |
| 5696 |
if !ts.fixtures.file_exists("/sys/kernel/profiling") { |
| 5697 |
println!("test skipped: /sys/kernel/profiling does not exist"); |
| 5698 |
return; |
| 5699 |
} |
| 5700 |
|
| 5701 |
let at = &ts.fixtures; |
| 5702 |
ts.ucmd().arg("/sys/kernel/profiling").arg("b").succeeds(); |
| 5703 |
|
| 5704 |
let dest_size = std::fs::metadata(at.plus("b")) |
| 5705 |
.expect("Metadata of copied file cannot be read") |
| 5706 |
.size(); |
| 5707 |
assert!(dest_size > 0); |
| 5708 |
} |
| 5709 |
#[test] |
| 5710 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 5711 |
#[cfg_attr(wasi_runner, ignore = "WASI: --sparse is only supported on linux")] |
| 5712 |
fn test_cp_debug_reflink_auto_sparse_always_non_sparse_file_with_long_zero_sequence() { |
| 5713 |
let ts = TestScenario::new(util_name!()); |
| 5714 |
|
| 5715 |
let buf: Vec<u8> = vec![0; 4096 * 4]; |
| 5716 |
let at = &ts.fixtures; |
| 5717 |
at.touch("a"); |
| 5718 |
at.append_bytes("a", &buf); |
| 5719 |
at.append_bytes("a", "hello".as_bytes()); |
| 5720 |
|
| 5721 |
ts.ucmd() |
| 5722 |
.arg("--debug") |
| 5723 |
.arg("--sparse=always") |
| 5724 |
.arg("a") |
| 5725 |
.arg("b") |
| 5726 |
.succeeds() |
| 5727 |
.stdout_contains("copy offload: avoided, reflink: unsupported, sparse detection: zeros"); |
| 5728 |
|
| 5729 |
let dst_file_metadata = std::fs::metadata(at.plus("b")).unwrap(); |
| 5730 |
assert_eq!( |
| 5731 |
dst_file_metadata.blocks(), |
| 5732 |
dst_file_metadata.blksize() / 512 |
| 5733 |
); |
| 5734 |
} |
| 5735 |
|
| 5736 |
#[test] |
| 5737 |
#[cfg(target_os = "linux")] |
| 5738 |
#[cfg_attr(wasi_runner, ignore = "WASI: --sparse is only supported on linux")] |
| 5739 |
fn test_cp_debug_sparse_never_empty_sparse_file() { |
| 5740 |
let ts = TestScenario::new(util_name!()); |
| 5741 |
let at = &ts.fixtures; |
| 5742 |
at.touch("a"); |
| 5743 |
|
| 5744 |
ts.ucmd() |
| 5745 |
.arg("--debug") |
| 5746 |
.arg("--sparse=never") |
| 5747 |
.arg("a") |
| 5748 |
.arg("b") |
| 5749 |
.succeeds() |
| 5750 |
.stdout_contains("copy offload: avoided, reflink: no, sparse detection: no"); |
| 5751 |
} |
| 5752 |
|
| 5753 |
#[test] |
| 5754 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 5755 |
#[cfg_attr(wasi_runner, ignore = "WASI: --sparse is only supported on linux")] |
| 5756 |
fn test_cp_debug_reflink_never_sparse_always_non_sparse_file_with_long_zero_sequence() { |
| 5757 |
let ts = TestScenario::new(util_name!()); |
| 5758 |
|
| 5759 |
let buf: Vec<u8> = vec![0; 4096 * 4]; |
| 5760 |
let at = &ts.fixtures; |
| 5761 |
at.touch("a"); |
| 5762 |
at.append_bytes("a", &buf); |
| 5763 |
at.append_bytes("a", "hello".as_bytes()); |
| 5764 |
|
| 5765 |
ts.ucmd() |
| 5766 |
.arg("--debug") |
| 5767 |
.arg("--reflink=never") |
| 5768 |
.arg("--sparse=always") |
| 5769 |
.arg("a") |
| 5770 |
.arg("b") |
| 5771 |
.succeeds() |
| 5772 |
.stdout_contains("copy offload: avoided, reflink: no, sparse detection: zeros"); |
| 5773 |
|
| 5774 |
let dst_file_metadata = std::fs::metadata(at.plus("b")).unwrap(); |
| 5775 |
assert_eq!( |
| 5776 |
dst_file_metadata.blocks(), |
| 5777 |
dst_file_metadata.blksize() / 512 |
| 5778 |
); |
| 5779 |
} |
| 5780 |
|
| 5781 |
#[test] |
| 5782 |
#[cfg(target_os = "linux")] |
| 5783 |
#[cfg_attr( |
| 5784 |
wasi_runner, |
| 5785 |
ignore = "WASI: /sys and /proc virtual files are not exposed to the sandbox" |
| 5786 |
)] |
| 5787 |
fn test_cp_debug_sparse_always_sparse_virtual_file() { |
| 5788 |
// This file has existed at least since 2008, so we assume that it is present on "all" Linux kernels. |
| 5789 |
// https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-profiling |
| 5790 |
let ts = TestScenario::new(util_name!()); |
| 5791 |
|
| 5792 |
// in case the kernel was not built with profiling support, e.g. WSL |
| 5793 |
if !ts.fixtures.file_exists("/sys/kernel/profiling") { |
| 5794 |
println!("test skipped: /sys/kernel/profiling does not exist"); |
| 5795 |
return; |
| 5796 |
} |
| 5797 |
|
| 5798 |
ts.ucmd() |
| 5799 |
.arg("--debug") |
| 5800 |
.arg("--sparse=always") |
| 5801 |
.arg("/sys/kernel/profiling") |
| 5802 |
.arg("b") |
| 5803 |
.succeeds() |
| 5804 |
.stdout_contains( |
| 5805 |
"copy offload: avoided, reflink: unsupported, sparse detection: SEEK_HOLE + zeros", |
| 5806 |
); |
| 5807 |
} |
| 5808 |
|
| 5809 |
#[test] |
| 5810 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 5811 |
#[cfg_attr(wasi_runner, ignore = "WASI: --sparse is only supported on linux")] |
| 5812 |
fn test_cp_debug_reflink_never_less_than_512_bytes() { |
| 5813 |
let ts = TestScenario::new(util_name!()); |
| 5814 |
|
| 5815 |
let at = &ts.fixtures; |
| 5816 |
at.write_bytes("a", "hello".as_bytes()); |
| 5817 |
let f = std::fs::OpenOptions::new() |
| 5818 |
.write(true) |
| 5819 |
.open(at.plus("a")) |
| 5820 |
.unwrap(); |
| 5821 |
f.set_len(400).unwrap(); |
| 5822 |
|
| 5823 |
ts.ucmd() |
| 5824 |
.arg("--debug") |
| 5825 |
.arg("--reflink=never") |
| 5826 |
.arg("a") |
| 5827 |
.arg("b") |
| 5828 |
.succeeds() |
| 5829 |
.stdout_contains("copy offload: avoided, reflink: no, sparse detection: no"); |
| 5830 |
} |
| 5831 |
|
| 5832 |
#[test] |
| 5833 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 5834 |
#[cfg_attr(wasi_runner, ignore = "WASI: --sparse is only supported on linux")] |
| 5835 |
fn test_cp_debug_reflink_never_sparse_never_empty_file_with_hole() { |
| 5836 |
let ts = TestScenario::new(util_name!()); |
| 5837 |
let at = &ts.fixtures; |
| 5838 |
at.touch("a"); |
| 5839 |
let f = std::fs::OpenOptions::new() |
| 5840 |
.write(true) |
| 5841 |
.open(at.plus("a")) |
| 5842 |
.unwrap(); |
| 5843 |
f.set_len(10000).unwrap(); |
| 5844 |
|
| 5845 |
ts.ucmd() |
| 5846 |
.arg("--debug") |
| 5847 |
.arg("--reflink=never") |
| 5848 |
.arg("--sparse=never") |
| 5849 |
.arg("a") |
| 5850 |
.arg("b") |
| 5851 |
.succeeds() |
| 5852 |
.stdout_contains("copy offload: unknown, reflink: no, sparse detection: SEEK_HOLE"); |
| 5853 |
} |
| 5854 |
|
| 5855 |
#[test] |
| 5856 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 5857 |
#[cfg_attr(wasi_runner, ignore = "WASI: --sparse is only supported on linux")] |
| 5858 |
fn test_cp_debug_reflink_never_file_with_hole() { |
| 5859 |
let ts = TestScenario::new(util_name!()); |
| 5860 |
let at = &ts.fixtures; |
| 5861 |
at.touch("a"); |
| 5862 |
let f = std::fs::OpenOptions::new() |
| 5863 |
.write(true) |
| 5864 |
.open(at.plus("a")) |
| 5865 |
.unwrap(); |
| 5866 |
f.set_len(10000).unwrap(); |
| 5867 |
at.append_bytes("a", "hello".as_bytes()); |
| 5868 |
|
| 5869 |
ts.ucmd() |
| 5870 |
.arg("--debug") |
| 5871 |
.arg("--reflink=never") |
| 5872 |
.arg("--sparse=never") |
| 5873 |
.arg("a") |
| 5874 |
.arg("b") |
| 5875 |
.succeeds() |
| 5876 |
.stdout_contains("copy offload: avoided, reflink: no, sparse detection: SEEK_HOLE"); |
| 5877 |
} |
| 5878 |
|
| 5879 |
#[test] |
| 5880 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 5881 |
#[cfg_attr(wasi_runner, ignore = "WASI: --sparse is only supported on linux")] |
| 5882 |
fn test_cp_debug_sparse_never_less_than_512_bytes() { |
| 5883 |
let ts = TestScenario::new(util_name!()); |
| 5884 |
|
| 5885 |
let at = &ts.fixtures; |
| 5886 |
at.write_bytes("a", "hello".as_bytes()); |
| 5887 |
let f = std::fs::OpenOptions::new() |
| 5888 |
.write(true) |
| 5889 |
.open(at.plus("a")) |
| 5890 |
.unwrap(); |
| 5891 |
f.set_len(400).unwrap(); |
| 5892 |
|
| 5893 |
ts.ucmd() |
| 5894 |
.arg("--debug") |
| 5895 |
.arg("--reflink=auto") |
| 5896 |
.arg("--sparse=never") |
| 5897 |
.arg("a") |
| 5898 |
.arg("b") |
| 5899 |
.succeeds() |
| 5900 |
.stdout_contains("copy offload: avoided, reflink: no, sparse detection: no"); |
| 5901 |
} |
| 5902 |
|
| 5903 |
#[test] |
| 5904 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 5905 |
#[cfg_attr(wasi_runner, ignore = "WASI: --sparse is only supported on linux")] |
| 5906 |
fn test_cp_debug_sparse_never_without_hole() { |
| 5907 |
let ts = TestScenario::new(util_name!()); |
| 5908 |
|
| 5909 |
let at = &ts.fixtures; |
| 5910 |
at.write_bytes("a", "hello".as_bytes()); |
| 5911 |
|
| 5912 |
let filler_bytes = [0_u8; 10000]; |
| 5913 |
|
| 5914 |
at.append_bytes("a", &filler_bytes); |
| 5915 |
|
| 5916 |
ts.ucmd() |
| 5917 |
.arg("--reflink=auto") |
| 5918 |
.arg("--sparse=never") |
| 5919 |
.arg("--debug") |
| 5920 |
.arg("a") |
| 5921 |
.arg("b") |
| 5922 |
.succeeds() |
| 5923 |
.stdout_contains("copy offload: avoided, reflink: no, sparse detection: no"); |
| 5924 |
} |
| 5925 |
|
| 5926 |
#[test] |
| 5927 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 5928 |
#[cfg_attr(wasi_runner, ignore = "WASI: --sparse is only supported on linux")] |
| 5929 |
fn test_cp_debug_sparse_never_empty_file_with_hole() { |
| 5930 |
let ts = TestScenario::new(util_name!()); |
| 5931 |
let at = &ts.fixtures; |
| 5932 |
at.touch("a"); |
| 5933 |
let f = std::fs::OpenOptions::new() |
| 5934 |
.write(true) |
| 5935 |
.open(at.plus("a")) |
| 5936 |
.unwrap(); |
| 5937 |
f.set_len(10000).unwrap(); |
| 5938 |
|
| 5939 |
ts.ucmd() |
| 5940 |
.arg("--debug") |
| 5941 |
.arg("--reflink=auto") |
| 5942 |
.arg("--sparse=never") |
| 5943 |
.arg("a") |
| 5944 |
.arg("b") |
| 5945 |
.succeeds() |
| 5946 |
.stdout_contains("copy offload: unknown, reflink: no, sparse detection: SEEK_HOLE"); |
| 5947 |
} |
| 5948 |
|
| 5949 |
#[test] |
| 5950 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 5951 |
#[cfg_attr(wasi_runner, ignore = "WASI: --sparse is only supported on linux")] |
| 5952 |
fn test_cp_debug_sparse_never_file_with_hole() { |
| 5953 |
let ts = TestScenario::new(util_name!()); |
| 5954 |
let at = &ts.fixtures; |
| 5955 |
at.touch("a"); |
| 5956 |
let f = std::fs::OpenOptions::new() |
| 5957 |
.write(true) |
| 5958 |
.open(at.plus("a")) |
| 5959 |
.unwrap(); |
| 5960 |
f.set_len(10000).unwrap(); |
| 5961 |
at.append_bytes("a", "hello".as_bytes()); |
| 5962 |
|
| 5963 |
ts.ucmd() |
| 5964 |
.arg("--debug") |
| 5965 |
.arg("--reflink=auto") |
| 5966 |
.arg("--sparse=never") |
| 5967 |
.arg("a") |
| 5968 |
.arg("b") |
| 5969 |
.succeeds() |
| 5970 |
.stdout_contains("copy offload: avoided, reflink: no, sparse detection: SEEK_HOLE"); |
| 5971 |
} |
| 5972 |
|
| 5973 |
#[test] |
| 5974 |
#[cfg(target_os = "linux")] |
| 5975 |
#[cfg_attr( |
| 5976 |
wasi_runner, |
| 5977 |
ignore = "WASI: /sys and /proc virtual files are not exposed to the sandbox" |
| 5978 |
)] |
| 5979 |
fn test_cp_debug_default_sparse_virtual_file() { |
| 5980 |
// This file has existed at least since 2008, so we assume that it is present on "all" Linux kernels. |
| 5981 |
// https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-profiling |
| 5982 |
let ts = TestScenario::new(util_name!()); |
| 5983 |
|
| 5984 |
// in case the kernel was not built with profiling support, e.g. WSL |
| 5985 |
if !ts.fixtures.file_exists("/sys/kernel/profiling") { |
| 5986 |
println!("test skipped: /sys/kernel/profiling does not exist"); |
| 5987 |
return; |
| 5988 |
} |
| 5989 |
|
| 5990 |
ts.ucmd() |
| 5991 |
.arg("--debug") |
| 5992 |
.arg("/sys/kernel/profiling") |
| 5993 |
.arg("b") |
| 5994 |
.succeeds() |
| 5995 |
.stdout_contains( |
| 5996 |
"copy offload: unsupported, reflink: unsupported, sparse detection: SEEK_HOLE", |
| 5997 |
); |
| 5998 |
} |
| 5999 |
|
| 6000 |
#[test] |
| 6001 |
#[cfg(target_os = "linux")] |
| 6002 |
#[cfg_attr( |
| 6003 |
wasi_runner, |
| 6004 |
ignore = "WASI: /sys and /proc virtual files are not exposed to the sandbox" |
| 6005 |
)] |
| 6006 |
fn test_cp_debug_sparse_never_zero_sized_virtual_file() { |
| 6007 |
let ts = TestScenario::new(util_name!()); |
| 6008 |
ts.ucmd() |
| 6009 |
.arg("--debug") |
| 6010 |
.arg("--sparse=never") |
| 6011 |
.arg("/proc/version") |
| 6012 |
.arg("b") |
| 6013 |
.succeeds() |
| 6014 |
.stdout_contains("copy offload: avoided, reflink: no, sparse detection: no"); |
| 6015 |
} |
| 6016 |
|
| 6017 |
// A file that stats as zero-sized but still returns data (/proc entries) |
| 6018 |
// is probed for content by reading from it. The copy reuses that same |
| 6019 |
// descriptor, so it has to start over at offset 0 -- otherwise everything |
| 6020 |
// the probe consumed is dropped and the destination ends up empty. The |
| 6021 |
// --debug tests above only look at the reported strategy, not the bytes. |
| 6022 |
#[test] |
| 6023 |
#[cfg(target_os = "linux")] |
| 6024 |
#[cfg_attr( |
| 6025 |
wasi_runner, |
| 6026 |
ignore = "WASI: /sys and /proc virtual files are not exposed to the sandbox" |
| 6027 |
)] |
| 6028 |
fn test_cp_zero_sized_virtual_file_contents() { |
| 6029 |
let expected = std::fs::read_to_string("/proc/version").unwrap(); |
| 6030 |
assert!(!expected.is_empty()); |
| 6031 |
|
| 6032 |
for extra in [ |
| 6033 |
&[][..], |
| 6034 |
&["--sparse=never"], |
| 6035 |
&["--sparse=always"], |
| 6036 |
&["--sparse=auto"], |
| 6037 |
&["--reflink=never"], |
| 6038 |
&["--reflink=never", "--sparse=always"], |
| 6039 |
] { |
| 6040 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 6041 |
ucmd.args(extra) |
| 6042 |
.args(&["/proc/version", "copied"]) |
| 6043 |
.succeeds() |
| 6044 |
.no_output(); |
| 6045 |
assert_eq!(at.read("copied"), expected, "with {extra:?}"); |
| 6046 |
} |
| 6047 |
} |
| 6048 |
|
| 6049 |
#[test] |
| 6050 |
#[cfg(target_os = "linux")] |
| 6051 |
#[cfg_attr( |
| 6052 |
wasi_runner, |
| 6053 |
ignore = "WASI: /sys and /proc virtual files are not exposed to the sandbox" |
| 6054 |
)] |
| 6055 |
fn test_cp_debug_default_zero_sized_virtual_file() { |
| 6056 |
let ts = TestScenario::new(util_name!()); |
| 6057 |
ts.ucmd() |
| 6058 |
.arg("--debug") |
| 6059 |
.arg("/proc/version") |
| 6060 |
.arg("b") |
| 6061 |
.succeeds() |
| 6062 |
.stdout_contains("copy offload: unsupported, reflink: unsupported, sparse detection: no"); |
| 6063 |
} |
| 6064 |
|
| 6065 |
#[test] |
| 6066 |
#[cfg(any(target_os = "linux", target_os = "android"))] |
| 6067 |
#[cfg_attr(wasi_runner, ignore = "WASI: --sparse is only supported on linux")] |
| 6068 |
fn test_cp_debug_reflink_never_without_hole() { |
| 6069 |
let ts = TestScenario::new(util_name!()); |
| 6070 |
let filler_bytes = [0_u8; 1000]; |
| 6071 |
let at = &ts.fixtures; |
| 6072 |
at.write("a", "hello"); |
| 6073 |
at.append_bytes("a", &filler_bytes); |
| 6074 |
|
| 6075 |
ts.ucmd() |
| 6076 |
.arg("--debug") |
| 6077 |
.arg("--reflink=never") |
| 6078 |
.arg("a") |
| 6079 |
.arg("b") |
| 6080 |
.succeeds() |
| 6081 |
.stdout_contains("copy offload: avoided, reflink: no, sparse detection: no"); |
| 6082 |
} |
| 6083 |
|
| 6084 |
#[test] |
| 6085 |
#[cfg_attr( |
| 6086 |
wasi_runner, |
| 6087 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 6088 |
)] |
| 6089 |
fn test_cp_force_remove_destination_attributes_only_with_symlink() { |
| 6090 |
let scene = TestScenario::new(util_name!()); |
| 6091 |
let at = &scene.fixtures; |
| 6092 |
|
| 6093 |
at.write("file1", "1"); |
| 6094 |
at.write("file2", "2"); |
| 6095 |
at.symlink_file("file1", "sym1"); |
| 6096 |
|
| 6097 |
scene |
| 6098 |
.ucmd() |
| 6099 |
.args(&[ |
| 6100 |
"-a", |
| 6101 |
"--remove-destination", |
| 6102 |
"--attributes-only", |
| 6103 |
"sym1", |
| 6104 |
"file2", |
| 6105 |
]) |
| 6106 |
.succeeds(); |
| 6107 |
|
| 6108 |
assert!( |
| 6109 |
at.symlink_exists("file2"), |
| 6110 |
"file2 is not a symbolic link as expected" |
| 6111 |
); |
| 6112 |
|
| 6113 |
assert_eq!( |
| 6114 |
at.read("file1"), |
| 6115 |
at.read("file2"), |
| 6116 |
"Contents of file1 and file2 do not match" |
| 6117 |
); |
| 6118 |
} |
| 6119 |
|
| 6120 |
#[test] |
| 6121 |
fn test_cp_no_dereference_attributes_only_with_symlink() { |
| 6122 |
let scene = TestScenario::new(util_name!()); |
| 6123 |
let at = &scene.fixtures; |
| 6124 |
at.write("file1", "1"); |
| 6125 |
at.write("file2", "2"); |
| 6126 |
at.write("file2.exp", "2"); |
| 6127 |
at.symlink_file("file1", "sym1"); |
| 6128 |
|
| 6129 |
let result = scene |
| 6130 |
.ucmd() |
| 6131 |
.args(&["--no-dereference", "--attributes-only", "sym1", "file2"]) |
| 6132 |
.fails(); |
| 6133 |
|
| 6134 |
assert_eq!(result.code(), 1, "cp command did not fail"); |
| 6135 |
|
| 6136 |
assert_eq!( |
| 6137 |
at.read("file2"), |
| 6138 |
at.read("file2.exp"), |
| 6139 |
"file2 content does not match expected" |
| 6140 |
); |
| 6141 |
} |
| 6142 |
#[cfg(all(unix, not(target_os = "android")))] |
| 6143 |
#[cfg(test)] |
| 6144 |
/// contains the test for cp when the source and destination points to the same file |
| 6145 |
mod same_file { |
| 6146 |
|
| 6147 |
use std::os::unix::fs::MetadataExt; |
| 6148 |
use uutests::util::TestScenario; |
| 6149 |
use uutests::util_name; |
| 6150 |
|
| 6151 |
const FILE_NAME: &str = "foo"; |
| 6152 |
const SYMLINK_NAME: &str = "symlink"; |
| 6153 |
const CONTENTS: &str = "abcd"; |
| 6154 |
|
| 6155 |
// the following tests tries to copy a file to the symlink of the same file with |
| 6156 |
// various options |
| 6157 |
#[test] |
| 6158 |
#[cfg_attr( |
| 6159 |
wasi_runner, |
| 6160 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 6161 |
)] |
| 6162 |
fn test_same_file_from_file_to_symlink() { |
| 6163 |
for option in ["-d", "-f", "-df"] { |
| 6164 |
let scene = TestScenario::new(util_name!()); |
| 6165 |
let at = &scene.fixtures; |
| 6166 |
at.write(FILE_NAME, CONTENTS); |
| 6167 |
at.symlink_file(FILE_NAME, SYMLINK_NAME); |
| 6168 |
scene |
| 6169 |
.ucmd() |
| 6170 |
.args(&[option, FILE_NAME, SYMLINK_NAME]) |
| 6171 |
.fails() |
| 6172 |
.stderr_contains("'foo' and 'symlink' are the same file"); |
| 6173 |
assert!(at.symlink_exists(SYMLINK_NAME)); |
| 6174 |
assert_eq!(FILE_NAME, at.resolve_link(SYMLINK_NAME)); |
| 6175 |
assert!(at.file_exists(FILE_NAME)); |
| 6176 |
assert_eq!(at.read(FILE_NAME), CONTENTS); |
| 6177 |
} |
| 6178 |
} |
| 6179 |
|
| 6180 |
#[test] |
| 6181 |
fn test_same_file_from_file_to_symlink_with_rem_option() { |
| 6182 |
let scene = TestScenario::new(util_name!()); |
| 6183 |
let at = &scene.fixtures; |
| 6184 |
at.write(FILE_NAME, CONTENTS); |
| 6185 |
at.symlink_file(FILE_NAME, SYMLINK_NAME); |
| 6186 |
scene |
| 6187 |
.ucmd() |
| 6188 |
.args(&["--rem", FILE_NAME, SYMLINK_NAME]) |
| 6189 |
.succeeds(); |
| 6190 |
assert!(at.file_exists(SYMLINK_NAME)); |
| 6191 |
assert_eq!(at.read(FILE_NAME), CONTENTS); |
| 6192 |
assert!(at.file_exists(FILE_NAME)); |
| 6193 |
assert_eq!(at.read(FILE_NAME), CONTENTS); |
| 6194 |
} |
| 6195 |
|
| 6196 |
#[test] |
| 6197 |
#[cfg_attr( |
| 6198 |
wasi_runner, |
| 6199 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 6200 |
)] |
| 6201 |
fn test_same_file_from_file_to_symlink_with_backup_option() { |
| 6202 |
for option in ["-b", "-bd", "-bf", "-bdf"] { |
| 6203 |
let scene = TestScenario::new(util_name!()); |
| 6204 |
let at = &scene.fixtures; |
| 6205 |
let backup = "symlink~"; |
| 6206 |
at.write(FILE_NAME, CONTENTS); |
| 6207 |
at.symlink_file(FILE_NAME, SYMLINK_NAME); |
| 6208 |
scene |
| 6209 |
.ucmd() |
| 6210 |
.args(&[option, FILE_NAME, SYMLINK_NAME]) |
| 6211 |
.succeeds(); |
| 6212 |
assert!(at.symlink_exists(backup)); |
| 6213 |
assert_eq!(FILE_NAME, at.resolve_link(backup)); |
| 6214 |
assert!(at.file_exists(SYMLINK_NAME)); |
| 6215 |
assert_eq!(at.read(SYMLINK_NAME), CONTENTS); |
| 6216 |
assert!(at.file_exists(FILE_NAME)); |
| 6217 |
assert_eq!(at.read(FILE_NAME), CONTENTS); |
| 6218 |
} |
| 6219 |
} |
| 6220 |
|
| 6221 |
#[test] |
| 6222 |
#[cfg_attr( |
| 6223 |
wasi_runner, |
| 6224 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 6225 |
)] |
| 6226 |
fn test_same_file_from_file_to_symlink_with_link_option() { |
| 6227 |
for option in ["-l", "-dl"] { |
| 6228 |
let scene = TestScenario::new(util_name!()); |
| 6229 |
let at = &scene.fixtures; |
| 6230 |
at.write(FILE_NAME, CONTENTS); |
| 6231 |
at.symlink_file(FILE_NAME, SYMLINK_NAME); |
| 6232 |
scene |
| 6233 |
.ucmd() |
| 6234 |
.args(&[option, FILE_NAME, SYMLINK_NAME]) |
| 6235 |
.fails() |
| 6236 |
.stderr_contains("cp: cannot create hard link 'symlink' to 'foo'"); |
| 6237 |
assert!(at.symlink_exists(SYMLINK_NAME)); |
| 6238 |
assert_eq!(FILE_NAME, at.resolve_link(SYMLINK_NAME)); |
| 6239 |
assert!(at.file_exists(FILE_NAME)); |
| 6240 |
assert_eq!(at.read(FILE_NAME), CONTENTS); |
| 6241 |
} |
| 6242 |
} |
| 6243 |
|
| 6244 |
#[test] |
| 6245 |
#[cfg_attr( |
| 6246 |
wasi_runner, |
| 6247 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 6248 |
)] |
| 6249 |
fn test_same_file_from_file_to_symlink_with_options_link_and_force() { |
| 6250 |
for option in ["-fl", "-dfl"] { |
| 6251 |
let scene = TestScenario::new(util_name!()); |
| 6252 |
let at = &scene.fixtures; |
| 6253 |
at.write(FILE_NAME, CONTENTS); |
| 6254 |
at.symlink_file(FILE_NAME, SYMLINK_NAME); |
| 6255 |
scene |
| 6256 |
.ucmd() |
| 6257 |
.args(&[option, FILE_NAME, SYMLINK_NAME]) |
| 6258 |
.succeeds(); |
| 6259 |
assert!(at.file_exists(FILE_NAME)); |
| 6260 |
assert!(at.file_exists(SYMLINK_NAME)); |
| 6261 |
assert_eq!(at.read(FILE_NAME), CONTENTS); |
| 6262 |
} |
| 6263 |
} |
| 6264 |
|
| 6265 |
#[test] |
| 6266 |
#[cfg_attr( |
| 6267 |
wasi_runner, |
| 6268 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 6269 |
)] |
| 6270 |
fn test_same_file_from_file_to_symlink_with_options_backup_and_link() { |
| 6271 |
for option in ["-bl", "-bdl", "-bfl", "-bdfl"] { |
| 6272 |
let scene = TestScenario::new(util_name!()); |
| 6273 |
let at = &scene.fixtures; |
| 6274 |
let backup = "symlink~"; |
| 6275 |
at.write(FILE_NAME, CONTENTS); |
| 6276 |
at.symlink_file(FILE_NAME, SYMLINK_NAME); |
| 6277 |
scene |
| 6278 |
.ucmd() |
| 6279 |
.args(&[option, FILE_NAME, SYMLINK_NAME]) |
| 6280 |
.succeeds(); |
| 6281 |
assert!(at.file_exists(FILE_NAME)); |
| 6282 |
assert!(at.file_exists(SYMLINK_NAME)); |
| 6283 |
assert!(at.symlink_exists(backup)); |
| 6284 |
assert_eq!(FILE_NAME, at.resolve_link(backup)); |
| 6285 |
assert_eq!(at.read(FILE_NAME), CONTENTS); |
| 6286 |
} |
| 6287 |
} |
| 6288 |
|
| 6289 |
#[test] |
| 6290 |
#[cfg_attr( |
| 6291 |
wasi_runner, |
| 6292 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 6293 |
)] |
| 6294 |
fn test_same_file_from_file_to_symlink_with_options_symlink() { |
| 6295 |
let scene = TestScenario::new(util_name!()); |
| 6296 |
let at = &scene.fixtures; |
| 6297 |
at.write(FILE_NAME, CONTENTS); |
| 6298 |
at.symlink_file(FILE_NAME, SYMLINK_NAME); |
| 6299 |
scene |
| 6300 |
.ucmd() |
| 6301 |
.args(&["-s", FILE_NAME, SYMLINK_NAME]) |
| 6302 |
.fails() |
| 6303 |
.stderr_contains("cp: cannot create symlink 'symlink' to 'foo'"); |
| 6304 |
assert!(at.file_exists(FILE_NAME)); |
| 6305 |
assert!(at.symlink_exists(SYMLINK_NAME)); |
| 6306 |
assert_eq!(FILE_NAME, at.resolve_link(SYMLINK_NAME)); |
| 6307 |
assert_eq!(at.read(FILE_NAME), CONTENTS); |
| 6308 |
} |
| 6309 |
|
| 6310 |
#[test] |
| 6311 |
#[cfg_attr( |
| 6312 |
wasi_runner, |
| 6313 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 6314 |
)] |
| 6315 |
fn test_same_file_from_file_to_symlink_with_options_symlink_and_force() { |
| 6316 |
let scene = TestScenario::new(util_name!()); |
| 6317 |
let at = &scene.fixtures; |
| 6318 |
at.write(FILE_NAME, CONTENTS); |
| 6319 |
at.symlink_file(FILE_NAME, SYMLINK_NAME); |
| 6320 |
scene |
| 6321 |
.ucmd() |
| 6322 |
.args(&["-sf", FILE_NAME, SYMLINK_NAME]) |
| 6323 |
.succeeds(); |
| 6324 |
assert!(at.file_exists(FILE_NAME)); |
| 6325 |
assert!(at.symlink_exists(SYMLINK_NAME)); |
| 6326 |
assert_eq!(FILE_NAME, at.resolve_link(SYMLINK_NAME)); |
| 6327 |
assert_eq!(at.read(FILE_NAME), CONTENTS); |
| 6328 |
} |
| 6329 |
// the following tests tries to copy a symlink to the file that symlink points to with |
| 6330 |
// various options |
| 6331 |
#[test] |
| 6332 |
#[cfg_attr( |
| 6333 |
wasi_runner, |
| 6334 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 6335 |
)] |
| 6336 |
fn test_same_file_from_symlink_to_file() { |
| 6337 |
for option in ["-d", "-f", "-df", "--rem"] { |
| 6338 |
let scene = TestScenario::new(util_name!()); |
| 6339 |
let at = &scene.fixtures; |
| 6340 |
at.write(FILE_NAME, CONTENTS); |
| 6341 |
at.symlink_file(FILE_NAME, SYMLINK_NAME); |
| 6342 |
scene |
| 6343 |
.ucmd() |
| 6344 |
.args(&[option, SYMLINK_NAME, FILE_NAME]) |
| 6345 |
.fails() |
| 6346 |
.stderr_contains("'symlink' and 'foo' are the same file"); |
| 6347 |
assert!(at.file_exists(FILE_NAME)); |
| 6348 |
assert!(at.symlink_exists(SYMLINK_NAME)); |
| 6349 |
assert_eq!(FILE_NAME, at.resolve_link(SYMLINK_NAME)); |
| 6350 |
assert_eq!(at.read(FILE_NAME), CONTENTS); |
| 6351 |
} |
| 6352 |
} |
| 6353 |
|
| 6354 |
#[test] |
| 6355 |
#[cfg_attr( |
| 6356 |
wasi_runner, |
| 6357 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 6358 |
)] |
| 6359 |
fn test_same_file_from_symlink_to_file_with_option_backup() { |
| 6360 |
for option in ["-b", "-bf"] { |
| 6361 |
let scene = TestScenario::new(util_name!()); |
| 6362 |
let at = &scene.fixtures; |
| 6363 |
at.write(FILE_NAME, CONTENTS); |
| 6364 |
at.symlink_file(FILE_NAME, SYMLINK_NAME); |
| 6365 |
scene |
| 6366 |
.ucmd() |
| 6367 |
.args(&[option, SYMLINK_NAME, FILE_NAME]) |
| 6368 |
.fails() |
| 6369 |
.stderr_contains("'symlink' and 'foo' are the same file"); |
| 6370 |
assert!(at.file_exists(FILE_NAME)); |
| 6371 |
assert!(at.symlink_exists(SYMLINK_NAME)); |
| 6372 |
assert_eq!(FILE_NAME, at.resolve_link(SYMLINK_NAME)); |
| 6373 |
assert_eq!(at.read(FILE_NAME), CONTENTS); |
| 6374 |
} |
| 6375 |
} |
| 6376 |
#[test] |
| 6377 |
#[cfg_attr( |
| 6378 |
wasi_runner, |
| 6379 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 6380 |
)] |
| 6381 |
fn test_same_file_from_symlink_to_file_with_option_backup_without_deref() { |
| 6382 |
for option in ["-bd", "-bdf"] { |
| 6383 |
let scene = TestScenario::new(util_name!()); |
| 6384 |
let at = &scene.fixtures; |
| 6385 |
let backup = "foo~"; |
| 6386 |
at.write(FILE_NAME, CONTENTS); |
| 6387 |
at.symlink_file(FILE_NAME, SYMLINK_NAME); |
| 6388 |
scene |
| 6389 |
.ucmd() |
| 6390 |
.args(&[option, SYMLINK_NAME, FILE_NAME]) |
| 6391 |
.succeeds(); |
| 6392 |
assert!(at.file_exists(backup)); |
| 6393 |
assert!(at.symlink_exists(FILE_NAME)); |
| 6394 |
// this doesn't makes sense but this is how gnu does it |
| 6395 |
assert_eq!(FILE_NAME, at.resolve_link(FILE_NAME)); |
| 6396 |
assert_eq!(FILE_NAME, at.resolve_link(SYMLINK_NAME)); |
| 6397 |
assert_eq!(at.read(backup), CONTENTS); |
| 6398 |
} |
| 6399 |
} |
| 6400 |
|
| 6401 |
#[test] |
| 6402 |
#[cfg_attr( |
| 6403 |
wasi_runner, |
| 6404 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 6405 |
)] |
| 6406 |
fn test_same_file_from_symlink_to_file_with_options_link() { |
| 6407 |
for option in ["-l", "-dl", "-fl", "-bl", "-bfl"] { |
| 6408 |
let scene = TestScenario::new(util_name!()); |
| 6409 |
let at = &scene.fixtures; |
| 6410 |
at.write(FILE_NAME, CONTENTS); |
| 6411 |
at.symlink_file(FILE_NAME, SYMLINK_NAME); |
| 6412 |
scene |
| 6413 |
.ucmd() |
| 6414 |
.args(&[option, SYMLINK_NAME, FILE_NAME]) |
| 6415 |
.succeeds(); |
| 6416 |
assert!(at.file_exists(FILE_NAME)); |
| 6417 |
assert!(at.symlink_exists(SYMLINK_NAME)); |
| 6418 |
assert_eq!(FILE_NAME, at.resolve_link(SYMLINK_NAME)); |
| 6419 |
assert_eq!(at.read(FILE_NAME), CONTENTS); |
| 6420 |
} |
| 6421 |
} |
| 6422 |
|
| 6423 |
#[test] |
| 6424 |
#[cfg_attr( |
| 6425 |
wasi_runner, |
| 6426 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 6427 |
)] |
| 6428 |
fn test_same_file_from_symlink_to_file_with_option_symlink() { |
| 6429 |
for option in ["-s", "-sf"] { |
| 6430 |
let scene = TestScenario::new(util_name!()); |
| 6431 |
let at = &scene.fixtures; |
| 6432 |
at.write(FILE_NAME, CONTENTS); |
| 6433 |
at.symlink_file(FILE_NAME, SYMLINK_NAME); |
| 6434 |
scene |
| 6435 |
.ucmd() |
| 6436 |
.args(&[option, SYMLINK_NAME, FILE_NAME]) |
| 6437 |
.fails() |
| 6438 |
.stderr_contains("'symlink' and 'foo' are the same file"); |
| 6439 |
assert!(at.file_exists(FILE_NAME)); |
| 6440 |
assert!(at.symlink_exists(SYMLINK_NAME)); |
| 6441 |
assert_eq!(FILE_NAME, at.resolve_link(SYMLINK_NAME)); |
| 6442 |
assert_eq!(at.read(FILE_NAME), CONTENTS); |
| 6443 |
} |
| 6444 |
} |
| 6445 |
|
| 6446 |
// the following tests tries to copy a file to the same file with various options |
| 6447 |
#[test] |
| 6448 |
fn test_same_file_from_file_to_file() { |
| 6449 |
for option in ["-d", "-f", "-df", "--rem"] { |
| 6450 |
let scene = TestScenario::new(util_name!()); |
| 6451 |
let at = &scene.fixtures; |
| 6452 |
at.write(FILE_NAME, CONTENTS); |
| 6453 |
scene |
| 6454 |
.ucmd() |
| 6455 |
.args(&[option, FILE_NAME, FILE_NAME]) |
| 6456 |
.fails() |
| 6457 |
.stderr_contains("'foo' and 'foo' are the same file"); |
| 6458 |
assert!(at.file_exists(FILE_NAME)); |
| 6459 |
assert_eq!(at.read(FILE_NAME), CONTENTS); |
| 6460 |
} |
| 6461 |
} |
| 6462 |
#[test] |
| 6463 |
fn test_same_file_from_file_to_file_with_backup() { |
| 6464 |
for option in ["-b", "-bd"] { |
| 6465 |
let scene = TestScenario::new(util_name!()); |
| 6466 |
let at = &scene.fixtures; |
| 6467 |
at.write(FILE_NAME, CONTENTS); |
| 6468 |
scene |
| 6469 |
.ucmd() |
| 6470 |
.args(&[option, FILE_NAME, FILE_NAME]) |
| 6471 |
.fails() |
| 6472 |
.stderr_contains("'foo' and 'foo' are the same file"); |
| 6473 |
assert!(at.file_exists(FILE_NAME)); |
| 6474 |
assert_eq!(at.read(FILE_NAME), CONTENTS); |
| 6475 |
} |
| 6476 |
} |
| 6477 |
|
| 6478 |
#[test] |
| 6479 |
#[cfg_attr( |
| 6480 |
wasi_runner, |
| 6481 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 6482 |
)] |
| 6483 |
fn test_same_file_from_file_to_file_with_options_backup_and_no_deref() { |
| 6484 |
for option in ["-bf", "-bdf"] { |
| 6485 |
let scene = TestScenario::new(util_name!()); |
| 6486 |
let at = &scene.fixtures; |
| 6487 |
let backup = "foo~"; |
| 6488 |
at.write(FILE_NAME, CONTENTS); |
| 6489 |
scene |
| 6490 |
.ucmd() |
| 6491 |
.args(&[option, FILE_NAME, FILE_NAME]) |
| 6492 |
.succeeds(); |
| 6493 |
assert!(at.file_exists(FILE_NAME)); |
| 6494 |
assert!(at.file_exists(backup)); |
| 6495 |
assert_eq!(at.read(FILE_NAME), CONTENTS); |
| 6496 |
assert_eq!(at.read(backup), CONTENTS); |
| 6497 |
} |
| 6498 |
} |
| 6499 |
|
| 6500 |
#[test] |
| 6501 |
#[cfg_attr( |
| 6502 |
wasi_runner, |
| 6503 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 6504 |
)] |
| 6505 |
fn test_same_file_from_file_to_file_with_options_link() { |
| 6506 |
for option in ["-l", "-dl", "-fl", "-dfl", "-bl", "-bdl"] { |
| 6507 |
let scene = TestScenario::new(util_name!()); |
| 6508 |
let at = &scene.fixtures; |
| 6509 |
let backup = "foo~"; |
| 6510 |
at.write(FILE_NAME, CONTENTS); |
| 6511 |
scene |
| 6512 |
.ucmd() |
| 6513 |
.args(&[option, FILE_NAME, FILE_NAME]) |
| 6514 |
.succeeds(); |
| 6515 |
assert!(at.file_exists(FILE_NAME)); |
| 6516 |
assert!(!at.file_exists(backup)); |
| 6517 |
assert_eq!(at.read(FILE_NAME), CONTENTS); |
| 6518 |
} |
| 6519 |
} |
| 6520 |
|
| 6521 |
#[test] |
| 6522 |
#[cfg_attr( |
| 6523 |
wasi_runner, |
| 6524 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 6525 |
)] |
| 6526 |
fn test_same_file_from_file_to_file_with_options_link_and_backup_and_force() { |
| 6527 |
for option in ["-bfl", "-bdfl"] { |
| 6528 |
let scene = TestScenario::new(util_name!()); |
| 6529 |
let at = &scene.fixtures; |
| 6530 |
let backup = "foo~"; |
| 6531 |
at.write(FILE_NAME, CONTENTS); |
| 6532 |
scene |
| 6533 |
.ucmd() |
| 6534 |
.args(&[option, FILE_NAME, FILE_NAME]) |
| 6535 |
.succeeds(); |
| 6536 |
assert!(at.file_exists(FILE_NAME)); |
| 6537 |
assert!(at.file_exists(backup)); |
| 6538 |
assert_eq!(at.read(FILE_NAME), CONTENTS); |
| 6539 |
assert_eq!(at.read(backup), CONTENTS); |
| 6540 |
} |
| 6541 |
} |
| 6542 |
|
| 6543 |
#[test] |
| 6544 |
fn test_same_file_from_file_to_file_with_options_symlink() { |
| 6545 |
for option in ["-s", "-sf"] { |
| 6546 |
let scene = TestScenario::new(util_name!()); |
| 6547 |
let at = &scene.fixtures; |
| 6548 |
at.write(FILE_NAME, CONTENTS); |
| 6549 |
scene |
| 6550 |
.ucmd() |
| 6551 |
.args(&[option, FILE_NAME, FILE_NAME]) |
| 6552 |
.fails() |
| 6553 |
.stderr_contains("'foo' and 'foo' are the same file"); |
| 6554 |
assert!(at.file_exists(FILE_NAME)); |
| 6555 |
assert_eq!(at.read(FILE_NAME), CONTENTS); |
| 6556 |
} |
| 6557 |
} |
| 6558 |
|
| 6559 |
// the following tests tries to copy a symlink that points to a file to a symlink |
| 6560 |
// that points to the same file with various options |
| 6561 |
#[test] |
| 6562 |
#[cfg_attr( |
| 6563 |
wasi_runner, |
| 6564 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 6565 |
)] |
| 6566 |
fn test_same_file_from_symlink_to_symlink_with_option_no_deref() { |
| 6567 |
for option in ["-d", "-df"] { |
| 6568 |
let scene = TestScenario::new(util_name!()); |
| 6569 |
let at = &scene.fixtures; |
| 6570 |
let symlink1 = "sl1"; |
| 6571 |
let symlink2 = "sl2"; |
| 6572 |
at.write(FILE_NAME, CONTENTS); |
| 6573 |
at.symlink_file(FILE_NAME, symlink1); |
| 6574 |
at.symlink_file(FILE_NAME, symlink2); |
| 6575 |
scene.ucmd().args(&[option, symlink1, symlink2]).succeeds(); |
| 6576 |
assert!(at.file_exists(FILE_NAME)); |
| 6577 |
assert_eq!(at.read(FILE_NAME), CONTENTS); |
| 6578 |
assert_eq!(FILE_NAME, at.resolve_link(symlink1)); |
| 6579 |
assert_eq!(FILE_NAME, at.resolve_link(symlink2)); |
| 6580 |
} |
| 6581 |
} |
| 6582 |
|
| 6583 |
#[test] |
| 6584 |
#[cfg_attr( |
| 6585 |
wasi_runner, |
| 6586 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 6587 |
)] |
| 6588 |
fn test_same_file_from_symlink_to_symlink_with_option_force() { |
| 6589 |
let scene = TestScenario::new(util_name!()); |
| 6590 |
let at = &scene.fixtures; |
| 6591 |
let symlink1 = "sl1"; |
| 6592 |
let symlink2 = "sl2"; |
| 6593 |
at.write(FILE_NAME, CONTENTS); |
| 6594 |
at.symlink_file(FILE_NAME, symlink1); |
| 6595 |
at.symlink_file(FILE_NAME, symlink2); |
| 6596 |
scene |
| 6597 |
.ucmd() |
| 6598 |
.args(&["-f", symlink1, symlink2]) |
| 6599 |
.fails() |
| 6600 |
.stderr_contains("'sl1' and 'sl2' are the same file"); |
| 6601 |
assert!(at.file_exists(FILE_NAME)); |
| 6602 |
assert_eq!(at.read(FILE_NAME), CONTENTS); |
| 6603 |
assert_eq!(FILE_NAME, at.resolve_link(symlink1)); |
| 6604 |
assert_eq!(FILE_NAME, at.resolve_link(symlink2)); |
| 6605 |
} |
| 6606 |
|
| 6607 |
#[test] |
| 6608 |
#[cfg_attr( |
| 6609 |
wasi_runner, |
| 6610 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 6611 |
)] |
| 6612 |
fn test_same_file_from_symlink_to_symlink_with_option_rem() { |
| 6613 |
let scene = TestScenario::new(util_name!()); |
| 6614 |
let at = &scene.fixtures; |
| 6615 |
let symlink1 = "sl1"; |
| 6616 |
let symlink2 = "sl2"; |
| 6617 |
at.write(FILE_NAME, CONTENTS); |
| 6618 |
at.symlink_file(FILE_NAME, symlink1); |
| 6619 |
at.symlink_file(FILE_NAME, symlink2); |
| 6620 |
scene.ucmd().args(&["--rem", symlink1, symlink2]).succeeds(); |
| 6621 |
assert!(at.file_exists(FILE_NAME)); |
| 6622 |
assert_eq!(at.read(FILE_NAME), CONTENTS); |
| 6623 |
assert_eq!(FILE_NAME, at.resolve_link(symlink1)); |
| 6624 |
assert!(at.file_exists(symlink2)); |
| 6625 |
assert_eq!(at.read(symlink2), CONTENTS); |
| 6626 |
} |
| 6627 |
|
| 6628 |
#[test] |
| 6629 |
#[cfg_attr( |
| 6630 |
wasi_runner, |
| 6631 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 6632 |
)] |
| 6633 |
fn test_same_file_from_symlink_to_symlink_with_option_backup() { |
| 6634 |
for option in ["-b", "-bf"] { |
| 6635 |
let scene = TestScenario::new(util_name!()); |
| 6636 |
let at = &scene.fixtures; |
| 6637 |
let symlink1 = "sl1"; |
| 6638 |
let symlink2 = "sl2"; |
| 6639 |
let backup = "sl2~"; |
| 6640 |
at.write(FILE_NAME, CONTENTS); |
| 6641 |
at.symlink_file(FILE_NAME, symlink1); |
| 6642 |
at.symlink_file(FILE_NAME, symlink2); |
| 6643 |
scene.ucmd().args(&[option, symlink1, symlink2]).succeeds(); |
| 6644 |
assert!(at.file_exists(FILE_NAME)); |
| 6645 |
assert_eq!(at.read(FILE_NAME), CONTENTS); |
| 6646 |
assert_eq!(FILE_NAME, at.resolve_link(symlink1)); |
| 6647 |
assert!(at.file_exists(symlink2)); |
| 6648 |
assert_eq!(at.read(symlink2), CONTENTS); |
| 6649 |
assert_eq!(FILE_NAME, at.resolve_link(backup)); |
| 6650 |
} |
| 6651 |
} |
| 6652 |
|
| 6653 |
#[test] |
| 6654 |
#[cfg_attr( |
| 6655 |
wasi_runner, |
| 6656 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 6657 |
)] |
| 6658 |
fn test_same_file_from_symlink_to_symlink_with_option_backup_and_no_deref() { |
| 6659 |
for option in ["-bd", "-bdf"] { |
| 6660 |
let scene = TestScenario::new(util_name!()); |
| 6661 |
let at = &scene.fixtures; |
| 6662 |
let symlink1 = "sl1"; |
| 6663 |
let symlink2 = "sl2"; |
| 6664 |
let backup = "sl2~"; |
| 6665 |
at.write(FILE_NAME, CONTENTS); |
| 6666 |
at.symlink_file(FILE_NAME, symlink1); |
| 6667 |
at.symlink_file(FILE_NAME, symlink2); |
| 6668 |
scene.ucmd().args(&[option, symlink1, symlink2]).succeeds(); |
| 6669 |
assert!(at.file_exists(FILE_NAME)); |
| 6670 |
assert_eq!(at.read(FILE_NAME), CONTENTS); |
| 6671 |
assert_eq!(FILE_NAME, at.resolve_link(symlink1)); |
| 6672 |
assert_eq!(FILE_NAME, at.resolve_link(symlink2)); |
| 6673 |
assert_eq!(FILE_NAME, at.resolve_link(backup)); |
| 6674 |
} |
| 6675 |
} |
| 6676 |
#[test] |
| 6677 |
#[cfg_attr( |
| 6678 |
wasi_runner, |
| 6679 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 6680 |
)] |
| 6681 |
fn test_same_file_from_symlink_to_symlink_with_option_link() { |
| 6682 |
let scene = TestScenario::new(util_name!()); |
| 6683 |
let at = &scene.fixtures; |
| 6684 |
let symlink1 = "sl1"; |
| 6685 |
let symlink2 = "sl2"; |
| 6686 |
at.write(FILE_NAME, CONTENTS); |
| 6687 |
at.symlink_file(FILE_NAME, symlink1); |
| 6688 |
at.symlink_file(FILE_NAME, symlink2); |
| 6689 |
scene |
| 6690 |
.ucmd() |
| 6691 |
.args(&["-l", symlink1, symlink2]) |
| 6692 |
.fails() |
| 6693 |
.stderr_contains("cannot create hard link 'sl2' to 'sl1'"); |
| 6694 |
assert!(at.file_exists(FILE_NAME)); |
| 6695 |
assert_eq!(at.read(FILE_NAME), CONTENTS); |
| 6696 |
assert_eq!(FILE_NAME, at.resolve_link(symlink1)); |
| 6697 |
assert_eq!(FILE_NAME, at.resolve_link(symlink2)); |
| 6698 |
} |
| 6699 |
|
| 6700 |
#[test] |
| 6701 |
#[cfg_attr( |
| 6702 |
wasi_runner, |
| 6703 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 6704 |
)] |
| 6705 |
fn test_same_file_from_symlink_to_symlink_with_option_force_link() { |
| 6706 |
let scene = TestScenario::new(util_name!()); |
| 6707 |
let at = &scene.fixtures; |
| 6708 |
let symlink1 = "sl1"; |
| 6709 |
let symlink2 = "sl2"; |
| 6710 |
at.write(FILE_NAME, CONTENTS); |
| 6711 |
at.symlink_file(FILE_NAME, symlink1); |
| 6712 |
at.symlink_file(FILE_NAME, symlink2); |
| 6713 |
scene.ucmd().args(&["-fl", symlink1, symlink2]).succeeds(); |
| 6714 |
assert!(at.file_exists(FILE_NAME)); |
| 6715 |
assert_eq!(at.read(FILE_NAME), CONTENTS); |
| 6716 |
assert_eq!(FILE_NAME, at.resolve_link(symlink1)); |
| 6717 |
assert!(at.file_exists(symlink2)); |
| 6718 |
assert_eq!(at.read(symlink2), CONTENTS); |
| 6719 |
} |
| 6720 |
|
| 6721 |
#[test] |
| 6722 |
#[cfg_attr( |
| 6723 |
wasi_runner, |
| 6724 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 6725 |
)] |
| 6726 |
fn test_same_file_from_symlink_to_symlink_with_option_backup_and_link() { |
| 6727 |
for option in ["-bl", "-bfl"] { |
| 6728 |
let scene = TestScenario::new(util_name!()); |
| 6729 |
let at = &scene.fixtures; |
| 6730 |
let symlink1 = "sl1"; |
| 6731 |
let symlink2 = "sl2"; |
| 6732 |
let backup = "sl2~"; |
| 6733 |
at.write(FILE_NAME, CONTENTS); |
| 6734 |
at.symlink_file(FILE_NAME, symlink1); |
| 6735 |
at.symlink_file(FILE_NAME, symlink2); |
| 6736 |
scene.ucmd().args(&[option, symlink1, symlink2]).succeeds(); |
| 6737 |
assert!(at.file_exists(FILE_NAME)); |
| 6738 |
assert_eq!(at.read(FILE_NAME), CONTENTS); |
| 6739 |
assert_eq!(FILE_NAME, at.resolve_link(symlink1)); |
| 6740 |
assert!(at.file_exists(symlink2)); |
| 6741 |
assert_eq!(at.read(symlink2), CONTENTS); |
| 6742 |
assert_eq!(FILE_NAME, at.resolve_link(backup)); |
| 6743 |
} |
| 6744 |
} |
| 6745 |
|
| 6746 |
#[test] |
| 6747 |
#[cfg_attr( |
| 6748 |
wasi_runner, |
| 6749 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 6750 |
)] |
| 6751 |
fn test_same_file_from_symlink_to_symlink_with_option_symlink() { |
| 6752 |
let scene = TestScenario::new(util_name!()); |
| 6753 |
let at = &scene.fixtures; |
| 6754 |
let symlink1 = "sl1"; |
| 6755 |
let symlink2 = "sl2"; |
| 6756 |
at.write(FILE_NAME, CONTENTS); |
| 6757 |
at.symlink_file(FILE_NAME, symlink1); |
| 6758 |
at.symlink_file(FILE_NAME, symlink2); |
| 6759 |
scene |
| 6760 |
.ucmd() |
| 6761 |
.args(&["-s", symlink1, symlink2]) |
| 6762 |
.fails() |
| 6763 |
.stderr_contains("cannot create symlink 'sl2' to 'sl1'"); |
| 6764 |
assert!(at.file_exists(FILE_NAME)); |
| 6765 |
assert_eq!(at.read(FILE_NAME), CONTENTS); |
| 6766 |
assert_eq!(FILE_NAME, at.resolve_link(symlink1)); |
| 6767 |
assert_eq!(FILE_NAME, at.resolve_link(symlink2)); |
| 6768 |
} |
| 6769 |
|
| 6770 |
#[test] |
| 6771 |
#[cfg_attr( |
| 6772 |
wasi_runner, |
| 6773 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 6774 |
)] |
| 6775 |
fn test_same_file_from_symlink_to_symlink_with_option_symlink_and_force() { |
| 6776 |
let scene = TestScenario::new(util_name!()); |
| 6777 |
let at = &scene.fixtures; |
| 6778 |
let symlink1 = "sl1"; |
| 6779 |
let symlink2 = "sl2"; |
| 6780 |
at.write(FILE_NAME, CONTENTS); |
| 6781 |
at.symlink_file(FILE_NAME, symlink1); |
| 6782 |
at.symlink_file(FILE_NAME, symlink2); |
| 6783 |
scene.ucmd().args(&["-sf", symlink1, symlink2]).succeeds(); |
| 6784 |
assert!(at.file_exists(FILE_NAME)); |
| 6785 |
assert_eq!(at.read(FILE_NAME), CONTENTS); |
| 6786 |
assert_eq!(FILE_NAME, at.resolve_link(symlink1)); |
| 6787 |
assert_eq!(symlink1, at.resolve_link(symlink2)); |
| 6788 |
} |
| 6789 |
|
| 6790 |
#[test] |
| 6791 |
fn test_same_symlink_to_itself_no_dereference() { |
| 6792 |
let scene = TestScenario::new(util_name!()); |
| 6793 |
let at = &scene.fixtures; |
| 6794 |
at.write(FILE_NAME, CONTENTS); |
| 6795 |
at.symlink_file(FILE_NAME, SYMLINK_NAME); |
| 6796 |
scene |
| 6797 |
.ucmd() |
| 6798 |
.args(&["-P", SYMLINK_NAME, SYMLINK_NAME]) |
| 6799 |
.fails() |
| 6800 |
.stderr_contains("are the same file"); |
| 6801 |
} |
| 6802 |
|
| 6803 |
#[test] |
| 6804 |
fn test_same_dangling_symlink_to_itself_no_dereference() { |
| 6805 |
let scene = TestScenario::new(util_name!()); |
| 6806 |
let at = &scene.fixtures; |
| 6807 |
at.symlink_file("nonexistent_file", SYMLINK_NAME); |
| 6808 |
scene |
| 6809 |
.ucmd() |
| 6810 |
.args(&["-P", SYMLINK_NAME, SYMLINK_NAME]) |
| 6811 |
.fails() |
| 6812 |
.stderr_contains("are the same file"); |
| 6813 |
} |
| 6814 |
|
| 6815 |
// the following tests tries to copy file to a hardlink of the same file with |
| 6816 |
// various options |
| 6817 |
#[test] |
| 6818 |
fn test_same_file_from_file_to_hardlink() { |
| 6819 |
for option in ["-d", "-f", "-df"] { |
| 6820 |
let scene = TestScenario::new(util_name!()); |
| 6821 |
let at = &scene.fixtures; |
| 6822 |
let hardlink = "hardlink"; |
| 6823 |
at.write(FILE_NAME, CONTENTS); |
| 6824 |
at.hard_link(FILE_NAME, hardlink); |
| 6825 |
|
| 6826 |
scene |
| 6827 |
.ucmd() |
| 6828 |
.args(&[option, FILE_NAME, hardlink]) |
| 6829 |
.fails() |
| 6830 |
.stderr_contains("'foo' and 'hardlink' are the same file"); |
| 6831 |
assert!(at.file_exists(FILE_NAME)); |
| 6832 |
assert!(at.file_exists(hardlink)); |
| 6833 |
assert_eq!(at.read(FILE_NAME), CONTENTS); |
| 6834 |
} |
| 6835 |
} |
| 6836 |
|
| 6837 |
#[test] |
| 6838 |
#[cfg_attr( |
| 6839 |
wasi_runner, |
| 6840 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 6841 |
)] |
| 6842 |
fn test_same_file_from_file_to_hardlink_with_option_rem() { |
| 6843 |
let scene = TestScenario::new(util_name!()); |
| 6844 |
let at = &scene.fixtures; |
| 6845 |
let hardlink = "hardlink"; |
| 6846 |
at.write(FILE_NAME, CONTENTS); |
| 6847 |
at.hard_link(FILE_NAME, hardlink); |
| 6848 |
scene |
| 6849 |
.ucmd() |
| 6850 |
.args(&["--rem", FILE_NAME, hardlink]) |
| 6851 |
.succeeds(); |
| 6852 |
assert!(at.file_exists(FILE_NAME)); |
| 6853 |
assert!(at.file_exists(hardlink)); |
| 6854 |
assert_eq!(at.read(FILE_NAME), CONTENTS); |
| 6855 |
} |
| 6856 |
|
| 6857 |
#[test] |
| 6858 |
#[cfg_attr( |
| 6859 |
wasi_runner, |
| 6860 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 6861 |
)] |
| 6862 |
fn test_same_file_from_file_to_hardlink_with_option_backup() { |
| 6863 |
for option in ["-b", "-bd", "-bf", "-bdf"] { |
| 6864 |
let scene = TestScenario::new(util_name!()); |
| 6865 |
let at = &scene.fixtures; |
| 6866 |
let hardlink = "hardlink"; |
| 6867 |
let backup = "hardlink~"; |
| 6868 |
at.write(FILE_NAME, CONTENTS); |
| 6869 |
at.hard_link(FILE_NAME, hardlink); |
| 6870 |
scene.ucmd().args(&[option, FILE_NAME, hardlink]).succeeds(); |
| 6871 |
assert!(at.file_exists(FILE_NAME)); |
| 6872 |
assert!(at.file_exists(hardlink)); |
| 6873 |
assert!(at.file_exists(backup)); |
| 6874 |
assert_eq!(at.read(FILE_NAME), CONTENTS); |
| 6875 |
} |
| 6876 |
} |
| 6877 |
|
| 6878 |
#[test] |
| 6879 |
#[cfg_attr( |
| 6880 |
wasi_runner, |
| 6881 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 6882 |
)] |
| 6883 |
fn test_same_file_from_file_to_hardlink_with_option_link() { |
| 6884 |
for option in ["-l", "-dl", "-fl", "-dfl", "-bl", "-bdl", "-bfl", "-bdfl"] { |
| 6885 |
let scene = TestScenario::new(util_name!()); |
| 6886 |
let at = &scene.fixtures; |
| 6887 |
let hardlink = "hardlink"; |
| 6888 |
at.write(FILE_NAME, CONTENTS); |
| 6889 |
at.hard_link(FILE_NAME, hardlink); |
| 6890 |
scene.ucmd().args(&[option, FILE_NAME, hardlink]).succeeds(); |
| 6891 |
assert!(at.file_exists(FILE_NAME)); |
| 6892 |
assert!(at.file_exists(hardlink)); |
| 6893 |
assert_eq!(at.read(FILE_NAME), CONTENTS); |
| 6894 |
} |
| 6895 |
} |
| 6896 |
|
| 6897 |
#[test] |
| 6898 |
fn test_same_file_from_file_to_hardlink_with_option_symlink() { |
| 6899 |
for option in ["-s", "-sf"] { |
| 6900 |
let scene = TestScenario::new(util_name!()); |
| 6901 |
let at = &scene.fixtures; |
| 6902 |
let hardlink = "hardlink"; |
| 6903 |
at.write(FILE_NAME, CONTENTS); |
| 6904 |
at.hard_link(FILE_NAME, hardlink); |
| 6905 |
scene |
| 6906 |
.ucmd() |
| 6907 |
.args(&[option, FILE_NAME, hardlink]) |
| 6908 |
.fails() |
| 6909 |
.stderr_contains("'foo' and 'hardlink' are the same file"); |
| 6910 |
assert!(at.file_exists(FILE_NAME)); |
| 6911 |
assert!(at.file_exists(hardlink)); |
| 6912 |
assert_eq!(at.read(FILE_NAME), CONTENTS); |
| 6913 |
} |
| 6914 |
} |
| 6915 |
|
| 6916 |
// the following tests tries to copy symlink to a hardlink of the same symlink with |
| 6917 |
// various options |
| 6918 |
#[test] |
| 6919 |
#[cfg_attr( |
| 6920 |
wasi_runner, |
| 6921 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 6922 |
)] |
| 6923 |
fn test_same_file_from_hard_link_of_symlink_to_symlink() { |
| 6924 |
let scene = TestScenario::new(util_name!()); |
| 6925 |
let at = &scene.fixtures; |
| 6926 |
let hardlink_to_symlink = "hlsl"; |
| 6927 |
at.write(FILE_NAME, CONTENTS); |
| 6928 |
at.symlink_file(FILE_NAME, SYMLINK_NAME); |
| 6929 |
at.hard_link(SYMLINK_NAME, hardlink_to_symlink); |
| 6930 |
scene |
| 6931 |
.ucmd() |
| 6932 |
.args(&[hardlink_to_symlink, SYMLINK_NAME]) |
| 6933 |
.fails() |
| 6934 |
.stderr_contains("cp: 'hlsl' and 'symlink' are the same file"); |
| 6935 |
assert!(at.file_exists(FILE_NAME)); |
| 6936 |
assert!(at.symlink_exists(SYMLINK_NAME)); |
| 6937 |
assert!(at.symlink_exists(hardlink_to_symlink)); |
| 6938 |
assert_eq!(FILE_NAME, at.resolve_link(SYMLINK_NAME)); |
| 6939 |
assert_eq!(FILE_NAME, at.resolve_link(hardlink_to_symlink)); |
| 6940 |
assert_eq!(at.read(FILE_NAME), CONTENTS); |
| 6941 |
} |
| 6942 |
|
| 6943 |
#[test] |
| 6944 |
#[cfg_attr( |
| 6945 |
wasi_runner, |
| 6946 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 6947 |
)] |
| 6948 |
fn test_same_file_from_hard_link_of_symlink_to_symlink_with_option_force() { |
| 6949 |
let scene = TestScenario::new(util_name!()); |
| 6950 |
let at = &scene.fixtures; |
| 6951 |
let hardlink_to_symlink = "hlsl"; |
| 6952 |
at.write(FILE_NAME, CONTENTS); |
| 6953 |
at.symlink_file(FILE_NAME, SYMLINK_NAME); |
| 6954 |
at.hard_link(SYMLINK_NAME, hardlink_to_symlink); |
| 6955 |
scene |
| 6956 |
.ucmd() |
| 6957 |
.args(&["-f", hardlink_to_symlink, SYMLINK_NAME]) |
| 6958 |
.fails() |
| 6959 |
.stderr_contains("cp: 'hlsl' and 'symlink' are the same file"); |
| 6960 |
assert!(at.file_exists(FILE_NAME)); |
| 6961 |
assert!(at.symlink_exists(SYMLINK_NAME)); |
| 6962 |
assert!(at.symlink_exists(hardlink_to_symlink)); |
| 6963 |
assert_eq!(FILE_NAME, at.resolve_link(SYMLINK_NAME)); |
| 6964 |
assert_eq!(FILE_NAME, at.resolve_link(hardlink_to_symlink)); |
| 6965 |
assert_eq!(at.read(FILE_NAME), CONTENTS); |
| 6966 |
} |
| 6967 |
|
| 6968 |
#[test] |
| 6969 |
#[cfg_attr( |
| 6970 |
wasi_runner, |
| 6971 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 6972 |
)] |
| 6973 |
fn test_same_file_from_hard_link_of_symlink_to_symlink_with_option_no_deref() { |
| 6974 |
for option in ["-d", "-df"] { |
| 6975 |
let scene = TestScenario::new(util_name!()); |
| 6976 |
let at = &scene.fixtures; |
| 6977 |
let hardlink_to_symlink = "hlsl"; |
| 6978 |
at.write(FILE_NAME, CONTENTS); |
| 6979 |
at.symlink_file(FILE_NAME, SYMLINK_NAME); |
| 6980 |
at.hard_link(SYMLINK_NAME, hardlink_to_symlink); |
| 6981 |
scene |
| 6982 |
.ucmd() |
| 6983 |
.args(&[option, hardlink_to_symlink, SYMLINK_NAME]) |
| 6984 |
.succeeds(); |
| 6985 |
assert!(at.file_exists(FILE_NAME)); |
| 6986 |
assert!(at.symlink_exists(SYMLINK_NAME)); |
| 6987 |
assert!(at.symlink_exists(hardlink_to_symlink)); |
| 6988 |
assert_eq!(FILE_NAME, at.resolve_link(SYMLINK_NAME)); |
| 6989 |
assert_eq!(FILE_NAME, at.resolve_link(hardlink_to_symlink)); |
| 6990 |
assert_eq!(at.read(FILE_NAME), CONTENTS); |
| 6991 |
} |
| 6992 |
} |
| 6993 |
|
| 6994 |
#[test] |
| 6995 |
#[cfg_attr( |
| 6996 |
wasi_runner, |
| 6997 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 6998 |
)] |
| 6999 |
fn test_same_file_from_hard_link_of_symlink_to_symlink_with_option_rem() { |
| 7000 |
let scene = TestScenario::new(util_name!()); |
| 7001 |
let at = &scene.fixtures; |
| 7002 |
let hardlink_to_symlink = "hlsl"; |
| 7003 |
at.write(FILE_NAME, CONTENTS); |
| 7004 |
at.symlink_file(FILE_NAME, SYMLINK_NAME); |
| 7005 |
at.hard_link(SYMLINK_NAME, hardlink_to_symlink); |
| 7006 |
scene |
| 7007 |
.ucmd() |
| 7008 |
.args(&["--rem", hardlink_to_symlink, SYMLINK_NAME]) |
| 7009 |
.succeeds(); |
| 7010 |
assert!(at.file_exists(FILE_NAME)); |
| 7011 |
assert!(at.file_exists(SYMLINK_NAME)); |
| 7012 |
assert!(!at.symlink_exists(SYMLINK_NAME)); |
| 7013 |
assert!(at.symlink_exists(hardlink_to_symlink)); |
| 7014 |
assert_eq!(FILE_NAME, at.resolve_link(hardlink_to_symlink)); |
| 7015 |
assert_eq!(at.read(FILE_NAME), CONTENTS); |
| 7016 |
assert_eq!(at.read(SYMLINK_NAME), CONTENTS); |
| 7017 |
} |
| 7018 |
|
| 7019 |
#[test] |
| 7020 |
#[cfg_attr( |
| 7021 |
wasi_runner, |
| 7022 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 7023 |
)] |
| 7024 |
fn test_same_file_from_hard_link_of_symlink_to_symlink_with_option_backup() { |
| 7025 |
for option in ["-b", "-bf"] { |
| 7026 |
let scene = TestScenario::new(util_name!()); |
| 7027 |
let at = &scene.fixtures; |
| 7028 |
let backup = "symlink~"; |
| 7029 |
let hardlink_to_symlink = "hlsl"; |
| 7030 |
at.write(FILE_NAME, CONTENTS); |
| 7031 |
at.symlink_file(FILE_NAME, SYMLINK_NAME); |
| 7032 |
at.hard_link(SYMLINK_NAME, hardlink_to_symlink); |
| 7033 |
scene |
| 7034 |
.ucmd() |
| 7035 |
.args(&[option, hardlink_to_symlink, SYMLINK_NAME]) |
| 7036 |
.succeeds(); |
| 7037 |
assert!(at.file_exists(FILE_NAME)); |
| 7038 |
assert!(at.file_exists(SYMLINK_NAME)); |
| 7039 |
assert!(!at.symlink_exists(SYMLINK_NAME)); |
| 7040 |
assert!(at.symlink_exists(hardlink_to_symlink)); |
| 7041 |
assert_eq!(FILE_NAME, at.resolve_link(hardlink_to_symlink)); |
| 7042 |
assert!(at.symlink_exists(backup)); |
| 7043 |
assert_eq!(FILE_NAME, at.resolve_link(backup)); |
| 7044 |
assert_eq!(at.read(FILE_NAME), CONTENTS); |
| 7045 |
assert_eq!(at.read(SYMLINK_NAME), CONTENTS); |
| 7046 |
} |
| 7047 |
} |
| 7048 |
|
| 7049 |
#[test] |
| 7050 |
#[cfg_attr( |
| 7051 |
wasi_runner, |
| 7052 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 7053 |
)] |
| 7054 |
fn test_same_file_from_hard_link_of_symlink_to_symlink_with_option_backup_and_no_deref() { |
| 7055 |
for option in ["-bd", "-bdf"] { |
| 7056 |
let scene = TestScenario::new(util_name!()); |
| 7057 |
let at = &scene.fixtures; |
| 7058 |
let backup = "symlink~"; |
| 7059 |
let hardlink_to_symlink = "hlsl"; |
| 7060 |
at.write(FILE_NAME, CONTENTS); |
| 7061 |
at.symlink_file(FILE_NAME, SYMLINK_NAME); |
| 7062 |
at.hard_link(SYMLINK_NAME, hardlink_to_symlink); |
| 7063 |
scene |
| 7064 |
.ucmd() |
| 7065 |
.args(&[option, hardlink_to_symlink, SYMLINK_NAME]) |
| 7066 |
.succeeds(); |
| 7067 |
assert!(at.file_exists(FILE_NAME)); |
| 7068 |
assert!(at.symlink_exists(SYMLINK_NAME)); |
| 7069 |
assert_eq!(FILE_NAME, at.resolve_link(SYMLINK_NAME)); |
| 7070 |
assert!(at.symlink_exists(hardlink_to_symlink)); |
| 7071 |
assert_eq!(FILE_NAME, at.resolve_link(hardlink_to_symlink)); |
| 7072 |
assert!(at.symlink_exists(backup)); |
| 7073 |
assert_eq!(FILE_NAME, at.resolve_link(backup)); |
| 7074 |
assert_eq!(at.read(FILE_NAME), CONTENTS); |
| 7075 |
} |
| 7076 |
} |
| 7077 |
|
| 7078 |
#[test] |
| 7079 |
#[cfg_attr( |
| 7080 |
wasi_runner, |
| 7081 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 7082 |
)] |
| 7083 |
fn test_same_file_from_hard_link_of_symlink_to_symlink_with_option_link() { |
| 7084 |
let scene = TestScenario::new(util_name!()); |
| 7085 |
let at = &scene.fixtures; |
| 7086 |
let hardlink_to_symlink = "hlsl"; |
| 7087 |
at.write(FILE_NAME, CONTENTS); |
| 7088 |
at.symlink_file(FILE_NAME, SYMLINK_NAME); |
| 7089 |
at.hard_link(SYMLINK_NAME, hardlink_to_symlink); |
| 7090 |
scene |
| 7091 |
.ucmd() |
| 7092 |
.args(&["-l", hardlink_to_symlink, SYMLINK_NAME]) |
| 7093 |
.fails() |
| 7094 |
.stderr_contains("cannot create hard link 'symlink' to 'hlsl'"); |
| 7095 |
assert!(at.file_exists(FILE_NAME)); |
| 7096 |
assert!(at.symlink_exists(SYMLINK_NAME)); |
| 7097 |
assert!(at.symlink_exists(hardlink_to_symlink)); |
| 7098 |
assert_eq!(FILE_NAME, at.resolve_link(SYMLINK_NAME)); |
| 7099 |
assert_eq!(FILE_NAME, at.resolve_link(hardlink_to_symlink)); |
| 7100 |
assert_eq!(at.read(FILE_NAME), CONTENTS); |
| 7101 |
} |
| 7102 |
|
| 7103 |
#[test] |
| 7104 |
#[cfg_attr( |
| 7105 |
wasi_runner, |
| 7106 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 7107 |
)] |
| 7108 |
fn test_same_file_from_hard_link_of_symlink_to_symlink_with_option_link_and_no_deref() { |
| 7109 |
for option in ["-dl", "-dfl"] { |
| 7110 |
let scene = TestScenario::new(util_name!()); |
| 7111 |
let at = &scene.fixtures; |
| 7112 |
let hardlink_to_symlink = "hlsl"; |
| 7113 |
at.write(FILE_NAME, CONTENTS); |
| 7114 |
at.symlink_file(FILE_NAME, SYMLINK_NAME); |
| 7115 |
at.hard_link(SYMLINK_NAME, hardlink_to_symlink); |
| 7116 |
scene |
| 7117 |
.ucmd() |
| 7118 |
.args(&[option, hardlink_to_symlink, SYMLINK_NAME]) |
| 7119 |
.succeeds(); |
| 7120 |
assert!(at.file_exists(FILE_NAME)); |
| 7121 |
assert!(at.symlink_exists(SYMLINK_NAME)); |
| 7122 |
assert!(at.symlink_exists(hardlink_to_symlink)); |
| 7123 |
assert_eq!(FILE_NAME, at.resolve_link(SYMLINK_NAME)); |
| 7124 |
assert_eq!(FILE_NAME, at.resolve_link(hardlink_to_symlink)); |
| 7125 |
assert_eq!(at.read(FILE_NAME), CONTENTS); |
| 7126 |
} |
| 7127 |
} |
| 7128 |
|
| 7129 |
#[test] |
| 7130 |
#[cfg_attr( |
| 7131 |
wasi_runner, |
| 7132 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 7133 |
)] |
| 7134 |
fn test_same_file_from_hard_link_of_symlink_to_symlink_with_option_link_and_force() { |
| 7135 |
let scene = TestScenario::new(util_name!()); |
| 7136 |
let at = &scene.fixtures; |
| 7137 |
let hardlink_to_symlink = "hlsl"; |
| 7138 |
at.write(FILE_NAME, CONTENTS); |
| 7139 |
at.symlink_file(FILE_NAME, SYMLINK_NAME); |
| 7140 |
at.hard_link(SYMLINK_NAME, hardlink_to_symlink); |
| 7141 |
scene |
| 7142 |
.ucmd() |
| 7143 |
.args(&["-fl", hardlink_to_symlink, SYMLINK_NAME]) |
| 7144 |
.succeeds(); |
| 7145 |
assert!(at.file_exists(FILE_NAME)); |
| 7146 |
assert!(at.file_exists(SYMLINK_NAME)); |
| 7147 |
assert!(!at.symlink_exists(SYMLINK_NAME)); |
| 7148 |
assert!(at.symlink_exists(hardlink_to_symlink)); |
| 7149 |
assert_eq!(FILE_NAME, at.resolve_link(hardlink_to_symlink)); |
| 7150 |
assert_eq!(at.read(FILE_NAME), CONTENTS); |
| 7151 |
} |
| 7152 |
|
| 7153 |
#[test] |
| 7154 |
#[cfg_attr( |
| 7155 |
wasi_runner, |
| 7156 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 7157 |
)] |
| 7158 |
fn test_same_file_from_hard_link_of_symlink_to_symlink_with_option_link_and_backup() { |
| 7159 |
for option in ["-bl", "-bfl"] { |
| 7160 |
let scene = TestScenario::new(util_name!()); |
| 7161 |
let at = &scene.fixtures; |
| 7162 |
let backup = "symlink~"; |
| 7163 |
let hardlink_to_symlink = "hlsl"; |
| 7164 |
at.write(FILE_NAME, CONTENTS); |
| 7165 |
at.symlink_file(FILE_NAME, SYMLINK_NAME); |
| 7166 |
at.hard_link(SYMLINK_NAME, hardlink_to_symlink); |
| 7167 |
scene |
| 7168 |
.ucmd() |
| 7169 |
.args(&[option, hardlink_to_symlink, SYMLINK_NAME]) |
| 7170 |
.succeeds(); |
| 7171 |
assert!(at.file_exists(FILE_NAME)); |
| 7172 |
assert!(at.file_exists(SYMLINK_NAME)); |
| 7173 |
assert!(!at.symlink_exists(SYMLINK_NAME)); |
| 7174 |
assert!(at.symlink_exists(hardlink_to_symlink)); |
| 7175 |
assert_eq!(FILE_NAME, at.resolve_link(hardlink_to_symlink)); |
| 7176 |
assert!(at.symlink_exists(backup)); |
| 7177 |
assert_eq!(FILE_NAME, at.resolve_link(backup)); |
| 7178 |
assert_eq!(at.read(FILE_NAME), CONTENTS); |
| 7179 |
} |
| 7180 |
} |
| 7181 |
|
| 7182 |
#[test] |
| 7183 |
fn test_same_file_from_hard_link_of_symlink_to_symlink_with_options_backup_link_no_deref() { |
| 7184 |
for option in ["-bdl", "-bdfl"] { |
| 7185 |
let scene = TestScenario::new(util_name!()); |
| 7186 |
let at = &scene.fixtures; |
| 7187 |
let hardlink_to_symlink = "hlsl"; |
| 7188 |
at.write(FILE_NAME, CONTENTS); |
| 7189 |
at.symlink_file(FILE_NAME, SYMLINK_NAME); |
| 7190 |
at.hard_link(SYMLINK_NAME, hardlink_to_symlink); |
| 7191 |
scene |
| 7192 |
.ucmd() |
| 7193 |
.args(&[option, hardlink_to_symlink, SYMLINK_NAME]) |
| 7194 |
.succeeds(); |
| 7195 |
assert!(at.file_exists(FILE_NAME)); |
| 7196 |
assert!(at.symlink_exists(SYMLINK_NAME)); |
| 7197 |
assert!(at.symlink_exists(hardlink_to_symlink)); |
| 7198 |
assert_eq!(FILE_NAME, at.resolve_link(SYMLINK_NAME)); |
| 7199 |
assert_eq!(FILE_NAME, at.resolve_link(hardlink_to_symlink)); |
| 7200 |
assert_eq!(at.read(FILE_NAME), CONTENTS); |
| 7201 |
} |
| 7202 |
} |
| 7203 |
|
| 7204 |
#[test] |
| 7205 |
#[cfg_attr( |
| 7206 |
wasi_runner, |
| 7207 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 7208 |
)] |
| 7209 |
fn test_same_file_from_hard_link_of_symlink_to_symlink_with_option_symlink() { |
| 7210 |
let scene = TestScenario::new(util_name!()); |
| 7211 |
let at = &scene.fixtures; |
| 7212 |
let hardlink_to_symlink = "hlsl"; |
| 7213 |
at.write(FILE_NAME, CONTENTS); |
| 7214 |
at.symlink_file(FILE_NAME, SYMLINK_NAME); |
| 7215 |
at.hard_link(SYMLINK_NAME, hardlink_to_symlink); |
| 7216 |
scene |
| 7217 |
.ucmd() |
| 7218 |
.args(&["-s", hardlink_to_symlink, SYMLINK_NAME]) |
| 7219 |
.fails() |
| 7220 |
.stderr_contains("cannot create symlink 'symlink' to 'hlsl'"); |
| 7221 |
assert!(at.file_exists(FILE_NAME)); |
| 7222 |
assert!(at.symlink_exists(SYMLINK_NAME)); |
| 7223 |
assert!(at.symlink_exists(hardlink_to_symlink)); |
| 7224 |
assert_eq!(FILE_NAME, at.resolve_link(SYMLINK_NAME)); |
| 7225 |
assert_eq!(FILE_NAME, at.resolve_link(hardlink_to_symlink)); |
| 7226 |
assert_eq!(at.read(FILE_NAME), CONTENTS); |
| 7227 |
} |
| 7228 |
|
| 7229 |
#[test] |
| 7230 |
#[cfg_attr( |
| 7231 |
wasi_runner, |
| 7232 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 7233 |
)] |
| 7234 |
fn test_same_file_from_hard_link_of_symlink_to_symlink_with_option_symlink_and_force() { |
| 7235 |
let scene = TestScenario::new(util_name!()); |
| 7236 |
let at = &scene.fixtures; |
| 7237 |
let hardlink_to_symlink = "hlsl"; |
| 7238 |
at.write(FILE_NAME, CONTENTS); |
| 7239 |
at.symlink_file(FILE_NAME, SYMLINK_NAME); |
| 7240 |
at.hard_link(SYMLINK_NAME, hardlink_to_symlink); |
| 7241 |
scene |
| 7242 |
.ucmd() |
| 7243 |
.args(&["-sf", hardlink_to_symlink, SYMLINK_NAME]) |
| 7244 |
.succeeds(); |
| 7245 |
assert!(at.file_exists(FILE_NAME)); |
| 7246 |
assert!(at.symlink_exists(SYMLINK_NAME)); |
| 7247 |
assert!(at.symlink_exists(hardlink_to_symlink)); |
| 7248 |
assert_eq!(hardlink_to_symlink, at.resolve_link(SYMLINK_NAME)); |
| 7249 |
assert_eq!(FILE_NAME, at.resolve_link(hardlink_to_symlink)); |
| 7250 |
assert_eq!(at.read(FILE_NAME), CONTENTS); |
| 7251 |
} |
| 7252 |
|
| 7253 |
#[test] |
| 7254 |
#[cfg_attr( |
| 7255 |
wasi_runner, |
| 7256 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 7257 |
)] |
| 7258 |
fn test_hardlink_of_symlink_to_hardlink_of_same_symlink_with_option_no_deref() { |
| 7259 |
let scene = TestScenario::new(util_name!()); |
| 7260 |
let at = &scene.fixtures; |
| 7261 |
let hardlink1 = "hardlink_to_symlink_1"; |
| 7262 |
let hardlink2 = "hardlink_to_symlink_2"; |
| 7263 |
at.write(FILE_NAME, CONTENTS); |
| 7264 |
at.symlink_file(FILE_NAME, SYMLINK_NAME); |
| 7265 |
at.hard_link(SYMLINK_NAME, hardlink1); |
| 7266 |
at.hard_link(SYMLINK_NAME, hardlink2); |
| 7267 |
let ino = at.symlink_metadata(hardlink1).ino(); |
| 7268 |
assert_eq!(ino, at.symlink_metadata(hardlink2).ino()); // Sanity check |
| 7269 |
scene.ucmd().args(&["-P", hardlink1, hardlink2]).succeeds(); |
| 7270 |
assert!(at.file_exists(FILE_NAME)); |
| 7271 |
assert!(at.symlink_exists(SYMLINK_NAME)); |
| 7272 |
// If hardlink a and b point to the same symlink, then cp a b doesn't create a new file |
| 7273 |
assert_eq!(ino, at.symlink_metadata(hardlink1).ino()); |
| 7274 |
assert_eq!(ino, at.symlink_metadata(hardlink2).ino()); |
| 7275 |
} |
| 7276 |
} |
| 7277 |
|
| 7278 |
// the following tests are for how the cp should behave when the source is a symlink |
| 7279 |
// and link option is given |
| 7280 |
#[cfg(all(unix, not(target_os = "android")))] |
| 7281 |
mod link_deref { |
| 7282 |
|
| 7283 |
use std::os::unix::fs::MetadataExt; |
| 7284 |
use uutests::util::{AtPath, TestScenario}; |
| 7285 |
use uutests::util_name; |
| 7286 |
|
| 7287 |
const FILE: &str = "file"; |
| 7288 |
const FILE_LINK: &str = "file_link"; |
| 7289 |
const DIR: &str = "dir"; |
| 7290 |
const DIR_LINK: &str = "dir_link"; |
| 7291 |
const DANG_LINK: &str = "dang_link"; |
| 7292 |
const DST: &str = "dst"; |
| 7293 |
|
| 7294 |
fn setup_link_deref_tests(source: &str, at: &AtPath) { |
| 7295 |
match source { |
| 7296 |
FILE_LINK => { |
| 7297 |
at.touch(FILE); |
| 7298 |
at.symlink_file(FILE, FILE_LINK); |
| 7299 |
} |
| 7300 |
DIR_LINK => { |
| 7301 |
at.mkdir(DIR); |
| 7302 |
at.symlink_dir(DIR, DIR_LINK); |
| 7303 |
} |
| 7304 |
DANG_LINK => at.symlink_file("nowhere", DANG_LINK), |
| 7305 |
_ => {} |
| 7306 |
} |
| 7307 |
} |
| 7308 |
|
| 7309 |
// cp --link shouldn't deref source if -P is given |
| 7310 |
#[test] |
| 7311 |
fn test_cp_symlink_as_source_with_link_and_no_deref() { |
| 7312 |
for src in [FILE_LINK, DIR_LINK, DANG_LINK] { |
| 7313 |
for r in [false, true] { |
| 7314 |
let scene = TestScenario::new(util_name!()); |
| 7315 |
let at = &scene.fixtures; |
| 7316 |
setup_link_deref_tests(src, at); |
| 7317 |
let mut args = vec!["--link", "-P", src, DST]; |
| 7318 |
if r { |
| 7319 |
args.push("-R"); |
| 7320 |
} |
| 7321 |
scene.ucmd().args(&args).succeeds().no_stderr(); |
| 7322 |
at.is_symlink(DST); |
| 7323 |
let src_ino = at.symlink_metadata(src).ino(); |
| 7324 |
let dest_ino = at.symlink_metadata(DST).ino(); |
| 7325 |
assert_eq!(src_ino, dest_ino); |
| 7326 |
} |
| 7327 |
} |
| 7328 |
} |
| 7329 |
|
| 7330 |
// Dereferencing should fail for dangling symlink. |
| 7331 |
#[test] |
| 7332 |
#[cfg_attr( |
| 7333 |
wasi_runner, |
| 7334 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 7335 |
)] |
| 7336 |
fn test_cp_dang_link_as_source_with_link() { |
| 7337 |
for option in ["", "-L", "-H"] { |
| 7338 |
for r in [false, true] { |
| 7339 |
let scene = TestScenario::new(util_name!()); |
| 7340 |
let at = &scene.fixtures; |
| 7341 |
setup_link_deref_tests(DANG_LINK, at); |
| 7342 |
let mut args = vec!["--link", DANG_LINK, DST]; |
| 7343 |
if r { |
| 7344 |
args.push("-R"); |
| 7345 |
} |
| 7346 |
if !option.is_empty() { |
| 7347 |
args.push(option); |
| 7348 |
} |
| 7349 |
scene |
| 7350 |
.ucmd() |
| 7351 |
.args(&args) |
| 7352 |
.fails() |
| 7353 |
.stderr_contains("No such file or directory"); |
| 7354 |
} |
| 7355 |
} |
| 7356 |
} |
| 7357 |
|
| 7358 |
// Dereferencing should fail for the 'dir_link' without -R. |
| 7359 |
#[test] |
| 7360 |
#[cfg_attr( |
| 7361 |
wasi_runner, |
| 7362 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 7363 |
)] |
| 7364 |
fn test_cp_dir_link_as_source_with_link() { |
| 7365 |
for option in ["", "-L", "-H"] { |
| 7366 |
let scene = TestScenario::new(util_name!()); |
| 7367 |
let at = &scene.fixtures; |
| 7368 |
setup_link_deref_tests(DIR_LINK, at); |
| 7369 |
let mut args = vec!["--link", DIR_LINK, DST]; |
| 7370 |
if !option.is_empty() { |
| 7371 |
args.push(option); |
| 7372 |
} |
| 7373 |
scene |
| 7374 |
.ucmd() |
| 7375 |
.args(&args) |
| 7376 |
.fails() |
| 7377 |
.stderr_contains("cp: -r not specified; omitting directory"); |
| 7378 |
} |
| 7379 |
} |
| 7380 |
|
| 7381 |
// cp --link -R 'dir_link' should create a new directory. |
| 7382 |
#[test] |
| 7383 |
#[cfg_attr( |
| 7384 |
wasi_runner, |
| 7385 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 7386 |
)] |
| 7387 |
fn test_cp_dir_link_as_source_with_link_and_r() { |
| 7388 |
for option in ["", "-L", "-H"] { |
| 7389 |
let scene = TestScenario::new(util_name!()); |
| 7390 |
let at = &scene.fixtures; |
| 7391 |
setup_link_deref_tests(DIR_LINK, at); |
| 7392 |
let mut args = vec!["--link", "-R", DIR_LINK, DST]; |
| 7393 |
if !option.is_empty() { |
| 7394 |
args.push(option); |
| 7395 |
} |
| 7396 |
scene.ucmd().args(&args).succeeds(); |
| 7397 |
at.dir_exists(DST); |
| 7398 |
} |
| 7399 |
} |
| 7400 |
|
| 7401 |
//cp --link 'file_link' should create a hard link to the target. |
| 7402 |
#[test] |
| 7403 |
#[cfg_attr( |
| 7404 |
wasi_runner, |
| 7405 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 7406 |
)] |
| 7407 |
fn test_cp_file_link_as_source_with_link() { |
| 7408 |
for option in ["", "-L", "-H"] { |
| 7409 |
for r in [false, true] { |
| 7410 |
let scene = TestScenario::new(util_name!()); |
| 7411 |
let at = &scene.fixtures; |
| 7412 |
setup_link_deref_tests(FILE_LINK, at); |
| 7413 |
let mut args = vec!["--link", "-R", FILE_LINK, DST]; |
| 7414 |
if !option.is_empty() { |
| 7415 |
args.push(option); |
| 7416 |
} |
| 7417 |
if r { |
| 7418 |
args.push("-R"); |
| 7419 |
} |
| 7420 |
scene.ucmd().args(&args).succeeds(); |
| 7421 |
at.file_exists(DST); |
| 7422 |
let src_ino = at.symlink_metadata(FILE).ino(); |
| 7423 |
let dest_ino = at.symlink_metadata(DST).ino(); |
| 7424 |
assert_eq!(src_ino, dest_ino); |
| 7425 |
} |
| 7426 |
} |
| 7427 |
} |
| 7428 |
} |
| 7429 |
|
| 7430 |
// The cp command might create directories with excessively permissive permissions temporarily, |
| 7431 |
// which could be problematic if we aim to preserve ownership or mode. For example, when |
| 7432 |
// copying a directory, the destination directory could temporarily be setgid on some filesystems. |
| 7433 |
// This temporary setgid status could grant access to other users who share the same group |
| 7434 |
// ownership as the newly created directory. To mitigate this issue, when creating a directory we |
| 7435 |
// disable these excessive permissions. |
| 7436 |
#[test] |
| 7437 |
#[cfg(unix)] |
| 7438 |
#[cfg(not(target_os = "openbsd"))] |
| 7439 |
#[cfg_attr( |
| 7440 |
wasi_runner, |
| 7441 |
ignore = "WASI: no chmod syscall, so required mode/ownership preservation always fails" |
| 7442 |
)] |
| 7443 |
fn test_dir_perm_race_with_preserve_mode_and_ownership() { |
| 7444 |
const SRC_DIR: &str = "src"; |
| 7445 |
const DEST_DIR: &str = "dest"; |
| 7446 |
const FIFO: &str = "src/fifo"; |
| 7447 |
for attr in ["mode", "ownership"] { |
| 7448 |
let scene = TestScenario::new(util_name!()); |
| 7449 |
let at = &scene.fixtures; |
| 7450 |
at.mkdir(SRC_DIR); |
| 7451 |
at.mkdir(DEST_DIR); |
| 7452 |
at.set_mode(SRC_DIR, 0o775); |
| 7453 |
at.set_mode(DEST_DIR, 0o2775); |
| 7454 |
at.mkfifo(FIFO); |
| 7455 |
let child = scene |
| 7456 |
.ucmd() |
| 7457 |
.args(&[ |
| 7458 |
format!("--preserve={attr}").as_str(), |
| 7459 |
"-R", |
| 7460 |
"--copy-contents", |
| 7461 |
"--parents", |
| 7462 |
SRC_DIR, |
| 7463 |
DEST_DIR, |
| 7464 |
]) |
| 7465 |
// make sure permissions weren't disabled because of umask. |
| 7466 |
.umask(0) |
| 7467 |
.run_no_wait(); |
| 7468 |
// while cp wait for fifo we could check the dirs created by cp |
| 7469 |
let timeout = Duration::from_secs(10); |
| 7470 |
let start_time = std::time::Instant::now(); |
| 7471 |
// wait for cp to create dirs |
| 7472 |
loop { |
| 7473 |
assert!( |
| 7474 |
start_time.elapsed() < timeout, |
| 7475 |
"timed out: cp took too long to create destination directory" |
| 7476 |
); |
| 7477 |
if at.dir_exists(format!("{DEST_DIR}/{SRC_DIR}")) { |
| 7478 |
break; |
| 7479 |
} |
| 7480 |
sleep(Duration::from_millis(100)); |
| 7481 |
} |
| 7482 |
let mode = at.metadata(&format!("{DEST_DIR}/{SRC_DIR}")).mode(); |
| 7483 |
#[allow(clippy::unnecessary_cast, clippy::cast_lossless)] |
| 7484 |
let mask = if attr == "mode" { |
| 7485 |
libc::S_IWGRP | libc::S_IWOTH |
| 7486 |
} else { |
| 7487 |
libc::S_IRWXG | libc::S_IRWXO |
| 7488 |
} as u32; |
| 7489 |
assert_eq!(mode & mask, 0, "unwanted permissions are present - {attr}"); |
| 7490 |
at.write(FIFO, "done"); |
| 7491 |
child.wait().unwrap().succeeded(); |
| 7492 |
} |
| 7493 |
} |
| 7494 |
|
| 7495 |
#[test] |
| 7496 |
// when -d and -a are overridden with --preserve or --no-preserve make sure that it only |
| 7497 |
// overrides attributes not other flags like -r or --no_deref implied in -a and -d. |
| 7498 |
#[cfg_attr( |
| 7499 |
wasi_runner, |
| 7500 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 7501 |
)] |
| 7502 |
fn test_preserve_attrs_overriding_1() { |
| 7503 |
const FILE: &str = "file"; |
| 7504 |
const SYMLINK: &str = "symlink"; |
| 7505 |
const DEST: &str = "dest"; |
| 7506 |
for f in ["-d", "-a"] { |
| 7507 |
let scene = TestScenario::new(util_name!()); |
| 7508 |
let at = &scene.fixtures; |
| 7509 |
at.make_file(FILE); |
| 7510 |
at.symlink_file(FILE, SYMLINK); |
| 7511 |
scene |
| 7512 |
.ucmd() |
| 7513 |
.args(&[f, "--no-preserve=all", SYMLINK, DEST]) |
| 7514 |
.succeeds(); |
| 7515 |
at.symlink_exists(DEST); |
| 7516 |
} |
| 7517 |
} |
| 7518 |
|
| 7519 |
#[test] |
| 7520 |
#[cfg(all(unix, not(target_os = "android")))] |
| 7521 |
#[cfg_attr( |
| 7522 |
wasi_runner, |
| 7523 |
ignore = "WASI: no chmod syscall, so required mode/ownership preservation always fails" |
| 7524 |
)] |
| 7525 |
fn test_preserve_attrs_overriding_2() { |
| 7526 |
const FILE1: &str = "file1"; |
| 7527 |
const FILE2: &str = "file2"; |
| 7528 |
const FOLDER: &str = "folder"; |
| 7529 |
const DEST: &str = "dest"; |
| 7530 |
for mut args in [ |
| 7531 |
// All of the following to args should tell cp to preserve mode and |
| 7532 |
// timestamp, but not the link. |
| 7533 |
vec!["-r", "--preserve=mode,link,timestamp", "--no-preserve=link"], |
| 7534 |
vec![ |
| 7535 |
"-r", |
| 7536 |
"--preserve=mode", |
| 7537 |
"--preserve=link", |
| 7538 |
"--preserve=timestamp", |
| 7539 |
"--no-preserve=link", |
| 7540 |
], |
| 7541 |
vec![ |
| 7542 |
"-r", |
| 7543 |
"--preserve=mode,link", |
| 7544 |
"--no-preserve=link", |
| 7545 |
"--preserve=timestamp", |
| 7546 |
], |
| 7547 |
vec!["-a", "--no-preserve=link"], |
| 7548 |
vec!["-r", "--preserve", "--no-preserve=link"], |
| 7549 |
] { |
| 7550 |
let scene = TestScenario::new(util_name!()); |
| 7551 |
let at = &scene.fixtures; |
| 7552 |
at.mkdir(FOLDER); |
| 7553 |
at.make_file(&format!("{FOLDER}/{FILE1}")); |
| 7554 |
at.set_mode(&format!("{FOLDER}/{FILE1}"), 0o775); |
| 7555 |
at.hard_link(&format!("{FOLDER}/{FILE1}"), &format!("{FOLDER}/{FILE2}")); |
| 7556 |
args.append(&mut vec![FOLDER, DEST]); |
| 7557 |
let src_file1_metadata = at.metadata(&format!("{FOLDER}/{FILE1}")); |
| 7558 |
scene.ucmd().args(&args).succeeds(); |
| 7559 |
at.dir_exists(DEST); |
| 7560 |
let dest_file1_metadata = at.metadata(&format!("{DEST}/{FILE1}")); |
| 7561 |
let dest_file2_metadata = at.metadata(&format!("{DEST}/{FILE2}")); |
| 7562 |
assert_eq!( |
| 7563 |
src_file1_metadata.modified().unwrap(), |
| 7564 |
dest_file1_metadata.modified().unwrap() |
| 7565 |
); |
| 7566 |
assert_eq!(src_file1_metadata.mode(), dest_file1_metadata.mode()); |
| 7567 |
assert_ne!(dest_file1_metadata.ino(), dest_file2_metadata.ino()); |
| 7568 |
} |
| 7569 |
} |
| 7570 |
|
| 7571 |
#[test] |
| 7572 |
#[cfg(unix)] |
| 7573 |
#[cfg_attr( |
| 7574 |
wasi_runner, |
| 7575 |
ignore = "WASI: no chmod syscall, so required mode/ownership preservation always fails" |
| 7576 |
)] |
| 7577 |
fn test_no_preserve_mode_with_later_preserve() { |
| 7578 |
let scene = TestScenario::new(util_name!()); |
| 7579 |
let at = &scene.fixtures; |
| 7580 |
at.touch("src"); |
| 7581 |
at.set_mode("src", 0o755); |
| 7582 |
|
| 7583 |
// 1. cp --no-preserve=mode src dst1 -> mode should not have execute bit (default umask strips permissions) |
| 7584 |
scene |
| 7585 |
.ucmd() |
| 7586 |
.args(&["--no-preserve=mode", "src", "dst1"]) |
| 7587 |
.succeeds(); |
| 7588 |
let dst1_mode = at.metadata("dst1").mode() & 0o777; |
| 7589 |
assert_ne!(dst1_mode & 0o111, 0o111); |
| 7590 |
|
| 7591 |
// 2. cp --no-preserve=mode --preserve=timestamps src dst2 -> mode should match dst1 (--no-preserve=mode not forgotten) |
| 7592 |
scene |
| 7593 |
.ucmd() |
| 7594 |
.args(&["--no-preserve=mode", "--preserve=timestamps", "src", "dst2"]) |
| 7595 |
.succeeds(); |
| 7596 |
let dst2_mode = at.metadata("dst2").mode() & 0o777; |
| 7597 |
assert_eq!(dst1_mode, dst2_mode); |
| 7598 |
|
| 7599 |
// 3. cp --no-preserve=all --preserve=timestamps src dst3 -> mode should match dst1 |
| 7600 |
scene |
| 7601 |
.ucmd() |
| 7602 |
.args(&["--no-preserve=all", "--preserve=timestamps", "src", "dst3"]) |
| 7603 |
.succeeds(); |
| 7604 |
let dst3_mode = at.metadata("dst3").mode() & 0o777; |
| 7605 |
assert_eq!(dst1_mode, dst3_mode); |
| 7606 |
|
| 7607 |
// 4. cp --preserve=timestamps --no-preserve=mode src dst4 -> mode should match dst1 |
| 7608 |
scene |
| 7609 |
.ucmd() |
| 7610 |
.args(&["--preserve=timestamps", "--no-preserve=mode", "src", "dst4"]) |
| 7611 |
.succeeds(); |
| 7612 |
let dst4_mode = at.metadata("dst4").mode() & 0o777; |
| 7613 |
assert_eq!(dst1_mode, dst4_mode); |
| 7614 |
|
| 7615 |
// 5. cp --no-preserve=mode --preserve=mode src dst5 -> mode should be preserved (0o755) because later flag wins |
| 7616 |
scene |
| 7617 |
.ucmd() |
| 7618 |
.args(&["--no-preserve=mode", "--preserve=mode", "src", "dst5"]) |
| 7619 |
.succeeds(); |
| 7620 |
let dst5_mode = at.metadata("dst5").mode() & 0o777; |
| 7621 |
assert_eq!(dst5_mode, 0o755); |
| 7622 |
} |
| 7623 |
|
| 7624 |
/// Test the behavior of preserving permissions when copying through a symlink |
| 7625 |
#[test] |
| 7626 |
#[cfg(unix)] |
| 7627 |
#[cfg_attr( |
| 7628 |
wasi_runner, |
| 7629 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 7630 |
)] |
| 7631 |
fn test_cp_symlink_permissions() { |
| 7632 |
let scene = TestScenario::new(util_name!()); |
| 7633 |
let at = &scene.fixtures; |
| 7634 |
at.touch("a"); |
| 7635 |
at.set_mode("a", 0o700); |
| 7636 |
at.symlink_file("a", "symlink"); |
| 7637 |
at.mkdir("dest"); |
| 7638 |
scene |
| 7639 |
.ucmd() |
| 7640 |
.args(&["--preserve", "symlink", "dest"]) |
| 7641 |
.succeeds(); |
| 7642 |
let dest_dir_metadata = at.metadata("dest/symlink"); |
| 7643 |
let src_dir_metadata = at.metadata("a"); |
| 7644 |
assert_eq!( |
| 7645 |
src_dir_metadata.permissions().mode(), |
| 7646 |
dest_dir_metadata.permissions().mode() |
| 7647 |
); |
| 7648 |
} |
| 7649 |
|
| 7650 |
/// Test the behavior of preserving permissions of parents when copying through a symlink |
| 7651 |
#[test] |
| 7652 |
#[cfg(unix)] |
| 7653 |
#[cfg_attr( |
| 7654 |
wasi_runner, |
| 7655 |
ignore = "WASI: no chmod syscall, so required mode/ownership preservation always fails" |
| 7656 |
)] |
| 7657 |
fn test_cp_parents_symlink_permissions_file() { |
| 7658 |
let scene = TestScenario::new(util_name!()); |
| 7659 |
let at = &scene.fixtures; |
| 7660 |
at.mkdir("a"); |
| 7661 |
at.touch("a/file"); |
| 7662 |
at.set_mode("a", 0o700); |
| 7663 |
at.symlink_dir("a", "symlink"); |
| 7664 |
at.mkdir("dest"); |
| 7665 |
scene |
| 7666 |
.ucmd() |
| 7667 |
.args(&["--parents", "-a", "symlink/file", "dest"]) |
| 7668 |
.succeeds(); |
| 7669 |
let dest_dir_metadata = at.metadata("dest/symlink"); |
| 7670 |
let src_dir_metadata = at.metadata("a"); |
| 7671 |
assert_eq!( |
| 7672 |
src_dir_metadata.permissions().mode(), |
| 7673 |
dest_dir_metadata.permissions().mode() |
| 7674 |
); |
| 7675 |
} |
| 7676 |
|
| 7677 |
/// A destination subdirectory that is really a symlink must not be descended |
| 7678 |
/// into: doing so writes the source subtree through the link and out of the |
| 7679 |
/// destination tree. GNU refuses with "cannot overwrite non-directory ... with |
| 7680 |
/// directory". |
| 7681 |
#[test] |
| 7682 |
#[cfg(unix)] |
| 7683 |
fn test_cp_recursive_dest_subdir_symlink_not_followed() { |
| 7684 |
let scene = TestScenario::new(util_name!()); |
| 7685 |
let at = &scene.fixtures; |
| 7686 |
at.mkdir_all("src/hooks"); |
| 7687 |
at.write("src/hooks/payload", "PAYLOAD"); |
| 7688 |
at.mkdir("dst"); |
| 7689 |
at.mkdir("outside"); |
| 7690 |
at.symlink_dir("../outside", "dst/hooks"); |
| 7691 |
|
| 7692 |
scene |
| 7693 |
.ucmd() |
| 7694 |
.args(&["-a", "src/.", "dst"]) |
| 7695 |
.fails() |
| 7696 |
.stderr_contains("cannot overwrite non-directory"); |
| 7697 |
|
| 7698 |
assert!( |
| 7699 |
!at.file_exists("outside/payload"), |
| 7700 |
"cp wrote through the destination symlink and escaped the target tree" |
| 7701 |
); |
| 7702 |
} |
| 7703 |
|
| 7704 |
/// A symlinked directory named as the *target* is still a legitimate |
| 7705 |
/// destination -- only entries discovered inside the tree are refused. |
| 7706 |
#[test] |
| 7707 |
#[cfg(unix)] |
| 7708 |
#[cfg_attr( |
| 7709 |
wasi_runner, |
| 7710 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 7711 |
)] |
| 7712 |
fn test_cp_recursive_target_dir_symlink_still_allowed() { |
| 7713 |
let scene = TestScenario::new(util_name!()); |
| 7714 |
let at = &scene.fixtures; |
| 7715 |
at.mkdir_all("srcdir"); |
| 7716 |
at.write("srcdir/f", "X"); |
| 7717 |
at.mkdir("real"); |
| 7718 |
at.symlink_dir("real", "dstlink"); |
| 7719 |
|
| 7720 |
scene.ucmd().args(&["-r", "srcdir", "dstlink/"]).succeeds(); |
| 7721 |
|
| 7722 |
assert!(at.file_exists("real/srcdir/f")); |
| 7723 |
} |
| 7724 |
|
| 7725 |
/// Test the behavior of preserving permissions of parents when copying through |
| 7726 |
/// a symlink when source is a dir. |
| 7727 |
#[test] |
| 7728 |
#[cfg(unix)] |
| 7729 |
#[cfg_attr( |
| 7730 |
wasi_runner, |
| 7731 |
ignore = "WASI: no chmod syscall, so required mode/ownership preservation always fails" |
| 7732 |
)] |
| 7733 |
fn test_cp_parents_symlink_permissions_dir() { |
| 7734 |
let scene = TestScenario::new(util_name!()); |
| 7735 |
let at = &scene.fixtures; |
| 7736 |
at.mkdir_all("a/b"); |
| 7737 |
at.set_mode("a", 0o755); // Set mode for the actual directory |
| 7738 |
at.symlink_dir("a", "symlink"); |
| 7739 |
at.mkdir("dest"); |
| 7740 |
scene |
| 7741 |
.ucmd() |
| 7742 |
.args(&["--parents", "-a", "symlink/b", "dest"]) |
| 7743 |
.succeeds(); |
| 7744 |
let dest_dir_metadata = at.metadata("dest/symlink"); |
| 7745 |
let src_dir_metadata = at.metadata("a"); |
| 7746 |
assert_eq!( |
| 7747 |
src_dir_metadata.permissions().mode(), |
| 7748 |
dest_dir_metadata.permissions().mode() |
| 7749 |
); |
| 7750 |
} |
| 7751 |
|
| 7752 |
/// Test the behavior of copying a file to a destination with parents using absolute paths. |
| 7753 |
#[cfg(unix)] |
| 7754 |
#[test] |
| 7755 |
#[cfg_attr( |
| 7756 |
wasi_runner, |
| 7757 |
ignore = "WASI sandbox: host paths (/dev, /private) not visible" |
| 7758 |
)] |
| 7759 |
fn test_cp_parents_absolute_path() { |
| 7760 |
let scene = TestScenario::new(util_name!()); |
| 7761 |
let at = &scene.fixtures; |
| 7762 |
at.mkdir_all("a/b"); |
| 7763 |
at.touch("a/b/f"); |
| 7764 |
at.mkdir("dest"); |
| 7765 |
let src = format!("{}/a/b/f", at.root_dir_resolved()); |
| 7766 |
scene |
| 7767 |
.ucmd() |
| 7768 |
.args(&["--parents", src.as_str(), "dest"]) |
| 7769 |
.succeeds(); |
| 7770 |
let res = format!("dest{}/a/b/f", at.root_dir_resolved()); |
| 7771 |
at.file_exists(res); |
| 7772 |
} |
| 7773 |
|
| 7774 |
#[test] |
| 7775 |
fn test_copy_symlink_overwrite() { |
| 7776 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 7777 |
at.mkdir("a"); |
| 7778 |
at.mkdir("b"); |
| 7779 |
at.mkdir("c"); |
| 7780 |
|
| 7781 |
at.write("t", "hello"); |
| 7782 |
at.relative_symlink_file("../t", "a/1"); |
| 7783 |
at.relative_symlink_file("../t", "b/1"); |
| 7784 |
|
| 7785 |
ucmd.arg("--no-dereference") |
| 7786 |
.arg("a/1") |
| 7787 |
.arg("b/1") |
| 7788 |
.arg("c") |
| 7789 |
.fails() |
| 7790 |
.stderr_only(if cfg!(not(windows)) { |
| 7791 |
"cp: will not overwrite just-created 'c/1' with 'b/1'\n" |
| 7792 |
} else { |
| 7793 |
"cp: will not overwrite just-created 'c\\1' with 'b/1'\n" |
| 7794 |
}); |
| 7795 |
} |
| 7796 |
|
| 7797 |
#[test] |
| 7798 |
fn test_symlink_mode_overwrite() { |
| 7799 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 7800 |
at.mkdir("a"); |
| 7801 |
at.mkdir("b"); |
| 7802 |
|
| 7803 |
at.write("a/t", "hello"); |
| 7804 |
at.write("b/t", "hello"); |
| 7805 |
|
| 7806 |
if cfg!(not(windows)) { |
| 7807 |
ucmd.arg("-s") |
| 7808 |
.arg("a/t") |
| 7809 |
.arg("b/t") |
| 7810 |
.arg(".") |
| 7811 |
.fails() |
| 7812 |
.stderr_only("cp: will not overwrite just-created './t' with 'b/t'\n"); |
| 7813 |
|
| 7814 |
assert_eq!(at.read("./t"), "hello"); |
| 7815 |
} else { |
| 7816 |
ucmd.arg("-s") |
| 7817 |
.arg("a\\t") |
| 7818 |
.arg("b\\t") |
| 7819 |
.arg(".") |
| 7820 |
.fails() |
| 7821 |
.stderr_only("cp: will not overwrite just-created '.\\t' with 'b\\t'\n"); |
| 7822 |
|
| 7823 |
assert_eq!(at.read(".\\t"), "hello"); |
| 7824 |
} |
| 7825 |
} |
| 7826 |
|
| 7827 |
// make sure that cp backup dest symlink before removing it. |
| 7828 |
#[test] |
| 7829 |
fn test_cp_with_options_backup_and_rem_when_dest_is_symlink() { |
| 7830 |
let scene = TestScenario::new(util_name!()); |
| 7831 |
let at = &scene.fixtures; |
| 7832 |
at.write("file", "xyz"); |
| 7833 |
at.mkdir("inner_dir"); |
| 7834 |
at.write("inner_dir/inner_file", "abc"); |
| 7835 |
at.relative_symlink_file("inner_file", "inner_dir/sl"); |
| 7836 |
scene |
| 7837 |
.ucmd() |
| 7838 |
.args(&["-b", "--rem", "file", "inner_dir/sl"]) |
| 7839 |
.succeeds(); |
| 7840 |
assert!(at.file_exists("inner_dir/inner_file")); |
| 7841 |
assert_eq!(at.read("inner_dir/inner_file"), "abc"); |
| 7842 |
assert!(at.symlink_exists("inner_dir/sl~")); |
| 7843 |
assert!(!at.symlink_exists("inner_dir/sl")); |
| 7844 |
assert_eq!(at.read("inner_dir/sl"), "xyz"); |
| 7845 |
} |
| 7846 |
|
| 7847 |
#[test] |
| 7848 |
fn test_cp_single_file() { |
| 7849 |
let (_at, mut ucmd) = at_and_ucmd!(); |
| 7850 |
ucmd.arg(TEST_HELLO_WORLD_SOURCE) |
| 7851 |
.fails_with_code(1) |
| 7852 |
.stderr_contains("missing destination file"); |
| 7853 |
} |
| 7854 |
|
| 7855 |
#[test] |
| 7856 |
fn test_cp_no_file() { |
| 7857 |
let (_at, mut ucmd) = at_and_ucmd!(); |
| 7858 |
ucmd.fails_with_code(1) |
| 7859 |
.stderr_contains("error: the following required arguments were not provided:"); |
| 7860 |
} |
| 7861 |
|
| 7862 |
#[test] |
| 7863 |
#[cfg(all( |
| 7864 |
unix, |
| 7865 |
not(any(target_vendor = "apple", target_os = "android", target_os = "openbsd")) |
| 7866 |
))] |
| 7867 |
fn test_cp_preserve_xattr_readonly_source() { |
| 7868 |
use std::process::Command; |
| 7869 |
use uutests::util::compare_xattrs; |
| 7870 |
|
| 7871 |
let scene = TestScenario::new(util_name!()); |
| 7872 |
let at = &scene.fixtures; |
| 7873 |
|
| 7874 |
let source_file = "a"; |
| 7875 |
let dest_file = "e"; |
| 7876 |
|
| 7877 |
at.touch(source_file); |
| 7878 |
|
| 7879 |
let xattr_key = "user.test"; |
| 7880 |
match Command::new("setfattr") |
| 7881 |
.args([ |
| 7882 |
"-n", |
| 7883 |
xattr_key, |
| 7884 |
"-v", |
| 7885 |
"value", |
| 7886 |
&at.plus_as_string(source_file), |
| 7887 |
]) |
| 7888 |
.status() |
| 7889 |
.map(|status| status.code()) |
| 7890 |
{ |
| 7891 |
Ok(Some(0)) => {} |
| 7892 |
Ok(_) => { |
| 7893 |
println!("test skipped: setfattr failed"); |
| 7894 |
return; |
| 7895 |
} |
| 7896 |
Err(e) => { |
| 7897 |
println!("test skipped: setfattr failed with {e}"); |
| 7898 |
return; |
| 7899 |
} |
| 7900 |
} |
| 7901 |
|
| 7902 |
let getfattr_output = Command::new("getfattr") |
| 7903 |
.args([&at.plus_as_string(source_file)]) |
| 7904 |
.output() |
| 7905 |
.expect("Failed to run `getfattr` on the destination file"); |
| 7906 |
|
| 7907 |
assert!( |
| 7908 |
getfattr_output.status.success(), |
| 7909 |
"getfattr did not run successfully: {}", |
| 7910 |
String::from_utf8_lossy(&getfattr_output.stderr) |
| 7911 |
); |
| 7912 |
|
| 7913 |
let stdout = String::from_utf8_lossy(&getfattr_output.stdout); |
| 7914 |
assert!( |
| 7915 |
stdout.contains(xattr_key), |
| 7916 |
"Expected '{xattr_key}' not found in getfattr output:\n{stdout}" |
| 7917 |
); |
| 7918 |
|
| 7919 |
at.set_readonly(source_file); |
| 7920 |
assert!( |
| 7921 |
scene |
| 7922 |
.fixtures |
| 7923 |
.metadata(source_file) |
| 7924 |
.permissions() |
| 7925 |
.readonly() |
| 7926 |
); |
| 7927 |
|
| 7928 |
scene |
| 7929 |
.ucmd() |
| 7930 |
.args(&[ |
| 7931 |
"--preserve=xattr", |
| 7932 |
&at.plus_as_string(source_file), |
| 7933 |
&at.plus_as_string(dest_file), |
| 7934 |
]) |
| 7935 |
.succeeds() |
| 7936 |
.no_output(); |
| 7937 |
|
| 7938 |
assert!(scene.fixtures.metadata(dest_file).permissions().readonly()); |
| 7939 |
assert!( |
| 7940 |
compare_xattrs(&at.plus(source_file), &at.plus(dest_file)), |
| 7941 |
"Extended attributes were not preserved" |
| 7942 |
); |
| 7943 |
} |
| 7944 |
|
| 7945 |
#[test] |
| 7946 |
#[cfg(unix)] |
| 7947 |
#[cfg_attr( |
| 7948 |
wasi_runner, |
| 7949 |
ignore = "WASI: no chmod syscall, so required mode/ownership preservation always fails" |
| 7950 |
)] |
| 7951 |
fn test_cp_archive_preserves_directory_permissions() { |
| 7952 |
// Test for issue #8407 |
| 7953 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 7954 |
|
| 7955 |
at.mkdir("test-images"); |
| 7956 |
|
| 7957 |
let subdirs = ["fail", "gif-test-suite", "randomly-modified", "reftests"]; |
| 7958 |
let mode = 0o755; |
| 7959 |
|
| 7960 |
for (i, subdir) in subdirs.iter().enumerate() { |
| 7961 |
let path = format!("test-images/{subdir}"); |
| 7962 |
at.mkdir(&path); |
| 7963 |
at.set_mode(&path, mode); |
| 7964 |
at.write(&format!("{path}/test{}.txt", i + 1), "test content"); |
| 7965 |
} |
| 7966 |
|
| 7967 |
ucmd.arg("-a") |
| 7968 |
.arg("test-images") |
| 7969 |
.arg("test-images-copy") |
| 7970 |
.succeeds(); |
| 7971 |
|
| 7972 |
let check_mode = |path: &str| { |
| 7973 |
let metadata = at.metadata(path); |
| 7974 |
let mode = metadata.permissions().mode(); |
| 7975 |
// Check that the permissions are 755 (only checking the last 9 bits) |
| 7976 |
assert_eq!( |
| 7977 |
mode & 0o777, |
| 7978 |
0o755, |
| 7979 |
"Directory {path} has incorrect permissions: {:o}", |
| 7980 |
mode & 0o777 |
| 7981 |
); |
| 7982 |
}; |
| 7983 |
|
| 7984 |
for subdir in subdirs { |
| 7985 |
check_mode(&format!("test-images-copy/{subdir}")); |
| 7986 |
} |
| 7987 |
} |
| 7988 |
|
| 7989 |
#[test] |
| 7990 |
#[cfg(unix)] |
| 7991 |
#[cfg_attr(target_vendor = "apple", ignore = "Flaky on MacOS, see #8453")] |
| 7992 |
#[cfg_attr( |
| 7993 |
wasi_runner, |
| 7994 |
ignore = "WASI sandbox: host paths (/dev, /private) not visible" |
| 7995 |
)] |
| 7996 |
fn test_cp_from_stdin() { |
| 7997 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 7998 |
let target = "target"; |
| 7999 |
let test_string = "Hello, World!\n"; |
| 8000 |
|
| 8001 |
ucmd.arg("/dev/fd/0") |
| 8002 |
.arg(target) |
| 8003 |
.pipe_in(test_string) |
| 8004 |
.succeeds(); |
| 8005 |
|
| 8006 |
assert!(at.file_exists(target)); |
| 8007 |
assert_eq!(at.read(target), test_string); |
| 8008 |
} |
| 8009 |
|
| 8010 |
#[test] |
| 8011 |
fn test_cp_update_older_interactive_prompt_yes() { |
| 8012 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 8013 |
let old_file = "old"; |
| 8014 |
let new_file = "new"; |
| 8015 |
|
| 8016 |
let f = at.make_file(old_file); |
| 8017 |
f.set_modified(std::time::UNIX_EPOCH).unwrap(); |
| 8018 |
at.touch(new_file); |
| 8019 |
|
| 8020 |
ucmd.args(&["-i", "-v", "--update=older", new_file, old_file]) |
| 8021 |
.pipe_in("Y\n") |
| 8022 |
.stderr_to_stdout() |
| 8023 |
.succeeds() |
| 8024 |
.stdout_is("cp: overwrite 'old'? 'new' -> 'old'\n"); |
| 8025 |
} |
| 8026 |
|
| 8027 |
#[test] |
| 8028 |
fn test_cp_update_older_interactive_prompt_no() { |
| 8029 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 8030 |
let old_file = "old"; |
| 8031 |
let new_file = "new"; |
| 8032 |
|
| 8033 |
let f = at.make_file(old_file); |
| 8034 |
f.set_modified(std::time::UNIX_EPOCH).unwrap(); |
| 8035 |
at.touch(new_file); |
| 8036 |
|
| 8037 |
ucmd.args(&["-i", "-v", "--update=older", new_file, old_file]) |
| 8038 |
.pipe_in("N\n") |
| 8039 |
.stderr_to_stdout() |
| 8040 |
.fails() |
| 8041 |
.stdout_is("cp: overwrite 'old'? "); |
| 8042 |
} |
| 8043 |
|
| 8044 |
#[test] |
| 8045 |
fn test_cp_update_none_interactive_prompt_no() { |
| 8046 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 8047 |
let old_file = "old"; |
| 8048 |
let new_file = "new"; |
| 8049 |
|
| 8050 |
at.write(old_file, "old content"); |
| 8051 |
at.write(new_file, "new content"); |
| 8052 |
|
| 8053 |
ucmd.args(&["-i", "--update=none", new_file, old_file]) |
| 8054 |
.succeeds() |
| 8055 |
.no_output(); |
| 8056 |
|
| 8057 |
assert_eq!(at.read(old_file), "old content"); |
| 8058 |
assert_eq!(at.read(new_file), "new content"); |
| 8059 |
} |
| 8060 |
|
| 8061 |
/// only unix has `/dev/fd/0` |
| 8062 |
#[cfg(unix)] |
| 8063 |
#[cfg_attr(target_vendor = "apple", ignore = "Flaky on MacOS, see #8453")] |
| 8064 |
#[cfg_attr( |
| 8065 |
wasi_runner, |
| 8066 |
ignore = "WASI sandbox: host paths (/dev, /private) not visible" |
| 8067 |
)] |
| 8068 |
#[test] |
| 8069 |
fn test_cp_from_stream() { |
| 8070 |
let target = "target"; |
| 8071 |
let test_string1 = "longer: Hello, World!\n"; |
| 8072 |
let test_string2 = "shorter"; |
| 8073 |
let scenario = TestScenario::new(util_name!()); |
| 8074 |
let at = &scenario.fixtures; |
| 8075 |
at.touch(target); |
| 8076 |
|
| 8077 |
let mut ucmd = scenario.ucmd(); |
| 8078 |
ucmd.arg("/dev/fd/0") |
| 8079 |
.arg(target) |
| 8080 |
.pipe_in(test_string1) |
| 8081 |
.succeeds(); |
| 8082 |
assert_eq!(at.read(target), test_string1); |
| 8083 |
|
| 8084 |
let mut ucmd = scenario.ucmd(); |
| 8085 |
ucmd.arg("/dev/fd/0") |
| 8086 |
.arg(target) |
| 8087 |
.pipe_in(test_string2) |
| 8088 |
.succeeds(); |
| 8089 |
assert_eq!(at.read(target), test_string2); |
| 8090 |
} |
| 8091 |
|
| 8092 |
/// only unix has `/dev/fd/0` |
| 8093 |
#[cfg(unix)] |
| 8094 |
#[cfg_attr(target_vendor = "apple", ignore = "Flaky on MacOS, see #8453")] |
| 8095 |
#[cfg_attr( |
| 8096 |
wasi_runner, |
| 8097 |
ignore = "WASI sandbox: host paths (/dev, /private) not visible" |
| 8098 |
)] |
| 8099 |
#[test] |
| 8100 |
fn test_cp_from_stream_permission() { |
| 8101 |
let target = "target"; |
| 8102 |
let link = "link"; |
| 8103 |
let test_string = "Hello, World!\n"; |
| 8104 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 8105 |
|
| 8106 |
at.touch(target); |
| 8107 |
at.symlink_file(target, link); |
| 8108 |
let mode = 0o777; |
| 8109 |
at.set_mode("target", mode); |
| 8110 |
|
| 8111 |
ucmd.arg("/dev/fd/0") |
| 8112 |
.arg(link) |
| 8113 |
.pipe_in(test_string) |
| 8114 |
.succeeds(); |
| 8115 |
|
| 8116 |
assert_eq!(at.read(target), test_string); |
| 8117 |
assert_eq!(at.metadata(target).permissions().mode(), 0o100_777); |
| 8118 |
} |
| 8119 |
|
| 8120 |
#[test] |
| 8121 |
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))] |
| 8122 |
fn test_cp_selinux() { |
| 8123 |
let ts = TestScenario::new(util_name!()); |
| 8124 |
let at = &ts.fixtures; |
| 8125 |
let args = ["-Z", "--context=unconfined_u:object_r:user_tmp_t:s0"]; |
| 8126 |
at.touch(TEST_HELLO_WORLD_SOURCE); |
| 8127 |
for arg in args { |
| 8128 |
ts.ucmd() |
| 8129 |
.arg(arg) |
| 8130 |
.arg(TEST_HELLO_WORLD_SOURCE) |
| 8131 |
.arg(TEST_HELLO_WORLD_DEST) |
| 8132 |
.succeeds(); |
| 8133 |
assert!(at.file_exists(TEST_HELLO_WORLD_DEST)); |
| 8134 |
|
| 8135 |
let selinux_perm = get_getfattr_output(&at.plus_as_string(TEST_HELLO_WORLD_DEST)); |
| 8136 |
|
| 8137 |
assert!( |
| 8138 |
selinux_perm.contains("unconfined_u"), |
| 8139 |
"Expected 'foo' not found in getfattr output:\n{selinux_perm}" |
| 8140 |
); |
| 8141 |
at.remove(&at.plus_as_string(TEST_HELLO_WORLD_DEST)); |
| 8142 |
} |
| 8143 |
} |
| 8144 |
|
| 8145 |
#[test] |
| 8146 |
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))] |
| 8147 |
fn test_cp_selinux_default_context_relative_dest() { |
| 8148 |
// -Z labels the destination with the context the policy has for its path, |
| 8149 |
// and the policy only lists absolute paths: a relative destination used to |
| 8150 |
// match nothing and silently keep the context it already carried. |
| 8151 |
use std::path::Path; |
| 8152 |
use uucore::selinux::set_selinux_security_context; |
| 8153 |
|
| 8154 |
let ts = TestScenario::new(util_name!()); |
| 8155 |
let at = &ts.fixtures; |
| 8156 |
at.touch(TEST_HELLO_WORLD_SOURCE); |
| 8157 |
|
| 8158 |
// A type the policy will not hand out for a path under the test directory, |
| 8159 |
// so that the comparison below still sees a difference when -Z leaves the |
| 8160 |
// relative destination alone. Usable even when mcstransd is not running. |
| 8161 |
let ctx = "root:object_r:etc_t:s0".to_string(); |
| 8162 |
for dest in ["relative", "absolute"] { |
| 8163 |
at.touch(dest); |
| 8164 |
if set_selinux_security_context(Path::new(&at.plus_as_string(dest)), Some(&ctx)).is_err() { |
| 8165 |
return; |
| 8166 |
} |
| 8167 |
} |
| 8168 |
|
| 8169 |
let absolute = at.plus_as_string("absolute"); |
| 8170 |
ts.ucmd() |
| 8171 |
.args(&["-Z", TEST_HELLO_WORLD_SOURCE, &absolute]) |
| 8172 |
.succeeds(); |
| 8173 |
ts.ucmd() |
| 8174 |
.args(&["-Z", TEST_HELLO_WORLD_SOURCE, "relative"]) |
| 8175 |
.succeeds(); |
| 8176 |
|
| 8177 |
// Compare the type only, the one field -Z is about. |
| 8178 |
let selinux_type = |context: &str| context.split(':').nth(2).unwrap_or("").to_string(); |
| 8179 |
assert_eq!( |
| 8180 |
selinux_type(&get_getfattr_output(&at.plus_as_string("relative"))), |
| 8181 |
selinux_type(&get_getfattr_output(&absolute)), |
| 8182 |
"-Z gave a relative and an absolute destination different contexts" |
| 8183 |
); |
| 8184 |
} |
| 8185 |
|
| 8186 |
#[test] |
| 8187 |
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))] |
| 8188 |
fn test_cp_selinux_invalid() { |
| 8189 |
let scene = TestScenario::new(util_name!()); |
| 8190 |
let at = &scene.fixtures; |
| 8191 |
at.touch(TEST_HELLO_WORLD_SOURCE); |
| 8192 |
let args = [ |
| 8193 |
"--context=a", |
| 8194 |
"--context=unconfined_u:object_r:user_tmp_t:s0:a", |
| 8195 |
"--context=nconfined_u:object_r:user_tmp_t:s0", |
| 8196 |
]; |
| 8197 |
for arg in args { |
| 8198 |
new_ucmd!() |
| 8199 |
.arg(arg) |
| 8200 |
.arg(TEST_HELLO_WORLD_SOURCE) |
| 8201 |
.arg(TEST_HELLO_WORLD_DEST) |
| 8202 |
.fails() |
| 8203 |
.stderr_contains("failed to"); |
| 8204 |
if at.file_exists(TEST_HELLO_WORLD_DEST) { |
| 8205 |
at.remove(TEST_HELLO_WORLD_DEST); |
| 8206 |
} |
| 8207 |
} |
| 8208 |
} |
| 8209 |
|
| 8210 |
#[test] |
| 8211 |
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))] |
| 8212 |
fn test_cp_preserve_selinux() { |
| 8213 |
let ts = TestScenario::new(util_name!()); |
| 8214 |
let at = &ts.fixtures; |
| 8215 |
let args = ["-Z", "--context=unconfined_u:object_r:user_tmp_t:s0"]; |
| 8216 |
at.touch(TEST_HELLO_WORLD_SOURCE); |
| 8217 |
for arg in args { |
| 8218 |
ts.ucmd() |
| 8219 |
.arg(arg) |
| 8220 |
.arg(TEST_HELLO_WORLD_SOURCE) |
| 8221 |
.arg(TEST_HELLO_WORLD_DEST) |
| 8222 |
.arg("--preserve=all") |
| 8223 |
.succeeds(); |
| 8224 |
assert!(at.file_exists(TEST_HELLO_WORLD_DEST)); |
| 8225 |
let selinux_perm_dest = get_getfattr_output(&at.plus_as_string(TEST_HELLO_WORLD_DEST)); |
| 8226 |
assert!( |
| 8227 |
selinux_perm_dest.contains("unconfined_u"), |
| 8228 |
"Expected 'foo' not found in getfattr output:\n{selinux_perm_dest}" |
| 8229 |
); |
| 8230 |
assert_eq!( |
| 8231 |
get_getfattr_output(&at.plus_as_string(TEST_HELLO_WORLD_SOURCE)), |
| 8232 |
selinux_perm_dest |
| 8233 |
); |
| 8234 |
|
| 8235 |
#[cfg(all(unix, not(target_os = "freebsd"), not(target_os = "openbsd")))] |
| 8236 |
{ |
| 8237 |
// Assert that the mode, ownership, and timestamps are preserved |
| 8238 |
// NOTICE: the ownership is not modified on the src file, because that requires root permissions |
| 8239 |
let metadata_src = at.metadata(TEST_HELLO_WORLD_SOURCE); |
| 8240 |
let metadata_dst = at.metadata(TEST_HELLO_WORLD_DEST); |
| 8241 |
assert_metadata_eq!(metadata_src, metadata_dst); |
| 8242 |
} |
| 8243 |
|
| 8244 |
at.remove(&at.plus_as_string(TEST_HELLO_WORLD_DEST)); |
| 8245 |
} |
| 8246 |
} |
| 8247 |
|
| 8248 |
#[test] |
| 8249 |
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))] |
| 8250 |
fn test_cp_preserve_selinux_admin_context() { |
| 8251 |
let ts = TestScenario::new(util_name!()); |
| 8252 |
let at = &ts.fixtures; |
| 8253 |
|
| 8254 |
at.touch(TEST_HELLO_WORLD_SOURCE); |
| 8255 |
|
| 8256 |
// Get the default SELinux context for the destination file path |
| 8257 |
// On Debian/Ubuntu, this program is provided by the selinux-utils package |
| 8258 |
// On Fedora/RHEL, this program is provided by the libselinux-devel package |
| 8259 |
let output = std::process::Command::new("matchpathcon") |
| 8260 |
.arg(at.plus_as_string(TEST_HELLO_WORLD_DEST)) |
| 8261 |
.output() |
| 8262 |
.expect("failed to execute matchpathcon command"); |
| 8263 |
|
| 8264 |
assert!( |
| 8265 |
output.status.success(), |
| 8266 |
"matchpathcon command failed: {}", |
| 8267 |
String::from_utf8_lossy(&output.stderr) |
| 8268 |
); |
| 8269 |
|
| 8270 |
let output_str = String::from_utf8_lossy(&output.stdout); |
| 8271 |
let default_context = output_str |
| 8272 |
.split_whitespace() |
| 8273 |
.nth(1) |
| 8274 |
.unwrap_or_default() |
| 8275 |
.to_string(); |
| 8276 |
|
| 8277 |
assert!( |
| 8278 |
!default_context.is_empty(), |
| 8279 |
"Unable to determine default SELinux context for the test file" |
| 8280 |
); |
| 8281 |
|
| 8282 |
let cmd_result = ts |
| 8283 |
.ucmd() |
| 8284 |
.arg("-Z") |
| 8285 |
.arg(format!("--context={default_context}")) |
| 8286 |
.arg(TEST_HELLO_WORLD_SOURCE) |
| 8287 |
.arg(TEST_HELLO_WORLD_DEST) |
| 8288 |
.run(); |
| 8289 |
|
| 8290 |
println!("cp command result: {cmd_result:?}"); |
| 8291 |
|
| 8292 |
if !cmd_result.succeeded() { |
| 8293 |
println!("Skipping test: Cannot set SELinux context, system may not support this context"); |
| 8294 |
return; |
| 8295 |
} |
| 8296 |
|
| 8297 |
assert!(at.file_exists(TEST_HELLO_WORLD_DEST)); |
| 8298 |
|
| 8299 |
let selinux_perm_dest = get_getfattr_output(&at.plus_as_string(TEST_HELLO_WORLD_DEST)); |
| 8300 |
println!("Destination SELinux context: {selinux_perm_dest}"); |
| 8301 |
|
| 8302 |
assert_eq!(default_context, selinux_perm_dest); |
| 8303 |
|
| 8304 |
at.remove(&at.plus_as_string(TEST_HELLO_WORLD_DEST)); |
| 8305 |
} |
| 8306 |
|
| 8307 |
#[test] |
| 8308 |
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))] |
| 8309 |
fn test_cp_selinux_context_priority() { |
| 8310 |
// This test verifies that -Z takes priority over --context |
| 8311 |
|
| 8312 |
let ts = TestScenario::new(util_name!()); |
| 8313 |
let at = &ts.fixtures; |
| 8314 |
|
| 8315 |
at.write(TEST_HELLO_WORLD_SOURCE, "source content"); |
| 8316 |
|
| 8317 |
// First, set a known context on source file (only if system supports it) |
| 8318 |
let setup_result = ts |
| 8319 |
.ucmd() |
| 8320 |
.arg("--context=unconfined_u:object_r:user_tmp_t:s0") |
| 8321 |
.arg(TEST_HELLO_WORLD_SOURCE) |
| 8322 |
.arg("initial_context.txt") |
| 8323 |
.run(); |
| 8324 |
|
| 8325 |
// If the system doesn't support setting contexts, skip the test |
| 8326 |
if !setup_result.succeeded() { |
| 8327 |
println!("Skipping test: System doesn't support setting SELinux contexts"); |
| 8328 |
return; |
| 8329 |
} |
| 8330 |
|
| 8331 |
// Create different copies with different context options |
| 8332 |
|
| 8333 |
// 1. Using --preserve=context |
| 8334 |
ts.ucmd() |
| 8335 |
.arg("--preserve=context") |
| 8336 |
.arg(TEST_HELLO_WORLD_SOURCE) |
| 8337 |
.arg("preserve.txt") |
| 8338 |
.succeeds(); |
| 8339 |
|
| 8340 |
// 2. Using --context with a different context (we already know this works from setup) |
| 8341 |
ts.ucmd() |
| 8342 |
.arg("--context=unconfined_u:object_r:user_tmp_t:s0") |
| 8343 |
.arg(TEST_HELLO_WORLD_SOURCE) |
| 8344 |
.arg("context.txt") |
| 8345 |
.succeeds(); |
| 8346 |
|
| 8347 |
// 3. Using -Z (should use default type context) |
| 8348 |
ts.ucmd() |
| 8349 |
.arg("-Z") |
| 8350 |
.arg(TEST_HELLO_WORLD_SOURCE) |
| 8351 |
.arg("z_flag.txt") |
| 8352 |
.succeeds(); |
| 8353 |
|
| 8354 |
// 4. Using both -Z and --context (Z should win) |
| 8355 |
ts.ucmd() |
| 8356 |
.arg("-Z") |
| 8357 |
.arg("--context=unconfined_u:object_r:user_tmp_t:s0") |
| 8358 |
.arg(TEST_HELLO_WORLD_SOURCE) |
| 8359 |
.arg("z_and_context.txt") |
| 8360 |
.succeeds(); |
| 8361 |
|
| 8362 |
// Get all the contexts |
| 8363 |
let source_ctx = get_getfattr_output(&at.plus_as_string(TEST_HELLO_WORLD_SOURCE)); |
| 8364 |
let preserve_ctx = get_getfattr_output(&at.plus_as_string("preserve.txt")); |
| 8365 |
let context_ctx = get_getfattr_output(&at.plus_as_string("context.txt")); |
| 8366 |
let z_ctx = get_getfattr_output(&at.plus_as_string("z_flag.txt")); |
| 8367 |
let z_and_context_ctx = get_getfattr_output(&at.plus_as_string("z_and_context.txt")); |
| 8368 |
|
| 8369 |
if source_ctx.is_empty() { |
| 8370 |
println!("Skipping test assertions: Failed to get SELinux contexts"); |
| 8371 |
return; |
| 8372 |
} |
| 8373 |
assert_eq!( |
| 8374 |
source_ctx, preserve_ctx, |
| 8375 |
"--preserve=context should match the source context" |
| 8376 |
); |
| 8377 |
assert_eq!( |
| 8378 |
source_ctx, context_ctx, |
| 8379 |
"--preserve=context should match the source context" |
| 8380 |
); |
| 8381 |
assert_eq!( |
| 8382 |
z_ctx, z_and_context_ctx, |
| 8383 |
"-Z context should be the same regardless of --context" |
| 8384 |
); |
| 8385 |
} |
| 8386 |
|
| 8387 |
#[test] |
| 8388 |
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))] |
| 8389 |
fn test_cp_selinux_empty_context() { |
| 8390 |
// This test verifies that --context without a value works like -Z |
| 8391 |
|
| 8392 |
let ts = TestScenario::new(util_name!()); |
| 8393 |
let at = &ts.fixtures; |
| 8394 |
at.write(TEST_HELLO_WORLD_SOURCE, "test content"); |
| 8395 |
|
| 8396 |
// Try creating copies - if this fails, the system doesn't support SELinux properly |
| 8397 |
let z_result = ts |
| 8398 |
.ucmd() |
| 8399 |
.arg("-Z") |
| 8400 |
.arg(TEST_HELLO_WORLD_SOURCE) |
| 8401 |
.arg("z_flag.txt") |
| 8402 |
.run(); |
| 8403 |
|
| 8404 |
if !z_result.succeeded() { |
| 8405 |
println!("Skipping test: SELinux contexts not supported"); |
| 8406 |
return; |
| 8407 |
} |
| 8408 |
|
| 8409 |
// Now try with --context (no value) |
| 8410 |
let context_result = ts |
| 8411 |
.ucmd() |
| 8412 |
.arg("--context") |
| 8413 |
.arg(TEST_HELLO_WORLD_SOURCE) |
| 8414 |
.arg("empty_context.txt") |
| 8415 |
.run(); |
| 8416 |
|
| 8417 |
if !context_result.succeeded() { |
| 8418 |
println!("Skipping test: Empty context parameter not supported"); |
| 8419 |
return; |
| 8420 |
} |
| 8421 |
|
| 8422 |
let z_ctx = get_getfattr_output(&at.plus_as_string("z_flag.txt")); |
| 8423 |
let empty_ctx = get_getfattr_output(&at.plus_as_string("empty_context.txt")); |
| 8424 |
|
| 8425 |
if !z_ctx.is_empty() && !empty_ctx.is_empty() { |
| 8426 |
assert_eq!( |
| 8427 |
z_ctx, empty_ctx, |
| 8428 |
"--context without a value should behave like -Z" |
| 8429 |
); |
| 8430 |
} |
| 8431 |
} |
| 8432 |
|
| 8433 |
#[test] |
| 8434 |
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))] |
| 8435 |
fn test_cp_selinux_recursive() { |
| 8436 |
// Test SELinux context preservation in recursive directory copies |
| 8437 |
|
| 8438 |
let ts = TestScenario::new(util_name!()); |
| 8439 |
let at = &ts.fixtures; |
| 8440 |
|
| 8441 |
at.mkdir("source_dir"); |
| 8442 |
at.write("source_dir/file1.txt", "file1 content"); |
| 8443 |
at.mkdir("source_dir/subdir"); |
| 8444 |
at.write("source_dir/subdir/file2.txt", "file2 content"); |
| 8445 |
|
| 8446 |
let setup_result = ts |
| 8447 |
.ucmd() |
| 8448 |
.arg("--context=unconfined_u:object_r:user_tmp_t:s0") |
| 8449 |
.arg("source_dir/file1.txt") |
| 8450 |
.arg("source_dir/context_set.txt") |
| 8451 |
.run(); |
| 8452 |
|
| 8453 |
if !setup_result.succeeded() { |
| 8454 |
println!("Skipping test: System doesn't support setting SELinux contexts"); |
| 8455 |
return; |
| 8456 |
} |
| 8457 |
|
| 8458 |
ts.ucmd() |
| 8459 |
.arg("-rZ") |
| 8460 |
.arg("source_dir") |
| 8461 |
.arg("dest_dir_z") |
| 8462 |
.succeeds(); |
| 8463 |
|
| 8464 |
ts.ucmd() |
| 8465 |
.arg("-r") |
| 8466 |
.arg("--preserve=context") |
| 8467 |
.arg("source_dir") |
| 8468 |
.arg("dest_dir_preserve") |
| 8469 |
.succeeds(); |
| 8470 |
|
| 8471 |
let z_dir_ctx = get_getfattr_output(&at.plus_as_string("dest_dir_z")); |
| 8472 |
let preserve_dir_ctx = get_getfattr_output(&at.plus_as_string("dest_dir_preserve")); |
| 8473 |
|
| 8474 |
if !z_dir_ctx.is_empty() && !preserve_dir_ctx.is_empty() { |
| 8475 |
assert!( |
| 8476 |
z_dir_ctx.contains("_u:"), |
| 8477 |
"SELinux contexts not properly set with -Z flag" |
| 8478 |
); |
| 8479 |
|
| 8480 |
assert!( |
| 8481 |
preserve_dir_ctx.contains("_u:"), |
| 8482 |
"SELinux contexts not properly preserved with --preserve=context" |
| 8483 |
); |
| 8484 |
} |
| 8485 |
} |
| 8486 |
|
| 8487 |
#[test] |
| 8488 |
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))] |
| 8489 |
fn test_cp_preserve_context_root() { |
| 8490 |
use uutests::util::run_ucmd_as_root; |
| 8491 |
let scene = TestScenario::new(util_name!()); |
| 8492 |
let at = &scene.fixtures; |
| 8493 |
|
| 8494 |
let source_file = "c"; |
| 8495 |
let dest_file = "e"; |
| 8496 |
at.touch(source_file); |
| 8497 |
|
| 8498 |
let context = "root:object_r:tmp_t:s0"; |
| 8499 |
|
| 8500 |
let chcon_result = std::process::Command::new("chcon") |
| 8501 |
.arg(context) |
| 8502 |
.arg(at.plus_as_string(source_file)) |
| 8503 |
.status(); |
| 8504 |
|
| 8505 |
if !chcon_result.is_ok_and(|status| status.success()) { |
| 8506 |
println!("Skipping test: Failed to set context: {context}"); |
| 8507 |
return; |
| 8508 |
} |
| 8509 |
|
| 8510 |
// Copy the file with preserved context |
| 8511 |
// Only works as root |
| 8512 |
if let Ok(result) = run_ucmd_as_root(&scene, &["--preserve=context", source_file, dest_file]) { |
| 8513 |
let src_ctx = get_getfattr_output(&at.plus_as_string(source_file)); |
| 8514 |
let dest_ctx = get_getfattr_output(&at.plus_as_string(dest_file)); |
| 8515 |
println!("Source context: {src_ctx}"); |
| 8516 |
println!("Destination context: {dest_ctx}"); |
| 8517 |
|
| 8518 |
if !result.succeeded() { |
| 8519 |
println!("Skipping test: Failed to copy with preserved context"); |
| 8520 |
return; |
| 8521 |
} |
| 8522 |
|
| 8523 |
let dest_context = get_getfattr_output(&at.plus_as_string(dest_file)); |
| 8524 |
|
| 8525 |
assert!( |
| 8526 |
dest_context.contains("root:object_r:tmp_t"), |
| 8527 |
"Expected context '{context}' not found in destination context: '{dest_context}'", |
| 8528 |
); |
| 8529 |
} else { |
| 8530 |
print!("Test skipped; requires root user"); |
| 8531 |
} |
| 8532 |
} |
| 8533 |
|
| 8534 |
// Regression test for https://github.com/uutils/coreutils/issues/9105. |
| 8535 |
// Absolute operands must not require resolving the current working directory. |
| 8536 |
#[cfg(unix)] |
| 8537 |
#[rstest] |
| 8538 |
#[case::existing_target(true)] |
| 8539 |
#[case::new_target(false)] |
| 8540 |
#[cfg_attr( |
| 8541 |
wasi_runner, |
| 8542 |
ignore = "WASI: wasmtime cannot exec the binary via a raw shell from a deleted cwd" |
| 8543 |
)] |
| 8544 |
fn test_cp_absolute_paths_from_deleted_cwd(#[case] target_exists: bool) { |
| 8545 |
use std::process::Command; |
| 8546 |
|
| 8547 |
let ts = TestScenario::new(util_name!()); |
| 8548 |
let at = &ts.fixtures; |
| 8549 |
at.mkdir_all("src/sub"); |
| 8550 |
at.write("src/sub/file", "contents"); |
| 8551 |
at.mkdir("deleted-cwd"); |
| 8552 |
if target_exists { |
| 8553 |
at.mkdir("dst"); |
| 8554 |
} |
| 8555 |
|
| 8556 |
let source = at.plus("src"); |
| 8557 |
let target = at.plus("dst"); |
| 8558 |
let deleted_cwd = at.plus("deleted-cwd"); |
| 8559 |
let output = Command::new("sh") |
| 8560 |
.arg("-c") |
| 8561 |
.arg("cd \"$1\" && rmdir \"$1\" && exec \"$2\" \"$3\" -Ra --no-preserve=ownership \"$4\" \"$5\"") |
| 8562 |
.arg("sh") |
| 8563 |
.arg(&deleted_cwd) |
| 8564 |
.arg(&ts.bin_path) |
| 8565 |
.arg(&ts.util_name) |
| 8566 |
.arg(&source) |
| 8567 |
.arg(&target) |
| 8568 |
.output() |
| 8569 |
.unwrap(); |
| 8570 |
|
| 8571 |
assert!( |
| 8572 |
output.status.success(), |
| 8573 |
"cp failed from a deleted cwd: {}", |
| 8574 |
String::from_utf8_lossy(&output.stderr) |
| 8575 |
); |
| 8576 |
let copied_file = if target_exists { |
| 8577 |
target.join("src/sub/file") |
| 8578 |
} else { |
| 8579 |
target.join("sub/file") |
| 8580 |
}; |
| 8581 |
assert_eq!(std::fs::read_to_string(copied_file).unwrap(), "contents"); |
| 8582 |
} |
| 8583 |
|
| 8584 |
// Test copying current directory (.) to an existing directory. |
| 8585 |
// This tests the special case where we copy the current directory |
| 8586 |
// to an existing directory, ensuring the directory name is properly |
| 8587 |
// stripped from the descendant path. |
| 8588 |
#[test] |
| 8589 |
#[cfg_attr( |
| 8590 |
wasi_runner, |
| 8591 |
ignore = "WASI sandbox: relative '..' path canonicalization differs, causing a false self-copy detection" |
| 8592 |
)] |
| 8593 |
fn test_cp_current_directory_to_existing_directory() { |
| 8594 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 8595 |
|
| 8596 |
// Create source directory with files |
| 8597 |
at.mkdir("source_dir"); |
| 8598 |
at.touch("source_dir/file1.txt"); |
| 8599 |
at.touch("source_dir/file2.txt"); |
| 8600 |
at.mkdir("source_dir/subdir"); |
| 8601 |
at.touch("source_dir/subdir/file3.txt"); |
| 8602 |
|
| 8603 |
// Create existing destination directory |
| 8604 |
at.mkdir("dest_dir"); |
| 8605 |
|
| 8606 |
// Copy current directory (.) to existing directory |
| 8607 |
// This should copy the contents of source_dir to dest_dir |
| 8608 |
ucmd.current_dir(at.plus("source_dir")) |
| 8609 |
.args(&["-r", ".", "../dest_dir"]) |
| 8610 |
.succeeds(); |
| 8611 |
|
| 8612 |
// Verify files were copied correctly |
| 8613 |
assert!(at.file_exists("dest_dir/file1.txt")); |
| 8614 |
assert!(at.file_exists("dest_dir/file2.txt")); |
| 8615 |
assert!(at.dir_exists("dest_dir/subdir")); |
| 8616 |
assert!(at.file_exists("dest_dir/subdir/file3.txt")); |
| 8617 |
|
| 8618 |
// Verify the directory structure is correct (no extra nesting) |
| 8619 |
assert!(!at.file_exists("dest_dir/source_dir/file1.txt")); |
| 8620 |
} |
| 8621 |
|
| 8622 |
// Test copying current directory (.) to a new directory. |
| 8623 |
// This should create the new directory and copy contents. |
| 8624 |
#[test] |
| 8625 |
#[cfg_attr( |
| 8626 |
wasi_runner, |
| 8627 |
ignore = "WASI sandbox: relative '..' path canonicalization differs, causing a false self-copy detection" |
| 8628 |
)] |
| 8629 |
fn test_cp_current_directory_to_new_directory() { |
| 8630 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 8631 |
|
| 8632 |
// Create source directory with files |
| 8633 |
at.mkdir("source_dir"); |
| 8634 |
at.touch("source_dir/file1.txt"); |
| 8635 |
at.touch("source_dir/file2.txt"); |
| 8636 |
at.mkdir("source_dir/subdir"); |
| 8637 |
at.touch("source_dir/subdir/file3.txt"); |
| 8638 |
|
| 8639 |
// Copy current directory (.) to new directory |
| 8640 |
ucmd.current_dir(at.plus("source_dir")) |
| 8641 |
.args(&["-r", ".", "../new_dest_dir"]) |
| 8642 |
.succeeds(); |
| 8643 |
|
| 8644 |
// Verify the new directory was created |
| 8645 |
assert!(at.dir_exists("new_dest_dir")); |
| 8646 |
|
| 8647 |
// Verify files were copied correctly |
| 8648 |
assert!(at.file_exists("new_dest_dir/file1.txt")); |
| 8649 |
assert!(at.file_exists("new_dest_dir/file2.txt")); |
| 8650 |
assert!(at.dir_exists("new_dest_dir/subdir")); |
| 8651 |
assert!(at.file_exists("new_dest_dir/subdir/file3.txt")); |
| 8652 |
} |
| 8653 |
|
| 8654 |
// Regression test for Launchpad #2167118: |
| 8655 |
// When copying current directory (.), files or directories inside it that share |
| 8656 |
// the same name as the current directory itself must not be stripped to empty paths. |
| 8657 |
#[test] |
| 8658 |
#[cfg_attr( |
| 8659 |
wasi_runner, |
| 8660 |
ignore = "WASI sandbox: relative '..' path canonicalization differs, causing a false self-copy detection" |
| 8661 |
)] |
| 8662 |
fn test_cp_current_directory_with_entry_matching_parent_basename() { |
| 8663 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 8664 |
|
| 8665 |
at.mkdir("demo"); |
| 8666 |
at.touch("demo/file"); |
| 8667 |
at.touch("demo/demo"); // file has the same name as parent directory |
| 8668 |
|
| 8669 |
// Copy current directory (.) to a new directory (reproducer from LP #2167118) |
| 8670 |
ucmd.current_dir(at.plus("demo")) |
| 8671 |
.args(&["-R", ".", "../out"]) |
| 8672 |
.succeeds(); |
| 8673 |
|
| 8674 |
assert!(at.file_exists("out/file")); |
| 8675 |
assert!(at.file_exists("out/demo")); |
| 8676 |
|
| 8677 |
// Also test copying to an already existing destination directory |
| 8678 |
at.mkdir("existing_out"); |
| 8679 |
let mut ucmd2 = uutests::new_ucmd!(); |
| 8680 |
ucmd2 |
| 8681 |
.current_dir(at.plus("demo")) |
| 8682 |
.args(&["-R", ".", "../existing_out"]) |
| 8683 |
.succeeds(); |
| 8684 |
|
| 8685 |
assert!(at.file_exists("existing_out/file")); |
| 8686 |
assert!(at.file_exists("existing_out/demo")); |
| 8687 |
} |
| 8688 |
|
| 8689 |
// Test copying current directory (.) with verbose output. |
| 8690 |
// This ensures the verbose output shows the correct paths. |
| 8691 |
#[test] |
| 8692 |
#[cfg_attr( |
| 8693 |
wasi_runner, |
| 8694 |
ignore = "WASI sandbox: relative '..' path canonicalization differs, causing a false self-copy detection" |
| 8695 |
)] |
| 8696 |
fn test_cp_current_directory_verbose() { |
| 8697 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 8698 |
|
| 8699 |
// Create source directory with files |
| 8700 |
at.mkdir("source_dir"); |
| 8701 |
at.touch("source_dir/file1.txt"); |
| 8702 |
at.touch("source_dir/file2.txt"); |
| 8703 |
|
| 8704 |
// Create existing destination directory |
| 8705 |
at.mkdir("dest_dir"); |
| 8706 |
|
| 8707 |
// Copy current directory (.) to existing directory with verbose output |
| 8708 |
let result = ucmd |
| 8709 |
.current_dir(at.plus("source_dir")) |
| 8710 |
.args(&["-rv", ".", "../dest_dir"]) |
| 8711 |
.succeeds(); |
| 8712 |
|
| 8713 |
// Verify files were copied |
| 8714 |
assert!(at.file_exists("dest_dir/file1.txt")); |
| 8715 |
assert!(at.file_exists("dest_dir/file2.txt")); |
| 8716 |
|
| 8717 |
// Check that verbose output shows correct paths |
| 8718 |
let output = result.stdout_str(); |
| 8719 |
// The verbose output should show the files being copied |
| 8720 |
// The exact path format may vary, so we check for the file names |
| 8721 |
assert!(output.contains("file1.txt")); |
| 8722 |
assert!(output.contains("file2.txt")); |
| 8723 |
// Also check that the destination directory is mentioned |
| 8724 |
assert!(output.contains("dest_dir")); |
| 8725 |
} |
| 8726 |
|
| 8727 |
// Test copying current directory (.) with preserve attributes. |
| 8728 |
// This ensures attributes are preserved when copying the current directory. |
| 8729 |
#[test] |
| 8730 |
#[cfg(all(not(windows), not(target_os = "freebsd"), not(target_os = "openbsd")))] |
| 8731 |
#[cfg_attr( |
| 8732 |
wasi_runner, |
| 8733 |
ignore = "WASI sandbox: relative '..' path canonicalization differs, causing a false self-copy detection" |
| 8734 |
)] |
| 8735 |
fn test_cp_current_directory_preserve_attributes() { |
| 8736 |
use std::os::unix::prelude::MetadataExt; |
| 8737 |
|
| 8738 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 8739 |
|
| 8740 |
// Create source directory with files |
| 8741 |
at.mkdir("source_dir"); |
| 8742 |
at.touch("source_dir/file1.txt"); |
| 8743 |
at.touch("source_dir/file2.txt"); |
| 8744 |
|
| 8745 |
// Set specific permissions on the source files |
| 8746 |
at.set_mode("source_dir/file1.txt", 0o644); |
| 8747 |
at.set_mode("source_dir/file2.txt", 0o755); |
| 8748 |
|
| 8749 |
// Set specific timestamps on the source files (1 hour ago) |
| 8750 |
let previous = std::time::SystemTime::now() - Duration::from_secs(3600); |
| 8751 |
let file1 = std::fs::OpenOptions::new() |
| 8752 |
.write(true) |
| 8753 |
.open(at.plus("source_dir/file1.txt")) |
| 8754 |
.unwrap(); |
| 8755 |
file1 |
| 8756 |
.set_times( |
| 8757 |
std::fs::FileTimes::new() |
| 8758 |
.set_accessed(previous) |
| 8759 |
.set_modified(previous), |
| 8760 |
) |
| 8761 |
.unwrap(); |
| 8762 |
let file2 = std::fs::OpenOptions::new() |
| 8763 |
.write(true) |
| 8764 |
.open(at.plus("source_dir/file2.txt")) |
| 8765 |
.unwrap(); |
| 8766 |
file2 |
| 8767 |
.set_times( |
| 8768 |
std::fs::FileTimes::new() |
| 8769 |
.set_accessed(previous) |
| 8770 |
.set_modified(previous), |
| 8771 |
) |
| 8772 |
.unwrap(); |
| 8773 |
|
| 8774 |
// Create existing destination directory |
| 8775 |
at.mkdir("dest_dir"); |
| 8776 |
|
| 8777 |
// Copy current directory (.) with preserve attributes |
| 8778 |
ucmd.current_dir(at.plus("source_dir")) |
| 8779 |
.args(&["-rp", ".", "../dest_dir"]) |
| 8780 |
.succeeds(); |
| 8781 |
|
| 8782 |
// Verify files were copied |
| 8783 |
assert!(at.file_exists("dest_dir/file1.txt")); |
| 8784 |
assert!(at.file_exists("dest_dir/file2.txt")); |
| 8785 |
|
| 8786 |
// Verify that permissions are preserved |
| 8787 |
let src_metadata1 = at.metadata("source_dir/file1.txt"); |
| 8788 |
let dst_metadata1 = at.metadata("dest_dir/file1.txt"); |
| 8789 |
assert_eq!( |
| 8790 |
src_metadata1.mode() & 0o7777, |
| 8791 |
dst_metadata1.mode() & 0o7777, |
| 8792 |
"file1.txt permissions not preserved" |
| 8793 |
); |
| 8794 |
|
| 8795 |
let src_metadata2 = at.metadata("source_dir/file2.txt"); |
| 8796 |
let dst_metadata2 = at.metadata("dest_dir/file2.txt"); |
| 8797 |
assert_eq!( |
| 8798 |
src_metadata2.mode() & 0o7777, |
| 8799 |
dst_metadata2.mode() & 0o7777, |
| 8800 |
"file2.txt permissions not preserved" |
| 8801 |
); |
| 8802 |
|
| 8803 |
// Verify that timestamps are preserved |
| 8804 |
let src_modified1 = src_metadata1.modified().unwrap(); |
| 8805 |
let dst_modified1 = dst_metadata1.modified().unwrap(); |
| 8806 |
assert_eq!( |
| 8807 |
src_modified1, dst_modified1, |
| 8808 |
"file1.txt timestamps not preserved" |
| 8809 |
); |
| 8810 |
|
| 8811 |
let src_modified2 = src_metadata2.modified().unwrap(); |
| 8812 |
let dst_modified2 = dst_metadata2.modified().unwrap(); |
| 8813 |
assert_eq!( |
| 8814 |
src_modified2, dst_modified2, |
| 8815 |
"file2.txt timestamps not preserved" |
| 8816 |
); |
| 8817 |
} |
| 8818 |
|
| 8819 |
// Test that copying current directory (.) to itself is disallowed. |
| 8820 |
// This should fail with an appropriate error message. |
| 8821 |
#[test] |
| 8822 |
fn test_cp_current_directory_to_itself_disallowed() { |
| 8823 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 8824 |
|
| 8825 |
// Create a directory |
| 8826 |
at.mkdir("test_dir"); |
| 8827 |
at.touch("test_dir/file1.txt"); |
| 8828 |
|
| 8829 |
// Try to copy current directory (.) to itself |
| 8830 |
ucmd.current_dir(at.plus("test_dir")) |
| 8831 |
.args(&["-r", ".", "."]) |
| 8832 |
.fails() |
| 8833 |
.stderr_contains("cannot copy a directory"); |
| 8834 |
} |
| 8835 |
|
| 8836 |
// Test copying current directory (.) with symlinks. |
| 8837 |
// This ensures symlinks are handled correctly when copying the current directory. |
| 8838 |
#[test] |
| 8839 |
#[cfg_attr( |
| 8840 |
wasi_runner, |
| 8841 |
ignore = "WASI sandbox: relative '..' path canonicalization differs, causing a false self-copy detection" |
| 8842 |
)] |
| 8843 |
fn test_cp_current_directory_with_symlinks() { |
| 8844 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 8845 |
|
| 8846 |
// Create source directory with files and symlinks |
| 8847 |
at.mkdir("source_dir"); |
| 8848 |
at.touch("source_dir/file1.txt"); |
| 8849 |
at.symlink_file("file1.txt", "source_dir/link1.txt"); |
| 8850 |
at.mkdir("source_dir/subdir"); |
| 8851 |
at.touch("source_dir/subdir/file2.txt"); |
| 8852 |
at.symlink_file("../file1.txt", "source_dir/subdir/link2.txt"); |
| 8853 |
|
| 8854 |
// Create existing destination directory |
| 8855 |
at.mkdir("dest_dir"); |
| 8856 |
|
| 8857 |
// Copy current directory (.) to existing directory |
| 8858 |
ucmd.current_dir(at.plus("source_dir")) |
| 8859 |
.args(&["-r", ".", "../dest_dir"]) |
| 8860 |
.succeeds(); |
| 8861 |
|
| 8862 |
// Verify files and symlinks were copied correctly |
| 8863 |
assert!(at.file_exists("dest_dir/file1.txt")); |
| 8864 |
assert!(at.is_symlink("dest_dir/link1.txt")); |
| 8865 |
assert!(at.dir_exists("dest_dir/subdir")); |
| 8866 |
assert!(at.file_exists("dest_dir/subdir/file2.txt")); |
| 8867 |
assert!(at.is_symlink("dest_dir/subdir/link2.txt")); |
| 8868 |
} |
| 8869 |
|
| 8870 |
#[test] |
| 8871 |
#[cfg(not(windows))] |
| 8872 |
#[cfg_attr( |
| 8873 |
wasi_runner, |
| 8874 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 8875 |
)] |
| 8876 |
fn test_cp_no_dereference_symlink_with_parents() { |
| 8877 |
let ts = TestScenario::new(util_name!()); |
| 8878 |
let at = &ts.fixtures; |
| 8879 |
at.mkdir("directory"); |
| 8880 |
at.symlink_file("directory", "symlink-to-directory"); |
| 8881 |
|
| 8882 |
ts.ucmd() |
| 8883 |
.args(&["--parents", "--no-dereference", "symlink-to-directory", "x"]) |
| 8884 |
.fails() |
| 8885 |
.stderr_contains("with --parents, the destination must be a directory"); |
| 8886 |
|
| 8887 |
at.mkdir("x"); |
| 8888 |
ts.ucmd() |
| 8889 |
.args(&["--parents", "--no-dereference", "symlink-to-directory", "x"]) |
| 8890 |
.succeeds(); |
| 8891 |
assert_eq!(at.resolve_link("x/symlink-to-directory"), "directory"); |
| 8892 |
} |
| 8893 |
|
| 8894 |
#[test] |
| 8895 |
#[cfg(unix)] |
| 8896 |
#[cfg_attr( |
| 8897 |
wasi_runner, |
| 8898 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 8899 |
)] |
| 8900 |
fn test_cp_recursive_no_dereference_symlink_to_directory() { |
| 8901 |
let ts = TestScenario::new(util_name!()); |
| 8902 |
let at = &ts.fixtures; |
| 8903 |
|
| 8904 |
at.mkdir("source_dir"); |
| 8905 |
at.touch("source_dir/file.txt"); |
| 8906 |
at.symlink_file("source_dir", "symlink_to_dir"); |
| 8907 |
|
| 8908 |
// Copy with -r --no-dereference (or -rP): should copy the symlink, not the directory contents |
| 8909 |
ts.ucmd() |
| 8910 |
.args(&["-r", "--no-dereference", "symlink_to_dir", "dest"]) |
| 8911 |
.succeeds(); |
| 8912 |
|
| 8913 |
assert!(at.is_symlink("dest")); |
| 8914 |
assert_eq!(at.resolve_link("dest"), "source_dir"); |
| 8915 |
} |
| 8916 |
|
| 8917 |
#[test] |
| 8918 |
#[cfg(unix)] |
| 8919 |
fn test_cp_recursive_files_ending_in_backslash() { |
| 8920 |
let ts = TestScenario::new(util_name!()); |
| 8921 |
let at = &ts.fixtures; |
| 8922 |
at.mkdir("a"); |
| 8923 |
at.touch("a/foo\\"); |
| 8924 |
ts.ucmd().args(&["-r", "a", "b"]).succeeds(); |
| 8925 |
assert!(at.file_exists("b/foo\\")); |
| 8926 |
} |
| 8927 |
|
| 8928 |
#[test] |
| 8929 |
fn test_cp_no_preserve_target_directory() { |
| 8930 |
/* Expected result: |
| 8931 |
├── a |
| 8932 |
│ └── b |
| 8933 |
│ └── c |
| 8934 |
│ └── d |
| 8935 |
│ └── f1 |
| 8936 |
├── d |
| 8937 |
│ └── f1 |
| 8938 |
└── e |
| 8939 |
├── b |
| 8940 |
│ └── c |
| 8941 |
│ └── d |
| 8942 |
│ ├── c |
| 8943 |
│ │ └── d |
| 8944 |
│ │ └── f1 |
| 8945 |
│ └── f1 |
| 8946 |
├── d |
| 8947 |
│ └── f1 |
| 8948 |
├── f2 |
| 8949 |
└── f3 |
| 8950 |
*/ |
| 8951 |
|
| 8952 |
let ts = TestScenario::new(util_name!()); |
| 8953 |
let at = &ts.fixtures; |
| 8954 |
at.mkdir_all("a/b/c/d"); |
| 8955 |
at.touch("a/b/c/d/f1"); |
| 8956 |
ts.ucmd().args(&["-rT", "a", "e"]).succeeds(); |
| 8957 |
at.touch("e/f2"); |
| 8958 |
ts.ucmd().args(&["-rT", "a/", "e/"]).succeeds(); |
| 8959 |
at.touch("e/f3"); |
| 8960 |
ts.ucmd().args(&["-rvT", "a/b/c", "e/"]).succeeds(); |
| 8961 |
ts.ucmd().args(&["-rvT", "a/b/", "e/b/c/d/"]).succeeds(); |
| 8962 |
ts.ucmd().args(&["-rT", "a/b/c", "."]).succeeds(); |
| 8963 |
assert!(!at.dir_exists("e/a")); |
| 8964 |
assert!(at.file_exists("e/b/c/d/f1")); |
| 8965 |
assert!(at.file_exists("e/b/c/d/c/d/f1")); |
| 8966 |
assert!(!at.dir_exists("e/c")); |
| 8967 |
assert!(!at.dir_exists("e/c/d/b")); |
| 8968 |
assert!(at.file_exists("e/d/f1")); |
| 8969 |
assert!(at.file_exists("./d/f1")); |
| 8970 |
assert!(at.file_exists("e/f2")); |
| 8971 |
assert!(at.file_exists("e/f3")); |
| 8972 |
} |
| 8973 |
|
| 8974 |
#[test] |
| 8975 |
fn test_cp_recurse_verbose_output() { |
| 8976 |
let source_dir = "source_dir"; |
| 8977 |
let target_dir = "target_dir"; |
| 8978 |
let file = "file"; |
| 8979 |
#[cfg(not(windows))] |
| 8980 |
let output = format!( |
| 8981 |
"'{source_dir}' -> '{target_dir}/'\n'{source_dir}/{file}' -> '{target_dir}/{file}'\n" |
| 8982 |
); |
| 8983 |
#[cfg(windows)] |
| 8984 |
let output = format!( |
| 8985 |
"'{source_dir}' -> '{target_dir}\\'\n'{source_dir}\\{file}' -> '{target_dir}\\{file}'\n" |
| 8986 |
); |
| 8987 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 8988 |
|
| 8989 |
at.mkdir(source_dir); |
| 8990 |
at.touch(format!("{source_dir}/{file}")); |
| 8991 |
|
| 8992 |
ucmd.arg(source_dir) |
| 8993 |
.arg(target_dir) |
| 8994 |
.arg("-r") |
| 8995 |
.arg("--verbose") |
| 8996 |
.succeeds() |
| 8997 |
.no_stderr() |
| 8998 |
.stdout_is(output); |
| 8999 |
} |
| 9000 |
|
| 9001 |
#[test] |
| 9002 |
#[cfg_attr( |
| 9003 |
wasi_runner, |
| 9004 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 9005 |
)] |
| 9006 |
fn test_cp_recurse_verbose_output_with_symlink() { |
| 9007 |
let source_dir = "source_dir"; |
| 9008 |
let target_dir = "target_dir"; |
| 9009 |
let file = "file"; |
| 9010 |
let symlink = "symlink"; |
| 9011 |
#[cfg(not(windows))] |
| 9012 |
let output = format!( |
| 9013 |
"'{source_dir}' -> '{target_dir}/'\n'{source_dir}/{symlink}' -> '{target_dir}/{symlink}'\n" |
| 9014 |
); |
| 9015 |
#[cfg(windows)] |
| 9016 |
let output = format!( |
| 9017 |
"'{source_dir}' -> '{target_dir}\\'\n'{source_dir}\\{symlink}' -> '{target_dir}\\{symlink}'\n" |
| 9018 |
); |
| 9019 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 9020 |
|
| 9021 |
at.mkdir(source_dir); |
| 9022 |
at.touch(file); |
| 9023 |
at.symlink_file(file, format!("{source_dir}/{symlink}").as_str()); |
| 9024 |
|
| 9025 |
ucmd.arg(source_dir) |
| 9026 |
.arg(target_dir) |
| 9027 |
.arg("-r") |
| 9028 |
.arg("--verbose") |
| 9029 |
.succeeds() |
| 9030 |
.no_stderr() |
| 9031 |
.stdout_is(output); |
| 9032 |
} |
| 9033 |
|
| 9034 |
#[test] |
| 9035 |
#[cfg_attr( |
| 9036 |
wasi_runner, |
| 9037 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 9038 |
)] |
| 9039 |
fn test_cp_recurse_verbose_output_with_symlink_already_exists() { |
| 9040 |
let source_dir = "source_dir"; |
| 9041 |
let target_dir = "target_dir"; |
| 9042 |
let file = "file"; |
| 9043 |
let symlink = "symlink"; |
| 9044 |
#[cfg(not(windows))] |
| 9045 |
let output = format!( |
| 9046 |
"removed '{target_dir}/{symlink}'\n'{source_dir}/{symlink}' -> '{target_dir}/{symlink}'\n" |
| 9047 |
); |
| 9048 |
#[cfg(windows)] |
| 9049 |
let output = format!( |
| 9050 |
"removed '{target_dir}\\{symlink}'\n'{source_dir}\\{symlink}' -> '{target_dir}\\{symlink}'\n" |
| 9051 |
); |
| 9052 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 9053 |
|
| 9054 |
at.mkdir(source_dir); |
| 9055 |
at.touch(file); |
| 9056 |
at.symlink_file(file, format!("{source_dir}/{symlink}").as_str()); |
| 9057 |
at.mkdir(target_dir); |
| 9058 |
at.symlink_file(file, format!("{target_dir}/{symlink}").as_str()); |
| 9059 |
|
| 9060 |
ucmd.arg(source_dir) |
| 9061 |
.arg(target_dir) |
| 9062 |
.arg("-r") |
| 9063 |
.arg("--verbose") |
| 9064 |
.arg("-T") |
| 9065 |
.succeeds() |
| 9066 |
.no_stderr() |
| 9067 |
.stdout_is(output); |
| 9068 |
} |
| 9069 |
|
| 9070 |
#[test] |
| 9071 |
#[cfg(unix)] |
| 9072 |
#[cfg_attr( |
| 9073 |
wasi_runner, |
| 9074 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 9075 |
)] |
| 9076 |
fn test_cp_hlp_flag_ordering() { |
| 9077 |
// GNU cp: "If more than one of -H, -L, and -P is specified, only the final one takes effect" |
| 9078 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 9079 |
at.touch("file.txt"); |
| 9080 |
at.symlink_file("file.txt", "symlink"); |
| 9081 |
|
| 9082 |
// -HP: P wins, copy symlink as symlink |
| 9083 |
ucmd.args(&["-HP", "symlink", "dest_hp"]).succeeds(); |
| 9084 |
assert!(at.is_symlink("dest_hp")); |
| 9085 |
|
| 9086 |
// -PH: H wins, copy target file |
| 9087 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 9088 |
at.touch("file.txt"); |
| 9089 |
at.symlink_file("file.txt", "symlink"); |
| 9090 |
ucmd.args(&["-PH", "symlink", "dest_ph"]).succeeds(); |
| 9091 |
assert!(!at.is_symlink("dest_ph")); |
| 9092 |
assert!(at.file_exists("dest_ph")); |
| 9093 |
} |
| 9094 |
|
| 9095 |
#[test] |
| 9096 |
#[cfg(unix)] |
| 9097 |
#[cfg_attr( |
| 9098 |
wasi_runner, |
| 9099 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 9100 |
)] |
| 9101 |
fn test_cp_archive_deref_flag_ordering() { |
| 9102 |
// (flags, expect_symlink): last flag wins; a/d imply -P, H/L dereference |
| 9103 |
for (flags, expect_symlink) in [ |
| 9104 |
("-Ha", true), |
| 9105 |
("-aH", false), |
| 9106 |
("-Hd", true), |
| 9107 |
("-dH", false), |
| 9108 |
("-La", true), |
| 9109 |
("-aL", false), |
| 9110 |
("-Ld", true), |
| 9111 |
("-dL", false), |
| 9112 |
] { |
| 9113 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 9114 |
at.touch("file.txt"); |
| 9115 |
at.symlink_file("file.txt", "symlink"); |
| 9116 |
let dest = format!("dest{flags}"); |
| 9117 |
ucmd.args(&[flags, "symlink", &dest]).succeeds(); |
| 9118 |
assert_eq!(at.is_symlink(&dest), expect_symlink, "failed for {flags}"); |
| 9119 |
} |
| 9120 |
} |
| 9121 |
|
| 9122 |
/// Regression test: -a keeps recursion when combined with -L/-H/-d. |
| 9123 |
/// https://github.com/uutils/coreutils/issues/13207 |
| 9124 |
#[test] |
| 9125 |
#[cfg(unix)] |
| 9126 |
#[cfg_attr( |
| 9127 |
wasi_runner, |
| 9128 |
ignore = "WASI: no chmod syscall, so required mode/ownership preservation always fails" |
| 9129 |
)] |
| 9130 |
fn test_cp_archive_deref_preserves_recursive() { |
| 9131 |
for flags in ["-afL", "-aLf", "-aHL", "-adL"] { |
| 9132 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 9133 |
at.mkdir("srcdir"); |
| 9134 |
at.touch("srcdir/file.txt"); |
| 9135 |
let dest = format!("dest_{}", flags.replace('-', "")); |
| 9136 |
ucmd.args(&[flags, "srcdir", &dest]).succeeds(); |
| 9137 |
assert!( |
| 9138 |
at.file_exists(format!("{dest}/file.txt")), |
| 9139 |
"failed for {flags}: destination file missing" |
| 9140 |
); |
| 9141 |
} |
| 9142 |
} |
| 9143 |
|
| 9144 |
/// -aL should preserve file permissions (--preserve=all from -a). |
| 9145 |
#[test] |
| 9146 |
#[cfg(unix)] |
| 9147 |
#[cfg_attr( |
| 9148 |
wasi_runner, |
| 9149 |
ignore = "WASI: no chmod syscall, so required mode/ownership preservation always fails" |
| 9150 |
)] |
| 9151 |
fn test_cp_archive_deref_preserves_mode() { |
| 9152 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 9153 |
at.mkdir("srcdir"); |
| 9154 |
at.touch("srcdir/file.txt"); |
| 9155 |
at.set_mode("srcdir/file.txt", 0o705); |
| 9156 |
ucmd.args(&["-aL", "srcdir", "dest"]).succeeds(); |
| 9157 |
let mode = at.metadata("dest/file.txt").permissions().mode(); |
| 9158 |
assert_eq!( |
| 9159 |
mode & 0o777, |
| 9160 |
0o705, |
| 9161 |
"-aL should preserve mode, got 0o{mode:o}" |
| 9162 |
); |
| 9163 |
} |
| 9164 |
|
| 9165 |
/// -dL should preserve hardlinks (--preserve=links from -d survives -L override). |
| 9166 |
#[test] |
| 9167 |
#[cfg(target_os = "linux")] |
| 9168 |
fn test_cp_no_deref_preserve_with_deref_keeps_hardlinks() { |
| 9169 |
use std::os::linux::fs::MetadataExt; |
| 9170 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 9171 |
at.touch("file1"); |
| 9172 |
at.hard_link("file1", "file2"); |
| 9173 |
at.mkdir("target_dir"); |
| 9174 |
ucmd.args(&["-dL", "file1", "file2", "target_dir"]) |
| 9175 |
.succeeds(); |
| 9176 |
// -dL: hardlink preserved → target_dir/file1 should have nlink == 2 |
| 9177 |
// (both file1 and file2 point to the same inode in target_dir) |
| 9178 |
let nlink = at.metadata("target_dir/file1").st_nlink(); |
| 9179 |
assert_eq!( |
| 9180 |
nlink, 2, |
| 9181 |
"-dL should preserve hardlinks (expected nlink=2, got nlink={nlink})" |
| 9182 |
); |
| 9183 |
} |
| 9184 |
|
| 9185 |
/// -aL inside a directory: inner symlinks should be dereferenced, |
| 9186 |
/// while -a preserves them (last-flag-wins for dereference). |
| 9187 |
#[test] |
| 9188 |
#[cfg(unix)] |
| 9189 |
#[cfg_attr( |
| 9190 |
wasi_runner, |
| 9191 |
ignore = "WASI: no chmod syscall, so required mode/ownership preservation always fails" |
| 9192 |
)] |
| 9193 |
fn test_cp_archive_deref_symlinks_inside_dir() { |
| 9194 |
use std::os::unix::fs::symlink; |
| 9195 |
let scene = TestScenario::new(util_name!()); |
| 9196 |
let at = &scene.fixtures; |
| 9197 |
at.mkdir("srcdir"); |
| 9198 |
at.touch("srcdir/real.txt"); |
| 9199 |
symlink("real.txt", at.plus_as_string("srcdir/link.txt")).unwrap(); |
| 9200 |
|
| 9201 |
// -a (no deref): inner symlinks preserved |
| 9202 |
scene.ucmd().args(&["-a", "srcdir", "dest_a"]).succeeds(); |
| 9203 |
assert!( |
| 9204 |
at.is_symlink("dest_a/link.txt"), |
| 9205 |
"-a: inner symlink should be preserved" |
| 9206 |
); |
| 9207 |
|
| 9208 |
// -aL (last is -L, deref): inner symlinks dereferenced |
| 9209 |
scene.ucmd().args(&["-aL", "srcdir", "dest_aL"]).succeeds(); |
| 9210 |
assert!( |
| 9211 |
!at.is_symlink("dest_aL/link.txt"), |
| 9212 |
"-aL: inner symlink should be dereferenced" |
| 9213 |
); |
| 9214 |
|
| 9215 |
// -La (last is -a, no deref): inner symlinks preserved |
| 9216 |
scene.ucmd().args(&["-La", "srcdir", "dest_La"]).succeeds(); |
| 9217 |
assert!( |
| 9218 |
at.is_symlink("dest_La/link.txt"), |
| 9219 |
"-La: inner symlink should be preserved" |
| 9220 |
); |
| 9221 |
} |
| 9222 |
|
| 9223 |
/// -aH: inner symlinks preserved (a wins for recursive), CLI symlinks followed (H wins for CLI). |
| 9224 |
#[test] |
| 9225 |
#[cfg(unix)] |
| 9226 |
#[cfg_attr( |
| 9227 |
wasi_runner, |
| 9228 |
ignore = "WASI: no chmod syscall, so required mode/ownership preservation always fails" |
| 9229 |
)] |
| 9230 |
fn test_cp_archive_cli_deref_inner_preserved() { |
| 9231 |
use std::os::unix::fs::symlink; |
| 9232 |
let scene = TestScenario::new(util_name!()); |
| 9233 |
let at = &scene.fixtures; |
| 9234 |
at.mkdir("srcdir"); |
| 9235 |
at.touch("srcdir/real.txt"); |
| 9236 |
symlink("real.txt", at.plus_as_string("srcdir/link.txt")).unwrap(); |
| 9237 |
|
| 9238 |
// -aH: CLI symlink dereferenced, inner symlinks preserved |
| 9239 |
scene.ucmd().args(&["-aH", "srcdir", "dest_aH"]).succeeds(); |
| 9240 |
assert!( |
| 9241 |
at.is_symlink("dest_aH/link.txt"), |
| 9242 |
"-aH: inner symlink should be preserved (a wins for recursive)" |
| 9243 |
); |
| 9244 |
|
| 9245 |
// -Ha: CLI + inner symlinks preserved (a wins since last) |
| 9246 |
scene.ucmd().args(&["-Ha", "srcdir", "dest_Ha"]).succeeds(); |
| 9247 |
assert!( |
| 9248 |
at.is_symlink("dest_Ha/link.txt"), |
| 9249 |
"-Ha: inner symlink should be preserved (a is last)" |
| 9250 |
); |
| 9251 |
} |
| 9252 |
|
| 9253 |
/// Precedence: repeating the same flag should take the last position. |
| 9254 |
#[test] |
| 9255 |
#[cfg(unix)] |
| 9256 |
#[cfg_attr( |
| 9257 |
wasi_runner, |
| 9258 |
ignore = "WASI: no chmod syscall, so required mode/ownership preservation always fails" |
| 9259 |
)] |
| 9260 |
fn test_cp_archive_deref_repeated_flag_last_wins() { |
| 9261 |
use std::os::unix::fs::symlink; |
| 9262 |
let scene = TestScenario::new(util_name!()); |
| 9263 |
let at = &scene.fixtures; |
| 9264 |
at.mkdir("srcdir"); |
| 9265 |
at.touch("srcdir/real.txt"); |
| 9266 |
symlink("real.txt", at.plus_as_string("srcdir/link.txt")).unwrap(); |
| 9267 |
|
| 9268 |
// -aL -a: -a is last, inner symlinks preserved |
| 9269 |
scene |
| 9270 |
.ucmd() |
| 9271 |
.args(&["-aL", "-a", "srcdir", "dest"]) |
| 9272 |
.succeeds(); |
| 9273 |
assert!( |
| 9274 |
at.is_symlink("dest/link.txt"), |
| 9275 |
"-aL -a: last -a should preserve inner symlinks" |
| 9276 |
); |
| 9277 |
|
| 9278 |
// -La -L: -L is last, inner symlinks dereferenced |
| 9279 |
scene |
| 9280 |
.ucmd() |
| 9281 |
.args(&["-La", "-L", "srcdir", "dest2"]) |
| 9282 |
.succeeds(); |
| 9283 |
assert!( |
| 9284 |
!at.is_symlink("dest2/link.txt"), |
| 9285 |
"-La -L: last -L should dereference inner symlinks" |
| 9286 |
); |
| 9287 |
} |
| 9288 |
|
| 9289 |
#[test] |
| 9290 |
fn test_cp_circular_symbolic_links_in_directory() { |
| 9291 |
let source_dir = "source_dir"; |
| 9292 |
let target_dir = "target_dir"; |
| 9293 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 9294 |
let separator = std::path::MAIN_SEPARATOR_STR; |
| 9295 |
|
| 9296 |
at.mkdir(source_dir); |
| 9297 |
at.symlink_file( |
| 9298 |
format!("{source_dir}/a").as_str(), |
| 9299 |
format!("{source_dir}/b").as_str(), |
| 9300 |
); |
| 9301 |
at.symlink_file( |
| 9302 |
format!("{source_dir}/b").as_str(), |
| 9303 |
format!("{source_dir}/a").as_str(), |
| 9304 |
); |
| 9305 |
|
| 9306 |
ucmd.arg(source_dir) |
| 9307 |
.arg(target_dir) |
| 9308 |
.arg("-rL") |
| 9309 |
.fails_with_code(1) |
| 9310 |
.stderr_contains(format!( |
| 9311 |
"IO error for operation on {source_dir}{separator}a" |
| 9312 |
)) |
| 9313 |
.stderr_contains(format!( |
| 9314 |
"IO error for operation on {source_dir}{separator}b" |
| 9315 |
)); |
| 9316 |
} |
| 9317 |
|
| 9318 |
/// Test that copying to an existing file maintains its permissions, unix only because .mode() only |
| 9319 |
/// works on Unix |
| 9320 |
#[test] |
| 9321 |
#[cfg(unix)] |
| 9322 |
fn test_cp_to_existing_file_permissions() { |
| 9323 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 9324 |
|
| 9325 |
at.touch("src"); |
| 9326 |
at.touch("dst"); |
| 9327 |
|
| 9328 |
let src_path = at.plus("src"); |
| 9329 |
let dst_path = at.plus("dst"); |
| 9330 |
|
| 9331 |
let mut src_permissions = std::fs::metadata(&src_path).unwrap().permissions(); |
| 9332 |
src_permissions.set_readonly(true); |
| 9333 |
set_permissions(&src_path, src_permissions).unwrap(); |
| 9334 |
|
| 9335 |
let dst_mode = std::fs::metadata(&dst_path).unwrap().permissions().mode(); |
| 9336 |
|
| 9337 |
ucmd.args(&["src", "dst"]).succeeds(); |
| 9338 |
|
| 9339 |
let new_dst_mode = std::fs::metadata(&dst_path).unwrap().permissions().mode(); |
| 9340 |
assert_eq!(dst_mode, new_dst_mode); |
| 9341 |
} |
| 9342 |
|
| 9343 |
/// Test xattr ENOTSUP handling: -a/--preserve=all silent, --preserve=xattr errors |
| 9344 |
#[test] |
| 9345 |
#[cfg(target_os = "linux")] |
| 9346 |
fn test_cp_xattr_enotsup_handling() { |
| 9347 |
use std::process::Command; |
| 9348 |
let scene = TestScenario::new(util_name!()); |
| 9349 |
let at = &scene.fixtures; |
| 9350 |
at.write("src", "x"); |
| 9351 |
|
| 9352 |
// Check if setfattr is available and source fs supports xattrs |
| 9353 |
if !Command::new("setfattr") |
| 9354 |
.args(["-n", "user.t", "-v", "v", &at.plus_as_string("src")]) |
| 9355 |
.status() |
| 9356 |
.is_ok_and(|s| s.success()) |
| 9357 |
{ |
| 9358 |
return; // Skip: setfattr not available or source doesn't support xattrs |
| 9359 |
} |
| 9360 |
|
| 9361 |
// Check if /dev/shm exists |
| 9362 |
if !Path::new("/dev/shm").exists() { |
| 9363 |
return; // Skip: /dev/shm not available |
| 9364 |
} |
| 9365 |
|
| 9366 |
// Check if /dev/shm actually doesn't support xattrs by trying to set one |
| 9367 |
let shm_test_file = "/dev/shm/xattr_test_probe"; |
| 9368 |
std::fs::write(shm_test_file, "test").ok(); |
| 9369 |
let shm_supports_xattr = Command::new("setfattr") |
| 9370 |
.args(["-n", "user.t", "-v", "v", shm_test_file]) |
| 9371 |
.status() |
| 9372 |
.is_ok_and(|s| s.success()); |
| 9373 |
std::fs::remove_file(shm_test_file).ok(); |
| 9374 |
|
| 9375 |
if shm_supports_xattr { |
| 9376 |
return; // Skip: /dev/shm supports xattrs on this system |
| 9377 |
} |
| 9378 |
|
| 9379 |
// -a: silent success |
| 9380 |
scene |
| 9381 |
.ucmd() |
| 9382 |
.args(&["-a", &at.plus_as_string("src"), "/dev/shm/t1"]) |
| 9383 |
.succeeds() |
| 9384 |
.no_stderr(); |
| 9385 |
// --preserve=all: silent success |
| 9386 |
scene |
| 9387 |
.ucmd() |
| 9388 |
.args(&["--preserve=all", &at.plus_as_string("src"), "/dev/shm/t2"]) |
| 9389 |
.succeeds() |
| 9390 |
.no_stderr(); |
| 9391 |
// --preserve=xattr: must fail with proper message |
| 9392 |
scene |
| 9393 |
.ucmd() |
| 9394 |
.args(&["--preserve=xattr", &at.plus_as_string("src"), "/dev/shm/t3"]) |
| 9395 |
.fails() |
| 9396 |
.stderr_contains("setting attributes") |
| 9397 |
.stderr_contains("Operation not supported"); |
| 9398 |
for f in ["/dev/shm/t1", "/dev/shm/t2", "/dev/shm/t3"] { |
| 9399 |
std::fs::remove_file(f).ok(); |
| 9400 |
} |
| 9401 |
} |
| 9402 |
|
| 9403 |
/// A failed --preserve=xattr must not empty the already-copied destination; |
| 9404 |
/// only a failure to preserve the SELinux context empties it. |
| 9405 |
#[test] |
| 9406 |
#[cfg(target_os = "linux")] |
| 9407 |
fn test_cp_xattr_failure_keeps_dest_contents() { |
| 9408 |
use std::process::Command; |
| 9409 |
let scene = TestScenario::new(util_name!()); |
| 9410 |
|
| 9411 |
// tmpfs accepts large user-xattr values while ext4 and friends cap them |
| 9412 |
// near the block size, so copying such a source out of tmpfs makes |
| 9413 |
// --preserve=xattr fail only after the file data has been written. |
| 9414 |
// The fixtures dir may itself be on tmpfs, so put the destination in |
| 9415 |
// target/tmp, which lives on the build filesystem. |
| 9416 |
let big_value = "y".repeat(9_100); |
| 9417 |
let pid = std::process::id(); |
| 9418 |
let source = format!("/dev/shm/cp_keep_dest_{pid}"); |
| 9419 |
let dest_dir = PathBuf::from(env!("CARGO_TARGET_TMPDIR")).join(format!("cp_keep_dest_{pid}")); |
| 9420 |
if std_fs::write(&source, "kept content").is_err() || std_fs::create_dir(&dest_dir).is_err() { |
| 9421 |
return; // skip: no usable /dev/shm or target/tmp |
| 9422 |
} |
| 9423 |
let source_accepts = Command::new("setfattr") |
| 9424 |
.args(["-n", "user.huge", "-v", &big_value, &source]) |
| 9425 |
.status() |
| 9426 |
.is_ok_and(|s| s.success()); |
| 9427 |
let probe = dest_dir.join("probe"); |
| 9428 |
std_fs::write(&probe, "x").unwrap(); |
| 9429 |
let dest_rejects = !Command::new("setfattr") |
| 9430 |
.args(["-n", "user.huge", "-v", &big_value]) |
| 9431 |
.arg(&probe) |
| 9432 |
.status() |
| 9433 |
.is_ok_and(|s| s.success()); |
| 9434 |
if !source_accepts || !dest_rejects { |
| 9435 |
std_fs::remove_file(&source).ok(); |
| 9436 |
std_fs::remove_dir_all(&dest_dir).ok(); |
| 9437 |
return; // skip: this filesystem combination cannot produce the failure |
| 9438 |
} |
| 9439 |
|
| 9440 |
let out = dest_dir.join("out"); |
| 9441 |
scene |
| 9442 |
.ucmd() |
| 9443 |
.arg("--preserve=xattr") |
| 9444 |
.arg(&source) |
| 9445 |
.arg(&out) |
| 9446 |
.fails() |
| 9447 |
.stderr_contains("setting attributes"); |
| 9448 |
assert_eq!(std_fs::read_to_string(&out).unwrap(), "kept content"); |
| 9449 |
|
| 9450 |
// A read-only source propagates its mode to the destination; the failure |
| 9451 |
// handling must neither empty the file nor leave the mode altered. |
| 9452 |
set_permissions(&source, std_fs::Permissions::from_mode(0o444)).unwrap(); |
| 9453 |
let out_ro = dest_dir.join("out_ro"); |
| 9454 |
scene |
| 9455 |
.ucmd() |
| 9456 |
.arg("--preserve=xattr") |
| 9457 |
.arg(&source) |
| 9458 |
.arg(&out_ro) |
| 9459 |
.fails() |
| 9460 |
.stderr_contains("setting attributes"); |
| 9461 |
assert_eq!(std_fs::read_to_string(&out_ro).unwrap(), "kept content"); |
| 9462 |
assert_eq!( |
| 9463 |
std_fs::metadata(&out_ro).unwrap().mode() & 0o777, |
| 9464 |
0o444, |
| 9465 |
"destination mode should stay read-only" |
| 9466 |
); |
| 9467 |
|
| 9468 |
std_fs::remove_file(&source).ok(); |
| 9469 |
set_permissions(&out_ro, std_fs::Permissions::from_mode(0o644)).ok(); |
| 9470 |
std_fs::remove_dir_all(&dest_dir).ok(); |
| 9471 |
} |
| 9472 |
|
| 9473 |
#[test] |
| 9474 |
#[cfg(not(windows))] |
| 9475 |
#[cfg_attr( |
| 9476 |
wasi_runner, |
| 9477 |
ignore = "WASI: no chmod syscall, so required mode/ownership preservation always fails" |
| 9478 |
)] |
| 9479 |
fn test_cp_preserve_directory_permissions_by_default() { |
| 9480 |
let scene = TestScenario::new(util_name!()); |
| 9481 |
let at = &scene.fixtures; |
| 9482 |
|
| 9483 |
let dir = "a/b/c/d"; |
| 9484 |
let file = "foo.txt"; |
| 9485 |
|
| 9486 |
at.mkdir_all(dir); |
| 9487 |
|
| 9488 |
let file_path = format!("{dir}/{file}"); |
| 9489 |
|
| 9490 |
at.touch(file_path); |
| 9491 |
|
| 9492 |
scene.cmd("chmod").arg("-R").arg("555").arg("a").succeeds(); |
| 9493 |
scene.cmd("cp").arg("-r").arg("a").arg("b").succeeds(); |
| 9494 |
|
| 9495 |
scene.ucmd().arg("-r").arg("a").arg("c").succeeds(); |
| 9496 |
|
| 9497 |
// only verify owner bits on Android |
| 9498 |
if cfg!(target_os = "android") { |
| 9499 |
assert_eq!(at.metadata("b").mode() & 0o700, 0o500); |
| 9500 |
assert_eq!(at.metadata("b/b").mode() & 0o700, 0o500); |
| 9501 |
assert_eq!(at.metadata("b/b/c").mode() & 0o700, 0o500); |
| 9502 |
assert_eq!(at.metadata("b/b/c/d").mode() & 0o700, 0o500); |
| 9503 |
|
| 9504 |
assert_eq!(at.metadata("c").mode() & 0o700, 0o500); |
| 9505 |
assert_eq!(at.metadata("c/b").mode() & 0o700, 0o500); |
| 9506 |
assert_eq!(at.metadata("c/b/c").mode() & 0o700, 0o500); |
| 9507 |
assert_eq!(at.metadata("c/b/c/d").mode() & 0o700, 0o500); |
| 9508 |
} else { |
| 9509 |
assert_eq!(at.metadata("b").mode(), 0o40555); |
| 9510 |
assert_eq!(at.metadata("b/b").mode(), 0o40555); |
| 9511 |
assert_eq!(at.metadata("b/b/c").mode(), 0o40555); |
| 9512 |
assert_eq!(at.metadata("b/b/c/d").mode(), 0o40555); |
| 9513 |
|
| 9514 |
assert_eq!(at.metadata("c").mode(), 0o40555); |
| 9515 |
assert_eq!(at.metadata("c/b").mode(), 0o40555); |
| 9516 |
assert_eq!(at.metadata("c/b/c").mode(), 0o40555); |
| 9517 |
assert_eq!(at.metadata("c/b/c/d").mode(), 0o40555); |
| 9518 |
} |
| 9519 |
} |
| 9520 |
|
| 9521 |
#[test] |
| 9522 |
#[cfg(not(windows))] |
| 9523 |
fn test_cp_existing_perm_dir() { |
| 9524 |
let scene = TestScenario::new(util_name!()); |
| 9525 |
let at = &scene.fixtures; |
| 9526 |
|
| 9527 |
scene |
| 9528 |
.cmd("mkdir") |
| 9529 |
.arg("-p") |
| 9530 |
.arg("-m") |
| 9531 |
.arg("ug-s,u=rwx,g=rwx,o=rx") |
| 9532 |
.arg("src/dir") |
| 9533 |
.umask(0o022) |
| 9534 |
.succeeds(); |
| 9535 |
scene |
| 9536 |
.cmd("mkdir") |
| 9537 |
.arg("-p") |
| 9538 |
.arg("-m") |
| 9539 |
.arg("ug-s,u=rwx,g=,o=") |
| 9540 |
.arg("dst/dir") |
| 9541 |
.umask(0o022) |
| 9542 |
.succeeds(); |
| 9543 |
|
| 9544 |
scene.ucmd().arg("-r").arg("src/.").arg("dst/").succeeds(); |
| 9545 |
|
| 9546 |
let mode = at.metadata("dst/dir").mode(); |
| 9547 |
|
| 9548 |
assert_eq!(mode, 0o40700); |
| 9549 |
} |
| 9550 |
|
| 9551 |
#[test] |
| 9552 |
#[cfg(not(windows))] |
| 9553 |
fn test_cp_gnu_preserve_mode() { |
| 9554 |
use std::io; |
| 9555 |
|
| 9556 |
let scene = TestScenario::new(util_name!()); |
| 9557 |
let at = &scene.fixtures; |
| 9558 |
|
| 9559 |
scene.cmd("mkdir").arg("d1").succeeds(); |
| 9560 |
scene.cmd("mkdir").arg("d2").succeeds(); |
| 9561 |
scene.cmd("chmod").arg("705").arg("d2").succeeds(); |
| 9562 |
|
| 9563 |
scene |
| 9564 |
.ucmd() |
| 9565 |
.arg("--no-preserve=mode") |
| 9566 |
.arg("-r") |
| 9567 |
.arg("d2") |
| 9568 |
.arg("d3") |
| 9569 |
.set_stdout(io::stdout()) |
| 9570 |
.succeeds(); |
| 9571 |
|
| 9572 |
let d1_mode = at.metadata("d1").mode(); |
| 9573 |
let d3_mode = at.metadata("d3").mode(); |
| 9574 |
|
| 9575 |
assert_eq!(d1_mode, d3_mode); |
| 9576 |
} |
| 9577 |
|
| 9578 |
#[test] |
| 9579 |
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))] |
| 9580 |
fn test_cp_a_z_overrides_context() { |
| 9581 |
// Verifies -aZ succeeds (-Z overrides implicit --preserve=context from -a) |
| 9582 |
use std::path::Path; |
| 9583 |
use uucore::selinux::set_selinux_security_context; |
| 9584 |
|
| 9585 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 9586 |
at.touch("src"); |
| 9587 |
|
| 9588 |
let ctx = "unconfined_u:object_r:user_tmp_t:s0".to_string(); |
| 9589 |
if set_selinux_security_context(Path::new(&at.plus_as_string("src")), Some(&ctx)).is_err() { |
| 9590 |
return; |
| 9591 |
} |
| 9592 |
|
| 9593 |
ucmd.args(&["-aZ", "src", "dst"]).succeeds(); |
| 9594 |
} |
| 9595 |
|
| 9596 |
#[test] |
| 9597 |
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))] |
| 9598 |
fn test_cp_a_preserves_context() { |
| 9599 |
use std::path::Path; |
| 9600 |
use uucore::selinux::{get_selinux_security_context, set_selinux_security_context}; |
| 9601 |
|
| 9602 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 9603 |
at.touch("src"); |
| 9604 |
|
| 9605 |
let ctx = "unconfined_u:object_r:user_tmp_t:s0".to_string(); |
| 9606 |
if set_selinux_security_context(Path::new(&at.plus_as_string("src")), Some(&ctx)).is_err() { |
| 9607 |
return; |
| 9608 |
} |
| 9609 |
|
| 9610 |
let src_ctx = |
| 9611 |
get_selinux_security_context(Path::new(&at.plus_as_string("src")), false).unwrap(); |
| 9612 |
ucmd.args(&["-a", "src", "dst"]).succeeds(); |
| 9613 |
let dst_ctx = |
| 9614 |
get_selinux_security_context(Path::new(&at.plus_as_string("dst")), false).unwrap(); |
| 9615 |
|
| 9616 |
assert_eq!(src_ctx, dst_ctx, "-a should preserve SELinux context"); |
| 9617 |
} |
| 9618 |
|
| 9619 |
#[test] |
| 9620 |
#[cfg(all(feature = "selinux", any(target_os = "linux", target_os = "android")))] |
| 9621 |
fn test_cp_preserve_context_with_z_fails() { |
| 9622 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 9623 |
at.touch("src"); |
| 9624 |
ucmd.args(&["--preserve=context", "-Z", "src", "dst"]) |
| 9625 |
.fails() |
| 9626 |
.stderr_contains("cannot combine"); |
| 9627 |
} |
| 9628 |
|
| 9629 |
// Covers the happy path for issue #9750: when chown succeeds (src owner == |
| 9630 |
// current user), `cp -p` preserves setuid/setgid. The failure-path behavior — |
| 9631 |
// stripping setuid/setgid when chown cannot preserve ownership — requires a |
| 9632 |
// multi-user setup (source owned by a different uid, cp run as non-root) and |
| 9633 |
// is exercised by GNU's test suite; documenting here as future coverage. |
| 9634 |
#[test] |
| 9635 |
#[cfg(unix)] |
| 9636 |
#[cfg_attr( |
| 9637 |
wasi_runner, |
| 9638 |
ignore = "WASI: no chmod syscall, so required mode/ownership preservation always fails" |
| 9639 |
)] |
| 9640 |
fn test_cp_preserve_setuid_when_chown_succeeds() { |
| 9641 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 9642 |
at.touch("src"); |
| 9643 |
at.set_mode("src", 0o4755); |
| 9644 |
|
| 9645 |
ucmd.arg("-p").arg("src").arg("dst").succeeds(); |
| 9646 |
|
| 9647 |
let mode = at.metadata("dst").mode() & 0o7777; |
| 9648 |
assert_eq!( |
| 9649 |
mode & 0o4000, |
| 9650 |
0o4000, |
| 9651 |
"setuid bit should be preserved when chown succeeds (got mode {mode:o})" |
| 9652 |
); |
| 9653 |
} |
| 9654 |
|
| 9655 |
#[test] |
| 9656 |
#[cfg(all(unix, not(target_vendor = "apple")))] |
| 9657 |
#[cfg_attr( |
| 9658 |
wasi_runner, |
| 9659 |
ignore = "WASI: requires valid UTF-8 paths, so the non-UTF-8 error is expected" |
| 9660 |
)] |
| 9661 |
fn test_cp_recursive_non_utf8_source() { |
| 9662 |
use std::{ffi::OsStr, os::unix::ffi::OsStrExt}; |
| 9663 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 9664 |
|
| 9665 |
at.mkdir(OsStr::from_bytes(b"dir\x80")); |
| 9666 |
at.mkdir("dir2"); |
| 9667 |
at.touch(OsStr::from_bytes(b"dir\x80/a")); |
| 9668 |
|
| 9669 |
ucmd.arg("-r") |
| 9670 |
.arg(OsStr::from_bytes(b"dir\x80/.")) |
| 9671 |
.arg("dir2") |
| 9672 |
.succeeds() |
| 9673 |
.no_output(); |
| 9674 |
|
| 9675 |
assert!(at.plus("dir2").join("a").exists()); |
| 9676 |
} |
| 9677 |
|
| 9678 |
// Regression guard for issue #10011: cp now creates the destination with |
| 9679 |
// mode 0o600 instead of the umask-derived 0o666, so another user in a |
| 9680 |
// shared directory cannot open the file through its permissive initial |
| 9681 |
// mode before cp applies the final permissions. The final mode must still |
| 9682 |
// match the source mode masked by the running umask, so no user-visible |
| 9683 |
// behavior changes. |
| 9684 |
#[test] |
| 9685 |
#[cfg(unix)] |
| 9686 |
fn test_cp_final_mode_unchanged_after_restrictive_create() { |
| 9687 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 9688 |
at.touch("src"); |
| 9689 |
at.set_mode("src", 0o644); |
| 9690 |
|
| 9691 |
ucmd.umask(0o022).arg("src").arg("dst").succeeds(); |
| 9692 |
|
| 9693 |
let mode = at.metadata("dst").mode() & 0o777; |
| 9694 |
assert_eq!( |
| 9695 |
mode, 0o644, |
| 9696 |
"dst final mode should match source & ~umask (got {mode:o})" |
| 9697 |
); |
| 9698 |
} |
| 9699 |
|
| 9700 |
// Sanity check for the `-P` happy path: a symlink source is copied as a |
| 9701 |
// symlink, not by following it. The actual `O_NOFOLLOW` invariant for |
| 9702 |
// issue #10017 (path swap to a symlink between lstat and open) cannot be |
| 9703 |
// raced deterministically from a unit test; that is locked in by the |
| 9704 |
// strace check in util/check-safe-traversal.sh, which fails if a future |
| 9705 |
// change drops `O_NOFOLLOW` from the source open under `-P`. |
| 9706 |
#[test] |
| 9707 |
#[cfg(unix)] |
| 9708 |
#[cfg_attr( |
| 9709 |
wasi_runner, |
| 9710 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 9711 |
)] |
| 9712 |
fn test_cp_no_dereference_copies_symlink_as_symlink() { |
| 9713 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 9714 |
at.write("target", "secret target contents"); |
| 9715 |
at.symlink_file("target", "src_link"); |
| 9716 |
|
| 9717 |
ucmd.arg("-P").arg("src_link").arg("dst").succeeds(); |
| 9718 |
assert!(at.symlink_exists("dst")); |
| 9719 |
assert!(at.read_symlink("dst").ends_with("target")); |
| 9720 |
} |
| 9721 |
|
| 9722 |
// Regression for GNU tests/cp/deref-slink: when the destination exists as |
| 9723 |
// a symlink, `cp -d` (which implies --no-dereference for the source) must |
| 9724 |
// still follow the destination symlink and overwrite the link's target. |
| 9725 |
// `-P`/`-d` only forbids dereferencing on the source side; applying |
| 9726 |
// O_NOFOLLOW to the dest open broke this and surfaced as ELOOP. |
| 9727 |
#[test] |
| 9728 |
#[cfg(unix)] |
| 9729 |
#[cfg_attr( |
| 9730 |
wasi_runner, |
| 9731 |
ignore = "WASI sandbox: symlink/hardlink capability restrictions cause dangling-symlink/same-file detection to differ" |
| 9732 |
)] |
| 9733 |
fn test_cp_d_overwrites_existing_symlink_dest() { |
| 9734 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 9735 |
at.touch("f"); |
| 9736 |
at.touch("slink-target"); |
| 9737 |
at.symlink_file("slink-target", "slink"); |
| 9738 |
|
| 9739 |
ucmd.arg("-d").arg("f").arg("slink").succeeds(); |
| 9740 |
|
| 9741 |
// The destination symlink itself remains a symlink (GNU follows it |
| 9742 |
// through to the target rather than replacing it). |
| 9743 |
assert!(at.symlink_exists("slink")); |
| 9744 |
assert!(at.read_symlink("slink").ends_with("slink-target")); |
| 9745 |
} |
| 9746 |
|
| 9747 |
// Regression for GNU tests/cp/acl: `cp -p` must preserve POSIX ACLs on |
| 9748 |
// Linux. ACLs are part of GNU's `mode` preservation, not its `xattr` |
| 9749 |
// preservation, so the default-no-xattr change in #9704 must not strip |
| 9750 |
// them. Only runs when `setfacl` is available so non-ACL filesystems and |
| 9751 |
// non-Linux CI do not flag spurious failures. |
| 9752 |
#[test] |
| 9753 |
#[cfg(target_os = "linux")] |
| 9754 |
#[cfg_attr( |
| 9755 |
wasi_runner, |
| 9756 |
ignore = "WASI: ACL/xattr syscalls are not implemented, so -p's attribute preservation fails" |
| 9757 |
)] |
| 9758 |
fn test_cp_p_preserves_posix_acls() { |
| 9759 |
use std::process::Command; |
| 9760 |
|
| 9761 |
if Command::new("setfacl").arg("--version").output().is_err() { |
| 9762 |
return; |
| 9763 |
} |
| 9764 |
if Command::new("getfacl").arg("--version").output().is_err() { |
| 9765 |
return; |
| 9766 |
} |
| 9767 |
|
| 9768 |
let (at, mut ucmd) = at_and_ucmd!(); |
| 9769 |
at.touch("src"); |
| 9770 |
|
| 9771 |
let setfacl = Command::new("setfacl") |
| 9772 |
.arg("-m") |
| 9773 |
.arg("user:bin:rw-") |
| 9774 |
.arg(at.plus("src")) |
| 9775 |
.status(); |
| 9776 |
let Ok(status) = setfacl else { return }; |
| 9777 |
if !status.success() { |
| 9778 |
// Filesystem doesn't support ACLs; skip. |
| 9779 |
return; |
| 9780 |
} |
| 9781 |
|
| 9782 |
ucmd.arg("-p").arg("src").arg("dst").succeeds(); |
| 9783 |
|
| 9784 |
let src_acl = Command::new("getfacl") |
| 9785 |
.arg("--omit-header") |
| 9786 |
.arg(at.plus("src")) |
| 9787 |
.output() |
| 9788 |
.unwrap(); |
| 9789 |
let dst_acl = Command::new("getfacl") |
| 9790 |
.arg("--omit-header") |
| 9791 |
.arg(at.plus("dst")) |
| 9792 |
.output() |
| 9793 |
.unwrap(); |
| 9794 |
assert_eq!( |
| 9795 |
String::from_utf8_lossy(&src_acl.stdout), |
| 9796 |
String::from_utf8_lossy(&dst_acl.stdout), |
| 9797 |
"cp -p must preserve POSIX ACLs (GNU tests/cp/acl regression)", |
| 9798 |
); |
| 9799 |
} |
| 9800 |
|
| 9801 |
#[test] |
| 9802 |
fn test_progressbar_inexistent_source() { |
| 9803 |
let (_, mut ucmd) = at_and_ucmd!(); |
| 9804 |
ucmd.arg("-g") |
| 9805 |
.arg("inexistent1") |
| 9806 |
.arg("inexistent2") |
| 9807 |
.fails_with_code(1) |
| 9808 |
.stderr_contains("cp: cannot stat 'inexistent1': No such file or directory"); |
| 9809 |
} |