Skip to content

Commit 3852030

Browse files
committed
♻️ Add unified utils::fs::chmod wrapper and simplify restore_permissions
- Add chmod wrapper in utils/fs.rs following lchown pattern - Consolidate duplicate Unix/Windows chmod calls in extract.rs
1 parent c7895f1 commit 3852030

2 files changed

Lines changed: 15 additions & 21 deletions

File tree

cli/src/command/extract.rs

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,7 +1042,7 @@ fn resolve_owner(
10421042

10431043
#[inline]
10441044
fn restore_permissions(path: &Path, p: &Permission, owner: &OwnerSource) -> io::Result<()> {
1045-
#[cfg(unix)]
1045+
#[cfg(any(unix, windows))]
10461046
{
10471047
// Restore ownership only when owner is FromSource (i.e., same_owner is true)
10481048
if let OwnerSource::FromSource {
@@ -1061,26 +1061,7 @@ fn restore_permissions(path: &Path, p: &Permission, owner: &OwnerSource) -> io::
10611061
}
10621062
}
10631063
// Always restore mode permissions
1064-
utils::os::unix::fs::chmod(path, p.permissions())?;
1065-
}
1066-
#[cfg(windows)]
1067-
{
1068-
if let OwnerSource::FromSource {
1069-
uname,
1070-
gname,
1071-
uid,
1072-
gid,
1073-
} = owner
1074-
{
1075-
let (user, group) = resolve_owner(p, uname.as_deref(), gname.as_deref(), *uid, *gid);
1076-
match lchown(path, user, group) {
1077-
Err(e) if e.kind() == io::ErrorKind::PermissionDenied => {
1078-
log::warn!("failed to restore owner of {}: {}", path.display(), e)
1079-
}
1080-
r => r?,
1081-
}
1082-
}
1083-
utils::os::windows::fs::chmod(path, p.permissions())?;
1064+
utils::fs::chmod(path, p.permissions())?;
10841065
}
10851066
#[cfg(not(any(unix, windows)))]
10861067
{

cli/src/utils/fs.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,19 @@ pub(crate) fn lchown<P: AsRef<Path>>(
6666
inner(path.as_ref(), owner, group)
6767
}
6868

69+
#[cfg(any(windows, unix))]
70+
pub(crate) fn chmod<P: AsRef<Path>>(path: P, mode: u16) -> io::Result<()> {
71+
#[cfg(windows)]
72+
fn inner(path: &Path, mode: u16) -> io::Result<()> {
73+
windows::fs::chmod(path, mode)
74+
}
75+
#[cfg(unix)]
76+
fn inner(path: &Path, mode: u16) -> io::Result<()> {
77+
crate::utils::os::unix::fs::chmod(path, mode)
78+
}
79+
inner(path.as_ref(), mode)
80+
}
81+
6982
pub(crate) fn get_flags<P: AsRef<Path>>(path: P) -> io::Result<Vec<String>> {
7083
#[cfg(any(
7184
target_os = "macos",

0 commit comments

Comments
 (0)