1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 03:27:44 +00:00

uucore(perms): better support nameless uids, gids

Update the `wrap_chown()` function to support user IDs and group IDs
that do not correspond to named users or groups, respectively. Before
this commit, the result from `uid2usr()` and `gid2grp()` calls were
unwrapped because we assumed a user name or group name, respectively,
existed. However, this is not always true: for example, running the
command `sudo chown 12345 f` works even if there is no named user with
ID 12345. This commit expands `wrap_chown()` to work even if no name
exists for the user or group.
This commit is contained in:
Jeffrey Finkelstein 2022-05-08 23:02:32 -04:00 committed by Sylvestre Ledru
parent 6f5d5aa456
commit 08b6dd4975

View file

@ -92,22 +92,25 @@ pub fn wrap_chown<P: AsRef<Path>>(
);
if level == VerbosityLevel::Verbose {
out = if verbosity.groups_only {
let gid = meta.gid();
format!(
"{}\nfailed to change group of {} from {} to {}",
out,
path.quote(),
entries::gid2grp(meta.gid()).unwrap(),
entries::gid2grp(dest_gid).unwrap()
entries::gid2grp(gid).unwrap_or(gid.to_string()),
entries::gid2grp(dest_gid).unwrap_or(dest_gid.to_string())
)
} else {
let uid = meta.uid();
let gid = meta.gid();
format!(
"{}\nfailed to change ownership of {} from {}:{} to {}:{}",
out,
path.quote(),
entries::uid2usr(meta.uid()).unwrap(),
entries::gid2grp(meta.gid()).unwrap(),
entries::uid2usr(dest_uid).unwrap(),
entries::gid2grp(dest_gid).unwrap()
entries::uid2usr(uid).unwrap_or(uid.to_string()),
entries::gid2grp(gid).unwrap_or(gid.to_string()),
entries::uid2usr(dest_uid).unwrap_or(dest_uid.to_string()),
entries::gid2grp(dest_gid).unwrap_or(dest_gid.to_string())
)
};
};
@ -119,21 +122,24 @@ pub fn wrap_chown<P: AsRef<Path>>(
if changed {
match verbosity.level {
VerbosityLevel::Changes | VerbosityLevel::Verbose => {
let gid = meta.gid();
out = if verbosity.groups_only {
format!(
"changed group of {} from {} to {}",
path.quote(),
entries::gid2grp(meta.gid()).unwrap(),
entries::gid2grp(dest_gid).unwrap()
entries::gid2grp(gid).unwrap_or(gid.to_string()),
entries::gid2grp(dest_gid).unwrap_or(dest_gid.to_string())
)
} else {
let gid = meta.gid();
let uid = meta.uid();
format!(
"changed ownership of {} from {}:{} to {}:{}",
path.quote(),
entries::uid2usr(meta.uid()).unwrap(),
entries::gid2grp(meta.gid()).unwrap(),
entries::uid2usr(dest_uid).unwrap(),
entries::gid2grp(dest_gid).unwrap()
entries::uid2usr(uid).unwrap_or(uid.to_string()),
entries::gid2grp(gid).unwrap_or(gid.to_string()),
entries::uid2usr(dest_uid).unwrap_or(dest_uid.to_string()),
entries::gid2grp(dest_gid).unwrap_or(dest_gid.to_string())
)
};
}
@ -150,8 +156,8 @@ pub fn wrap_chown<P: AsRef<Path>>(
format!(
"ownership of {} retained as {}:{}",
path.quote(),
entries::uid2usr(dest_uid).unwrap(),
entries::gid2grp(dest_gid).unwrap()
entries::uid2usr(dest_uid).unwrap_or(dest_uid.to_string()),
entries::gid2grp(dest_gid).unwrap_or(dest_gid.to_string())
)
};
}