Skip to content

Commit 37c5cc5

Browse files
committed
added correct perms and more compiler flags
1 parent a799c7d commit 37c5cc5

File tree

4 files changed

+72
-91
lines changed

4 files changed

+72
-91
lines changed

Cargo.lock

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "nat"
3-
version = "1.0.8"
3+
version = "1.0.9"
44
authors = ["Will Lane <[email protected]>"]
55
description = "nat - the 'ls' replacement you never knew you needed"
66
license = "MIT"
@@ -14,6 +14,7 @@ users = "*"
1414
pretty-bytes = "*"
1515
ansi_term = "*"
1616
structopt = "*"
17+
libc = "*"
1718

1819
[profile.dev]
1920
opt-level = 0

src/main.rs

Lines changed: 39 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
extern crate pretty_bytes;
2+
extern crate libc;
23

34
use ansi_term::Style;
45
use chrono::{DateTime, Utc};
@@ -9,6 +10,7 @@ use std::{fs, io};
910
use structopt::StructOpt;
1011
use termion::color;
1112
use users::{get_current_uid, get_group_by_gid, get_user_by_uid, uid_t, get_current_gid};
13+
use libc::{S_IRGRP, S_IROTH, S_IRUSR, S_IWGRP, S_IWOTH, S_IWUSR, S_IXGRP, S_IXOTH, S_IXUSR};
1214

1315
mod single;
1416

@@ -25,11 +27,15 @@ struct Cli {
2527
help = "File to search for"
2628
)]
2729
file: String,
30+
31+
#[structopt(short = "l", long = "headline", help = "enable the headline")]
32+
headline_on: bool,
2833
}
2934

3035
fn main() -> Result<(), Box<dyn std::error::Error>> {
3136
let args = Cli::from_args();
3237
let directory = &args.path;
38+
let headline_on = &args.headline_on;
3339

3440
let entries = fs::read_dir(directory)?
3541
.map(|res| res.map(|e| e.path()))
@@ -51,10 +57,11 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
5157
}
5258

5359
let mut found = false;
54-
55-
draw_headline("permissions", 2, false);
56-
draw_headline("size", size_count - 4, true);
57-
draw_headline("last modified", 6, true);
60+
61+
if *headline_on {
62+
draw_headline("permissions", 0, false);
63+
draw_headline("size", 0, true);
64+
draw_headline("last modified", 0, true);
5865
let mut groups_size: i32 = 0;
5966
if get_group_by_gid(get_current_gid()).unwrap().name().to_str().unwrap().len() - 5 < 1 {
6067
groups_size = 0;
@@ -68,10 +75,11 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
6875
} else {
6976
user_size = get_user_by_uid(get_current_uid()).unwrap().name().to_str().unwrap().len() as i32 - 4;
7077
}
71-
draw_headline("user", user_size as usize, true);
78+
draw_headline("user", 0, true);
7279
draw_headline("name", 0, true);
7380

74-
print!("\n");
81+
print!("\n");
82+
}
7583

7684
if &args.file != "" {
7785
for e in &entries {
@@ -101,52 +109,12 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
101109

102110
for e in &entries {
103111
let meta = fs::symlink_metadata(&e)?;
104-
println!("{:?}", meta.permissions().mode() );
105112
let mode = meta.permissions().mode();
106-
let user_has_write_access = mode & 0o200;
107-
let user_has_read_write_access= mode & 0o600;
108-
let group_has_read_access = mode & 0o040;
109-
let others_have_exec_access = mode & 0o001;
110-
println!("{:o} {:o} {:o} {:o}", user_has_write_access, user_has_read_write_access, group_has_read_access, others_have_exec_access);
111-
let mut mode_count = 0;
112-
if user_has_write_access == 128 {
113-
print!("{}", color::Fg(color::Red));
114-
print!("w");
115-
print!("{}", color::Fg(color::White));
116-
print!("-");
117-
mode_count += 2;
118-
}
119-
if user_has_read_write_access == 384 {
120-
print!("{}", color::Fg(color::LightYellow));
121-
print!("r");
122-
print!("{}", color::Fg(color::LightRed));
123-
print!("w");
124-
print!("{}", color::Fg(color::White));
125-
print!("-");
126-
mode_count += 3;
127-
}
128-
if group_has_read_access == 32 {
129-
print!("{}", color::Fg(color::Green));
130-
print!("x");
131-
print!("{}", color::Fg(color::LightYellow));
132-
print!("a");
133-
print!("{}", color::Fg(color::White));
134-
print!("-");
135-
mode_count += 3;
136-
}
137-
if others_have_exec_access == 1 {
138-
print!("{}", color::Fg(color::Yellow));
139-
print!("xw");
140-
print!("{}", color::Fg(color::White));
141-
print!("-");
142-
mode_count += 3;
143-
}
113+
let mode_count = perms(mode as u16).len();
114+
144115
print!("{}", color::Fg(color::White));
145-
print!("-@");
146-
mode_count += 2;
147-
for _ in 0..(13 - mode_count) {
148-
print!(" ")
149-
}
116+
117+
print!("{}", perms(mode as u16));
150118

151119
for _ in 0..(size_count - convert(fs::symlink_metadata(&e)?.size() as f64).len()) {
152120
print!(" ")
@@ -233,3 +201,24 @@ fn get_user_name(uid: uid_t) -> String {
233201
.unwrap()
234202
.to_string()
235203
}
204+
205+
206+
pub fn perms(mode: u16) -> String {
207+
let user = triplet(mode, S_IRUSR, S_IWUSR, S_IXUSR);
208+
let group = triplet(mode, S_IRGRP, S_IWGRP, S_IXGRP);
209+
let other = triplet(mode, S_IROTH, S_IWOTH, S_IXOTH);
210+
[user, group, other].join("")
211+
}
212+
213+
pub fn triplet(mode: u16, read: u16, write: u16, execute: u16) -> String {
214+
match (mode & read, mode & write, mode & execute) {
215+
(0, 0, 0) => "---",
216+
(_, 0, 0) => "r--",
217+
(0, _, 0) => "-w-",
218+
(0, 0, _) => "--x",
219+
(_, 0, _) => "r-x",
220+
(_, _, 0) => "rw-",
221+
(0, _, _) => "-wx",
222+
(_, _, _) => "rwx",
223+
}.to_string()
224+
}

src/single.rs

Lines changed: 29 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,50 +6,20 @@ use std::fs;
66
use std::os::unix::fs::MetadataExt;
77
use termion::color;
88
use users::{get_user_by_uid, get_group_by_gid};
9+
use libc::{S_IRGRP, S_IROTH, S_IRUSR, S_IWGRP, S_IWOTH, S_IWUSR, S_IXGRP, S_IXOTH, S_IXUSR};
910

1011
pub fn single(e: &std::path::PathBuf, size_count: usize) -> Result<(), Box<dyn std::error::Error>> {
11-
let meta = fs::metadata(&e)?;
12+
let meta = fs::symlink_metadata(&e)?;
1213
let mode = meta.mode();
13-
let user_has_write_access = mode & 0o200;
14-
let user_has_read_write_access = mode & 0o600;
15-
let group_has_read_access = mode & 0o040;
16-
let others_have_exec_access = mode & 0o001;
1714
let mut mode_count = 0;
18-
if user_has_write_access == 128 {
19-
print!("{}", color::Fg(color::Red));
20-
print!("w");
21-
print!("{}", color::Fg(color::White));
22-
print!("-");
23-
mode_count += 2;
24-
}
25-
if user_has_read_write_access == 384 {
26-
print!("{}", color::Fg(color::LightYellow));
27-
print!("r");
28-
print!("{}", color::Fg(color::LightRed));
29-
print!("w");
30-
print!("{}", color::Fg(color::White));
31-
print!("-");
32-
mode_count += 3;
33-
}
34-
if group_has_read_access == 32 {
35-
print!("{}", color::Fg(color::Green));
36-
print!("x");
37-
print!("{}", color::Fg(color::LightYellow));
38-
print!("a");
39-
print!("{}", color::Fg(color::White));
40-
print!("-");
41-
mode_count += 3;
42-
}
43-
if others_have_exec_access == 1 {
44-
print!("{}", color::Fg(color::Yellow));
45-
print!("xw");
46-
print!("{}", color::Fg(color::White));
47-
print!("-");
48-
mode_count += 3;
49-
}
15+
16+
let mode_count = perms(mode as u16).len();
17+
5018
print!("{}", color::Fg(color::White));
51-
print!("-@");
52-
mode_count += 2;
19+
20+
print!("{}", perms(mode as u16));
21+
22+
5323
for _ in 0..(13 - mode_count) {
5424
print!(" ")
5525
}
@@ -113,3 +83,23 @@ pub fn single(e: &std::path::PathBuf, size_count: usize) -> Result<(), Box<dyn s
11383
}
11484
Ok(())
11585
}
86+
87+
fn triplet(mode: u16, read: u16, write: u16, execute: u16) -> String {
88+
match (mode & read, mode & write, mode & execute) {
89+
(0, 0, 0) => "---",
90+
(_, 0, 0) => "r--",
91+
(0, _, 0) => "-w-",
92+
(0, 0, _) => "--x",
93+
(_, 0, _) => "r-x",
94+
(_, _, 0) => "rw-",
95+
(0, _, _) => "-wx",
96+
(_, _, _) => "rwx",
97+
}.to_string()
98+
}
99+
100+
fn perms(mode: u16) -> String {
101+
let user = triplet(mode, S_IRUSR, S_IWUSR, S_IXUSR);
102+
let group = triplet(mode, S_IRGRP, S_IWGRP, S_IXGRP);
103+
let other = triplet(mode, S_IROTH, S_IWOTH, S_IXOTH);
104+
[user, group, other].join("")
105+
}

0 commit comments

Comments
 (0)