From 08b6dd497521e71cc3d0c743061c611686b0f819 Mon Sep 17 00:00:00 2001 From: Jeffrey Finkelstein Date: Sun, 8 May 2022 23:02:32 -0400 Subject: [PATCH] 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. --- src/uucore/src/lib/features/perms.rs | 34 ++++++++++++++++------------ 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/src/uucore/src/lib/features/perms.rs b/src/uucore/src/lib/features/perms.rs index d8e345313..ed9e1c001 100644 --- a/src/uucore/src/lib/features/perms.rs +++ b/src/uucore/src/lib/features/perms.rs @@ -92,22 +92,25 @@ pub fn wrap_chown>( ); 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>( 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>( 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()) ) }; }