From aabf5de23a4ce438fb4265262973e8c501bef549 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mo=C3=AFse=20Valvassori?= Date: Sat, 20 May 2023 12:44:45 +0200 Subject: [PATCH] factorize duplicate code in a function --- src/uucore/src/lib/features/perms.rs | 74 ++++++++++++---------------- 1 file changed, 32 insertions(+), 42 deletions(-) diff --git a/src/uucore/src/lib/features/perms.rs b/src/uucore/src/lib/features/perms.rs index 98496b86949..89896f5e4d0 100644 --- a/src/uucore/src/lib/features/perms.rs +++ b/src/uucore/src/lib/features/perms.rs @@ -271,27 +271,11 @@ impl ChownExecutor { } } } else { - if self.verbosity.level == VerbosityLevel::Verbose { - // Display a message when the current user/group doesn't match those specified in - // the `--from` args. - if self.dest_gid.is_none() { - let uid = meta.uid(); - println!( - "ownership of {} retained as {}", - path.quote(), - entries::uid2usr(uid).unwrap_or_else(|_| uid.to_string()), - ); - } else { - let uid = meta.uid(); - let gid = meta.gid(); - println!( - "ownership of {} retained as {}:{}", - path.quote(), - entries::uid2usr(uid).unwrap_or_else(|_| uid.to_string()), - entries::gid2grp(gid).unwrap_or_else(|_| gid.to_string()), - ); - } - } + self.print_verbose_ownership_retained_as( + path, + meta.uid(), + self.dest_gid.map(|_| meta.gid()), + ); 0 }; @@ -353,27 +337,11 @@ impl ChownExecutor { }; if !self.matched(meta.uid(), meta.gid()) { - if self.verbosity.level == VerbosityLevel::Verbose { - // Display a message when the current user/group doesn't match those specified in - // the `--from` args. - if self.dest_gid.is_none() { - let uid = meta.uid(); - println!( - "ownership of {} retained as {}", - path.quote(), - entries::uid2usr(uid).unwrap_or_else(|_| uid.to_string()), - ); - } else { - let uid = meta.uid(); - let gid = meta.gid(); - println!( - "ownership of {} retained as {}:{}", - path.quote(), - entries::uid2usr(uid).unwrap_or_else(|_| uid.to_string()), - entries::gid2grp(gid).unwrap_or_else(|_| gid.to_string()), - ); - } - } + self.print_verbose_ownership_retained_as( + path, + meta.uid(), + self.dest_gid.map(|_| meta.gid()), + ); continue; } @@ -435,6 +403,28 @@ impl ChownExecutor { IfFrom::UserGroup(u, g) => u == uid && g == gid, } } + + fn print_verbose_ownership_retained_as(&self, path: &Path, uid: u32, gid: Option) { + if self.verbosity.level == VerbosityLevel::Verbose { + match gid { + Some(gid) => { + println!( + "ownership of {} retained as {}:{}", + path.quote(), + entries::uid2usr(uid).unwrap_or_else(|_| uid.to_string()), + entries::gid2grp(gid).unwrap_or_else(|_| gid.to_string()), + ); + } + None => { + println!( + "ownership of {} retained as {}", + path.quote(), + entries::uid2usr(uid).unwrap_or_else(|_| uid.to_string()), + ); + } + } + } + } } pub mod options {