Skip to content

Commit 2916d2b

Browse files
cp: fix preserve-gid when canonicalize fails due to inaccessible parent dirs (#9803)
Co-authored-by: Sylvestre Ledru <sylvestre@debian.org>
1 parent 28ac732 commit 2916d2b

1 file changed

Lines changed: 7 additions & 5 deletions

File tree

src/uu/cp/src/cp.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2510,11 +2510,13 @@ fn copy_file(
25102510
}
25112511

25122512
if options.dereference(source_in_command_line) {
2513-
if let Ok(src) = canonicalize(source, MissingHandling::Normal, ResolveMode::Physical) {
2514-
if src.exists() {
2515-
copy_attributes(&src, dest, &options.attributes)?;
2516-
}
2517-
}
2513+
// Try to canonicalize, but if it fails (e.g., due to inaccessible parent directories),
2514+
// fall back to the original source path
2515+
let src_for_attrs = canonicalize(source, MissingHandling::Normal, ResolveMode::Physical)
2516+
.ok()
2517+
.filter(|p| p.exists())
2518+
.unwrap_or_else(|| source.to_path_buf());
2519+
copy_attributes(&src_for_attrs, dest, &options.attributes)?;
25182520
} else if source_is_stream && !source.exists() {
25192521
// Some stream files may not exist after we have copied it,
25202522
// like anonymous pipes. Thus, we can't really copy its

0 commit comments

Comments
 (0)