Add the setuid/setgid/sticky bit support

This commit is contained in:
Peltoche 2018-12-01 13:11:29 +01:00
parent 25c615002e
commit 21cc041775
No known key found for this signature in database
GPG key ID: CED68D0487156952
3 changed files with 39 additions and 50 deletions

View file

@ -13,6 +13,7 @@ pub enum Elem {
Read,
Write,
Exec,
ExecSticky,
NoAccess,
/// Last Time Modified
@ -44,6 +45,7 @@ lazy_static! {
m.insert(Elem::Read, Colour::Fixed(40)); // Green3
m.insert(Elem::Write, Colour::Fixed(192)); // DarkOliveGreen1
m.insert(Elem::Exec, Colour::Fixed(124)); // Red3
m.insert(Elem::ExecSticky, Colour::Fixed(13)); // Fuschsia
m.insert(Elem::NoAccess, Colour::Fixed(168)); // HotPink3
// Path Kind
@ -64,22 +66,3 @@ lazy_static! {
m
};
}
lazy_static! {
pub static ref PrecomputedElems : HashMap<Elem, String> = {
let mut m = HashMap::new();
// Permissions
m.insert(Elem::Read, Colors[&Elem::Read].paint(String::from("r")).to_string());
m.insert(Elem::Write, Colors[&Elem::Write].paint(String::from("w")).to_string());
m.insert(Elem::Exec, Colors[&Elem::Exec].paint(String::from("x")).to_string());
m.insert(Elem::NoAccess, Colors[&Elem::NoAccess].paint(String::from("-")).to_string());
// Note types
m.insert(Elem::File , Colors[&Elem::File].paint(String::from(".")).to_string());
m.insert(Elem::Dir, Colors[&Elem::Dir].paint(String::from("d")).to_string());
m.insert(Elem::SymLink, Colors[&Elem::SymLink].paint(String::from("l")).to_string());
m
};
}

View file

@ -1,4 +1,4 @@
use color::{Colors, Elem, PrecomputedElems};
use color::{Colors, Elem};
use icon;
use meta::{Meta, Type};
use std::time::{Duration, SystemTime, UNIX_EPOCH};
@ -77,9 +77,9 @@ impl Formatter {
let mut res = String::with_capacity(11);
match meta.node_type {
Type::File => res += PrecomputedElems[&Elem::File].as_str(),
Type::Directory => res += PrecomputedElems[&Elem::Dir].as_str(),
Type::SymLink(_) => res += PrecomputedElems[&Elem::SymLink].as_str(),
Type::File => res += &Colors[&Elem::File].paint("."),
Type::Directory => res += &Colors[&Elem::Dir].paint("d"),
Type::SymLink(_) => res += &Colors[&Elem::SymLink].paint("l"),
}
res += &meta.permissions.render();

View file

@ -1,4 +1,5 @@
use color::{Elem, PrecomputedElems};
use ansi_term::{ANSIString, Colour};
use color::{Colors, Elem};
use std::fs::Metadata;
use std::os::unix::fs::PermissionsExt;
@ -48,40 +49,45 @@ impl<'a> From<&'a Metadata> for Permissions {
impl Permissions {
pub fn render(&self) -> String {
let mut res = String::with_capacity(11);
let mut res = String::with_capacity(10);
res += &self.render_permission_set(self.user_read, self.user_write, self.user_execute);
res += &self.render_permission_set(self.group_read, self.group_write, self.group_execute);
res += &self.render_permission_set(self.other_read, self.other_write, self.other_execute);
let bit = |bit, chr: &'static str, color: Colour| {
if bit {
color.paint(chr).to_string()
} else {
Colors[&Elem::NoAccess].paint("-").to_string()
}
};
res += &bit(self.user_read, "r", Colors[&Elem::Read]);
res += &bit(self.user_write, "w", Colors[&Elem::Write]);
res += &self.execute_bit(self.setuid).to_string();
res += &bit(self.group_read, "r", Colors[&Elem::Read]);
res += &bit(self.group_write, "w", Colors[&Elem::Write]);
res += &self.execute_bit(self.setgid).to_string();
res += &bit(self.other_read, "r", Colors[&Elem::Read]);
res += &bit(self.other_write, "w", Colors[&Elem::Write]);
res += &self.other_execute_bit().to_string();
res
}
fn render_permission_set(&self, read: bool, write: bool, exec: bool) -> String {
let mut res = String::with_capacity(3);
// Read Permisssions
if read {
res += PrecomputedElems[&Elem::Read].as_str();
} else {
res += PrecomputedElems[&Elem::NoAccess].as_str();
fn execute_bit(&self, special: bool) -> ANSIString<'static> {
match (self.user_execute, special) {
(false, false) => Colors[&Elem::NoAccess].paint("-"),
(true, false) => Colors[&Elem::Exec].paint("x"),
(false, true) => Colors[&Elem::ExecSticky].paint("S"),
(true, true) => Colors[&Elem::ExecSticky].paint("s"),
}
}
// Write Permisssions
if write {
res += PrecomputedElems[&Elem::Write].as_str();
} else {
res += PrecomputedElems[&Elem::NoAccess].as_str();
fn other_execute_bit(&self) -> ANSIString<'static> {
match (self.other_execute, self.sticky) {
(false, false) => Colors[&Elem::NoAccess].paint("-"),
(true, false) => Colors[&Elem::Exec].paint("x"),
(false, true) => Colors[&Elem::ExecSticky].paint("T"),
(true, true) => Colors[&Elem::ExecSticky].paint("t"),
}
// Exec Permisssions
if exec {
res += PrecomputedElems[&Elem::Exec].as_str();
} else {
res += PrecomputedElems[&Elem::NoAccess].as_str();
}
res
}
}