Skip to content

Commit

Permalink
♻️ Change internal method return type
Browse files Browse the repository at this point in the history
- utils::fs::User::from_xxx -> Oprion<Self> to io::Result<Self>
- utils::fs::Group::from_xxx -> Oprion<Self> to io::Result<Self>
  • Loading branch information
ChanTsune committed Oct 9, 2024
1 parent 87aafbb commit f9a1d5b
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 37 deletions.
27 changes: 20 additions & 7 deletions cli/src/command/chown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,21 +79,34 @@ fn transform_entry<T>(entry: RegularEntry<T>, owner: &Owner) -> RegularEntry<T>
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())));
let user = user.and_then(|it| {
User::from_name(it)
.ok()
.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())));
let user = user.and_then(|it| {
User::from_name(it)
.ok()
.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())));
let group = group.and_then(|it| {
Group::from_name(it)
.ok()
.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())));
let group = group.and_then(|it| {
Group::from_name(it)
.ok()
.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()));
Expand Down
4 changes: 2 additions & 2 deletions cli/src/command/commons.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ pub(crate) fn apply_metadata(
let mode = meta.permissions().mode() as u16;
let uid = owner_options.uid.unwrap_or(meta.uid());
let gid = owner_options.gid.unwrap_or(meta.gid());
let user = User::from_uid(uid.into()).ok_or(io::ErrorKind::NotFound)?;
let group = Group::from_gid(gid.into()).ok_or(io::ErrorKind::NotFound)?;
let user = User::from_uid(uid.into())?;
let group = Group::from_gid(gid.into())?;
entry.permission(pna::Permission::new(
uid.into(),
owner_options.uname.unwrap_or(user.name().into()),
Expand Down
16 changes: 10 additions & 6 deletions cli/src/command/extract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,11 @@ fn permissions<'p>(
p: &'p Permission,
_: &'_ OwnerOptions,
) -> Option<(&'p Permission, Option<User>, Option<Group>)> {
Some((p, User::from_name(p.uname()), Group::from_name(p.gname())))
Some((
p,
User::from_name(p.uname()).ok(),
Group::from_name(p.gname()).ok(),
))
}

#[cfg(unix)]
Expand All @@ -412,22 +416,22 @@ fn permissions<'p>(
permission.gid(),
)
};
Some((permission, user, group))
Some((permission, user.ok(), group.ok()))
}

#[cfg(unix)]
fn search_owner(name: &str, id: u64) -> Option<User> {
fn search_owner(name: &str, id: u64) -> io::Result<User> {
let user = User::from_name(name);
if user.is_some() {
if user.is_ok() {
return user;
}
User::from_uid((id as u32).into())
}

#[cfg(unix)]
fn search_group(name: &str, id: u64) -> Option<Group> {
fn search_group(name: &str, id: u64) -> io::Result<Group> {
let group = Group::from_name(name);
if group.is_some() {
if group.is_ok() {
return group;
}
Group::from_gid((id as u32).into())
Expand Down
23 changes: 15 additions & 8 deletions cli/src/utils/os/redox/fs/owner.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
use nix::unistd::{Gid, Uid};
use std::io;

pub(crate) struct User(Uid);
impl User {
#[inline]
pub(crate) fn from_uid(uid: Uid) -> Option<Self> {
Some(Self(uid))
pub(crate) fn from_uid(uid: Uid) -> io::Result<Self> {
Ok(Self(uid))
}

#[inline]
pub(crate) fn from_name(_name: &str) -> Option<Self> {
None
pub(crate) fn from_name(_name: &str) -> io::Result<Self> {
Err(io::Error::new(
io::ErrorKind::Unsupported,
"can not find by username",
))
}

#[inline]
Expand All @@ -26,13 +30,16 @@ pub(crate) struct Group(Gid);

impl Group {
#[inline]
pub(crate) fn from_gid(gid: Gid) -> Option<Self> {
Some(Self(gid))
pub(crate) fn from_gid(gid: Gid) -> io::Result<Self> {
Ok(Self(gid))
}

#[inline]
pub(crate) fn from_name(_name: &str) -> Option<Self> {
None
pub(crate) fn from_name(_name: &str) -> io::Result<Self> {
Err(io::Error::new(
io::ErrorKind::Unsupported,
"can not find by gname",
))
}

#[inline]
Expand Down
41 changes: 33 additions & 8 deletions cli/src/utils/os/unix/fs/owner.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
use nix::unistd;
use nix::unistd::{Gid, Uid};
use std::io;

pub(crate) struct User(unistd::User);
impl User {
#[inline]
pub(crate) fn from_uid(uid: Uid) -> Option<Self> {
unistd::User::from_uid(uid).ok().flatten().map(Self)
pub(crate) fn from_uid(uid: Uid) -> io::Result<Self> {
if let Some(user) = unistd::User::from_uid(uid)? {
return Ok(Self(user));
}
Err(io::Error::new(
io::ErrorKind::NotFound,
format!("uid {} not found", uid),
))
}

#[inline]
pub(crate) fn from_name(name: &str) -> Option<Self> {
unistd::User::from_name(name).ok().flatten().map(Self)
pub(crate) fn from_name(name: &str) -> io::Result<Self> {
if let Some(user) = unistd::User::from_name(name)? {
return Ok(Self(user));
}
Err(io::Error::new(
io::ErrorKind::NotFound,
format!("username {} not found", name),
))
}

#[inline]
Expand All @@ -27,13 +40,25 @@ pub(crate) struct Group(unistd::Group);

impl Group {
#[inline]
pub(crate) fn from_gid(gid: Gid) -> Option<Self> {
unistd::Group::from_gid(gid).ok().flatten().map(Self)
pub(crate) fn from_gid(gid: Gid) -> io::Result<Self> {
if let Some(group) = unistd::Group::from_gid(gid)? {
return Ok(Self(group));
}
Err(io::Error::new(
io::ErrorKind::NotFound,
format!("gid {} not found", gid),
))
}

#[inline]
pub(crate) fn from_name(name: &str) -> Option<Self> {
unistd::Group::from_name(name).ok().flatten().map(Self)
pub(crate) fn from_name(name: &str) -> io::Result<Self> {
if let Some(group) = unistd::Group::from_name(name)? {
return Ok(Self(group));
}
Err(io::Error::new(
io::ErrorKind::NotFound,
format!("gname {} not found", name),
))
}

#[inline]
Expand Down
13 changes: 7 additions & 6 deletions cli/src/utils/os/windows/fs/owner.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
use crate::utils::os::windows::security;
use std::io;

pub(crate) struct User(security::Sid);
impl User {
#[inline]
pub(crate) fn from_name(name: &str) -> Option<Self> {
pub(crate) fn from_name(name: &str) -> io::Result<Self> {
Self::from_system_name(name, None)
}

#[inline]
pub(crate) fn from_system_name(name: &str, system: Option<&str>) -> Option<Self> {
security::Sid::try_from_name(name, system).ok().map(Self)
pub(crate) fn from_system_name(name: &str, system: Option<&str>) -> io::Result<Self> {
security::Sid::try_from_name(name, system).map(Self)
}

#[inline]
Expand All @@ -29,13 +30,13 @@ pub(crate) struct Group(security::Sid);

impl Group {
#[inline]
pub(crate) fn from_name(name: &str) -> Option<Self> {
pub(crate) fn from_name(name: &str) -> io::Result<Self> {
Self::from_system_name(name, None)
}

#[inline]
pub(crate) fn from_system_name(name: &str, system: Option<&str>) -> Option<Self> {
security::Sid::try_from_name(name, system).ok().map(Self)
pub(crate) fn from_system_name(name: &str, system: Option<&str>) -> io::Result<Self> {
security::Sid::try_from_name(name, system).map(Self)
}

#[inline]
Expand Down

0 comments on commit f9a1d5b

Please sign in to comment.