-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathexplain.rs
More file actions
42 lines (37 loc) · 918 Bytes
/
explain.rs
File metadata and controls
42 lines (37 loc) · 918 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
use std::io::Read;
use fs_err::File;
use std::fmt::{Display, Debug};
use std::path::Path;
use nix::unistd::getuid;
pub trait Explainable: Display + Debug + Send + Sync {
fn explain(&self) -> String;
}
pub fn exists(path: &Path) -> &'static str {
if path.exists() {
"exists"
} else {
"missing"
}
}
pub fn user() -> &'static str {
let uid = getuid();
if u32::from(uid) == 0 {
let mut buf = String::with_capacity(100);
match File::open("/proc/self/uid_map")
.and_then(|mut f| f.read_to_string(&mut buf))
{
Ok(_) => {
if buf == " 0 0 4294967295\n" {
"superuser"
} else {
"mapped-root"
}
}
Err(_) => {
"privileged"
}
}
} else {
"regular-user"
}
}