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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#![feature(test)]
#[macro_use] extern crate nom;
#[macro_use] extern crate lazy_static;
extern crate regex;
extern crate colored;
pub mod test;
pub mod types;
pub mod error;
pub mod cli_helpers;
pub mod prelude {
use std::fs;
use std::path::PathBuf;
use regex::Regex;
use types::*;
use colored::*;
use std::process::exit;
pub use cli_helpers::*;
pub use error::*;
#[cfg(not(os = "windows"))]
pub fn is_artifact(p: &PathBuf, re: Option<&Regex>) -> bool {
let path_str = p.clone().into_os_string().into_string().expect("OS string invalid.");
if let Some(r) = re {
r.is_match(&path_str)
}
else {
lazy_static! {
static ref REGEX: Regex =
Regex::new(r".*?\.(a|o|ll|keter|bc|dyn_o|out|rlib|crate|min\.js|hi|dyn_hi|toc|aux|fdb_latexmk|fls|egg-info|whl|js_a|js_hi|js_o|so.*|vba|crx)$")
.unwrap();
}
REGEX.is_match(&path_str)
}
}
#[cfg(os = "windows")]
pub fn is_artifact(p: PathBuf, re: Option<&Regex>) -> bool {
lazy_static! {
static ref REGEX: Regex = Regex::new(r".*?\.(exe|dll|ll|keter|bc|rlib|crate|min\.js|toc|aux|whl|vba|crx|out)$").unwrap();
}
let path_str = &p.into_os_string().into_string().expect("OS String invalid.");
if let Some(r) = re {
r.is_match(path_str)
}
else {
REGEX.is_match(path_str)
}
}
pub fn read_all(in_paths: &PathBuf,
depth: u8,
min_bytes: Option<u64>,
artifact_regex: Option<&Regex>,
excludes: Option<&Regex>,
silent: bool,
artifacts_only: bool) -> FileTree {
let mut tree = FileTree::new();
let min_size = min_bytes.map(|a| FileSize::new(a));
if let Ok(paths) = fs::read_dir(in_paths) {
for p in paths {
let path = p.unwrap().path();
let path_string = path.clone().into_os_string().into_string().expect("OS String invalid.");
let bool_loop = match excludes {
Some(ex) => !ex.is_match(&path_string),
_ => true,
};
if bool_loop {
if let Ok(metadata) = fs::metadata(&path) {
if metadata.is_file() {
if !artifacts_only || is_artifact(&path, artifact_regex) {
let file_size = FileSize::new(metadata.len());
if let Some(b) = min_bytes {
if file_size >= FileSize::new(b) {
tree.push(path_string, file_size, None, depth + 1, min_size);
}
}
else {
tree.push(path_string, file_size, None, depth + 1, min_size);
}
}
}
else if metadata.is_dir() {
let mut subtree = read_all(&path, depth + 1, min_bytes, artifact_regex, excludes, silent, artifacts_only);
let dir_size = subtree.file_size;
if let Some(b) = min_bytes {
if dir_size >= FileSize::new(b) {
tree.push(path_string, dir_size, Some(&mut subtree), depth + 1, min_size);
}
}
else { tree.push(path_string, dir_size, Some(&mut subtree), depth + 1, min_size); }
}
}
else if !silent { println!("{}: ignoring symlink at {}", "Warning".yellow(), path.display()); }
}
}
}
else if !in_paths.exists() {
println!("{}: path '{}' does not exist.", "Error".red(), &in_paths.display());
exit(0x0001);
}
else if !in_paths.is_dir() {
println!("{}: {} is not a directory.", "Error".red(), &in_paths.display());
exit(0x0001);
}
else if !silent {
println!("{}: permission denied for directory: {}", "Warning".yellow(), &in_paths.display());
}
tree
}
}