提交 72782230 编写于 作者: B Ben S

Refactor user/group code to be less indented

上级 b337f917
......@@ -116,13 +116,17 @@ impl<'a> File<'a> {
// Display the ID if the user/group doesn't exist, which
// usually means it was deleted but its files weren't.
User => {
let style = if unix.uid == self.stat.unstable.uid as u32 { Yellow.bold() } else { Plain };
let string = unix.get_user_name(self.stat.unstable.uid as u32).unwrap_or(self.stat.unstable.uid.to_str());
let uid = self.stat.unstable.uid as u32;
unix.load_user(uid);
let style = if unix.uid == uid { Yellow.bold() } else { Plain };
let string = unix.get_user_name(uid).unwrap_or(uid.to_str());
style.paint(string.as_slice())
},
Group => {
let name = unix.get_group_name(self.stat.unstable.gid as u32).unwrap_or(self.stat.unstable.gid.to_str());
let style = if unix.is_group_member(self.stat.unstable.gid as u32) { Yellow.normal() } else { Plain };
let gid = self.stat.unstable.gid as u32;
unix.load_group(gid);
let name = unix.get_group_name(gid).unwrap_or(gid.to_str());
let style = if unix.is_group_member(gid) { Yellow.normal() } else { Plain };
style.paint(name.as_slice())
},
}
......
......@@ -73,20 +73,27 @@ impl Unix {
}
}
pub fn get_user_name(&self, uid: u32) -> Option<String> {
self.user_names.get(&uid).clone()
}
pub fn get_group_name(&self, gid: u32) -> Option<String> {
self.group_names.get(&gid).clone()
}
pub fn is_group_member(&self, gid: u32) -> bool {
*self.groups.get(&gid)
}
pub fn get_user_name(&mut self, uid: u32) -> Option<String> {
self.user_names.find_or_insert_with(uid, |&u| {
let pw = unsafe { c::getpwuid(u as i32) };
pub fn load_user(&mut self, uid: u32) {
let pw = unsafe { c::getpwuid(uid as i32) };
if pw.is_not_null() {
return unsafe { Some(from_c_str(read(pw).pw_name)) };
let username = unsafe { Some(from_c_str(read(pw).pw_name)) };
self.user_names.insert(uid, username);
}
else {
return None;
self.user_names.insert(uid, None);
}
}).clone()
}
fn group_membership(group: **i8, uname: &String) -> bool {
......@@ -113,14 +120,11 @@ impl Unix {
}
}
pub fn get_group_name(&mut self, gid: u32) -> Option<String> {
match self.group_names.find_copy(&gid) {
Some(name) => name,
None => {
pub fn load_group(&mut self, gid: u32) {
match unsafe { c::getgrgid(gid).to_option() } {
None => {
self.group_names.insert(gid, None);
return None;
self.groups.insert(gid, false);
},
Some(r) => {
let group_name = unsafe { Some(from_c_str(r.gr_name)) };
......@@ -134,11 +138,10 @@ impl Unix {
self.groups.insert(gid, Unix::group_membership(r.gr_mem, &self.username));
}
return group_name;
}
}
self.group_names.insert(gid, group_name);
}
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册