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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
use crate::{Content, Decoder};
use std::{io::Read, path::PathBuf};
#[derive(Debug)]
pub enum ReadContent {
Directory,
Symlink {
target: PathBuf,
},
File {
executable: bool,
size: u64,
offset: u64,
data: Vec<u8>,
},
}
pub fn pretty_print_nar_content<R: Read>(dec: Decoder<R>) -> String {
use ReadContent as RC;
let mut res = vec![];
for (path, content) in decode_nar(dec) {
let path = match path {
None => "ROOT".into(),
Some(path) => {
let path = path.display().to_string();
let components: Vec<&str> = path.split('/').collect();
match components.last() {
Some(basename) => {
let depth = components.len();
let mut indented = String::new();
for _ in 0..depth - 1 {
indented.push_str("│ ");
}
indented.push_str("├── ");
indented.push_str(basename);
indented
}
None => "".to_string(),
}
}
};
res.push(match content {
RC::Directory => path,
RC::Symlink { target } => format!("{path} -> {}", target.display()),
RC::File {
executable,
size,
offset,
data,
} => format!(
"{path}: executable={executable}, size={size}, offset={offset}, data='{}'",
std::str::from_utf8(&data)
.unwrap()
.escape_default()
),
});
}
res.join("\n")
}
fn decode_nar<R: Read>(dec: Decoder<R>) -> Vec<(Option<PathBuf>, ReadContent)> {
let mut res = vec![];
for entry in dec.entries().unwrap() {
let entry = entry.unwrap();
let read_content = match entry.content {
Content::Symlink { target } => ReadContent::Symlink { target },
Content::Directory => ReadContent::Directory,
Content::File {
executable,
size,
offset,
mut data,
} => {
let mut read_data: Vec<u8> = vec![];
data.read_to_end(&mut read_data).unwrap();
ReadContent::File {
executable,
size,
offset,
data: read_data,
}
}
};
res.push((entry.path, read_content));
}
res
}