Skip to content

Commit e6af615

Browse files
committed
cp: use libc stat for destination check to support LD_PRELOAD tests
1 parent 2da2c90 commit e6af615

File tree

3 files changed

+8
-3
lines changed

3 files changed

+8
-3
lines changed

src/uu/cp/src/cp.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,8 +1390,7 @@ pub fn copy(sources: &[PathBuf], target: &Path, options: &Options) -> CopyResult
13901390
let dest = construct_dest_path(source, target, target_type, options)
13911391
.unwrap_or_else(|_| target.to_path_buf());
13921392

1393-
if fs::metadata(&dest).is_ok()
1394-
&& !fs::symlink_metadata(&dest)?.file_type().is_symlink()
1393+
if FileInformation::from_path(&dest, false).is_ok_and(|info| !info.is_symlink())
13951394
// if both `source` and `dest` are symlinks, it should be considered as an overwrite.
13961395
|| fs::metadata(source).is_ok()
13971396
&& fs::symlink_metadata(source)?.file_type().is_symlink()

src/uucore/src/lib/features/fs.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,12 @@ impl FileInformation {
166166
#[cfg(any(target_os = "netbsd", not(target_pointer_width = "64")))]
167167
return self.0.st_ino.into();
168168
}
169+
170+
/// Returns true if the file is a symbolic link.
171+
#[cfg(unix)]
172+
pub fn is_symlink(&self) -> bool {
173+
(self.0.st_mode as mode_t & S_IFMT) == S_IFLNK
174+
}
169175
}
170176

171177
#[cfg(unix)]

tests/by-util/test_cp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3118,7 +3118,7 @@ fn test_cp_archive_on_nonexistent_file() {
31183118
.arg(TEST_EXISTING_FILE)
31193119
.fails()
31203120
.stderr_only(
3121-
"cp: cannot stat 'nonexistent_file.txt': No such file or directory (os error 2)\n",
3121+
"cp: cannot stat 'nonexistent_file.txt': No such file or directory\n",
31223122
);
31233123
}
31243124

0 commit comments

Comments
 (0)