Skip to content

Commit

Permalink
♻️ Separate functions to convert entries for chown
Browse files Browse the repository at this point in the history
  • Loading branch information
ChanTsune committed Oct 4, 2024
1 parent e92e4d0 commit 82bd09f
Showing 1 changed file with 31 additions and 30 deletions.
61 changes: 31 additions & 30 deletions cli/src/command/chown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{
utils::{GlobPatterns, PathPartExt},
};
use clap::{Parser, ValueHint};
use pna::RegularEntry;
use std::ops::Not;
use std::{io, path::PathBuf, str::FromStr};

Expand Down Expand Up @@ -42,43 +43,43 @@ fn archive_chown(args: ChownCommand) -> io::Result<()> {
|entry| {
let entry = entry?;
if globs.matches_any(entry.header().path()) {
let metadata = entry.metadata().clone();
let permission = metadata.permission().map(|p| {
let user = args.owner.user();
#[cfg(unix)]
let user = user.and_then(|it| {
User::from_name(it).map(|it| (it.as_raw().into(), it.name().into()))
});
#[cfg(windows)]
let user = user
.and_then(|it| User::from_name(it).map(|it| (u64::MAX, it.name().into())));
#[cfg(not(any(unix, windows)))]
let user = user.map(|_| (p.uid(), p.uname().into()));
let (uid, uname) = user.unwrap_or_else(|| (p.uid(), p.uname().into()));

let group = args.owner.group();
#[cfg(unix)]
let group = group.and_then(|it| {
Group::from_name(it).map(|it| (it.as_raw().into(), it.name().into()))
});
#[cfg(windows)]
let group = group
.and_then(|it| Group::from_name(it).map(|it| (u64::MAX, it.name().into())));
#[cfg(not(any(unix, windows)))]
let group = group.map(|_| (p.gid(), p.gname().into()));
let (gid, gname) = group.unwrap_or_else(|| (p.gid(), p.gname().into()));
pna::Permission::new(uid, uname, gid, gname, p.permissions())
});
Ok(Some(
entry.with_metadata(metadata.with_permission(permission)),
))
Ok(Some(transform_entry(entry, &args.owner)))
} else {
Ok(Some(entry))
}
},
)
}

#[inline]
fn transform_entry<T>(entry: RegularEntry<T>, owner: &Owner) -> RegularEntry<T> {
let metadata = entry.metadata().clone();
let permission = metadata.permission().map(|p| {
let user = owner.user();
#[cfg(unix)]
let user = user
.and_then(|it| User::from_name(it).map(|it| (it.as_raw().into(), it.name().into())));
#[cfg(windows)]
let user = user.and_then(|it| User::from_name(it).map(|it| (u64::MAX, it.name().into())));
#[cfg(not(any(unix, windows)))]
let user = user.map(|_| (p.uid(), p.uname().into()));
let (uid, uname) = user.unwrap_or_else(|| (p.uid(), p.uname().into()));

let group = owner.group();
#[cfg(unix)]
let group = group
.and_then(|it| Group::from_name(it).map(|it| (it.as_raw().into(), it.name().into())));
#[cfg(windows)]
let group =
group.and_then(|it| Group::from_name(it).map(|it| (u64::MAX, it.name().into())));
#[cfg(not(any(unix, windows)))]
let group = group.map(|_| (p.gid(), p.gname().into()));
let (gid, gname) = group.unwrap_or_else(|| (p.gid(), p.gname().into()));
pna::Permission::new(uid, uname, gid, gname, p.permissions())
});
entry.with_metadata(metadata.with_permission(permission))
}

#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub(crate) struct Owner {
user: Option<String>,
Expand Down

0 comments on commit 82bd09f

Please sign in to comment.