-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
83 lines (73 loc) · 2.28 KB
/
main.rs
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use std::{fs, cmp, env};
use std::error::Error;
#[derive(Debug)]
struct Mapping {
uid: u32,
loweruid: u32,
count: u32,
}
impl Mapping {
fn new(uid: u32,loweruid: u32,count: u32) -> Self {
Mapping {
uid,
loweruid,
count,
}
}
fn sort(m: &mut [Mapping]) {
m.sort_by(|a,b| a.uid.cmp(&b.uid));
}
fn print_idmapped(m: &mut [Mapping],mut container_uid: u32,mut on_disk_uid: u32,mut amount: u32) {
Mapping::sort(m);
for line in m.iter() {
if(std::ops::Range { start: line.uid, end: line.uid+line.count}.contains(&container_uid)) {
let remaining_cap = (line.uid+line.count) - container_uid;
let k = cmp::min(remaining_cap, amount);
println!("{} {} {}",on_disk_uid,container_uid,k);
//host: println!("{} {} {}",on_disk_uid,line.loweruid-line.uid+container_uid,k);
on_disk_uid = on_disk_uid + k;
container_uid = container_uid + k;
amount = amount - k;
if amount <= 0 {
break;
}
}
}
if amount > 0 {
eprintln!("Could not map id {} {}",container_uid,amount);
}
}
}
fn main() -> Result<(), Box<dyn Error>> {
let args: Vec<String> = env::args().collect();
assert!(args.len() > 4);
let map = match args[1].as_str() {
"uid" => fs::read_to_string("/proc/self/uid_map")?,
"gid" => fs::read_to_string("/proc/self/gid_map")?,
_ => {
panic!("Args error!")
},
};
let mut v = read_filecontent_to_vec(&map);
let a = (args.len()-2) / 3;
let mut k = 2;
for i in 0..a {
// dbg!(i);
Mapping::print_idmapped(&mut v, args[k].parse()?, args[k+1].parse()?, args[k+2].parse()?);
k = k+3;
}
Ok(())
}
fn read_filecontent_to_vec(input: &str) -> Vec<Mapping>{
let mut v = Vec::new();
for lines in input.lines() {
let mut num: [u32;3] = [0;3];
for (i,n) in lines.split_ascii_whitespace().enumerate() {
num[i] = n.parse().unwrap();
assert!(i < 3);
}
let m = Mapping::new(num[0], num[1], num[2]);
v.push(m);
}
v
}