clonehunter 0.4.2

A simple command line utility that identifies groups of identical files and displays them to the console
Documentation
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
// Copyright (c) 2024 Venkatesh Omkaram

use chrono::{DateTime, Local};
use clap::builder::OsStr;
use colored::Colorize;
use hashbrown::HashMap;
use human_bytes::human_bytes;
use indicatif::ProgressBar;
use jwalk::WalkDir;
use lazy_static::lazy_static;
use rayon::iter::{ParallelBridge, ParallelIterator};
use serde::{Deserialize, Serialize};
use std::{
    fmt::Debug,
    fs::{self, File},
    hash::Hash,
    io::{stdin, stdout, BufWriter, Write},
    path::PathBuf,
    rc::Rc,
    sync::{Arc, Mutex},
    time::SystemTime,
};
use trait_defs::*;

#[cfg(target_os = "linux")]
use std::os::unix::fs::MetadataExt;

#[cfg(target_os = "windows")]
use std::os::windows::fs::MetadataExt;

use crate::common::{
    config::{OrderBy, OutputStyle, SortBy},
    trait_defs,
};

lazy_static! {
    /// A Lazy static reference to hold a List of Directory Paths
    pub static ref DIR_LIST: Mutex<Vec<PathBuf>> = Mutex::new(Vec::new());
    /// A Lazy static reference to hold a list of File Paths
    pub static ref FILE_LIST: Mutex<Vec<PathBuf>> = Mutex::new(Vec::new());
    /// A Lazy static reference which hold the file sizes in bytes
    pub static ref FILES_SIZE_BYTES: Mutex<Option<u64>> = Mutex::new(Some(0));
}

pub static mut VERBOSE: bool = false;

/// This function can be used for all sorts of confirmation input from the user
pub fn confirmation() -> String {
    let mut confirmation: String = String::new();

    print!("\nPlease type Y for yes, and N for no : ");

    let _ = stdout().flush();

    stdin()
        .read_line(&mut confirmation)
        .expect("You entered incorrect response");

    if let Some('\n') = confirmation.chars().next_back() {
        confirmation.pop();
    }

    if let Some('\r') = confirmation.chars().next_back() {
        confirmation.pop();
    }

    println!("\nYou typed: {}\n", confirmation.blink());

    confirmation
}

/// A simple macro which prints two items only when verbose printing is specified.
/// VERBOSE is a RwLock
#[macro_export]
macro_rules! logger {
    ($value: literal, $item: expr, $item2: expr) => {
        use clonehunter::common::core::VERBOSE;

        if unsafe { VERBOSE } {
            println!($value, $item, $item2);
        }
    };
}

/// A Struct which can help generate a Hash on its fields
#[derive(Hash)]
pub struct FileMetaData<'a> {
    pub file_name: &'a str,
    pub modified_date: SystemTime,
    pub file_size: u64,
}

/// Struct which holds SortBy and OrderBy User Options
pub struct SortOrder(pub SortBy, pub Option<OrderBy>);

/// Printer configuration
pub struct PrinterConfig {
    pub file: Option<File>,
    pub sort_order: SortOrder,
    pub output_style: OutputStyle,
}

/// JSON printer
#[derive(Serialize, Deserialize, Debug)]
pub struct PrinterJSONObject {
    pub duplicate_group_no: usize,
    pub duplicate_group_count: usize,
    pub duplicate_group_bytes_each: usize,
    pub duplicate_list: Vec<String>,
}

pub struct WalkConfig<'a> {
    pub ext: Option<&'a str>,
    pub max_depth: Option<usize>,
    pub max_file_size: Option<u64>,
    pub min_file_size: Option<u64>,
}

pub enum FileLimitingFactor {
    GreaterThan(usize),
    LessThan(usize),
}

pub fn file_list_generator(entry: &PathBuf, wc: &WalkConfig) {
    if let Some(x) = entry.extension() {
        if let Some(ext) = wc.ext {
            let mut vec_ext = ext.split(",");
            if vec_ext.any(|y| x.eq(y)) {
                FILE_LIST.lock().unwrap().push(
                    entry
                        .to_path_buf()
                        .canonicalize()
                        .unwrap_or_else(|_| entry.to_path_buf()),
                );
                if cfg!(unix) {
                    #[cfg(target_os = "linux")]
                    {
                        match FILES_SIZE_BYTES.lock().unwrap().as_mut() {
                            Some(o) => {
                                *o += match entry.metadata() {
                                    Ok(p) => p.size(),
                                    Err(_) => 0,
                                }
                            }
                            None => {}
                        }
                    }
                } else if cfg!(windows) {
                    #[cfg(target_os = "windows")]
                    {
                        match FILES_SIZE_BYTES.lock().unwrap().as_mut() {
                            Some(o) => {
                                *o += match entry.metadata() {
                                    Ok(p) => p.file_size(),
                                    Err(_) => 0,
                                }
                            }
                            None => {}
                        }
                    }
                }
            }
        } else {
            FILE_LIST.lock().unwrap().push(
                entry
                    .to_path_buf()
                    .canonicalize()
                    .unwrap_or_else(|_| entry.to_path_buf()),
            );
            if cfg!(unix) {
                #[cfg(target_os = "linux")]
                {
                    match FILES_SIZE_BYTES.lock().unwrap().as_mut() {
                        Some(o) => {
                            *o += match entry.metadata() {
                                Ok(p) => p.size(),
                                Err(_) => 0,
                            }
                        }
                        None => {}
                    }
                }
            } else if cfg!(windows) {
                #[cfg(target_os = "windows")]
                {
                    match FILES_SIZE_BYTES.lock().unwrap().as_mut() {
                        Some(o) => {
                            *o += match entry.metadata() {
                                Ok(p) => p.file_size(),
                                Err(_) => 0,
                            }
                        }
                        None => {}
                    }
                }
            }
        }
    }
}

// Common code for recurse_dirs and walk_dirs
fn walk_and_recurse_dirs_inner<T>(path: T, wc: &WalkConfig, pb: &ProgressBar, pos: Arc<Mutex<&mut u64>>)
where
    T: DirectoryMetaData,
{
    let metadata = path.get_metadata();
    let entry = Rc::new(path.get_path());

    if metadata.is_dir() {
        let base_path = entry.to_path_buf().canonicalize().unwrap();

        DIR_LIST.lock().unwrap().push(base_path);
    } else {
        **pos.lock().unwrap() += 1;
        pb.set_position(**pos.lock().unwrap());
        let mut actual_file_size = 0;
        if cfg!(unix) {
            #[cfg(target_os = "linux")]
            {
                actual_file_size = match entry.metadata() {
                    Ok(p) => p.size(),
                    Err(_) => 0,
                }
            }
        } else if cfg!(windows) {
            #[cfg(target_os = "windows")]
            {
                actual_file_size = match entry.metadata() {
                    Ok(p) => p.file_size(),
                    Err(_) => 0,
                }
            }
        };

        match wc.min_file_size {
            Some(x) => {
                if actual_file_size > x {
                    file_list_generator(&entry, wc);
                }
            }
            None => {
                match wc.max_file_size {
                    Some(x) => {
                        if actual_file_size < x {
                            file_list_generator(&entry, wc);
                        }
                    }
                    None => file_list_generator(&entry, wc),
                };
            }
        };
    }
}

/// Used to recursively capture path entries and capture them separately in two separate Vecs.
/// DIR_LIST is used to hold Directory paths.
/// FILE_LIST is used to hold File.
pub fn recurse_dirs(item: &PathBuf, wc: &WalkConfig, pb: &ProgressBar, pos: Arc<Mutex<&mut u64>>) {
    if item.is_dir() {
        if let Ok(paths) = fs::read_dir(item) {
            for path in paths {
                walk_and_recurse_dirs_inner(&path, wc, pb, pos.clone());
                recurse_dirs(&path.unwrap().path(), wc, pb, pos.clone());
            }
        }
    }
}

/// DIR_LIST is used to hold Directory paths.
/// FILE_LIST is used to hold File paths.
/// But uses WalkDir and Rayon to make it fast.
pub fn walk_dirs(item: &PathBuf, threads: u8, wc: &WalkConfig, pb: &ProgressBar, pos: Arc<Mutex<&mut u64>>) {
    if item.is_dir() {
        let _: Vec<_> = WalkDir::new(item)
            .skip_hidden(false)
            .max_depth(wc.max_depth.unwrap())
            .parallelism(jwalk::Parallelism::RayonNewPool(threads.into()))
            .into_iter()
            .par_bridge()
            .filter_map(|dir_entry| {
                walk_and_recurse_dirs_inner(&dir_entry, wc, pb, pos.clone());
                Some(())
            })
            .collect();
    }
}

pub enum LogLevel {
    INFO,
    ERROR,
}

pub fn log(level: LogLevel, message: &str) {
    let timestamp_fmt: &str = "[%Y-%m-%d %H:%M:%S.%3f]";
    let now = Local::now();
    let timestamp: DateTime<Local> =
        DateTime::from_naive_utc_and_offset(now.naive_utc(), *now.offset());
    let colored_level = match level {
        LogLevel::INFO => "INFO".bright_yellow(),
        LogLevel::ERROR => "ERROR".bright_red(),
    };
    let print = format!(
        "\n{} {}: {}",
        timestamp.format(timestamp_fmt),
        colored_level,
        message
    );

    match level {
        LogLevel::INFO => println!("{}", print),
        LogLevel::ERROR => eprintln!("{}", print),
    }
}

/// This free standing function helps to display all the duplicate file and their respective groups file sizes.
/// It filters for duplicate files from the provided arc_vec_paths HashMap, and figures out the file sizes for each
/// group based on arc_capacities HashMap. Once the filtering and printing to screen is completed, it return the total number of duplicate records count
pub fn print_duplicates<T, U, K>(
    arc_vec_paths: &mut Arc<Mutex<HashMap<K, T>>>,
    arc_capacities: &Arc<Mutex<HashMap<K, U>>>,
    print_config: PrinterConfig,
) -> (u64, u64)
where
    T: IntoIterator + ExactSize + Clone + Paths,
    <T as IntoIterator>::Item: Debug + Displayer,
    U: AsF64,
    K: Eq + Hash,
{
    let mut duplicates_count: u64 = 0;
    let mut duplicates_total_size: u64 = 0;
    let mut arc_vec_paths = arc_vec_paths.lock().unwrap();

    let arc_capacities = arc_capacities.lock().unwrap();

    arc_vec_paths
        .iter_mut()
        .filter(|x| x.1.len() > 1)
        .for_each(|x| duplicates_count += x.1.len() as u64);

    let filtered_duplicates_result = arc_vec_paths.iter_mut().filter(|x| x.1.len() > 1);
    let mut filtered_duplicates_result: Vec<(&K, &T)> = filtered_duplicates_result
        .map(|(&ref k, v)| (k, &*v))
        .collect();

    let sort_by = print_config.sort_order.0;
    let order_by = print_config.sort_order.1;

    match sort_by {
        SortBy::FileType => {
            // Sorts the duplicates based on the file extension
            filtered_duplicates_result.sort_by(|a, b| {
                a.1.get_path()
                    .extension()
                    .unwrap_or(&OsStr::default())
                    .cmp(&b.1.get_path().extension().unwrap_or(&OsStr::default()))
            });
        }
        SortBy::FileSize => {
            // Sorts the duplicates based on the file sizes
            filtered_duplicates_result.sort_by(|a, b| {
                let x = arc_capacities.get(a.0).unwrap();
                let x2 = arc_capacities.get(b.0).unwrap();
                x.cast().total_cmp(&x2.cast())
            });
        }
        SortBy::Both => {
            // Sorts the duplicates based on the file sizes
            filtered_duplicates_result.sort_by(|a, b| {
                let x = arc_capacities.get(a.0).unwrap();
                let x2 = arc_capacities.get(b.0).unwrap();
                x.cast().total_cmp(&x2.cast())
            });

            // Sorts the duplicates based on the file extension
            filtered_duplicates_result.sort_by(|a, b| {
                a.1.get_path()
                    .extension()
                    .unwrap_or(&OsStr::default())
                    .cmp(&b.1.get_path().extension().unwrap_or(&OsStr::default()))
            });
        }
    };

    match sort_by {
        SortBy::FileType => {}
        SortBy::FileSize | SortBy::Both => {
            if let Some(o) = order_by {
                match o {
                    OrderBy::Asc => {}
                    OrderBy::Desc => filtered_duplicates_result.reverse(),
                }
            }
        }
    };

    log(LogLevel::INFO, "Finished\n");

    if print_config.file.is_none() {
        println!("######## {} ########", "Report".bright_yellow().blink());
        // Prints the duplicates to the Screen
        for (u, (i, k)) in filtered_duplicates_result.into_iter().enumerate() {
            let x = arc_capacities.get(i).unwrap();
            let y = human_bytes(x.cast());
            duplicates_total_size += x.cast() as u64;
            let list = k.clone().into_iter().collect::<Vec<_>>();

            println!(
                "\nClone {:?}, {} ({} bytes) each * {}",
                u+1,
                y,
                x.cast(),
                list.len()
            );
            for i in list {
                println!("      {}", i.to_string().bright_blue());
            }
        }
    } else {
        // Write the output to a file
        let mut writer = BufWriter::new(print_config.file.unwrap());

        log(LogLevel::INFO, "Writing the output to the file");

        match print_config.output_style {
            OutputStyle::Default => {
                for (u, (i, k)) in filtered_duplicates_result.into_iter().enumerate() {
                    let x = arc_capacities.get(i).unwrap();
                    let y = human_bytes(x.cast());
                    let list = k.clone().into_iter().collect::<Vec<_>>();

                    duplicates_total_size += x.cast() as u64;

                    let header = format!(
                        "\nClone {:?}, {} ({} bytes) each * {}\n",
                        u+1,
                        y,
                        x.cast(),
                        list.len()
                    );
                    let _ = writer.write(header.as_bytes());

                    for i in k.clone().into_iter() {
                        let message = format!("      {:?}\n", i);
                        let _ = writer.write(message.as_bytes());
                    }
                }
            }
            OutputStyle::JSON => {
                let mut print_json_array = Vec::new();
                let mut json_output = String::new();

                for (u, (i, k)) in filtered_duplicates_result.into_iter().enumerate() {
                    let mut print_json_object = PrinterJSONObject {
                        duplicate_group_no: 0,
                        duplicate_group_count: 0,
                        duplicate_group_bytes_each: 0,
                        duplicate_list: Vec::new(),
                    };

                    let x = arc_capacities.get(i).unwrap();

                    duplicates_total_size += x.cast() as u64;

                    print_json_object.duplicate_group_no = u+1;
                    print_json_object.duplicate_group_count =
                        k.clone().into_iter().collect::<Vec<_>>().len();
                    print_json_object.duplicate_group_bytes_each = x.cast() as usize;

                    for i in k.clone().into_iter() {
                        print_json_object.duplicate_list.push(i.to_string());
                    }

                    print_json_array.push(print_json_object);

                    // Serialize it to a JSON string.
                    if let Ok(o) = serde_json::to_string_pretty(&print_json_array) {
                        json_output = o;
                    } else {
                        log(LogLevel::ERROR, "Failed to Serialize to JSON String")
                    }
                }

                let _ = writer.write(json_output.as_bytes());
            }
        };

        log(LogLevel::INFO, "Finished writing to the file");
    }

    (duplicates_count, duplicates_total_size)
}