Skip to content

Commit 5a322e1

Browse files
authored
Merge pull request #8385 from drinkcat/cp-try-gid-only
cp: Try to change gid only if changing uid+gid fails
2 parents c403958 + 8c45737 commit 5a322e1

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

src/uu/cp/src/cp.rs

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1708,20 +1708,30 @@ pub(crate) fn copy_attributes(
17081708

17091709
let dest_uid = source_metadata.uid();
17101710
let dest_gid = source_metadata.gid();
1711-
// gnu compatibility: cp doesn't report an error if it fails to set the ownership.
1712-
let _ = wrap_chown(
1713-
dest,
1714-
&dest
1715-
.symlink_metadata()
1716-
.map_err(|e| CpError::IoErrContext(e, context.to_owned()))?,
1717-
Some(dest_uid),
1718-
Some(dest_gid),
1719-
false,
1720-
Verbosity {
1721-
groups_only: false,
1722-
level: VerbosityLevel::Silent,
1723-
},
1724-
);
1711+
let meta = &dest
1712+
.symlink_metadata()
1713+
.map_err(|e| CpError::IoErrContext(e, context.to_owned()))?;
1714+
1715+
let try_chown = {
1716+
|uid| {
1717+
wrap_chown(
1718+
dest,
1719+
meta,
1720+
uid,
1721+
Some(dest_gid),
1722+
false,
1723+
Verbosity {
1724+
groups_only: false,
1725+
level: VerbosityLevel::Silent,
1726+
},
1727+
)
1728+
}
1729+
};
1730+
// gnu compatibility: cp doesn't report an error if it fails to set the ownership,
1731+
// and will fall back to changing only the gid if possible.
1732+
if try_chown(Some(dest_uid)).is_err() {
1733+
let _ = try_chown(None);
1734+
}
17251735
Ok(())
17261736
})?;
17271737

0 commit comments

Comments
 (0)