|
4 | 4 | // file that was distributed with this source code.
|
5 | 5 |
|
6 | 6 | // spell-checker:ignore (flags) reflink (fs) tmpfs (linux) rlimit Rlim NOFILE clob btrfs neve ROOTDIR USERDIR outfile uufs xattrs
|
7 |
| -// spell-checker:ignore bdfl hlsl IRWXO IRWXG nconfined matchpathcon libselinux-devel |
| 7 | +// spell-checker:ignore bdfl hlsl IRWXO IRWXG nconfined matchpathcon libselinux-devel prwx doesnotexist |
8 | 8 | use uucore::display::Quotable;
|
9 | 9 | use uutests::util::TestScenario;
|
10 | 10 | use uutests::{at_and_ucmd, new_ucmd, path_concat, util_name};
|
@@ -3087,13 +3087,89 @@ fn test_cp_link_backup() {
|
3087 | 3087 | fn test_cp_fifo() {
|
3088 | 3088 | let (at, mut ucmd) = at_and_ucmd!();
|
3089 | 3089 | at.mkfifo("fifo");
|
3090 |
| - ucmd.arg("-r") |
| 3090 | + // Also test that permissions are preserved |
| 3091 | + at.set_mode("fifo", 0o731); |
| 3092 | + ucmd.arg("--preserve=mode") |
| 3093 | + .arg("-r") |
3091 | 3094 | .arg("fifo")
|
3092 | 3095 | .arg("fifo2")
|
3093 | 3096 | .succeeds()
|
3094 | 3097 | .no_stderr()
|
3095 | 3098 | .no_stdout();
|
3096 | 3099 | assert!(at.is_fifo("fifo2"));
|
| 3100 | + |
| 3101 | + let metadata = std::fs::metadata(at.subdir.join("fifo2")).unwrap(); |
| 3102 | + let permission = uucore::fs::display_permissions(&metadata, true); |
| 3103 | + assert_eq!(permission, "prwx-wx--x".to_string()); |
| 3104 | +} |
| 3105 | + |
| 3106 | +#[cfg(all(unix, not(target_vendor = "apple")))] |
| 3107 | +fn find_other_group(current: u32) -> Option<u32> { |
| 3108 | + // Get the first group that doesn't match current |
| 3109 | + nix::unistd::getgroups().ok()?.iter().find_map(|group| { |
| 3110 | + let gid = group.as_raw(); |
| 3111 | + (gid != current).then_some(gid) |
| 3112 | + }) |
| 3113 | +} |
| 3114 | + |
| 3115 | +#[cfg(target_vendor = "apple")] |
| 3116 | +fn find_other_group(_current: u32) -> Option<u32> { |
| 3117 | + None |
| 3118 | +} |
| 3119 | + |
| 3120 | +#[test] |
| 3121 | +#[cfg(unix)] |
| 3122 | +fn test_cp_r_symlink() { |
| 3123 | + let (at, mut ucmd) = at_and_ucmd!(); |
| 3124 | + // Specifically test copying a link in a subdirectory, as the internal path |
| 3125 | + // is slightly different. |
| 3126 | + at.mkdir("tmp"); |
| 3127 | + // Create a symlink to a non-existent file to make sure |
| 3128 | + // we don't try to resolve it. |
| 3129 | + at.symlink_file("doesnotexist", "tmp/symlink"); |
| 3130 | + let symlink = at.subdir.join("tmp").join("symlink"); |
| 3131 | + |
| 3132 | + // If we can find such a group, change the owner to a non-default to test |
| 3133 | + // that (group) ownership is preserved. |
| 3134 | + let metadata = std::fs::symlink_metadata(&symlink).unwrap(); |
| 3135 | + let other_gid = find_other_group(metadata.gid()); |
| 3136 | + if let Some(gid) = other_gid { |
| 3137 | + uucore::perms::wrap_chown( |
| 3138 | + &symlink, |
| 3139 | + &metadata, |
| 3140 | + None, |
| 3141 | + Some(gid), |
| 3142 | + false, |
| 3143 | + uucore::perms::Verbosity::default(), |
| 3144 | + ) |
| 3145 | + .expect("Cannot chgrp symlink."); |
| 3146 | + } else { |
| 3147 | + println!("Cannot find a second group to chgrp to."); |
| 3148 | + } |
| 3149 | + |
| 3150 | + // Use -r to make sure we copy the symlink itself |
| 3151 | + // --preserve will include ownership |
| 3152 | + ucmd.arg("--preserve") |
| 3153 | + .arg("-r") |
| 3154 | + .arg("tmp") |
| 3155 | + .arg("tmp2") |
| 3156 | + .succeeds() |
| 3157 | + .no_stderr() |
| 3158 | + .no_stdout(); |
| 3159 | + |
| 3160 | + // Is symlink2 still a symlink, and does it point at the same place? |
| 3161 | + assert!(at.is_symlink("tmp2/symlink")); |
| 3162 | + let symlink2 = at.subdir.join("tmp2/symlink"); |
| 3163 | + assert_eq!( |
| 3164 | + std::fs::read_link(&symlink).unwrap(), |
| 3165 | + std::fs::read_link(&symlink2).unwrap(), |
| 3166 | + ); |
| 3167 | + |
| 3168 | + // If we found a suitable group, is the group correct after the copy. |
| 3169 | + if let Some(gid) = other_gid { |
| 3170 | + let metadata2 = std::fs::symlink_metadata(&symlink2).unwrap(); |
| 3171 | + assert_eq!(metadata2.gid(), gid); |
| 3172 | + } |
3097 | 3173 | }
|
3098 | 3174 |
|
3099 | 3175 | #[test]
|
|
0 commit comments