Debian Codegraph @

tests/by-util/test_ln.rs

raw
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 #![allow(clippy::similar_names)]
7
8 use std::path::PathBuf;
9 #[cfg(all(unix, not(target_os = "android")))]
10 use std::time::Duration;
11 use uutests::at_and_ucmd;
12 use uutests::new_ucmd;
13 use uutests::util::TestScenario;
14 use uutests::util_name;
15
16 #[test]
17 fn test_invalid_arg() {
18 new_ucmd!().arg("--definitely-invalid").fails_with_code(1);
19 }
20
21 #[test]
22 fn test_symlink_existing_file() {
23 let (at, mut ucmd) = at_and_ucmd!();
24 let file = "test_symlink_existing_file";
25 let link = "test_symlink_existing_file_link";
26
27 at.touch(file);
28
29 ucmd.args(&["-s", file, link]).succeeds().no_stderr();
30
31 assert!(at.file_exists(file));
32 assert!(at.is_symlink(link));
33 assert_eq!(at.resolve_link(link), file);
34 }
35
36 #[test]
37 fn test_symlink_dangling_file() {
38 let (at, mut ucmd) = at_and_ucmd!();
39 let file = "test_symlink_dangling_file";
40 let link = "test_symlink_dangling_file_link";
41
42 ucmd.args(&["-s", file, link]).succeeds().no_stderr();
43 assert!(!at.file_exists(file));
44 assert!(at.is_symlink(link));
45 assert_eq!(at.resolve_link(link), file);
46 }
47
48 #[test]
49 fn test_symlink_existing_directory() {
50 let (at, mut ucmd) = at_and_ucmd!();
51 let dir = "test_symlink_existing_dir";
52 let link = "test_symlink_existing_dir_link";
53
54 at.mkdir(dir);
55
56 ucmd.args(&["-s", dir, link]).succeeds().no_stderr();
57 assert!(at.dir_exists(dir));
58 assert!(at.is_symlink(link));
59 assert_eq!(at.resolve_link(link), dir);
60 }
61
62 #[test]
63 fn test_symlink_dangling_directory() {
64 let (at, mut ucmd) = at_and_ucmd!();
65 let dir = "test_symlink_dangling_dir";
66 let link = "test_symlink_dangling_dir_link";
67
68 ucmd.args(&["-s", dir, link]).succeeds().no_stderr();
69 assert!(!at.dir_exists(dir));
70 assert!(at.is_symlink(link));
71 assert_eq!(at.resolve_link(link), dir);
72 }
73
74 #[test]
75 fn test_symlink_circular() {
76 let (at, mut ucmd) = at_and_ucmd!();
77 let link = "test_symlink_circular";
78
79 ucmd.args(&["-s", link]).succeeds().no_stderr();
80 assert!(at.is_symlink(link));
81 assert_eq!(at.resolve_link(link), link);
82 }
83
84 #[test]
85 fn test_symlink_do_not_overwrite() {
86 let (at, mut ucmd) = at_and_ucmd!();
87 let file = "test_symlink_do_not_overwrite";
88 let link = "test_symlink_do_not_overwrite_link";
89
90 at.touch(file);
91 at.touch(link);
92
93 ucmd.args(&["-s", file, link]).fails();
94 assert!(at.file_exists(file));
95 assert!(at.file_exists(link));
96 assert!(!at.is_symlink(link));
97 }
98
99 #[test]
100 fn test_symlink_overwrite_force() {
101 let (at, mut ucmd) = at_and_ucmd!();
102 let file_a = "test_symlink_overwrite_force_a";
103 let file_b = "test_symlink_overwrite_force_b";
104 let link = "test_symlink_overwrite_force_link";
105
106 // Create symlink
107 at.symlink_file(file_a, link);
108 assert!(at.is_symlink(link));
109 assert_eq!(at.resolve_link(link), file_a);
110
111 // Force overwrite of existing symlink
112 ucmd.args(&["--force", "-s", file_b, link]).succeeds();
113 assert!(at.is_symlink(link));
114 assert_eq!(at.resolve_link(link), file_b);
115 }
116
117 /// A forced replace must be atomic, so a concurrent creator always loses.
118 /// Fails reliably if unlink-then-create ever comes back.
119 #[test]
120 // Android's app-private filesystem refuses hard links.
121 #[cfg(all(unix, not(any(target_os = "redox", target_os = "android"))))]
122 fn test_force_replace_never_leaves_the_destination_name_free() {
123 use std::sync::Arc;
124 use std::sync::atomic::{AtomicBool, Ordering};
125
126 for symbolic in [true, false] {
127 let scene = TestScenario::new(util_name!());
128 let at = &scene.fixtures;
129 at.touch("a");
130 at.touch("b");
131 if symbolic {
132 at.symlink_file("a", "link");
133 } else {
134 at.hard_link("a", "link");
135 }
136
137 let target = at.plus("link");
138 let stop = Arc::new(AtomicBool::new(false));
139 let claimed = Arc::new(AtomicBool::new(false));
140
141 let (racer_stop, racer_claimed) = (stop.clone(), claimed.clone());
142 let racer = std::thread::spawn(move || {
143 while !racer_stop.load(Ordering::Relaxed) {
144 // Succeeds only if the name is unoccupied at this instant.
145 if std::os::unix::fs::symlink("claimed-by-attacker", &target).is_ok() {
146 racer_claimed.store(true, Ordering::Relaxed);
147 return;
148 }
149 }
150 });
151
152 for _ in 0..100 {
153 let mut args = vec!["--force"];
154 if symbolic {
155 args.push("-s");
156 }
157 args.extend_from_slice(&["b", "link"]);
158 scene.ucmd().args(&args).succeeds();
159 }
160
161 stop.store(true, Ordering::Relaxed);
162 racer.join().unwrap();
163
164 assert!(
165 !claimed.load(Ordering::Relaxed),
166 "destination name was unoccupied during a forced replace (symbolic={symbolic})"
167 );
168 }
169 }
170
171 /// Replacing a destination that is already a link to the same inode leaves
172 /// `rename` with nothing to do, and the temporary must not survive that.
173 #[test]
174 // Android's app-private filesystem refuses hard links.
175 #[cfg(all(unix, not(any(target_os = "redox", target_os = "android"))))]
176 fn test_force_replace_same_inode_leaves_no_temp_file() {
177 let scene = TestScenario::new(util_name!());
178 let at = &scene.fixtures;
179 at.touch("a");
180 at.hard_link("a", "b");
181
182 scene.ucmd().args(&["--force", "a", "b"]).succeeds();
183
184 let leftovers: Vec<_> = std::fs::read_dir(at.as_string())
185 .unwrap()
186 .filter_map(|e| e.ok().map(|e| e.file_name().to_string_lossy().into_owned()))
187 .filter(|name| name != "a" && name != "b")
188 .collect();
189 assert!(
190 leftovers.is_empty(),
191 "forced replace left a temporary behind: {leftovers:?}"
192 );
193 }
194
195 #[test]
196 fn test_symlink_overwrite_force_overrides_interactive() {
197 let (at, mut ucmd) = at_and_ucmd!();
198 let file_a = "test_symlink_overwrite_force_a";
199 let file_b = "test_symlink_overwrite_force_b";
200 let link = "test_symlink_overwrite_force_link";
201
202 // Create symlink
203 at.symlink_file(file_a, link);
204 assert!(at.is_symlink(link));
205 assert_eq!(at.resolve_link(link), file_a);
206
207 // Force overwrite of existing symlink
208 ucmd.args(&["-i", "-f", "-s", file_b, link]).succeeds();
209 assert!(at.is_symlink(link));
210 assert_eq!(at.resolve_link(link), file_b);
211 }
212
213 #[test]
214 fn test_symlink_interactive() {
215 let scene = TestScenario::new(util_name!());
216 let at = &scene.fixtures;
217 let file = "test_symlink_interactive_file";
218 let link = "test_symlink_interactive_file_link";
219
220 at.touch(file);
221 at.touch(link);
222
223 scene
224 .ucmd()
225 .args(&["-i", "-s", file, link])
226 .pipe_in("n")
227 .fails()
228 .no_stdout();
229
230 assert!(at.file_exists(file));
231 assert!(!at.is_symlink(link));
232
233 scene
234 .ucmd()
235 .args(&["-i", "-s", file, link])
236 .pipe_in("Yesh") // spell-checker:disable-line
237 .succeeds()
238 .no_stdout();
239
240 assert!(at.file_exists(file));
241 assert!(at.is_symlink(link));
242 assert_eq!(at.resolve_link(link), file);
243 }
244
245 #[test]
246 fn test_symlink_interactive_overrides_force() {
247 let scene = TestScenario::new(util_name!());
248 let at = &scene.fixtures;
249 let file = "test_symlink_interactive_file";
250 let link = "test_symlink_interactive_file_link";
251
252 at.touch(file);
253 at.touch(link);
254
255 scene
256 .ucmd()
257 .args(&["-f", "-i", "-s", file, link])
258 .pipe_in("n")
259 .fails()
260 .no_stdout();
261
262 assert!(at.file_exists(file));
263 assert!(!at.is_symlink(link));
264
265 scene
266 .ucmd()
267 .args(&["-f", "-i", "-s", file, link])
268 .pipe_in("Yesh") // spell-checker:disable-line
269 .succeeds()
270 .no_stdout();
271
272 assert!(at.file_exists(file));
273 assert!(at.is_symlink(link));
274 assert_eq!(at.resolve_link(link), file);
275 }
276
277 #[test]
278 fn test_symlink_simple_backup() {
279 let (at, mut ucmd) = at_and_ucmd!();
280 let file = "test_symlink_simple_backup";
281 let link = "test_symlink_simple_backup_link";
282
283 at.touch(file);
284 at.symlink_file(file, link);
285 assert!(at.file_exists(file));
286 assert!(at.is_symlink(link));
287 assert_eq!(at.resolve_link(link), file);
288
289 ucmd.args(&["-b", "-s", file, link]).succeeds().no_stderr();
290
291 assert!(at.file_exists(file));
292
293 assert!(at.is_symlink(link));
294 assert_eq!(at.resolve_link(link), file);
295
296 let backup = &format!("{link}~");
297 assert!(at.is_symlink(backup));
298 assert_eq!(at.resolve_link(backup), file);
299 }
300
301 #[test]
302 fn test_symlink_custom_backup_suffix() {
303 let (at, mut ucmd) = at_and_ucmd!();
304 let file = "test_symlink_custom_backup_suffix";
305 let link = "test_symlink_custom_backup_suffix_link";
306 let suffix = "super-suffix-of-the-century";
307
308 at.touch(file);
309 at.symlink_file(file, link);
310 assert!(at.file_exists(file));
311 assert!(at.is_symlink(link));
312 assert_eq!(at.resolve_link(link), file);
313
314 let arg = &format!("--suffix={suffix}");
315 ucmd.args(&["-b", arg, "-s", file, link])
316 .succeeds()
317 .no_stderr();
318 assert!(at.file_exists(file));
319
320 assert!(at.is_symlink(link));
321 assert_eq!(at.resolve_link(link), file);
322
323 let backup = &format!("{link}{suffix}");
324 assert!(at.is_symlink(backup));
325 assert_eq!(at.resolve_link(backup), file);
326 }
327
328 #[test]
329 fn test_symlink_suffix_without_backup_option() {
330 let scene = TestScenario::new(util_name!());
331 let at = &scene.fixtures;
332
333 at.write("a", "a\n");
334 at.write("b", "b2\n");
335
336 assert!(at.file_exists("a"));
337 assert!(at.file_exists("b"));
338 let suffix = ".sfx";
339 let suffix_arg = &format!("--suffix={suffix}");
340 scene
341 .ucmd()
342 .args(&["-s", "-f", suffix_arg, "a", "b"])
343 .succeeds()
344 .no_stderr();
345 assert!(at.file_exists("a"));
346 assert!(at.file_exists("b"));
347 assert_eq!(at.read("a"), "a\n");
348 assert_eq!(at.read("b"), "a\n");
349 // we should have created backup for b file
350 assert_eq!(at.read(&format!("b{suffix}")), "b2\n");
351 }
352
353 #[test]
354 fn test_symlink_custom_backup_suffix_hyphen_value() {
355 let (at, mut ucmd) = at_and_ucmd!();
356 let file = "test_symlink_custom_backup_suffix";
357 let link = "test_symlink_custom_backup_suffix_link";
358 let suffix = "-v";
359
360 at.touch(file);
361 at.symlink_file(file, link);
362 assert!(at.file_exists(file));
363 assert!(at.is_symlink(link));
364 assert_eq!(at.resolve_link(link), file);
365
366 let arg = &format!("--suffix={suffix}");
367 ucmd.args(&["-b", arg, "-s", file, link])
368 .succeeds()
369 .no_stderr();
370 assert!(at.file_exists(file));
371
372 assert!(at.is_symlink(link));
373 assert_eq!(at.resolve_link(link), file);
374
375 let backup = &format!("{link}{suffix}");
376 assert!(at.is_symlink(backup));
377 assert_eq!(at.resolve_link(backup), file);
378 }
379
380 #[test]
381 fn test_symlink_backup_numbering() {
382 let (at, mut ucmd) = at_and_ucmd!();
383 let file = "test_symlink_backup_numbering";
384 let link = "test_symlink_backup_numbering_link";
385
386 at.touch(file);
387 at.symlink_file(file, link);
388 assert!(at.file_exists(file));
389 assert!(at.is_symlink(link));
390 assert_eq!(at.resolve_link(link), file);
391
392 ucmd.args(&["-s", "--backup=t", file, link])
393 .succeeds()
394 .no_stderr();
395 assert!(at.file_exists(file));
396
397 assert!(at.is_symlink(link));
398 assert_eq!(at.resolve_link(link), file);
399
400 let backup = &format!("{link}.~1~");
401 assert!(at.is_symlink(backup));
402 assert_eq!(at.resolve_link(backup), file);
403 }
404
405 #[test]
406 fn test_symlink_existing_backup() {
407 let (at, mut ucmd) = at_and_ucmd!();
408 let file = "test_symlink_existing_backup";
409 let link = "test_symlink_existing_backup_link";
410 let link_backup = "test_symlink_existing_backup_link.~1~";
411 let resulting_backup = "test_symlink_existing_backup_link.~2~";
412
413 // Create symlink and verify
414 at.touch(file);
415 at.symlink_file(file, link);
416 assert!(at.file_exists(file));
417 assert!(at.is_symlink(link));
418 assert_eq!(at.resolve_link(link), file);
419
420 // Create backup symlink and verify
421 at.symlink_file(file, link_backup);
422 assert!(at.file_exists(file));
423 assert!(at.is_symlink(link_backup));
424 assert_eq!(at.resolve_link(link_backup), file);
425
426 ucmd.args(&["-s", "--backup=nil", file, link])
427 .succeeds()
428 .no_stderr();
429 assert!(at.file_exists(file));
430
431 assert!(at.is_symlink(link_backup));
432 assert_eq!(at.resolve_link(link_backup), file);
433
434 assert!(at.is_symlink(resulting_backup));
435 assert_eq!(at.resolve_link(resulting_backup), file);
436 }
437
438 #[test]
439 fn test_symlink_target_dir() {
440 let (at, mut ucmd) = at_and_ucmd!();
441 let dir = "test_ln_target_dir_dir";
442 let file_a = "test_ln_target_dir_file_a";
443 let file_b = "test_ln_target_dir_file_b";
444
445 at.touch(file_a);
446 at.touch(file_b);
447 at.mkdir(dir);
448
449 ucmd.args(&["-s", "-t", dir, file_a, file_b])
450 .succeeds()
451 .no_stderr();
452
453 let file_a_link = &format!("{dir}/{file_a}");
454 assert!(at.is_symlink(file_a_link));
455 assert_eq!(at.resolve_link(file_a_link), file_a);
456
457 let file_b_link = &format!("{dir}/{file_b}");
458 assert!(at.is_symlink(file_b_link));
459 assert_eq!(at.resolve_link(file_b_link), file_b);
460 }
461
462 #[test]
463 #[cfg(target_os = "linux")]
464 #[cfg_attr(
465 wasi_runner,
466 ignore = "WASI: non-utf8 arguments cannot be passed through the spawned test harness"
467 )]
468 fn test_symlink_target_dir_non_utf8_source_name() {
469 use std::ffi::OsStr;
470 use std::fs;
471 use std::os::unix::ffi::OsStrExt;
472
473 let scene = TestScenario::new(util_name!());
474 let at = &scene.fixtures;
475 let target_dir = "test_symlink_target_dir_non_utf8";
476 let source_bytes = b"source_\xFF\xFE";
477 let source_name = OsStr::from_bytes(source_bytes);
478
479 at.mkdir(target_dir);
480 at.touch(source_name);
481
482 scene
483 .ucmd()
484 .args(&["-s", "-t", target_dir])
485 .arg(source_name)
486 .succeeds()
487 .no_stderr();
488
489 let target_dir_path = at.plus(target_dir);
490 let mut dir_entries = fs::read_dir(&target_dir_path)
491 .expect("reading target directory entries after creating symlink");
492 let created_entry = dir_entries
493 .next()
494 .expect("finding created entry in target directory")
495 .expect("reading created target-directory entry");
496 assert!(
497 dir_entries.next().is_none(),
498 "expected only one created entry in target directory"
499 );
500 assert_eq!(
501 created_entry.file_name().as_os_str().as_bytes(),
502 source_bytes
503 );
504
505 let created_link_path = target_dir_path.join(source_name);
506 let created_link_target = fs::read_link(&created_link_path)
507 .expect("reading created symlink target in target directory");
508 assert_eq!(created_link_target.as_os_str().as_bytes(), source_bytes);
509 }
510
511 #[test]
512 fn test_symlink_target_dir_from_dir() {
513 let (at, mut ucmd) = at_and_ucmd!();
514 let dir = "test_ln_target_dir_dir";
515 let from_dir = "test_ln_target_dir_from_dir";
516 let filename_a = "test_ln_target_dir_file_a";
517 let filename_b = "test_ln_target_dir_file_b";
518 let file_a = &format!("{from_dir}/{filename_a}");
519 let file_b = &format!("{from_dir}/{filename_b}");
520
521 at.mkdir(from_dir);
522 at.touch(file_a);
523 at.touch(file_b);
524 at.mkdir(dir);
525
526 ucmd.args(&["-s", "-t", dir, file_a, file_b])
527 .succeeds()
528 .no_stderr();
529
530 let file_a_link = &format!("{dir}/{filename_a}");
531 assert!(at.is_symlink(file_a_link));
532 assert_eq!(&at.resolve_link(file_a_link), file_a);
533
534 let file_b_link = &format!("{dir}/{filename_b}");
535 assert!(at.is_symlink(file_b_link));
536 assert_eq!(&at.resolve_link(file_b_link), file_b);
537 }
538
539 #[test]
540 fn test_symlink_overwrite_dir_fail() {
541 let (at, mut ucmd) = at_and_ucmd!();
542 let path_a = "test_symlink_overwrite_dir_a";
543 let path_b = "test_symlink_overwrite_dir_b";
544
545 at.touch(path_a);
546 at.mkdir(path_b);
547
548 assert!(
549 !ucmd
550 .args(&["-s", "-T", path_a, path_b])
551 .fails()
552 .stderr_str()
553 .is_empty()
554 );
555 }
556
557 #[test]
558 fn test_symlink_errors() {
559 let (at, mut ucmd) = at_and_ucmd!();
560 let dir = "test_symlink_errors_dir";
561 let file_a = "test_symlink_errors_file_a";
562 let file_b = "test_symlink_errors_file_b";
563
564 at.mkdir(dir);
565 at.touch(file_a);
566 at.touch(file_b);
567
568 // $ ln -T -t a b
569 // ln: cannot combine --target-directory (-t) and --no-target-directory (-T)
570 ucmd.args(&["-T", "-t", dir, file_a, file_b]).fails();
571 }
572
573 #[test]
574 fn test_symlink_verbose() {
575 let scene = TestScenario::new(util_name!());
576 let at = &scene.fixtures;
577 let file_a = "test_symlink_verbose_file_a";
578 let file_b = "test_symlink_verbose_file_b";
579
580 at.touch(file_a);
581
582 scene
583 .ucmd()
584 .args(&["-s", "-v", file_a, file_b])
585 .succeeds()
586 .stdout_only(format!("'{file_b}' -> '{file_a}'\n"));
587
588 at.touch(file_b);
589
590 scene
591 .ucmd()
592 .args(&["-s", "-v", "-b", file_a, file_b])
593 .succeeds()
594 .stdout_only(format!("'{file_b}' -> '{file_a}' (backup: '{file_b}~')\n"));
595 }
596
597 #[test]
598 fn test_symlink_target_only() {
599 let (at, mut ucmd) = at_and_ucmd!();
600 let dir = "test_symlink_target_only";
601
602 at.mkdir(dir);
603
604 assert!(
605 !ucmd
606 .args(&["-s", "-t", dir])
607 .fails()
608 .stderr_str()
609 .is_empty()
610 );
611 }
612
613 #[test]
614 fn test_symlink_implicit_target_dir() {
615 let (at, mut ucmd) = at_and_ucmd!();
616 let dir = "test_symlink_implicit_target_dir";
617 // On windows, slashes aren't allowed in symlink targets, so use
618 // PathBuf to construct `file` instead of simple "dir/file".
619 let filename = "test_symlink_implicit_target_file";
620 let path = PathBuf::from(dir).join(filename);
621 let file = &path.to_string_lossy();
622
623 at.mkdir(dir);
624 at.touch(&path);
625
626 ucmd.args(&["-s", file]).succeeds().no_stderr();
627
628 assert!(at.file_exists(filename));
629 assert!(at.is_symlink(filename));
630 assert_eq!(at.resolve_link(filename), *file);
631 }
632
633 #[test]
634 #[cfg_attr(wasi_runner, ignore = "WASI sandbox: host paths not visible")]
635 fn test_symlink_to_dir_2args() {
636 let (at, mut ucmd) = at_and_ucmd!();
637 let filename = "test_symlink_to_dir_2args_file";
638 let from_file = &format!("{}/{filename}", at.as_string());
639 let to_dir = "test_symlink_to_dir_2args_to_dir";
640 let to_file = &format!("{to_dir}/{filename}");
641
642 at.mkdir(to_dir);
643 at.touch(from_file);
644
645 ucmd.args(&["-s", from_file, to_dir]).succeeds().no_stderr();
646
647 assert!(at.file_exists(to_file));
648 assert!(at.is_symlink(to_file));
649 assert_eq!(at.resolve_link(to_file), filename);
650 }
651
652 #[test]
653 fn test_symlink_missing_destination() {
654 let (at, mut ucmd) = at_and_ucmd!();
655 let file = "test_symlink_missing_destination";
656
657 at.touch(file);
658
659 ucmd.args(&["-s", "-T", file]).fails().stderr_is(format!(
660 "ln: missing destination file operand after '{file}'\n"
661 ));
662 }
663
664 #[test]
665 fn test_symlink_error_includes_destination() {
666 let (at, mut ucmd) = at_and_ucmd!();
667 let file = "test_symlink_error_includes_destination";
668 let link = "no_such_dir/test_symlink_error_includes_destination";
669
670 at.touch(file);
671
672 ucmd.args(&["-s", file, link]).fails().stderr_is(format!(
673 "ln: failed to create symbolic link '{link}': No such file or directory\n"
674 ));
675 }
676
677 #[test]
678 fn test_symlink_relative() {
679 let (at, mut ucmd) = at_and_ucmd!();
680 let file_a = "test_symlink_relative_a";
681 let link = "test_symlink_relative_link";
682
683 at.touch(file_a);
684
685 // relative symlink
686 ucmd.args(&["-r", "-s", file_a, link]).succeeds();
687 assert!(at.is_symlink(link));
688 assert_eq!(at.resolve_link(link), file_a);
689 }
690
691 #[test]
692 fn test_symlink_relative_path() {
693 let (at, mut ucmd) = at_and_ucmd!();
694 let dir = "test_symlink_existing_dir";
695 let file_a = "test_symlink_relative_a";
696 let link = "test_symlink_relative_link";
697 let multi_dir =
698 "test_symlink_existing_dir/../test_symlink_existing_dir/../test_symlink_existing_dir/../";
699 let p = PathBuf::from(multi_dir).join(file_a);
700 at.mkdir(dir);
701
702 // relative symlink
703 // Thanks to -r, all the ../ should be resolved to a single file
704 ucmd.args(&["-r", "-s", "-v", &p.to_string_lossy(), link])
705 .succeeds()
706 .stdout_only(format!("'{link}' -> '{file_a}'\n"));
707 assert!(at.is_symlink(link));
708 assert_eq!(at.resolve_link(link), file_a);
709
710 // Run the same command without -r to verify that we keep the full
711 // crazy path
712 let (at, mut ucmd) = at_and_ucmd!();
713 ucmd.args(&["-s", "-v", &p.to_string_lossy(), link])
714 .succeeds()
715 .stdout_only(format!("'{link}' -> '{}'\n", p.to_string_lossy()));
716 assert!(at.is_symlink(link));
717 assert_eq!(at.resolve_link(link), p.to_string_lossy());
718 }
719
720 #[test]
721 fn test_symlink_relative_dir() {
722 let (at, mut ucmd) = at_and_ucmd!();
723
724 let dir = "test_symlink_existing_dir";
725 let link = "test_symlink_existing_dir_link";
726
727 at.mkdir(dir);
728
729 ucmd.args(&["-s", "-r", dir, link]).succeeds().no_stderr();
730 assert!(at.dir_exists(dir));
731 assert!(at.is_symlink(link));
732 assert_eq!(at.resolve_link(link), dir);
733 }
734
735 #[test]
736 fn test_symlink_no_deref_dir() {
737 let scene = TestScenario::new(util_name!());
738 let at = &scene.fixtures;
739
740 let dir1 = "foo";
741 let dir2 = "bar";
742 let link = "baz";
743
744 at.mkdir(dir1);
745 at.mkdir(dir2);
746 scene
747 .ucmd()
748 .args(&["-s", dir2, link])
749 .succeeds()
750 .no_stderr();
751 assert!(at.dir_exists(dir1));
752 assert!(at.dir_exists(dir2));
753 assert!(at.is_symlink(link));
754 assert_eq!(at.resolve_link(link), dir2);
755
756 // try the normal behavior
757 scene
758 .ucmd()
759 .args(&["-sf", dir1, link])
760 .succeeds()
761 .no_stderr();
762 assert!(at.dir_exists(dir1));
763 assert!(at.dir_exists(dir2));
764 assert!(at.is_symlink("baz/foo"));
765 assert_eq!(at.resolve_link("baz/foo"), dir1);
766
767 // Doesn't work without the force
768 scene.ucmd().args(&["-sn", dir1, link]).fails();
769
770 // Try with the no-deref
771 scene
772 .ucmd()
773 .args(&["-sfn", dir1, link])
774 .succeeds()
775 .no_stderr();
776 assert!(at.dir_exists(dir1));
777 assert!(at.dir_exists(dir2));
778 assert!(at.is_symlink(link));
779 assert_eq!(at.resolve_link(link), dir1);
780 }
781
782 #[test]
783 fn test_symlink_no_deref_file_in_destination_dir() {
784 let scene = TestScenario::new(util_name!());
785 let at = &scene.fixtures;
786
787 let file1 = "foo";
788 let file2 = "bar";
789
790 let dest = "baz";
791
792 let link1 = "baz/foo";
793 let link2 = "baz/bar";
794
795 at.touch(file1);
796 at.touch(file2);
797 at.mkdir(dest);
798
799 assert!(at.file_exists(file1));
800 assert!(at.file_exists(file2));
801 assert!(at.dir_exists(dest));
802
803 // -n and -f should work alone
804 scene
805 .ucmd()
806 .args(&["-sn", file1, dest])
807 .succeeds()
808 .no_stderr();
809 assert!(at.is_symlink(link1));
810 assert_eq!(at.resolve_link(link1), file1);
811
812 scene
813 .ucmd()
814 .args(&["-sf", file1, dest])
815 .succeeds()
816 .no_stderr();
817 assert!(at.is_symlink(link1));
818 assert_eq!(at.resolve_link(link1), file1);
819
820 // -n alone should fail if destination exists already (it should now)
821 scene.ucmd().args(&["-sn", file1, dest]).fails();
822
823 // -nf should also work
824 scene
825 .ucmd()
826 .args(&["-snf", file1, dest])
827 .succeeds()
828 .no_stderr();
829 assert!(at.is_symlink(link1));
830 assert_eq!(at.resolve_link(link1), file1);
831
832 scene
833 .ucmd()
834 .args(&["-snf", file1, file2, dest])
835 .succeeds()
836 .no_stderr();
837 assert!(at.is_symlink(link1));
838 assert_eq!(at.resolve_link(link1), file1);
839 assert!(at.is_symlink(link2));
840 assert_eq!(at.resolve_link(link2), file2);
841 }
842
843 #[test]
844 fn test_symlink_no_deref_file() {
845 let scene = TestScenario::new(util_name!());
846 let at = &scene.fixtures;
847
848 let file1 = "foo";
849 let file2 = "bar";
850 let link = "baz";
851
852 at.touch(file1);
853 at.touch(file2);
854 scene
855 .ucmd()
856 .args(&["-s", file2, link])
857 .succeeds()
858 .no_stderr();
859 assert!(at.file_exists(file1));
860 assert!(at.file_exists(file2));
861 assert!(at.is_symlink(link));
862 assert_eq!(at.resolve_link(link), file2);
863
864 // try the normal behavior
865 scene
866 .ucmd()
867 .args(&["-sf", file1, link])
868 .succeeds()
869 .no_stderr();
870 assert!(at.file_exists(file1));
871 assert!(at.file_exists(file2));
872 assert!(at.is_symlink("baz"));
873 assert_eq!(at.resolve_link("baz"), file1);
874
875 // Doesn't work without the force
876 scene.ucmd().args(&["-sn", file1, link]).fails();
877
878 // Try with the no-deref
879 scene.ucmd().args(&["-sfn", file1, link]).succeeds();
880 assert!(at.file_exists(file1));
881 assert!(at.file_exists(file2));
882 assert!(at.is_symlink(link));
883 assert_eq!(at.resolve_link(link), file1);
884 }
885
886 #[test]
887 fn test_relative_requires_symbolic() {
888 new_ucmd!().args(&["-r", "foo", "bar"]).fails();
889 }
890
891 #[test]
892 fn test_relative_target_with_no_parent() {
893 let (at, mut ucmd) = at_and_ucmd!();
894 at.write("src", "data");
895 ucmd.args(&["-rsfT", "src", ""]).fails_with_code(1);
896 }
897
898 #[test]
899 fn test_relative_dst_already_symlink() {
900 let (at, mut ucmd) = at_and_ucmd!();
901 at.touch("file1");
902 at.symlink_file("file1", "file2");
903 ucmd.arg("-srf").arg("file1").arg("file2").succeeds();
904 at.is_symlink("file2");
905 }
906
907 #[test]
908 #[cfg_attr(
909 wasi_runner,
910 ignore = "WASI: read_link on absolute paths fails under wasmtime via spawned test harness"
911 )]
912 fn test_relative_src_already_symlink() {
913 let (at, mut ucmd) = at_and_ucmd!();
914 at.touch("file1");
915 at.symlink_file("file1", "file2");
916 ucmd.arg("-sr").arg("file2").arg("file3").succeeds();
917 assert!(at.resolve_link("file3").ends_with("file1"));
918 }
919
920 #[test]
921 fn test_relative_recursive() {
922 let (at, mut ucmd) = at_and_ucmd!();
923 at.mkdir("dir");
924 ucmd.args(&["-sr", "dir", "dir/recursive"]).succeeds();
925 assert_eq!(at.resolve_link("dir/recursive"), ".");
926 }
927
928 #[test]
929 fn test_backup_same_file() {
930 let (at, mut ucmd) = at_and_ucmd!();
931 at.touch("file1");
932 ucmd.args(&["--backup", "file1", "./file1"])
933 .fails()
934 .stderr_contains("n: 'file1' and './file1' are the same file");
935 }
936
937 #[test]
938 #[cfg(not(target_os = "android"))]
939 fn test_backup_existing_hard_linked_under_different_name() {
940 let (at, mut ucmd) = at_and_ucmd!();
941 at.touch("a");
942 at.hard_link("a", "b");
943
944 ucmd.args(&["--backup", "a", "b"]).succeeds().no_stderr();
945
946 assert!(at.file_exists("a"));
947 assert!(at.file_exists("b"));
948 assert!(at.file_exists("b~"));
949 }
950
951 #[test]
952 #[cfg(all(unix, not(target_os = "android")))]
953 fn test_backup_existing_hard_linked_target_is_fifo() {
954 let (at, mut ucmd) = at_and_ucmd!();
955 at.touch("a");
956 at.hard_link("a", "b");
957 at.mkfifo("b~");
958
959 ucmd.args(&["--backup", "a", "b"])
960 .timeout(Duration::from_secs(10))
961 .succeeds()
962 .no_stderr();
963
964 assert!(at.file_exists("a"));
965 assert!(at.file_exists("b"));
966 assert!(!at.is_fifo("b~"));
967 }
968
969 #[test]
970 fn test_backup_force() {
971 let scene = TestScenario::new(util_name!());
972 let at = &scene.fixtures;
973
974 at.write("a", "a\n");
975 at.write("b", "b2\n");
976
977 scene.ucmd().args(&["-s", "b", "b~"]).succeeds().no_stderr();
978 assert!(at.file_exists("a"));
979 assert!(at.file_exists("b"));
980 assert!(at.file_exists("b~"));
981 scene
982 .ucmd()
983 .args(&["-s", "-f", "--b=simple", "a", "b"])
984 .succeeds()
985 .no_stderr();
986 assert!(at.file_exists("a"));
987 assert!(at.file_exists("b"));
988 assert!(at.file_exists("b~"));
989 assert_eq!(at.read("a"), "a\n");
990 assert_eq!(at.read("b"), "a\n");
991 // we should have the same content as b as we had time to do a backup
992 assert_eq!(at.read("b~"), "b2\n");
993 }
994
995 #[test]
996 fn test_hard_logical() {
997 let (at, mut ucmd) = at_and_ucmd!();
998 let file_a = "file1";
999 let link = "symlink1";
1000 let target = "hard-to-a";
1001 let target2 = "hard-to-a2";
1002 at.touch(file_a);
1003 at.symlink_file(file_a, link);
1004
1005 ucmd.args(&["-P", "-L", link, target]);
1006 assert!(!at.is_symlink(target));
1007
1008 ucmd.args(&["-P", "-L", "-s", link, target2]);
1009 assert!(!at.is_symlink(target2));
1010 }
1011
1012 #[test]
1013 fn test_hard_logical_non_exit_fail() {
1014 let scene = TestScenario::new(util_name!());
1015 let at = &scene.fixtures;
1016
1017 let file_a = "/no-such-dir";
1018 let link = "hard-to-dangle";
1019
1020 at.relative_symlink_dir(file_a, "no-such-dir");
1021
1022 scene
1023 .ucmd()
1024 .args(&["-L", "no-such-dir", link])
1025 .fails()
1026 .stderr_contains("failed to access 'no-such-dir'");
1027 }
1028
1029 #[test]
1030 fn test_hard_logical_dir_fail() {
1031 let scene = TestScenario::new(util_name!());
1032 let at = &scene.fixtures;
1033 let dir = "d";
1034 at.mkdir(dir);
1035 let target = "link-to-dir";
1036
1037 scene.ucmd().args(&["-s", dir, target]);
1038
1039 scene
1040 .ucmd()
1041 .args(&["-L", target, "hard-to-dir-link"])
1042 .fails()
1043 .stderr_contains("failed to create hard link 'hard-to-dir-link'");
1044 }
1045
1046 #[test]
1047 fn test_symlink_remove_existing_same_src_and_dest() {
1048 let (at, mut ucmd) = at_and_ucmd!();
1049 at.touch("a");
1050 at.write("a", "sample");
1051 ucmd.args(&["-sf", "a", "a"])
1052 .fails_with_code(1)
1053 .stderr_contains("'a' and 'a' are the same file");
1054 assert!(at.file_exists("a") && !at.symlink_exists("a"));
1055 assert_eq!(at.read("a"), "sample");
1056 }
1057
1058 #[test]
1059 fn test_force_same_file_detected_after_canonicalization() {
1060 let (at, mut ucmd) = at_and_ucmd!();
1061
1062 at.write("file", "hello");
1063
1064 ucmd.args(&["-f", "file", "./file"])
1065 .fails_with_code(1)
1066 .stderr_contains("are the same file");
1067
1068 assert!(at.file_exists("file"));
1069 assert_eq!(at.read("file"), "hello");
1070 }
1071
1072 #[test]
1073 #[cfg(not(target_os = "android"))]
1074 fn test_force_ln_existing_hard_link_entry() {
1075 let scene = TestScenario::new(util_name!());
1076 let at = &scene.fixtures;
1077
1078 at.write("file", "hardlink\n");
1079 at.mkdir("dir");
1080
1081 scene.ucmd().args(&["file", "dir"]).succeeds().no_stderr();
1082 assert!(at.file_exists("dir/file"));
1083
1084 scene
1085 .ucmd()
1086 .args(&["-f", "file", "dir"])
1087 .succeeds()
1088 .no_stderr();
1089
1090 assert!(at.file_exists("file"));
1091 assert!(at.file_exists("dir/file"));
1092 assert_eq!(at.read("file"), "hardlink\n");
1093 assert_eq!(at.read("dir/file"), "hardlink\n");
1094
1095 #[cfg(unix)]
1096 {
1097 use std::os::unix::fs::MetadataExt;
1098 let source_inode = at.metadata("file").ino();
1099 let target_inode = at.metadata("dir/file").ino();
1100 assert_eq!(source_inode, target_inode);
1101 }
1102 }
1103
1104 #[test]
1105 #[cfg(not(target_os = "android"))]
1106 fn test_ln_seen_file() {
1107 let ts = TestScenario::new(util_name!());
1108 let at = &ts.fixtures;
1109
1110 at.mkdir("a");
1111 at.mkdir("b");
1112 at.mkdir("c");
1113 at.write("a/f", "a");
1114 at.write("b/f", "b");
1115
1116 let result = ts.ucmd().arg("a/f").arg("b/f").arg("c").fails();
1117
1118 #[cfg(not(unix))]
1119 assert!(
1120 result
1121 .stderr_str()
1122 .contains("will not overwrite just-created 'c\\f' with 'b/f'")
1123 );
1124 #[cfg(unix)]
1125 assert!(
1126 result
1127 .stderr_str()
1128 .contains("will not overwrite just-created 'c/f' with 'b/f'")
1129 );
1130
1131 assert!(at.plus("c").join("f").exists());
1132 // b/f still exists
1133 assert!(at.plus("b").join("f").exists());
1134 // a/f still exists
1135 assert!(at.plus("a").join("f").exists());
1136 #[cfg(unix)]
1137 {
1138 use std::os::unix::fs::MetadataExt;
1139 // Check inode numbers
1140 let inode_a_f = at.plus("a").join("f").metadata().unwrap().ino();
1141 let inode_b_f = at.plus("b").join("f").metadata().unwrap().ino();
1142 let inode_c_f = at.plus("c").join("f").metadata().unwrap().ino();
1143
1144 assert_eq!(
1145 inode_a_f, inode_c_f,
1146 "Inode numbers of a/f and c/f should be equal"
1147 );
1148 assert_ne!(
1149 inode_b_f, inode_c_f,
1150 "Inode numbers of b/f and c/f should not be equal"
1151 );
1152 }
1153 }
1154
1155 #[test]
1156 #[cfg(target_os = "linux")]
1157 #[cfg_attr(wasi_runner, ignore = "WASI: argv/filenames must be valid UTF-8")]
1158 fn test_ln_non_utf8_paths() {
1159 use std::ffi::OsStr;
1160 use std::os::unix::ffi::OsStrExt;
1161
1162 let scene = TestScenario::new(util_name!());
1163 let at = &scene.fixtures;
1164
1165 // Create a test file with non-UTF-8 bytes in the name
1166 let non_utf8_bytes = b"test_\xFF\xFE.txt";
1167 let non_utf8_name = OsStr::from_bytes(non_utf8_bytes);
1168 let non_utf8_link_bytes = b"link_\xFF\xFE.txt";
1169 let non_utf8_link_name = OsStr::from_bytes(non_utf8_link_bytes);
1170
1171 // Create the actual file
1172 at.touch(non_utf8_name);
1173
1174 // Test creating a hard link with non-UTF-8 file names
1175 scene
1176 .ucmd()
1177 .arg(non_utf8_name)
1178 .arg(non_utf8_link_name)
1179 .succeeds();
1180
1181 // Both files should exist
1182 assert!(at.file_exists(non_utf8_name));
1183 assert!(at.file_exists(non_utf8_link_name));
1184
1185 // Test creating a symbolic link with non-UTF-8 file names
1186 let symlink_bytes = b"symlink_\xFF\xFE.txt";
1187 let symlink_name = OsStr::from_bytes(symlink_bytes);
1188
1189 scene
1190 .ucmd()
1191 .args(&["-s"])
1192 .arg(non_utf8_name)
1193 .arg(symlink_name)
1194 .succeeds();
1195
1196 // Check if symlink was created successfully
1197 let symlink_path = at.plus(symlink_name);
1198 assert!(symlink_path.is_symlink());
1199 }
1200
1201 #[test]
1202 fn test_ln_hard_link_dir() {
1203 let scene = TestScenario::new(util_name!());
1204 let at = &scene.fixtures;
1205
1206 at.mkdir("dir");
1207
1208 scene
1209 .ucmd()
1210 .args(&["dir", "dir_link"])
1211 .fails()
1212 .stderr_contains("hard link not allowed for directory");
1213 }
1214
1215 #[test]
1216 fn test_ln_backup_no_path_traversal() {
1217 let scene = TestScenario::new(util_name!());
1218 let at = &scene.fixtures;
1219
1220 at.touch("a");
1221 at.touch("b");
1222 at.mkdir("b_");
1223
1224 scene
1225 .ucmd()
1226 .args(&["-S", "_/../c", "-s", "a", "b"])
1227 .succeeds();
1228
1229 assert!(!at.file_exists("c"));
1230 assert!(at.plus("b").is_symlink());
1231 assert!(at.file_exists("b~"));
1232 assert!(!at.plus("b~").is_symlink());
1233 }
1234
1235 #[test]
1236 fn test_ln_no_dereference_symbolic() {
1237 let scene = TestScenario::new(util_name!());
1238 let at = &scene.fixtures;
1239 at.mkdir("a");
1240 at.symlink_dir("a", "b");
1241 at.touch("x");
1242 scene
1243 .ucmd()
1244 .args(&["-n", "x", "b"])
1245 .fails()
1246 .stderr_contains("Already exists");
1247 assert!(!at.file_exists("a/x"));
1248 #[cfg(not(target_os = "android"))]
1249 {
1250 scene.ucmd().args(&["-bn", "x", "b"]).succeeds();
1251 assert!(!at.file_exists("a/x"));
1252 assert!(at.file_exists("b"));
1253 assert!(at.is_symlink("b~"));
1254 }
1255 }
1256
1257 #[test]
1258 fn test_ln_backup_nonexistent_rollback() {
1259 let scene = TestScenario::new(util_name!());
1260 let at = &scene.fixtures;
1261
1262 at.touch("dst");
1263
1264 scene
1265 .ucmd()
1266 .args(&["--backup", "/non/existent/path", "dst"])
1267 .fails();
1268
1269 assert!(!at.file_exists("dst~"));
1270 assert!(at.file_exists("dst"));
1271 }
1272
1273 #[test]
1274 fn test_hard_link_force_failed_link_keeps_destination() {
1275 // Regression for #14550: a failed forced link must not delete the destination.
1276 let (at, mut ucmd) = at_and_ucmd!();
1277 at.write("dst", "keep\n");
1278
1279 ucmd.args(&["-f", "no_such_source", "dst"]).fails();
1280
1281 assert!(at.file_exists("dst"));
1282 assert_eq!(at.read("dst"), "keep\n");
1283 }