-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
296 lines (276 loc) · 8.27 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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#![deny(unsafe_code)]
#![warn(rust_2018_idioms)]
mod args;
use anyhow::Context;
use clap::{Parser, ValueEnum};
use clap_verbosity_flag::ErrorLevel;
use std::fs::File;
use std::io::{self, Write};
use std::path::PathBuf;
use std::str::FromStr;
use yadf::{Fdupes, Machine};
fn main() -> anyhow::Result<()> {
human_panic::setup_panic!();
let timer = std::time::Instant::now();
let args = Args::init_from_env();
log::debug!("{:?}", args);
let config = build_config(&args);
log::debug!("{:?}", config);
args.algorithm.run(args.clone(), config)?;
log::debug!("{:?} elapsed", timer.elapsed());
Ok(())
}
fn write_output<H>(args: Args, bag: yadf::TreeBag<H::Hash, yadf::Path>) -> Result<(), anyhow::Error>
where
H: yadf::Hasher,
{
let rfactor = args.rfactor.unwrap_or_default();
let replicates = bag.replicates(rfactor.into());
match args.output {
Some(path) => {
let context = || format!("writing output to the file: {:?}", path.display());
let file = File::create(&path).with_context(context)?;
args.format.display::<_, H>(file, replicates)
}
None => args.format.display::<_, H>(io::stdout().lock(), replicates),
}
.context("writing output")?;
Ok(())
}
#[cfg(unix)]
fn build_config(args: &Args) -> yadf::Yadf<PathBuf> {
yadf::Yadf::builder()
.paths(args.paths.as_ref())
.minimum_file_size(args.min())
.maximum_file_size(args.max())
.regex(args.regex.clone())
.glob(args.pattern.clone())
.max_depth(args.max_depth)
.hard_links(args.hard_links)
.build()
}
#[cfg(not(unix))]
fn build_config(args: &Args) -> yadf::Yadf<PathBuf> {
yadf::Yadf::builder()
.paths(args.paths.as_ref())
.minimum_file_size(args.min())
.maximum_file_size(args.max())
.regex(args.regex.clone())
.glob(args.pattern.clone())
.max_depth(args.max_depth)
.build()
}
impl Algorithm {
fn run<P>(&self, args: Args, config: yadf::Yadf<P>) -> anyhow::Result<()>
where
P: AsRef<std::path::Path>,
{
log::debug!("using {:?} hashing", self);
match self {
Algorithm::AHash => {
write_output::<ahash::AHasher>(args, config.scan::<ahash::AHasher>())?
}
Algorithm::Blake3 => {
write_output::<blake3::Hasher>(args, config.scan::<blake3::Hasher>())?
}
Algorithm::Highway => write_output::<highway::HighwayHasher>(
args,
config.scan::<highway::HighwayHasher>(),
)?,
Algorithm::MetroHash => write_output::<metrohash::MetroHash128>(
args,
config.scan::<metrohash::MetroHash128>(),
)?,
Algorithm::SeaHash => {
write_output::<seahash::SeaHasher>(args, config.scan::<seahash::SeaHasher>())?
}
Algorithm::XxHash => write_output::<twox_hash::XxHash3_128>(
args,
config.scan::<twox_hash::XxHash3_128>(),
)?,
}
Ok(())
}
}
impl Format {
fn display<W, H>(
&self,
writer: W,
replicates: yadf::FileReplicates<'_, H::Hash>,
) -> anyhow::Result<()>
where
H: yadf::Hasher,
W: Write,
{
let mut writer = io::BufWriter::with_capacity(64 * 1024, writer);
match self {
Format::Json => {
serde_json::to_writer(&mut writer, &replicates)?;
writer.write_all(b"\n")?;
}
Format::JsonPretty => {
serde_json::to_writer_pretty(&mut writer, &replicates)?;
writer.write_all(b"\n")?;
}
Format::Csv => csv_to_writer::<_, H>(writer, &replicates)?,
Format::LdJson => ldjson_to_writer::<_, H>(writer, &replicates)?,
Format::Fdupes => writeln!(writer, "{}", replicates.display::<Fdupes>())?,
Format::Machine => writeln!(writer, "{}", replicates.display::<Machine>())?,
};
Ok(())
}
}
/// Yet Another Dupes Finder
#[derive(Parser, Debug, Clone)]
pub struct Args {
/// Directories to search
///
/// default is to search inside the current working directory
#[clap(value_parser)]
paths: Vec<PathBuf>,
/// Output format
#[clap(short, long, value_enum, default_value_t, ignore_case = true)]
format: Format,
/// Hashing algorithm
#[clap(short, long, value_enum, default_value_t, ignore_case = true)]
algorithm: Algorithm,
/// Excludes empty files
#[clap(short, long)]
no_empty: bool,
/// Minimum file size
#[clap(long, value_name = "size")]
min: Option<Byte>,
/// Maximum file size
#[clap(long, value_name = "size")]
max: Option<Byte>,
/// Maximum recursion depth
#[clap(short = 'd', long = "depth", value_name = "depth")]
max_depth: Option<usize>,
/// Treat hard links to same file as duplicates
#[cfg_attr(unix, clap(short = 'H', long))]
#[cfg(unix)]
hard_links: bool,
/// Check files with a name matching a Perl-style regex,
/// see: https://docs.rs/regex/1.4.2/regex/index.html#syntax
#[clap(short = 'R', long)]
regex: Option<regex::Regex>,
/// Check files with a name matching a glob pattern,
/// see: https://docs.rs/globset/0.4.6/globset/index.html#syntax
#[clap(short, long, value_name = "glob")]
pattern: Option<globset::Glob>,
#[clap(flatten)]
verbosity: clap_verbosity_flag::Verbosity<ErrorLevel>,
/// Replication factor [under|equal|over]:n
///
/// The default is `over:1`, to find uniques use `equal:1`,
/// to find files with less than 10 copies use `under:10`
#[clap(long)]
rfactor: Option<ReplicationFactor>,
/// Optional output file
#[clap(short, long)]
output: Option<PathBuf>,
}
#[derive(ValueEnum, Debug, Clone, Default)]
enum Format {
Csv,
#[default]
Fdupes,
Json,
JsonPretty,
LdJson,
Machine,
}
#[derive(ValueEnum, Debug, Clone, Default)]
#[clap(rename_all = "lower")]
enum Algorithm {
AHash,
Blake3,
#[default]
Highway,
MetroHash,
SeaHash,
XxHash,
}
#[derive(Debug, Clone)]
struct Byte(byte_unit::Byte);
impl FromStr for Byte {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
byte_unit::Byte::from_str(s)
.map(Byte)
.map_err(|e| e.to_string())
}
}
#[derive(Debug, PartialEq, Clone)]
enum ReplicationFactor {
Under(usize),
Equal(usize),
Over(usize),
}
/// mimic serde_json interface
fn csv_to_writer<W, H>(writer: W, replicates: &yadf::FileReplicates<'_, H::Hash>) -> csv::Result<()>
where
H: yadf::Hasher,
W: Write,
{
let mut writer = csv::WriterBuilder::new()
.flexible(true)
.has_headers(false)
.from_writer(writer);
writer.serialize(("count", "files"))?;
for files in replicates {
writer.serialize((files.len(), files))?;
}
Ok(())
}
/// mimic serde_json interface
fn ldjson_to_writer<W, H>(
mut writer: W,
replicates: &yadf::FileReplicates<'_, H::Hash>,
) -> anyhow::Result<()>
where
H: yadf::Hasher,
W: Write,
{
for files in replicates {
serde_json::to_writer(&mut writer, &files)?;
writeln!(writer)?;
}
Ok(())
}
// #[cfg(test)]
// mod tests {
// use super::*;
// use once_cell::sync::Lazy;
// static BAG: Lazy<yadf::TreeBag<u64, yadf::Path>> = Lazy::new(|| {
// vec![
// (77, "hello".into()),
// (77, "world".into()),
// (3, "foo".into()),
// (3, "bar".into()),
// ]
// .into_iter()
// .collect()
// });
// #[test]
// fn csv() {
// let mut buffer = Vec::new();
// let _ = csv_to_writer(&mut buffer, &BAG.duplicates());
// let result = String::from_utf8(buffer).unwrap();
// let expected = r#"count,files
// 2,foo,bar
// 2,hello,world
// "#;
// assert_eq!(result, expected);
// }
// #[test]
// fn ldjson() {
// let mut buffer = Vec::new();
// let _ = ldjson_to_writer(&mut buffer, &BAG.duplicates());
// let result = String::from_utf8(buffer).unwrap();
// let expected = r#"["foo","bar"]
// ["hello","world"]
// "#;
// assert_eq!(result, expected);
// }
// }