2021-05-09 04:29:52 +12:00
|
|
|
use std::collections::BTreeMap;
|
2021-02-21 00:28:06 +13:00
|
|
|
#[cfg(target_family = "unix")]
|
|
|
|
use std::collections::HashSet;
|
2022-01-06 10:47:27 +13:00
|
|
|
use std::fs::File;
|
2021-11-28 08:49:20 +13:00
|
|
|
use std::hash::Hasher;
|
2020-08-29 06:30:22 +12:00
|
|
|
use std::io::prelude::*;
|
2021-02-22 22:52:21 +13:00
|
|
|
use std::io::{self, Error, ErrorKind};
|
2021-11-28 08:49:20 +13:00
|
|
|
use std::io::{BufReader, BufWriter};
|
2021-02-21 00:28:06 +13:00
|
|
|
#[cfg(target_family = "unix")]
|
|
|
|
use std::os::unix::fs::MetadataExt;
|
2021-01-15 00:17:15 +13:00
|
|
|
use std::path::{Path, PathBuf};
|
2021-11-28 08:49:20 +13:00
|
|
|
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
|
|
|
|
use std::sync::Arc;
|
2023-05-03 22:44:28 +12:00
|
|
|
|
2023-05-03 08:37:12 +12:00
|
|
|
use std::{fs, mem};
|
2020-08-27 06:50:07 +12:00
|
|
|
|
2021-11-28 08:49:20 +13:00
|
|
|
use crossbeam_channel::Receiver;
|
2023-05-03 08:37:12 +12:00
|
|
|
use futures::channel::mpsc::UnboundedSender;
|
2022-09-14 08:18:07 +12:00
|
|
|
use humansize::format_size;
|
|
|
|
use humansize::BINARY;
|
2021-11-28 08:49:20 +13:00
|
|
|
use rayon::prelude::*;
|
2023-05-03 08:37:12 +12:00
|
|
|
use xxhash_rust::xxh3::Xxh3;
|
2021-11-28 08:49:20 +13:00
|
|
|
|
2023-05-03 22:44:28 +12:00
|
|
|
use crate::common::{open_cache_folder, prepare_thread_handler_common, send_info_and_wait_for_ending_all_threads};
|
2022-01-02 09:07:20 +13:00
|
|
|
use crate::common_dir_traversal::{CheckingMethod, DirTraversalBuilder, DirTraversalResult, FileEntry, ProgressData};
|
2020-09-27 03:52:13 +13:00
|
|
|
use crate::common_directory::Directories;
|
|
|
|
use crate::common_extensions::Extensions;
|
|
|
|
use crate::common_items::ExcludedItems;
|
|
|
|
use crate::common_messages::Messages;
|
|
|
|
use crate::common_traits::*;
|
2022-01-20 10:35:07 +13:00
|
|
|
use crate::flc;
|
|
|
|
use crate::localizer_core::generate_translation_hashmap;
|
2020-08-30 02:12:20 +12:00
|
|
|
|
2022-04-09 03:17:16 +12:00
|
|
|
const TEMP_HARDLINK_FILE: &str = "rzeczek.rxrxrxl";
|
|
|
|
|
2021-01-15 00:17:15 +13:00
|
|
|
#[derive(PartialEq, Eq, Clone, Debug, Copy)]
|
2020-10-25 07:44:21 +13:00
|
|
|
pub enum HashType {
|
|
|
|
Blake3,
|
2021-03-28 01:14:02 +13:00
|
|
|
Crc32,
|
|
|
|
Xxh3,
|
2020-10-25 07:44:21 +13:00
|
|
|
}
|
|
|
|
|
2021-02-22 22:52:21 +13:00
|
|
|
impl HashType {
|
|
|
|
fn hasher(self: &HashType) -> Box<dyn MyHasher> {
|
|
|
|
match self {
|
|
|
|
HashType::Blake3 => Box::new(blake3::Hasher::new()),
|
2021-03-28 01:14:02 +13:00
|
|
|
HashType::Crc32 => Box::new(crc32fast::Hasher::new()),
|
2023-05-03 08:37:12 +12:00
|
|
|
HashType::Xxh3 => Box::new(Xxh3::new()),
|
2021-02-22 22:52:21 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-19 22:21:14 +13:00
|
|
|
#[derive(Eq, PartialEq, Clone, Debug, Copy)]
|
2020-09-01 02:15:56 +12:00
|
|
|
pub enum DeleteMethod {
|
|
|
|
None,
|
2020-08-31 03:18:04 +12:00
|
|
|
AllExceptNewest,
|
|
|
|
AllExceptOldest,
|
|
|
|
OneOldest,
|
|
|
|
OneNewest,
|
2021-02-06 05:59:34 +13:00
|
|
|
HardLink,
|
2020-09-01 02:15:56 +12:00
|
|
|
}
|
|
|
|
|
2020-10-10 09:32:08 +13:00
|
|
|
#[derive(Default)]
|
2020-09-10 22:42:44 +12:00
|
|
|
pub struct Info {
|
2020-09-16 05:17:13 +12:00
|
|
|
pub number_of_groups_by_size: usize,
|
|
|
|
pub number_of_duplicated_files_by_size: usize,
|
|
|
|
pub number_of_groups_by_hash: usize,
|
|
|
|
pub number_of_duplicated_files_by_hash: usize,
|
2020-10-25 07:44:21 +13:00
|
|
|
pub number_of_groups_by_name: usize,
|
|
|
|
pub number_of_duplicated_files_by_name: usize,
|
2023-04-05 18:08:43 +12:00
|
|
|
pub number_of_groups_by_size_name: usize,
|
|
|
|
pub number_of_duplicated_files_by_size_name: usize,
|
2020-09-16 05:17:13 +12:00
|
|
|
pub lost_space_by_size: u64,
|
|
|
|
pub lost_space_by_hash: u64,
|
2020-09-10 22:42:44 +12:00
|
|
|
}
|
|
|
|
|
2020-10-10 09:32:08 +13:00
|
|
|
impl Info {
|
2023-01-29 06:54:02 +13:00
|
|
|
#[must_use]
|
2020-10-10 09:32:08 +13:00
|
|
|
pub fn new() -> Self {
|
|
|
|
Default::default()
|
2020-09-10 22:42:44 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-17 22:07:58 +12:00
|
|
|
pub struct DuplicateFinder {
|
2020-09-18 20:23:49 +12:00
|
|
|
text_messages: Messages,
|
2020-09-18 17:32:37 +12:00
|
|
|
information: Info,
|
2023-04-05 18:08:43 +12:00
|
|
|
files_with_identical_names: BTreeMap<String, Vec<FileEntry>>, // File Size, File Entry
|
|
|
|
files_with_identical_size_names: BTreeMap<(u64, String), Vec<FileEntry>>, // File (Size, Name), File Entry
|
|
|
|
files_with_identical_size: BTreeMap<u64, Vec<FileEntry>>, // File Size, File Entry
|
|
|
|
files_with_identical_hashes: BTreeMap<u64, Vec<Vec<FileEntry>>>, // File Size, next grouped by file size, next grouped by hash
|
|
|
|
files_with_identical_names_referenced: BTreeMap<String, (FileEntry, Vec<FileEntry>)>, // File Size, File Entry
|
|
|
|
files_with_identical_size_names_referenced: BTreeMap<(u64, String), (FileEntry, Vec<FileEntry>)>, // File (Size, Name), File Entry
|
|
|
|
files_with_identical_size_referenced: BTreeMap<u64, (FileEntry, Vec<FileEntry>)>, // File Size, File Entry
|
2021-12-24 21:18:55 +13:00
|
|
|
files_with_identical_hashes_referenced: BTreeMap<u64, Vec<(FileEntry, Vec<FileEntry>)>>, // File Size, next grouped by file size, next grouped by hash
|
2020-09-27 03:52:13 +13:00
|
|
|
directories: Directories,
|
|
|
|
allowed_extensions: Extensions,
|
|
|
|
excluded_items: ExcludedItems,
|
2020-09-17 22:07:58 +12:00
|
|
|
recursive_search: bool,
|
2020-10-03 20:51:02 +13:00
|
|
|
minimal_file_size: u64,
|
2021-08-07 09:23:11 +12:00
|
|
|
maximal_file_size: u64,
|
2020-09-17 22:07:58 +12:00
|
|
|
check_method: CheckingMethod,
|
|
|
|
delete_method: DeleteMethod,
|
2020-10-25 07:44:21 +13:00
|
|
|
hash_type: HashType,
|
2021-02-23 08:14:33 +13:00
|
|
|
ignore_hard_links: bool,
|
2021-03-02 00:23:43 +13:00
|
|
|
dryrun: bool,
|
2020-10-10 22:15:20 +13:00
|
|
|
stopped_search: bool,
|
2021-03-05 00:09:53 +13:00
|
|
|
use_cache: bool,
|
2021-12-02 08:09:04 +13:00
|
|
|
use_prehash_cache: bool,
|
2021-05-07 07:54:44 +12:00
|
|
|
minimal_cache_file_size: u64,
|
2021-12-02 08:09:04 +13:00
|
|
|
minimal_prehash_cache_file_size: u64,
|
2021-12-01 00:45:09 +13:00
|
|
|
delete_outdated_cache: bool,
|
2021-12-24 21:18:55 +13:00
|
|
|
use_reference_folders: bool,
|
2022-04-10 21:59:41 +12:00
|
|
|
case_sensitive_name_comparison: bool,
|
2020-09-17 22:07:58 +12:00
|
|
|
}
|
|
|
|
|
2020-09-10 22:42:44 +12:00
|
|
|
impl DuplicateFinder {
|
2023-01-29 06:54:02 +13:00
|
|
|
#[must_use]
|
2020-10-10 09:32:08 +13:00
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
2020-09-27 03:52:13 +13:00
|
|
|
text_messages: Messages::new(),
|
2020-09-18 17:32:37 +12:00
|
|
|
information: Info::new(),
|
2020-10-25 07:44:21 +13:00
|
|
|
files_with_identical_names: Default::default(),
|
2020-08-30 02:12:20 +12:00
|
|
|
files_with_identical_size: Default::default(),
|
2023-04-05 18:08:43 +12:00
|
|
|
files_with_identical_size_names: Default::default(),
|
2020-08-30 02:12:20 +12:00
|
|
|
files_with_identical_hashes: Default::default(),
|
2021-12-24 21:18:55 +13:00
|
|
|
files_with_identical_names_referenced: Default::default(),
|
2023-04-05 18:08:43 +12:00
|
|
|
files_with_identical_size_names_referenced: Default::default(),
|
2021-12-24 21:18:55 +13:00
|
|
|
files_with_identical_size_referenced: Default::default(),
|
|
|
|
files_with_identical_hashes_referenced: Default::default(),
|
2020-09-12 23:25:23 +12:00
|
|
|
recursive_search: true,
|
2020-09-27 03:52:13 +13:00
|
|
|
allowed_extensions: Extensions::new(),
|
2020-09-25 08:25:24 +12:00
|
|
|
check_method: CheckingMethod::None,
|
2020-09-17 22:07:58 +12:00
|
|
|
delete_method: DeleteMethod::None,
|
2021-08-07 09:23:11 +12:00
|
|
|
minimal_file_size: 8192,
|
|
|
|
maximal_file_size: u64::MAX,
|
2020-09-27 03:52:13 +13:00
|
|
|
directories: Directories::new(),
|
|
|
|
excluded_items: ExcludedItems::new(),
|
2020-10-10 22:15:20 +13:00
|
|
|
stopped_search: false,
|
2021-02-23 08:14:33 +13:00
|
|
|
ignore_hard_links: true,
|
2020-10-25 07:44:21 +13:00
|
|
|
hash_type: HashType::Blake3,
|
2021-03-02 00:23:43 +13:00
|
|
|
dryrun: false,
|
2021-03-05 00:09:53 +13:00
|
|
|
use_cache: true,
|
2021-12-02 08:09:04 +13:00
|
|
|
use_prehash_cache: true,
|
|
|
|
minimal_cache_file_size: 1024 * 1024 / 4, // By default cache only >= 256 KB files
|
|
|
|
minimal_prehash_cache_file_size: 0,
|
2021-12-01 00:45:09 +13:00
|
|
|
delete_outdated_cache: true,
|
2021-12-24 21:18:55 +13:00
|
|
|
use_reference_folders: false,
|
2022-04-10 21:59:41 +12:00
|
|
|
case_sensitive_name_comparison: false,
|
2020-08-30 02:12:20 +12:00
|
|
|
}
|
|
|
|
}
|
2020-09-18 17:32:37 +12:00
|
|
|
|
2023-05-03 08:37:12 +12:00
|
|
|
pub fn find_duplicates(&mut self, stop_receiver: Option<&Receiver<()>>, progress_sender: Option<&UnboundedSender<ProgressData>>) {
|
2020-09-27 03:52:13 +13:00
|
|
|
self.directories.optimize_directories(self.recursive_search, &mut self.text_messages);
|
2021-12-24 21:18:55 +13:00
|
|
|
self.use_reference_folders = !self.directories.reference_directories.is_empty();
|
2020-10-25 07:44:21 +13:00
|
|
|
|
|
|
|
match self.check_method {
|
|
|
|
CheckingMethod::Name => {
|
2023-04-05 18:08:43 +12:00
|
|
|
self.stopped_search = !self.check_files_name(stop_receiver, progress_sender); // TODO restore this to name
|
|
|
|
if self.stopped_search {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CheckingMethod::SizeName => {
|
|
|
|
self.stopped_search = !self.check_files_size_name(stop_receiver, progress_sender);
|
|
|
|
if self.stopped_search {
|
2020-10-25 07:44:21 +13:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CheckingMethod::Size => {
|
2023-04-05 18:08:43 +12:00
|
|
|
self.stopped_search = !self.check_files_size(stop_receiver, progress_sender);
|
|
|
|
if self.stopped_search {
|
2020-10-25 07:44:21 +13:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2021-12-02 00:37:17 +13:00
|
|
|
CheckingMethod::Hash => {
|
2023-04-05 18:08:43 +12:00
|
|
|
self.stopped_search = !self.check_files_size(stop_receiver, progress_sender);
|
|
|
|
if self.stopped_search {
|
2020-10-25 07:44:21 +13:00
|
|
|
return;
|
|
|
|
}
|
2023-04-05 18:08:43 +12:00
|
|
|
self.stopped_search = !self.check_files_hash(stop_receiver, progress_sender);
|
|
|
|
if self.stopped_search {
|
2020-10-25 07:44:21 +13:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CheckingMethod::None => {
|
|
|
|
panic!();
|
2020-10-10 22:15:20 +13:00
|
|
|
}
|
2020-09-27 03:52:13 +13:00
|
|
|
}
|
|
|
|
self.delete_files();
|
|
|
|
self.debug_print();
|
|
|
|
}
|
|
|
|
|
2021-12-01 00:45:09 +13:00
|
|
|
pub fn set_delete_outdated_cache(&mut self, delete_outdated_cache: bool) {
|
|
|
|
self.delete_outdated_cache = delete_outdated_cache;
|
|
|
|
}
|
|
|
|
|
2022-04-10 21:59:41 +12:00
|
|
|
pub fn set_case_sensitive_name_comparison(&mut self, case_sensitive_name_comparison: bool) {
|
|
|
|
self.case_sensitive_name_comparison = case_sensitive_name_comparison;
|
|
|
|
}
|
|
|
|
|
2023-01-29 06:54:02 +13:00
|
|
|
#[must_use]
|
2020-10-10 09:32:08 +13:00
|
|
|
pub const fn get_check_method(&self) -> &CheckingMethod {
|
2020-10-08 09:22:39 +13:00
|
|
|
&self.check_method
|
|
|
|
}
|
|
|
|
|
2023-01-29 06:54:02 +13:00
|
|
|
#[must_use]
|
2020-10-10 22:15:20 +13:00
|
|
|
pub fn get_stopped_search(&self) -> bool {
|
|
|
|
self.stopped_search
|
|
|
|
}
|
|
|
|
|
2021-05-07 07:54:44 +12:00
|
|
|
pub fn set_minimal_cache_file_size(&mut self, minimal_cache_file_size: u64) {
|
|
|
|
self.minimal_cache_file_size = minimal_cache_file_size;
|
|
|
|
}
|
|
|
|
|
2021-12-02 08:09:04 +13:00
|
|
|
pub fn set_minimal_prehash_cache_file_size(&mut self, minimal_prehash_cache_file_size: u64) {
|
|
|
|
self.minimal_prehash_cache_file_size = minimal_prehash_cache_file_size;
|
|
|
|
}
|
|
|
|
|
2023-01-29 06:54:02 +13:00
|
|
|
#[must_use]
|
2020-10-25 07:44:21 +13:00
|
|
|
pub const fn get_files_sorted_by_names(&self) -> &BTreeMap<String, Vec<FileEntry>> {
|
|
|
|
&self.files_with_identical_names
|
|
|
|
}
|
|
|
|
|
2021-03-05 00:09:53 +13:00
|
|
|
pub fn set_use_cache(&mut self, use_cache: bool) {
|
|
|
|
self.use_cache = use_cache;
|
|
|
|
}
|
|
|
|
|
2021-12-02 08:09:04 +13:00
|
|
|
pub fn set_use_prehash_cache(&mut self, use_prehash_cache: bool) {
|
|
|
|
self.use_prehash_cache = use_prehash_cache;
|
|
|
|
}
|
|
|
|
|
2023-01-29 06:54:02 +13:00
|
|
|
#[must_use]
|
2020-10-10 09:32:08 +13:00
|
|
|
pub const fn get_files_sorted_by_size(&self) -> &BTreeMap<u64, Vec<FileEntry>> {
|
2020-09-20 21:32:55 +12:00
|
|
|
&self.files_with_identical_size
|
|
|
|
}
|
|
|
|
|
2023-04-05 18:08:43 +12:00
|
|
|
#[must_use]
|
|
|
|
pub const fn get_files_sorted_by_size_name(&self) -> &BTreeMap<(u64, String), Vec<FileEntry>> {
|
|
|
|
&self.files_with_identical_size_names
|
|
|
|
}
|
|
|
|
|
2023-01-29 06:54:02 +13:00
|
|
|
#[must_use]
|
2020-10-10 09:32:08 +13:00
|
|
|
pub const fn get_files_sorted_by_hash(&self) -> &BTreeMap<u64, Vec<Vec<FileEntry>>> {
|
2020-09-20 21:32:55 +12:00
|
|
|
&self.files_with_identical_hashes
|
|
|
|
}
|
2021-08-07 09:23:11 +12:00
|
|
|
pub fn set_maximal_file_size(&mut self, maximal_file_size: u64) {
|
|
|
|
self.maximal_file_size = match maximal_file_size {
|
|
|
|
0 => 1,
|
|
|
|
t => t,
|
|
|
|
};
|
|
|
|
}
|
2020-09-20 21:32:55 +12:00
|
|
|
|
2023-01-29 06:54:02 +13:00
|
|
|
#[must_use]
|
2020-10-10 09:32:08 +13:00
|
|
|
pub const fn get_text_messages(&self) -> &Messages {
|
2020-09-18 20:23:49 +12:00
|
|
|
&self.text_messages
|
2020-09-18 17:32:37 +12:00
|
|
|
}
|
2020-09-25 08:25:24 +12:00
|
|
|
|
2023-01-29 06:54:02 +13:00
|
|
|
#[must_use]
|
2020-10-10 09:32:08 +13:00
|
|
|
pub const fn get_information(&self) -> &Info {
|
2020-09-18 17:32:37 +12:00
|
|
|
&self.information
|
2020-09-12 01:52:06 +12:00
|
|
|
}
|
2020-08-30 02:12:20 +12:00
|
|
|
|
2021-02-04 07:59:06 +13:00
|
|
|
pub fn set_hash_type(&mut self, hash_type: HashType) {
|
|
|
|
self.hash_type = hash_type;
|
|
|
|
}
|
|
|
|
|
2021-02-23 08:14:33 +13:00
|
|
|
pub fn set_ignore_hard_links(&mut self, ignore_hard_links: bool) {
|
|
|
|
self.ignore_hard_links = ignore_hard_links;
|
|
|
|
}
|
|
|
|
|
2021-03-02 00:23:43 +13:00
|
|
|
pub fn set_dryrun(&mut self, dryrun: bool) {
|
|
|
|
self.dryrun = dryrun;
|
|
|
|
}
|
|
|
|
|
2020-09-17 22:07:58 +12:00
|
|
|
pub fn set_check_method(&mut self, check_method: CheckingMethod) {
|
|
|
|
self.check_method = check_method;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_delete_method(&mut self, delete_method: DeleteMethod) {
|
|
|
|
self.delete_method = delete_method;
|
|
|
|
}
|
|
|
|
|
2020-10-03 20:51:02 +13:00
|
|
|
pub fn set_minimal_file_size(&mut self, minimal_file_size: u64) {
|
|
|
|
self.minimal_file_size = match minimal_file_size {
|
|
|
|
0 => 1,
|
|
|
|
t => t,
|
|
|
|
};
|
2020-08-30 02:12:20 +12:00
|
|
|
}
|
|
|
|
|
2023-01-29 06:54:02 +13:00
|
|
|
#[must_use]
|
2021-12-24 21:18:55 +13:00
|
|
|
pub fn get_use_reference(&self) -> bool {
|
|
|
|
self.use_reference_folders
|
|
|
|
}
|
|
|
|
|
2020-09-16 05:17:13 +12:00
|
|
|
pub fn set_recursive_search(&mut self, recursive_search: bool) {
|
|
|
|
self.recursive_search = recursive_search;
|
2020-09-12 23:25:23 +12:00
|
|
|
}
|
2020-09-25 08:25:24 +12:00
|
|
|
|
2022-04-24 06:21:46 +12:00
|
|
|
#[cfg(target_family = "unix")]
|
|
|
|
pub fn set_exclude_other_filesystems(&mut self, exclude_other_filesystems: bool) {
|
|
|
|
self.directories.set_exclude_other_filesystems(exclude_other_filesystems);
|
|
|
|
}
|
2022-07-20 05:09:52 +12:00
|
|
|
#[cfg(not(target_family = "unix"))]
|
|
|
|
pub fn set_exclude_other_filesystems(&mut self, _exclude_other_filesystems: bool) {}
|
2022-04-24 06:21:46 +12:00
|
|
|
|
2021-12-24 21:18:55 +13:00
|
|
|
pub fn set_included_directory(&mut self, included_directory: Vec<PathBuf>) {
|
|
|
|
self.directories.set_included_directory(included_directory, &mut self.text_messages);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_reference_directory(&mut self, reference_directory: Vec<PathBuf>) {
|
|
|
|
self.directories.set_reference_directory(reference_directory);
|
2020-09-25 08:25:24 +12:00
|
|
|
}
|
|
|
|
|
2021-01-11 08:44:10 +13:00
|
|
|
pub fn set_excluded_directory(&mut self, excluded_directory: Vec<PathBuf>) {
|
2020-09-27 03:52:13 +13:00
|
|
|
self.directories.set_excluded_directory(excluded_directory, &mut self.text_messages);
|
2020-08-30 02:12:20 +12:00
|
|
|
}
|
2021-12-24 21:18:55 +13:00
|
|
|
|
|
|
|
pub fn set_excluded_items(&mut self, excluded_items: Vec<String>) {
|
|
|
|
self.excluded_items.set_excluded_items(excluded_items, &mut self.text_messages);
|
|
|
|
}
|
2020-09-27 03:52:13 +13:00
|
|
|
pub fn set_allowed_extensions(&mut self, allowed_extensions: String) {
|
|
|
|
self.allowed_extensions.set_allowed_extensions(allowed_extensions, &mut self.text_messages);
|
2020-08-30 02:12:20 +12:00
|
|
|
}
|
|
|
|
|
2023-01-29 06:54:02 +13:00
|
|
|
#[must_use]
|
2021-12-24 21:18:55 +13:00
|
|
|
pub fn get_files_with_identical_hashes_referenced(&self) -> &BTreeMap<u64, Vec<(FileEntry, Vec<FileEntry>)>> {
|
|
|
|
&self.files_with_identical_hashes_referenced
|
|
|
|
}
|
|
|
|
|
2023-01-29 06:54:02 +13:00
|
|
|
#[must_use]
|
2021-12-24 21:18:55 +13:00
|
|
|
pub fn get_files_with_identical_name_referenced(&self) -> &BTreeMap<String, (FileEntry, Vec<FileEntry>)> {
|
|
|
|
&self.files_with_identical_names_referenced
|
|
|
|
}
|
|
|
|
|
2023-01-29 06:54:02 +13:00
|
|
|
#[must_use]
|
2021-12-24 21:18:55 +13:00
|
|
|
pub fn get_files_with_identical_size_referenced(&self) -> &BTreeMap<u64, (FileEntry, Vec<FileEntry>)> {
|
|
|
|
&self.files_with_identical_size_referenced
|
2020-08-27 06:50:07 +12:00
|
|
|
}
|
2020-08-30 02:12:20 +12:00
|
|
|
|
2023-04-05 18:08:43 +12:00
|
|
|
#[must_use]
|
|
|
|
pub fn get_files_with_identical_size_names_referenced(&self) -> &BTreeMap<(u64, String), (FileEntry, Vec<FileEntry>)> {
|
|
|
|
&self.files_with_identical_size_names_referenced
|
|
|
|
}
|
|
|
|
|
2023-05-03 08:37:12 +12:00
|
|
|
fn check_files_name(&mut self, stop_receiver: Option<&Receiver<()>>, progress_sender: Option<&UnboundedSender<ProgressData>>) -> bool {
|
2022-04-10 21:59:41 +12:00
|
|
|
let group_by_func = if self.case_sensitive_name_comparison {
|
|
|
|
|fe: &FileEntry| fe.path.file_name().unwrap().to_string_lossy().to_string()
|
|
|
|
} else {
|
|
|
|
|fe: &FileEntry| fe.path.file_name().unwrap().to_string_lossy().to_lowercase()
|
|
|
|
};
|
|
|
|
|
2022-01-02 09:07:20 +13:00
|
|
|
let result = DirTraversalBuilder::new()
|
|
|
|
.root_dirs(self.directories.included_directories.clone())
|
2022-04-10 21:59:41 +12:00
|
|
|
.group_by(group_by_func)
|
2022-01-02 09:07:20 +13:00
|
|
|
.stop_receiver(stop_receiver)
|
|
|
|
.progress_sender(progress_sender)
|
|
|
|
.checking_method(CheckingMethod::Name)
|
|
|
|
.directories(self.directories.clone())
|
|
|
|
.allowed_extensions(self.allowed_extensions.clone())
|
|
|
|
.excluded_items(self.excluded_items.clone())
|
|
|
|
.recursive_search(self.recursive_search)
|
|
|
|
.minimal_file_size(self.minimal_file_size)
|
|
|
|
.maximal_file_size(self.maximal_file_size)
|
|
|
|
.build()
|
|
|
|
.run();
|
|
|
|
match result {
|
2023-05-03 22:44:28 +12:00
|
|
|
DirTraversalResult::SuccessFiles { grouped_file_entries, warnings } => {
|
2022-01-02 09:07:20 +13:00
|
|
|
self.files_with_identical_names = grouped_file_entries;
|
|
|
|
self.text_messages.warnings.extend(warnings);
|
2020-12-02 22:25:27 +13:00
|
|
|
|
2022-01-02 09:07:20 +13:00
|
|
|
// Create new BTreeMap without single size entries(files have not duplicates)
|
|
|
|
let mut new_map: BTreeMap<String, Vec<FileEntry>> = Default::default();
|
2020-12-02 22:25:27 +13:00
|
|
|
|
2022-01-02 09:07:20 +13:00
|
|
|
for (name, vector) in &self.files_with_identical_names {
|
|
|
|
if vector.len() > 1 {
|
|
|
|
new_map.insert(name.clone(), vector.clone());
|
|
|
|
}
|
2020-12-02 22:25:27 +13:00
|
|
|
}
|
2022-01-02 09:07:20 +13:00
|
|
|
self.files_with_identical_names = new_map;
|
|
|
|
|
|
|
|
// Reference - only use in size, because later hash will be counted differently
|
|
|
|
if self.use_reference_folders {
|
|
|
|
let mut btree_map = Default::default();
|
|
|
|
mem::swap(&mut self.files_with_identical_names, &mut btree_map);
|
|
|
|
let reference_directories = self.directories.reference_directories.clone();
|
|
|
|
let vec = btree_map
|
|
|
|
.into_iter()
|
|
|
|
.filter_map(|(_size, vec_file_entry)| {
|
|
|
|
let mut files_from_referenced_folders = Vec::new();
|
|
|
|
let mut normal_files = Vec::new();
|
|
|
|
for file_entry in vec_file_entry {
|
2022-11-24 08:23:17 +13:00
|
|
|
if reference_directories.iter().any(|e| file_entry.path.starts_with(e)) {
|
2022-01-02 09:07:20 +13:00
|
|
|
files_from_referenced_folders.push(file_entry);
|
|
|
|
} else {
|
|
|
|
normal_files.push(file_entry);
|
2021-12-17 08:49:59 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-02 09:07:20 +13:00
|
|
|
if files_from_referenced_folders.is_empty() || normal_files.is_empty() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some((files_from_referenced_folders.pop().unwrap(), normal_files))
|
2021-12-17 08:49:59 +13:00
|
|
|
}
|
2022-01-02 09:07:20 +13:00
|
|
|
})
|
|
|
|
.collect::<Vec<(FileEntry, Vec<FileEntry>)>>();
|
|
|
|
for (fe, vec_fe) in vec {
|
|
|
|
self.files_with_identical_names_referenced.insert(fe.path.to_string_lossy().to_string(), (fe, vec_fe));
|
2020-10-25 07:44:21 +13:00
|
|
|
}
|
|
|
|
}
|
2023-04-05 18:08:43 +12:00
|
|
|
self.calculate_name_stats();
|
2021-12-24 21:18:55 +13:00
|
|
|
|
2022-01-02 09:07:20 +13:00
|
|
|
true
|
2021-12-24 21:18:55 +13:00
|
|
|
}
|
2022-01-02 09:07:20 +13:00
|
|
|
DirTraversalResult::SuccessFolders { .. } => {
|
|
|
|
unreachable!()
|
2021-12-24 21:18:55 +13:00
|
|
|
}
|
2022-01-02 09:07:20 +13:00
|
|
|
DirTraversalResult::Stopped => false,
|
2021-12-24 21:18:55 +13:00
|
|
|
}
|
2020-10-25 07:44:21 +13:00
|
|
|
}
|
|
|
|
|
2023-04-05 18:08:43 +12:00
|
|
|
fn calculate_name_stats(&mut self) {
|
|
|
|
if self.use_reference_folders {
|
|
|
|
for (_fe, vector) in self.files_with_identical_names_referenced.values() {
|
|
|
|
self.information.number_of_duplicated_files_by_name += vector.len();
|
|
|
|
self.information.number_of_groups_by_name += 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for vector in self.files_with_identical_names.values() {
|
|
|
|
self.information.number_of_duplicated_files_by_name += vector.len() - 1;
|
|
|
|
self.information.number_of_groups_by_name += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-03 08:37:12 +12:00
|
|
|
fn check_files_size_name(&mut self, stop_receiver: Option<&Receiver<()>>, progress_sender: Option<&UnboundedSender<ProgressData>>) -> bool {
|
2023-04-05 18:08:43 +12:00
|
|
|
let group_by_func = if self.case_sensitive_name_comparison {
|
|
|
|
|fe: &FileEntry| (fe.size, fe.path.file_name().unwrap().to_string_lossy().to_string())
|
|
|
|
} else {
|
|
|
|
|fe: &FileEntry| (fe.size, fe.path.file_name().unwrap().to_string_lossy().to_lowercase())
|
2021-12-19 11:45:37 +13:00
|
|
|
};
|
2023-04-05 18:08:43 +12:00
|
|
|
|
2022-01-02 09:07:20 +13:00
|
|
|
let result = DirTraversalBuilder::new()
|
|
|
|
.root_dirs(self.directories.included_directories.clone())
|
2023-04-05 18:08:43 +12:00
|
|
|
.group_by(group_by_func)
|
2022-01-02 09:07:20 +13:00
|
|
|
.stop_receiver(stop_receiver)
|
|
|
|
.progress_sender(progress_sender)
|
2023-04-05 18:08:43 +12:00
|
|
|
.checking_method(CheckingMethod::Name)
|
2022-01-02 09:07:20 +13:00
|
|
|
.directories(self.directories.clone())
|
|
|
|
.allowed_extensions(self.allowed_extensions.clone())
|
|
|
|
.excluded_items(self.excluded_items.clone())
|
|
|
|
.recursive_search(self.recursive_search)
|
|
|
|
.minimal_file_size(self.minimal_file_size)
|
|
|
|
.maximal_file_size(self.maximal_file_size)
|
|
|
|
.build()
|
|
|
|
.run();
|
|
|
|
match result {
|
2023-05-03 22:44:28 +12:00
|
|
|
DirTraversalResult::SuccessFiles { grouped_file_entries, warnings } => {
|
2023-04-05 18:08:43 +12:00
|
|
|
self.files_with_identical_size_names = grouped_file_entries;
|
2022-01-02 09:07:20 +13:00
|
|
|
self.text_messages.warnings.extend(warnings);
|
2020-12-02 22:25:27 +13:00
|
|
|
|
2022-01-02 09:07:20 +13:00
|
|
|
// Create new BTreeMap without single size entries(files have not duplicates)
|
2023-04-05 18:08:43 +12:00
|
|
|
let mut new_map: BTreeMap<(u64, String), Vec<FileEntry>> = Default::default();
|
2020-09-12 23:25:23 +12:00
|
|
|
|
2023-04-05 18:08:43 +12:00
|
|
|
for (name_size, vector) in &self.files_with_identical_size_names {
|
2022-01-02 09:07:20 +13:00
|
|
|
if vector.len() > 1 {
|
2023-04-05 18:08:43 +12:00
|
|
|
new_map.insert(name_size.clone(), vector.clone());
|
2022-01-02 09:07:20 +13:00
|
|
|
}
|
|
|
|
}
|
2023-04-05 18:08:43 +12:00
|
|
|
self.files_with_identical_size_names = new_map;
|
2020-10-15 05:41:37 +13:00
|
|
|
|
2022-01-02 09:07:20 +13:00
|
|
|
// Reference - only use in size, because later hash will be counted differently
|
2023-04-05 18:08:43 +12:00
|
|
|
if self.use_reference_folders {
|
2022-01-02 09:07:20 +13:00
|
|
|
let mut btree_map = Default::default();
|
2023-04-05 18:08:43 +12:00
|
|
|
mem::swap(&mut self.files_with_identical_size_names, &mut btree_map);
|
2022-01-02 09:07:20 +13:00
|
|
|
let reference_directories = self.directories.reference_directories.clone();
|
|
|
|
let vec = btree_map
|
|
|
|
.into_iter()
|
|
|
|
.filter_map(|(_size, vec_file_entry)| {
|
|
|
|
let mut files_from_referenced_folders = Vec::new();
|
|
|
|
let mut normal_files = Vec::new();
|
|
|
|
for file_entry in vec_file_entry {
|
2022-11-24 08:23:17 +13:00
|
|
|
if reference_directories.iter().any(|e| file_entry.path.starts_with(e)) {
|
2022-01-02 09:07:20 +13:00
|
|
|
files_from_referenced_folders.push(file_entry);
|
|
|
|
} else {
|
|
|
|
normal_files.push(file_entry);
|
2021-12-18 07:29:37 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-02 09:07:20 +13:00
|
|
|
if files_from_referenced_folders.is_empty() || normal_files.is_empty() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some((files_from_referenced_folders.pop().unwrap(), normal_files))
|
2021-12-18 07:29:37 +13:00
|
|
|
}
|
2022-01-02 09:07:20 +13:00
|
|
|
})
|
|
|
|
.collect::<Vec<(FileEntry, Vec<FileEntry>)>>();
|
|
|
|
for (fe, vec_fe) in vec {
|
2023-04-05 18:08:43 +12:00
|
|
|
self.files_with_identical_size_names_referenced
|
|
|
|
.insert((fe.size, fe.path.to_string_lossy().to_string()), (fe, vec_fe));
|
2020-08-30 05:11:55 +12:00
|
|
|
}
|
2020-08-28 04:57:56 +12:00
|
|
|
}
|
2023-04-05 18:08:43 +12:00
|
|
|
self.calculate_size_name_stats();
|
2020-09-18 00:01:14 +12:00
|
|
|
|
2023-04-05 18:08:43 +12:00
|
|
|
true
|
|
|
|
}
|
|
|
|
DirTraversalResult::SuccessFolders { .. } => {
|
|
|
|
unreachable!()
|
|
|
|
}
|
|
|
|
DirTraversalResult::Stopped => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn calculate_size_name_stats(&mut self) {
|
|
|
|
if self.use_reference_folders {
|
|
|
|
for ((size, _name), (_fe, vector)) in &self.files_with_identical_size_names_referenced {
|
|
|
|
self.information.number_of_duplicated_files_by_size_name += vector.len();
|
|
|
|
self.information.number_of_groups_by_size_name += 1;
|
|
|
|
self.information.lost_space_by_size += (vector.len() as u64) * size;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for ((size, _name), vector) in &self.files_with_identical_size_names {
|
|
|
|
self.information.number_of_duplicated_files_by_size_name += vector.len() - 1;
|
|
|
|
self.information.number_of_groups_by_size_name += 1;
|
|
|
|
self.information.lost_space_by_size += (vector.len() as u64 - 1) * size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Read file length and puts it to different boxes(each for different lengths)
|
|
|
|
/// If in box is only 1 result, then it is removed
|
2023-05-03 08:37:12 +12:00
|
|
|
fn check_files_size(&mut self, stop_receiver: Option<&Receiver<()>>, progress_sender: Option<&UnboundedSender<ProgressData>>) -> bool {
|
2023-04-05 18:08:43 +12:00
|
|
|
let max_stage = match self.check_method {
|
|
|
|
CheckingMethod::Size => 0,
|
|
|
|
CheckingMethod::Hash => 2,
|
|
|
|
_ => panic!(),
|
|
|
|
};
|
|
|
|
let result = DirTraversalBuilder::new()
|
|
|
|
.root_dirs(self.directories.included_directories.clone())
|
|
|
|
.group_by(|fe| fe.size)
|
|
|
|
.stop_receiver(stop_receiver)
|
|
|
|
.progress_sender(progress_sender)
|
|
|
|
.checking_method(self.check_method)
|
|
|
|
.max_stage(max_stage)
|
|
|
|
.directories(self.directories.clone())
|
|
|
|
.allowed_extensions(self.allowed_extensions.clone())
|
|
|
|
.excluded_items(self.excluded_items.clone())
|
|
|
|
.recursive_search(self.recursive_search)
|
|
|
|
.minimal_file_size(self.minimal_file_size)
|
|
|
|
.maximal_file_size(self.maximal_file_size)
|
|
|
|
.build()
|
|
|
|
.run();
|
|
|
|
match result {
|
2023-05-03 22:44:28 +12:00
|
|
|
DirTraversalResult::SuccessFiles { grouped_file_entries, warnings } => {
|
2023-04-05 18:08:43 +12:00
|
|
|
self.files_with_identical_size = grouped_file_entries;
|
|
|
|
self.text_messages.warnings.extend(warnings);
|
|
|
|
|
|
|
|
// Create new BTreeMap without single size entries(files have not duplicates)
|
|
|
|
let mut old_map: BTreeMap<u64, Vec<FileEntry>> = Default::default();
|
|
|
|
mem::swap(&mut old_map, &mut self.files_with_identical_size);
|
|
|
|
|
|
|
|
for (size, vec) in old_map {
|
|
|
|
if vec.len() <= 1 {
|
|
|
|
continue;
|
2021-12-24 21:18:55 +13:00
|
|
|
}
|
2023-04-05 18:08:43 +12:00
|
|
|
|
|
|
|
let vector = if self.ignore_hard_links { filter_hard_links(&vec) } else { vec };
|
|
|
|
|
|
|
|
if vector.len() > 1 {
|
|
|
|
self.files_with_identical_size.insert(size, vector);
|
2021-12-24 21:18:55 +13:00
|
|
|
}
|
2022-01-02 09:07:20 +13:00
|
|
|
}
|
2021-12-24 21:18:55 +13:00
|
|
|
|
2023-04-05 18:08:43 +12:00
|
|
|
self.filter_reference_folders_by_size();
|
|
|
|
self.calculate_size_stats();
|
|
|
|
|
2022-01-02 09:07:20 +13:00
|
|
|
true
|
2021-12-24 21:18:55 +13:00
|
|
|
}
|
2022-01-02 09:07:20 +13:00
|
|
|
DirTraversalResult::SuccessFolders { .. } => {
|
|
|
|
unreachable!()
|
2020-09-18 00:01:14 +12:00
|
|
|
}
|
2022-01-02 09:07:20 +13:00
|
|
|
DirTraversalResult::Stopped => false,
|
2020-09-18 00:01:14 +12:00
|
|
|
}
|
2020-08-28 04:57:56 +12:00
|
|
|
}
|
2020-09-25 08:25:24 +12:00
|
|
|
|
2023-04-05 18:08:43 +12:00
|
|
|
fn calculate_size_stats(&mut self) {
|
|
|
|
if self.use_reference_folders {
|
|
|
|
for (size, (_fe, vector)) in &self.files_with_identical_size_referenced {
|
|
|
|
self.information.number_of_duplicated_files_by_size += vector.len();
|
|
|
|
self.information.number_of_groups_by_size += 1;
|
|
|
|
self.information.lost_space_by_size += (vector.len() as u64) * size;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (size, vector) in &self.files_with_identical_size {
|
|
|
|
self.information.number_of_duplicated_files_by_size += vector.len() - 1;
|
|
|
|
self.information.number_of_groups_by_size += 1;
|
|
|
|
self.information.lost_space_by_size += (vector.len() as u64 - 1) * size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-29 06:30:22 +12:00
|
|
|
|
2023-04-05 18:08:43 +12:00
|
|
|
/// This step check for references, only when checking for size.
|
|
|
|
/// This is needed, because later reference folders looks for hashes, not size
|
|
|
|
fn filter_reference_folders_by_size(&mut self) {
|
|
|
|
if self.use_reference_folders && self.check_method == CheckingMethod::Size {
|
|
|
|
let mut btree_map = Default::default();
|
|
|
|
mem::swap(&mut self.files_with_identical_size, &mut btree_map);
|
|
|
|
let reference_directories = self.directories.reference_directories.clone();
|
|
|
|
let vec = btree_map
|
|
|
|
.into_iter()
|
|
|
|
.filter_map(|(_size, vec_file_entry)| {
|
|
|
|
let mut files_from_referenced_folders = Vec::new();
|
|
|
|
let mut normal_files = Vec::new();
|
|
|
|
for file_entry in vec_file_entry {
|
|
|
|
if reference_directories.iter().any(|e| file_entry.path.starts_with(e)) {
|
|
|
|
files_from_referenced_folders.push(file_entry);
|
|
|
|
} else {
|
|
|
|
normal_files.push(file_entry);
|
|
|
|
}
|
|
|
|
}
|
2020-12-02 22:25:27 +13:00
|
|
|
|
2023-04-05 18:08:43 +12:00
|
|
|
if files_from_referenced_folders.is_empty() || normal_files.is_empty() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some((files_from_referenced_folders.pop().unwrap(), normal_files))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect::<Vec<(FileEntry, Vec<FileEntry>)>>();
|
|
|
|
for (fe, vec_fe) in vec {
|
|
|
|
self.files_with_identical_size_referenced.insert(fe.size, (fe, vec_fe));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-02 22:25:27 +13:00
|
|
|
|
2023-05-03 08:37:12 +12:00
|
|
|
fn prehash_load_cache_at_start(&mut self) -> (BTreeMap<u64, Vec<FileEntry>>, BTreeMap<u64, Vec<FileEntry>>, BTreeMap<u64, Vec<FileEntry>>) {
|
2023-04-05 18:08:43 +12:00
|
|
|
// Cache algorithm
|
|
|
|
// - Load data from cache
|
|
|
|
// - Convert from BT<u64,Vec<FileEntry>> to BT<String,FileEntry>
|
|
|
|
// - Save to proper values
|
2023-05-03 08:37:12 +12:00
|
|
|
let loaded_hash_map;
|
|
|
|
let mut records_already_cached: BTreeMap<u64, Vec<FileEntry>> = Default::default();
|
|
|
|
let mut non_cached_files_to_check: BTreeMap<u64, Vec<FileEntry>> = Default::default();
|
|
|
|
|
2023-04-05 18:08:43 +12:00
|
|
|
if self.use_prehash_cache {
|
|
|
|
loaded_hash_map = match load_hashes_from_file(&mut self.text_messages, self.delete_outdated_cache, &self.hash_type, true) {
|
|
|
|
Some(t) => t,
|
|
|
|
None => Default::default(),
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut loaded_hash_map2: BTreeMap<String, FileEntry> = Default::default();
|
|
|
|
for vec_file_entry in loaded_hash_map.values() {
|
|
|
|
for file_entry in vec_file_entry {
|
|
|
|
loaded_hash_map2.insert(file_entry.path.to_string_lossy().to_string(), file_entry.clone());
|
|
|
|
}
|
|
|
|
}
|
2021-12-02 08:09:04 +13:00
|
|
|
|
2023-04-05 18:08:43 +12:00
|
|
|
#[allow(clippy::if_same_then_else)]
|
|
|
|
for vec_file_entry in self.files_with_identical_size.values() {
|
|
|
|
for file_entry in vec_file_entry {
|
|
|
|
let name = file_entry.path.to_string_lossy().to_string();
|
|
|
|
if !loaded_hash_map2.contains_key(&name) {
|
|
|
|
// If loaded data doesn't contains current image info
|
|
|
|
non_cached_files_to_check.entry(file_entry.size).or_insert_with(Vec::new).push(file_entry.clone());
|
|
|
|
} else if file_entry.size != loaded_hash_map2.get(&name).unwrap().size || file_entry.modified_date != loaded_hash_map2.get(&name).unwrap().modified_date {
|
|
|
|
// When size or modification date of image changed, then it is clear that is different image
|
|
|
|
non_cached_files_to_check.entry(file_entry.size).or_insert_with(Vec::new).push(file_entry.clone());
|
|
|
|
} else {
|
|
|
|
// Checking may be omitted when already there is entry with same size and modification date
|
|
|
|
records_already_cached.entry(file_entry.size).or_insert_with(Vec::new).push(file_entry.clone());
|
2020-10-24 17:56:39 +13:00
|
|
|
}
|
2021-12-02 08:09:04 +13:00
|
|
|
}
|
2023-04-05 18:08:43 +12:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
loaded_hash_map = Default::default();
|
|
|
|
mem::swap(&mut self.files_with_identical_size, &mut non_cached_files_to_check);
|
|
|
|
}
|
2023-05-03 08:37:12 +12:00
|
|
|
(loaded_hash_map, records_already_cached, non_cached_files_to_check)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn prehash_save_cache_at_exit(&mut self, loaded_hash_map: BTreeMap<u64, Vec<FileEntry>>, pre_hash_results: &Vec<(u64, BTreeMap<String, Vec<FileEntry>>, Vec<String>)>) {
|
|
|
|
if self.use_prehash_cache {
|
|
|
|
// All results = records already cached + computed results
|
|
|
|
let mut save_cache_to_hashmap: BTreeMap<String, FileEntry> = Default::default();
|
|
|
|
|
|
|
|
for (size, vec_file_entry) in loaded_hash_map {
|
|
|
|
if size >= self.minimal_prehash_cache_file_size {
|
|
|
|
for file_entry in vec_file_entry {
|
|
|
|
save_cache_to_hashmap.insert(file_entry.path.to_string_lossy().to_string(), file_entry.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size, hash_map, _errors) in pre_hash_results {
|
|
|
|
if *size >= self.minimal_prehash_cache_file_size {
|
|
|
|
for vec_file_entry in hash_map.values() {
|
|
|
|
for file_entry in vec_file_entry {
|
|
|
|
save_cache_to_hashmap.insert(file_entry.path.to_string_lossy().to_string(), file_entry.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
save_hashes_to_file(&save_cache_to_hashmap, &mut self.text_messages, &self.hash_type, true, self.minimal_prehash_cache_file_size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn prehashing(
|
|
|
|
&mut self,
|
|
|
|
stop_receiver: Option<&Receiver<()>>,
|
|
|
|
progress_sender: Option<&UnboundedSender<ProgressData>>,
|
|
|
|
pre_checked_map: &mut BTreeMap<u64, Vec<FileEntry>>,
|
|
|
|
) -> Option<()> {
|
|
|
|
let check_type = self.hash_type;
|
|
|
|
let check_was_stopped = AtomicBool::new(false); // Used for breaking from GUI and ending check thread
|
|
|
|
|
|
|
|
let progress_thread_run = Arc::new(AtomicBool::new(true));
|
|
|
|
let atomic_counter = Arc::new(AtomicUsize::new(0));
|
|
|
|
let progress_thread_handle = prepare_thread_handler_common(
|
|
|
|
progress_sender,
|
|
|
|
&progress_thread_run,
|
|
|
|
&atomic_counter,
|
|
|
|
1,
|
|
|
|
2,
|
|
|
|
self.files_with_identical_size.values().map(Vec::len).sum(),
|
|
|
|
self.check_method,
|
|
|
|
);
|
|
|
|
|
|
|
|
let (loaded_hash_map, records_already_cached, non_cached_files_to_check) = self.prehash_load_cache_at_start();
|
2021-12-02 08:09:04 +13:00
|
|
|
|
2023-04-05 18:08:43 +12:00
|
|
|
#[allow(clippy::type_complexity)]
|
|
|
|
let pre_hash_results: Vec<(u64, BTreeMap<String, Vec<FileEntry>>, Vec<String>)> = non_cached_files_to_check
|
|
|
|
.par_iter()
|
|
|
|
.map(|(size, vec_file_entry)| {
|
|
|
|
let mut hashmap_with_hash: BTreeMap<String, Vec<FileEntry>> = Default::default();
|
|
|
|
let mut errors: Vec<String> = Vec::new();
|
|
|
|
let mut buffer = [0u8; 1024 * 2];
|
|
|
|
|
2023-05-03 08:37:12 +12:00
|
|
|
atomic_counter.fetch_add(vec_file_entry.len(), Ordering::Relaxed);
|
2023-05-03 19:37:06 +12:00
|
|
|
if stop_receiver.is_some() && stop_receiver.unwrap().try_recv().is_ok() {
|
|
|
|
check_was_stopped.store(true, Ordering::Relaxed);
|
|
|
|
return None;
|
|
|
|
}
|
2023-04-05 18:08:43 +12:00
|
|
|
for file_entry in vec_file_entry {
|
|
|
|
match hash_calculation(&mut buffer, file_entry, &check_type, 0) {
|
|
|
|
Ok(hash_string) => {
|
|
|
|
hashmap_with_hash.entry(hash_string.clone()).or_insert_with(Vec::new).push(file_entry.clone());
|
2020-11-09 04:19:40 +13:00
|
|
|
}
|
2023-04-05 18:08:43 +12:00
|
|
|
Err(s) => errors.push(s),
|
2021-02-22 22:52:21 +13:00
|
|
|
}
|
2020-10-24 17:56:39 +13:00
|
|
|
}
|
2023-04-05 18:08:43 +12:00
|
|
|
Some((*size, hashmap_with_hash, errors))
|
|
|
|
})
|
|
|
|
.while_some()
|
|
|
|
.collect();
|
2020-11-09 04:19:40 +13:00
|
|
|
|
2023-05-03 08:37:12 +12:00
|
|
|
send_info_and_wait_for_ending_all_threads(&progress_thread_run, progress_thread_handle);
|
2021-12-02 08:09:04 +13:00
|
|
|
|
2023-04-05 18:08:43 +12:00
|
|
|
// Check if user aborted search(only from GUI)
|
|
|
|
if check_was_stopped.load(Ordering::Relaxed) {
|
|
|
|
return None;
|
|
|
|
}
|
2020-12-02 22:25:27 +13:00
|
|
|
|
2023-04-05 18:08:43 +12:00
|
|
|
// Add data from cache
|
|
|
|
for (size, vec_file_entry) in &records_already_cached {
|
|
|
|
pre_checked_map.entry(*size).or_insert_with(Vec::new).append(&mut vec_file_entry.clone());
|
|
|
|
}
|
2020-10-24 17:56:39 +13:00
|
|
|
|
2023-04-05 18:08:43 +12:00
|
|
|
// Check results
|
|
|
|
for (size, hash_map, errors) in &pre_hash_results {
|
|
|
|
self.text_messages.warnings.append(&mut errors.clone());
|
|
|
|
for vec_file_entry in hash_map.values() {
|
|
|
|
if vec_file_entry.len() > 1 {
|
|
|
|
pre_checked_map.entry(*size).or_insert_with(Vec::new).append(&mut vec_file_entry.clone());
|
|
|
|
}
|
2021-12-02 08:09:04 +13:00
|
|
|
}
|
2023-04-05 18:08:43 +12:00
|
|
|
}
|
2021-12-02 08:09:04 +13:00
|
|
|
|
2023-05-03 08:37:12 +12:00
|
|
|
self.prehash_save_cache_at_exit(loaded_hash_map, &pre_hash_results);
|
|
|
|
|
|
|
|
Some(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn full_hashing_load_cache_at_start(
|
|
|
|
&mut self,
|
|
|
|
mut pre_checked_map: BTreeMap<u64, Vec<FileEntry>>,
|
|
|
|
) -> (BTreeMap<u64, Vec<FileEntry>>, BTreeMap<u64, Vec<FileEntry>>, BTreeMap<u64, Vec<FileEntry>>) {
|
|
|
|
let loaded_hash_map;
|
|
|
|
let mut records_already_cached: BTreeMap<u64, Vec<FileEntry>> = Default::default();
|
|
|
|
let mut non_cached_files_to_check: BTreeMap<u64, Vec<FileEntry>> = Default::default();
|
|
|
|
|
|
|
|
if self.use_cache {
|
|
|
|
loaded_hash_map = match load_hashes_from_file(&mut self.text_messages, self.delete_outdated_cache, &self.hash_type, false) {
|
|
|
|
Some(t) => t,
|
|
|
|
None => Default::default(),
|
|
|
|
};
|
|
|
|
|
|
|
|
for (size, vec_file_entry) in pre_checked_map {
|
|
|
|
#[allow(clippy::collapsible_if)]
|
|
|
|
if !loaded_hash_map.contains_key(&size) {
|
|
|
|
// If loaded data doesn't contains current info
|
|
|
|
non_cached_files_to_check.insert(size, vec_file_entry);
|
|
|
|
} else {
|
|
|
|
let loaded_vec_file_entry = loaded_hash_map.get(&size).unwrap();
|
2021-12-02 08:09:04 +13:00
|
|
|
|
2023-04-05 18:08:43 +12:00
|
|
|
for file_entry in vec_file_entry {
|
2023-05-03 08:37:12 +12:00
|
|
|
let mut found: bool = false;
|
|
|
|
for loaded_file_entry in loaded_vec_file_entry {
|
|
|
|
if file_entry.path == loaded_file_entry.path && file_entry.modified_date == loaded_file_entry.modified_date {
|
|
|
|
records_already_cached.entry(file_entry.size).or_insert_with(Vec::new).push(loaded_file_entry.clone());
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
non_cached_files_to_check.entry(file_entry.size).or_insert_with(Vec::new).push(file_entry);
|
|
|
|
}
|
2021-12-02 08:09:04 +13:00
|
|
|
}
|
2020-10-10 22:15:20 +13:00
|
|
|
}
|
2020-11-09 04:19:40 +13:00
|
|
|
}
|
2023-05-03 08:37:12 +12:00
|
|
|
} else {
|
|
|
|
loaded_hash_map = Default::default();
|
|
|
|
mem::swap(&mut pre_checked_map, &mut non_cached_files_to_check);
|
|
|
|
}
|
|
|
|
(loaded_hash_map, records_already_cached, non_cached_files_to_check)
|
|
|
|
}
|
2021-12-02 08:09:04 +13:00
|
|
|
|
2023-05-03 08:37:12 +12:00
|
|
|
fn full_hashing_save_cache_at_exit(
|
|
|
|
&mut self,
|
|
|
|
records_already_cached: BTreeMap<u64, Vec<FileEntry>>,
|
|
|
|
full_hash_results: &mut Vec<(u64, BTreeMap<String, Vec<FileEntry>>, Vec<String>)>,
|
|
|
|
loaded_hash_map: BTreeMap<u64, Vec<FileEntry>>,
|
|
|
|
) {
|
|
|
|
if !self.use_cache {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
'main: for (size, vec_file_entry) in records_already_cached {
|
|
|
|
// Check if size already exists, if exists we must to change it outside because cannot have mut and non mut reference to full_hash_results
|
|
|
|
for (full_size, full_hashmap, _errors) in &mut (*full_hash_results) {
|
|
|
|
if size == *full_size {
|
|
|
|
for file_entry in vec_file_entry {
|
|
|
|
full_hashmap.entry(file_entry.hash.clone()).or_insert_with(Vec::new).push(file_entry);
|
2021-12-02 08:09:04 +13:00
|
|
|
}
|
2023-05-03 08:37:12 +12:00
|
|
|
continue 'main;
|
2021-12-02 08:09:04 +13:00
|
|
|
}
|
|
|
|
}
|
2023-05-03 08:37:12 +12:00
|
|
|
// Size doesn't exists add results to files
|
|
|
|
let mut temp_hashmap: BTreeMap<String, Vec<FileEntry>> = Default::default();
|
|
|
|
for file_entry in vec_file_entry {
|
|
|
|
temp_hashmap.entry(file_entry.hash.clone()).or_insert_with(Vec::new).push(file_entry);
|
|
|
|
}
|
|
|
|
full_hash_results.push((size, temp_hashmap, Vec::new()));
|
2023-04-05 18:08:43 +12:00
|
|
|
}
|
2021-12-02 08:09:04 +13:00
|
|
|
|
2023-05-03 08:37:12 +12:00
|
|
|
// Must save all results to file, old loaded from file with all currently counted results
|
|
|
|
let mut all_results: BTreeMap<String, FileEntry> = Default::default();
|
|
|
|
for (_size, vec_file_entry) in loaded_hash_map {
|
|
|
|
for file_entry in vec_file_entry {
|
|
|
|
all_results.insert(file_entry.path.to_string_lossy().to_string(), file_entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (_size, hashmap, _errors) in full_hash_results {
|
|
|
|
for vec_file_entry in hashmap.values() {
|
|
|
|
for file_entry in vec_file_entry {
|
|
|
|
all_results.insert(file_entry.path.to_string_lossy().to_string(), file_entry.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
save_hashes_to_file(&all_results, &mut self.text_messages, &self.hash_type, false, self.minimal_cache_file_size);
|
2023-04-05 18:08:43 +12:00
|
|
|
}
|
2020-11-09 04:19:40 +13:00
|
|
|
|
2023-04-05 18:08:43 +12:00
|
|
|
fn full_hashing(
|
|
|
|
&mut self,
|
|
|
|
stop_receiver: Option<&Receiver<()>>,
|
2023-05-03 08:37:12 +12:00
|
|
|
progress_sender: Option<&UnboundedSender<ProgressData>>,
|
|
|
|
pre_checked_map: BTreeMap<u64, Vec<FileEntry>>,
|
2023-04-05 18:08:43 +12:00
|
|
|
) -> Option<()> {
|
|
|
|
let check_was_stopped = AtomicBool::new(false); // Used for breaking from GUI and ending check thread
|
|
|
|
|
|
|
|
let check_type = self.hash_type;
|
2023-05-03 08:37:12 +12:00
|
|
|
|
2020-12-02 22:25:27 +13:00
|
|
|
let progress_thread_run = Arc::new(AtomicBool::new(true));
|
2023-05-03 08:37:12 +12:00
|
|
|
let atomic_counter = Arc::new(AtomicUsize::new(0));
|
2020-12-02 22:25:27 +13:00
|
|
|
|
2023-05-03 08:37:12 +12:00
|
|
|
let progress_thread_handle = prepare_thread_handler_common(
|
2023-04-05 18:08:43 +12:00
|
|
|
progress_sender,
|
2023-05-03 08:37:12 +12:00
|
|
|
&progress_thread_run,
|
|
|
|
&atomic_counter,
|
2023-04-05 18:08:43 +12:00
|
|
|
2,
|
|
|
|
2,
|
|
|
|
pre_checked_map.values().map(Vec::len).sum(),
|
2023-05-03 08:37:12 +12:00
|
|
|
self.check_method,
|
2023-04-05 18:08:43 +12:00
|
|
|
);
|
2020-12-02 22:25:27 +13:00
|
|
|
|
2021-12-02 08:09:04 +13:00
|
|
|
///////////////////////////////////////////////////////////////////////////// HASHING START
|
|
|
|
{
|
2023-05-03 08:37:12 +12:00
|
|
|
let (loaded_hash_map, records_already_cached, non_cached_files_to_check) = self.full_hashing_load_cache_at_start(pre_checked_map);
|
2021-01-15 00:17:15 +13:00
|
|
|
|
2023-05-03 08:37:12 +12:00
|
|
|
let mut full_hash_results: Vec<(u64, BTreeMap<String, Vec<FileEntry>>, Vec<String>)> = non_cached_files_to_check
|
2021-12-30 01:43:38 +13:00
|
|
|
.into_par_iter()
|
2021-12-02 08:09:04 +13:00
|
|
|
.map(|(size, vec_file_entry)| {
|
|
|
|
let mut hashmap_with_hash: BTreeMap<String, Vec<FileEntry>> = Default::default();
|
|
|
|
let mut errors: Vec<String> = Vec::new();
|
2022-01-07 08:28:22 +13:00
|
|
|
let mut buffer = [0u8; 1024 * 16];
|
2021-12-02 08:09:04 +13:00
|
|
|
|
2023-05-03 08:37:12 +12:00
|
|
|
atomic_counter.fetch_add(vec_file_entry.len(), Ordering::Relaxed);
|
2021-12-30 01:43:38 +13:00
|
|
|
for mut file_entry in vec_file_entry {
|
2021-12-02 08:09:04 +13:00
|
|
|
if stop_receiver.is_some() && stop_receiver.unwrap().try_recv().is_ok() {
|
2022-05-10 05:40:35 +12:00
|
|
|
check_was_stopped.store(true, Ordering::Relaxed);
|
2021-12-02 08:09:04 +13:00
|
|
|
return None;
|
|
|
|
}
|
2021-01-15 00:17:15 +13:00
|
|
|
|
2021-12-30 01:43:38 +13:00
|
|
|
match hash_calculation(&mut buffer, &file_entry, &check_type, u64::MAX) {
|
2021-12-02 08:09:04 +13:00
|
|
|
Ok(hash_string) => {
|
|
|
|
file_entry.hash = hash_string.clone();
|
2022-06-05 18:01:17 +12:00
|
|
|
hashmap_with_hash.entry(hash_string.clone()).or_insert_with(Vec::new).push(file_entry);
|
2021-02-22 22:52:21 +13:00
|
|
|
}
|
2021-12-02 08:09:04 +13:00
|
|
|
Err(s) => errors.push(s),
|
2020-11-09 04:19:40 +13:00
|
|
|
}
|
2021-12-02 08:09:04 +13:00
|
|
|
}
|
2021-12-30 01:43:38 +13:00
|
|
|
Some((size, hashmap_with_hash, errors))
|
2021-12-02 08:09:04 +13:00
|
|
|
})
|
|
|
|
.while_some()
|
|
|
|
.collect();
|
|
|
|
|
2023-05-03 08:37:12 +12:00
|
|
|
self.full_hashing_save_cache_at_exit(records_already_cached, &mut full_hash_results, loaded_hash_map);
|
2020-11-09 04:19:40 +13:00
|
|
|
|
2023-05-03 08:37:12 +12:00
|
|
|
send_info_and_wait_for_ending_all_threads(&progress_thread_run, progress_thread_handle);
|
2020-12-02 22:25:27 +13:00
|
|
|
|
2022-05-10 05:40:35 +12:00
|
|
|
// Break if stop was clicked after saving to cache
|
|
|
|
if check_was_stopped.load(Ordering::Relaxed) {
|
2023-04-05 18:08:43 +12:00
|
|
|
return None;
|
2021-12-02 08:09:04 +13:00
|
|
|
}
|
2020-11-09 04:19:40 +13:00
|
|
|
|
2021-12-02 08:09:04 +13:00
|
|
|
for (size, hash_map, mut errors) in full_hash_results {
|
|
|
|
self.text_messages.warnings.append(&mut errors);
|
|
|
|
for (_hash, vec_file_entry) in hash_map {
|
|
|
|
if vec_file_entry.len() > 1 {
|
2022-06-05 18:01:17 +12:00
|
|
|
self.files_with_identical_hashes.entry(size).or_insert_with(Vec::new).push(vec_file_entry);
|
2021-12-02 08:09:04 +13:00
|
|
|
}
|
2020-08-30 02:12:20 +12:00
|
|
|
}
|
2020-08-29 06:30:22 +12:00
|
|
|
}
|
2021-12-24 21:18:55 +13:00
|
|
|
}
|
2023-05-03 22:44:28 +12:00
|
|
|
|
2023-04-05 18:08:43 +12:00
|
|
|
Some(())
|
|
|
|
}
|
2021-12-24 21:18:55 +13:00
|
|
|
|
2023-04-05 18:08:43 +12:00
|
|
|
fn hash_reference_folders(&mut self) {
|
2021-12-24 21:18:55 +13:00
|
|
|
// Reference - only use in size, because later hash will be counted differently
|
|
|
|
if self.use_reference_folders {
|
|
|
|
let mut btree_map = Default::default();
|
|
|
|
mem::swap(&mut self.files_with_identical_hashes, &mut btree_map);
|
|
|
|
let reference_directories = self.directories.reference_directories.clone();
|
|
|
|
let vec = btree_map
|
|
|
|
.into_iter()
|
|
|
|
.filter_map(|(_size, vec_vec_file_entry)| {
|
|
|
|
let mut all_results_with_same_size = Vec::new();
|
|
|
|
for vec_file_entry in vec_vec_file_entry {
|
|
|
|
let mut files_from_referenced_folders = Vec::new();
|
|
|
|
let mut normal_files = Vec::new();
|
|
|
|
for file_entry in vec_file_entry {
|
2022-11-24 08:23:17 +13:00
|
|
|
if reference_directories.iter().any(|e| file_entry.path.starts_with(e)) {
|
2021-12-24 21:18:55 +13:00
|
|
|
files_from_referenced_folders.push(file_entry);
|
|
|
|
} else {
|
|
|
|
normal_files.push(file_entry);
|
|
|
|
}
|
|
|
|
}
|
2020-09-16 05:17:13 +12:00
|
|
|
|
2021-12-24 21:18:55 +13:00
|
|
|
if files_from_referenced_folders.is_empty() || normal_files.is_empty() {
|
|
|
|
continue;
|
|
|
|
}
|
2023-01-29 06:54:02 +13:00
|
|
|
all_results_with_same_size.push((files_from_referenced_folders.pop().unwrap(), normal_files));
|
2021-12-24 21:18:55 +13:00
|
|
|
}
|
|
|
|
if all_results_with_same_size.is_empty() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(all_results_with_same_size)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect::<Vec<Vec<(FileEntry, Vec<FileEntry>)>>>();
|
|
|
|
for vec_of_vec in vec {
|
|
|
|
self.files_with_identical_hashes_referenced.insert(vec_of_vec[0].0.size, vec_of_vec);
|
|
|
|
}
|
|
|
|
}
|
2020-11-09 04:19:40 +13:00
|
|
|
|
2021-12-24 21:18:55 +13:00
|
|
|
if self.use_reference_folders {
|
|
|
|
for (size, vector_vectors) in &self.files_with_identical_hashes_referenced {
|
|
|
|
for (_fe, vector) in vector_vectors {
|
|
|
|
self.information.number_of_duplicated_files_by_hash += vector.len();
|
|
|
|
self.information.number_of_groups_by_hash += 1;
|
|
|
|
self.information.lost_space_by_hash += (vector.len() as u64) * size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2021-12-02 08:09:04 +13:00
|
|
|
for (size, vector_vectors) in &self.files_with_identical_hashes {
|
|
|
|
for vector in vector_vectors {
|
|
|
|
self.information.number_of_duplicated_files_by_hash += vector.len() - 1;
|
|
|
|
self.information.number_of_groups_by_hash += 1;
|
|
|
|
self.information.lost_space_by_hash += (vector.len() as u64 - 1) * size;
|
|
|
|
}
|
2020-09-16 05:17:13 +12:00
|
|
|
}
|
|
|
|
}
|
2023-04-05 18:08:43 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
/// The slowest checking type, which must be applied after checking for size
|
2023-05-03 08:37:12 +12:00
|
|
|
fn check_files_hash(&mut self, stop_receiver: Option<&Receiver<()>>, progress_sender: Option<&UnboundedSender<ProgressData>>) -> bool {
|
2023-04-05 18:08:43 +12:00
|
|
|
assert_eq!(self.check_method, CheckingMethod::Hash);
|
|
|
|
|
|
|
|
let mut pre_checked_map: BTreeMap<u64, Vec<FileEntry>> = Default::default();
|
|
|
|
let ret = self.prehashing(stop_receiver, progress_sender, &mut pre_checked_map);
|
|
|
|
if ret.is_none() {
|
|
|
|
return false;
|
|
|
|
}
|
2020-09-16 05:17:13 +12:00
|
|
|
|
2023-04-05 18:08:43 +12:00
|
|
|
let ret = self.full_hashing(stop_receiver, progress_sender, pre_checked_map);
|
|
|
|
if ret.is_none() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.hash_reference_folders();
|
2021-01-16 00:41:45 +13:00
|
|
|
|
|
|
|
// Clean unused data
|
|
|
|
self.files_with_identical_size = Default::default();
|
|
|
|
|
2020-10-10 22:15:20 +13:00
|
|
|
true
|
2020-08-29 06:30:22 +12:00
|
|
|
}
|
|
|
|
|
2023-01-29 06:54:02 +13:00
|
|
|
/// Function to delete files, from filed before `BTreeMap`
|
2020-09-27 03:52:13 +13:00
|
|
|
/// Using another function to delete files to avoid duplicates data
|
|
|
|
fn delete_files(&mut self) {
|
2020-10-25 07:44:21 +13:00
|
|
|
if self.delete_method == DeleteMethod::None {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-27 03:52:13 +13:00
|
|
|
match self.check_method {
|
2020-10-25 07:44:21 +13:00
|
|
|
CheckingMethod::Name => {
|
|
|
|
for vector in self.files_with_identical_names.values() {
|
2021-12-24 21:18:55 +13:00
|
|
|
let _tuple: (u64, usize, usize) = delete_files(vector, &self.delete_method, &mut self.text_messages, self.dryrun);
|
2020-10-25 07:44:21 +13:00
|
|
|
}
|
|
|
|
}
|
2023-04-05 18:08:43 +12:00
|
|
|
CheckingMethod::SizeName => {
|
|
|
|
for vector in self.files_with_identical_size_names.values() {
|
|
|
|
let _tuple: (u64, usize, usize) = delete_files(vector, &self.delete_method, &mut self.text_messages, self.dryrun);
|
|
|
|
}
|
|
|
|
}
|
2021-12-02 00:37:17 +13:00
|
|
|
CheckingMethod::Hash => {
|
2020-09-27 07:46:35 +13:00
|
|
|
for vector_vectors in self.files_with_identical_hashes.values() {
|
2020-09-27 06:51:28 +13:00
|
|
|
for vector in vector_vectors.iter() {
|
2021-12-24 21:18:55 +13:00
|
|
|
let _tuple: (u64, usize, usize) = delete_files(vector, &self.delete_method, &mut self.text_messages, self.dryrun);
|
2020-09-27 03:52:13 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CheckingMethod::Size => {
|
2020-09-27 07:46:35 +13:00
|
|
|
for vector in self.files_with_identical_size.values() {
|
2021-12-24 21:18:55 +13:00
|
|
|
let _tuple: (u64, usize, usize) = delete_files(vector, &self.delete_method, &mut self.text_messages, self.dryrun);
|
2020-09-27 03:52:13 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
CheckingMethod::None => {
|
|
|
|
//Just do nothing
|
|
|
|
panic!("Checking method should never be none.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-28 08:57:10 +13:00
|
|
|
|
2020-09-27 03:52:13 +13:00
|
|
|
impl Default for DuplicateFinder {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DebugPrint for DuplicateFinder {
|
2020-09-10 22:42:44 +12:00
|
|
|
#[allow(dead_code)]
|
2020-09-17 22:07:58 +12:00
|
|
|
#[allow(unreachable_code)]
|
2020-09-25 08:25:24 +12:00
|
|
|
/// Debugging printing - only available on debug build
|
2020-08-30 02:12:20 +12:00
|
|
|
fn debug_print(&self) {
|
2020-09-17 22:07:58 +12:00
|
|
|
#[cfg(not(debug_assertions))]
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2020-09-08 19:41:22 +12:00
|
|
|
println!("---------------DEBUG PRINT---------------");
|
2020-09-18 17:32:37 +12:00
|
|
|
println!("### Information's");
|
|
|
|
|
2020-09-18 20:23:49 +12:00
|
|
|
println!("Errors size - {}", self.text_messages.errors.len());
|
|
|
|
println!("Warnings size - {}", self.text_messages.warnings.len());
|
|
|
|
println!("Messages size - {}", self.text_messages.messages.len());
|
|
|
|
println!(
|
|
|
|
"Number of duplicated files by size(in groups) - {} ({})",
|
|
|
|
self.information.number_of_duplicated_files_by_size, self.information.number_of_groups_by_size
|
|
|
|
);
|
|
|
|
println!(
|
|
|
|
"Number of duplicated files by hash(in groups) - {} ({})",
|
|
|
|
self.information.number_of_duplicated_files_by_hash, self.information.number_of_groups_by_hash
|
|
|
|
);
|
2020-10-25 07:44:21 +13:00
|
|
|
println!(
|
|
|
|
"Number of duplicated files by name(in groups) - {} ({})",
|
|
|
|
self.information.number_of_duplicated_files_by_name, self.information.number_of_groups_by_name
|
|
|
|
);
|
2021-12-22 06:44:20 +13:00
|
|
|
println!(
|
|
|
|
"Lost space by size - {} ({} bytes)",
|
2022-09-14 08:18:07 +12:00
|
|
|
format_size(self.information.lost_space_by_size, BINARY),
|
2021-12-22 06:44:20 +13:00
|
|
|
self.information.lost_space_by_size
|
|
|
|
);
|
|
|
|
println!(
|
|
|
|
"Lost space by hash - {} ({} bytes)",
|
2022-09-14 08:18:07 +12:00
|
|
|
format_size(self.information.lost_space_by_hash, BINARY),
|
2021-12-22 06:44:20 +13:00
|
|
|
self.information.lost_space_by_hash
|
|
|
|
);
|
2020-09-16 05:17:13 +12:00
|
|
|
|
|
|
|
println!("### Other");
|
|
|
|
|
|
|
|
println!("Files list size - {}", self.files_with_identical_size.len());
|
|
|
|
println!("Hashed Files list size - {}", self.files_with_identical_hashes.len());
|
2020-09-27 03:52:13 +13:00
|
|
|
println!("Excluded items - {:?}", self.excluded_items.items);
|
|
|
|
println!("Included directories - {:?}", self.directories.included_directories);
|
|
|
|
println!("Excluded directories - {:?}", self.directories.excluded_directories);
|
2021-11-23 23:10:24 +13:00
|
|
|
println!("Recursive search - {}", self.recursive_search);
|
2022-04-24 06:21:46 +12:00
|
|
|
#[cfg(target_family = "unix")]
|
2022-06-01 03:52:55 +12:00
|
|
|
println!("Skip other filesystems - {}", self.directories.exclude_other_filesystems());
|
2020-10-03 20:51:02 +13:00
|
|
|
println!("Minimum file size - {:?}", self.minimal_file_size);
|
2020-09-26 23:48:53 +12:00
|
|
|
println!("Checking Method - {:?}", self.check_method);
|
|
|
|
println!("Delete Method - {:?}", self.delete_method);
|
2020-09-08 19:41:22 +12:00
|
|
|
println!("-----------------------------------------");
|
2020-08-27 06:50:07 +12:00
|
|
|
}
|
2020-09-27 03:52:13 +13:00
|
|
|
}
|
2021-11-28 08:57:10 +13:00
|
|
|
|
2020-09-27 03:52:13 +13:00
|
|
|
impl SaveResults for DuplicateFinder {
|
|
|
|
fn save_results_to_file(&mut self, file_name: &str) -> bool {
|
|
|
|
let file_name: String = match file_name {
|
|
|
|
"" => "results.txt".to_string(),
|
|
|
|
k => k.to_string(),
|
|
|
|
};
|
|
|
|
|
2021-01-09 23:52:43 +13:00
|
|
|
let file_handler = match File::create(&file_name) {
|
2020-09-27 03:52:13 +13:00
|
|
|
Ok(t) => t,
|
2021-11-15 03:53:55 +13:00
|
|
|
Err(e) => {
|
2022-12-21 20:44:26 +13:00
|
|
|
self.text_messages.errors.push(format!("Failed to create file {file_name}, reason {e}"));
|
2020-09-27 03:52:13 +13:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
2021-01-09 23:52:43 +13:00
|
|
|
let mut writer = BufWriter::new(file_handler);
|
2020-09-27 03:52:13 +13:00
|
|
|
|
2021-11-15 03:53:55 +13:00
|
|
|
if let Err(e) = writeln!(
|
2021-01-09 23:52:43 +13:00
|
|
|
writer,
|
2020-10-15 05:41:37 +13:00
|
|
|
"Results of searching {:?} with excluded directories {:?} and excluded items {:?}",
|
|
|
|
self.directories.included_directories, self.directories.excluded_directories, self.excluded_items.items
|
2021-11-15 03:53:55 +13:00
|
|
|
) {
|
2022-12-21 20:44:26 +13:00
|
|
|
self.text_messages.errors.push(format!("Failed to save results to file {file_name}, reason {e}"));
|
2020-10-15 05:41:37 +13:00
|
|
|
return false;
|
2020-09-27 03:52:13 +13:00
|
|
|
}
|
2020-10-25 07:44:21 +13:00
|
|
|
match self.check_method {
|
|
|
|
CheckingMethod::Name => {
|
2021-04-01 22:37:16 +13:00
|
|
|
if !self.files_with_identical_names.is_empty() {
|
2021-12-22 06:44:20 +13:00
|
|
|
writeln!(
|
|
|
|
writer,
|
|
|
|
"-------------------------------------------------Files with same names-------------------------------------------------"
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-10-25 07:44:21 +13:00
|
|
|
writeln!(
|
2021-01-09 23:52:43 +13:00
|
|
|
writer,
|
2020-10-25 07:44:21 +13:00
|
|
|
"Found {} files in {} groups with same name(may have different content)",
|
|
|
|
self.information.number_of_duplicated_files_by_name, self.information.number_of_groups_by_name,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
for (name, vector) in self.files_with_identical_names.iter().rev() {
|
2021-01-09 23:52:43 +13:00
|
|
|
writeln!(writer, "Name - {} - {} files ", name, vector.len()).unwrap();
|
2020-10-25 07:44:21 +13:00
|
|
|
for j in vector {
|
2021-01-09 23:52:43 +13:00
|
|
|
writeln!(writer, "{}", j.path.display()).unwrap();
|
2020-10-25 07:44:21 +13:00
|
|
|
}
|
2021-01-09 23:52:43 +13:00
|
|
|
writeln!(writer).unwrap();
|
2020-10-25 07:44:21 +13:00
|
|
|
}
|
|
|
|
} else {
|
2021-01-09 23:52:43 +13:00
|
|
|
write!(writer, "Not found any files with same names.").unwrap();
|
2020-09-27 03:52:13 +13:00
|
|
|
}
|
|
|
|
}
|
2023-04-05 18:08:43 +12:00
|
|
|
CheckingMethod::SizeName => {
|
|
|
|
if !self.files_with_identical_names.is_empty() {
|
|
|
|
writeln!(
|
|
|
|
writer,
|
|
|
|
"-------------------------------------------------Files with same size and names-------------------------------------------------"
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
writeln!(
|
|
|
|
writer,
|
|
|
|
"Found {} files in {} groups with same size and name(may have different content)",
|
|
|
|
self.information.number_of_duplicated_files_by_size_name, self.information.number_of_groups_by_size_name,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
for ((size, name), vector) in self.files_with_identical_size_names.iter().rev() {
|
|
|
|
writeln!(writer, "Name - {}, {} - {} files ", name, format_size(*size, BINARY), vector.len()).unwrap();
|
|
|
|
for j in vector {
|
|
|
|
writeln!(writer, "{}", j.path.display()).unwrap();
|
|
|
|
}
|
|
|
|
writeln!(writer).unwrap();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
write!(writer, "Not found any files with same size and names.").unwrap();
|
|
|
|
}
|
|
|
|
}
|
2020-10-25 07:44:21 +13:00
|
|
|
CheckingMethod::Size => {
|
|
|
|
if !self.files_with_identical_size.is_empty() {
|
2021-12-22 06:44:20 +13:00
|
|
|
writeln!(
|
|
|
|
writer,
|
|
|
|
"-------------------------------------------------Files with same size-------------------------------------------------"
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-10-25 07:44:21 +13:00
|
|
|
writeln!(
|
2021-01-09 23:52:43 +13:00
|
|
|
writer,
|
2020-10-25 07:44:21 +13:00
|
|
|
"Found {} duplicated files which in {} groups which takes {}.",
|
|
|
|
self.information.number_of_duplicated_files_by_size,
|
|
|
|
self.information.number_of_groups_by_size,
|
2022-09-14 08:18:07 +12:00
|
|
|
format_size(self.information.lost_space_by_size, BINARY)
|
2020-10-25 07:44:21 +13:00
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
for (size, vector) in self.files_with_identical_size.iter().rev() {
|
2022-09-14 08:18:07 +12:00
|
|
|
write!(writer, "\n---- Size {} ({}) - {} files \n", format_size(*size, BINARY), size, vector.len()).unwrap();
|
2020-09-27 03:52:13 +13:00
|
|
|
for file_entry in vector {
|
2021-01-09 23:52:43 +13:00
|
|
|
writeln!(writer, "{}", file_entry.path.display()).unwrap();
|
2020-09-27 03:52:13 +13:00
|
|
|
}
|
|
|
|
}
|
2020-10-25 07:44:21 +13:00
|
|
|
} else {
|
2021-01-09 23:52:43 +13:00
|
|
|
write!(writer, "Not found any duplicates.").unwrap();
|
2020-10-25 07:44:21 +13:00
|
|
|
}
|
|
|
|
}
|
2021-12-02 00:37:17 +13:00
|
|
|
CheckingMethod::Hash => {
|
2020-10-25 07:44:21 +13:00
|
|
|
if !self.files_with_identical_hashes.is_empty() {
|
2021-12-22 06:44:20 +13:00
|
|
|
writeln!(
|
|
|
|
writer,
|
|
|
|
"-------------------------------------------------Files with same hashes-------------------------------------------------"
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-10-25 07:44:21 +13:00
|
|
|
writeln!(
|
2021-01-09 23:52:43 +13:00
|
|
|
writer,
|
2020-10-25 07:44:21 +13:00
|
|
|
"Found {} duplicated files which in {} groups which takes {}.",
|
|
|
|
self.information.number_of_duplicated_files_by_hash,
|
|
|
|
self.information.number_of_groups_by_hash,
|
2022-09-14 08:18:07 +12:00
|
|
|
format_size(self.information.lost_space_by_hash, BINARY)
|
2020-10-25 07:44:21 +13:00
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
for (size, vectors_vector) in self.files_with_identical_hashes.iter().rev() {
|
|
|
|
for vector in vectors_vector {
|
2022-09-14 08:18:07 +12:00
|
|
|
writeln!(writer, "\n---- Size {} ({}) - {} files", format_size(*size, BINARY), size, vector.len()).unwrap();
|
2020-10-25 07:44:21 +13:00
|
|
|
for file_entry in vector {
|
2021-01-09 23:52:43 +13:00
|
|
|
writeln!(writer, "{}", file_entry.path.display()).unwrap();
|
2020-10-25 07:44:21 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2021-01-09 23:52:43 +13:00
|
|
|
write!(writer, "Not found any duplicates.").unwrap();
|
2020-09-27 03:52:13 +13:00
|
|
|
}
|
|
|
|
}
|
2020-10-25 07:44:21 +13:00
|
|
|
CheckingMethod::None => {
|
|
|
|
panic!();
|
|
|
|
}
|
2020-09-27 03:52:13 +13:00
|
|
|
}
|
2023-05-03 22:44:28 +12:00
|
|
|
|
2020-09-27 03:52:13 +13:00
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
2021-11-28 08:57:10 +13:00
|
|
|
|
2020-09-27 03:52:13 +13:00
|
|
|
impl PrintResults for DuplicateFinder {
|
2020-09-18 17:32:37 +12:00
|
|
|
/// Print information's about duplicated entries
|
2020-09-25 08:25:24 +12:00
|
|
|
/// Only needed for CLI
|
2020-09-27 03:52:13 +13:00
|
|
|
fn print_results(&self) {
|
2020-08-30 02:12:20 +12:00
|
|
|
let mut number_of_files: u64 = 0;
|
|
|
|
let mut number_of_groups: u64 = 0;
|
|
|
|
|
2020-09-17 23:35:11 +12:00
|
|
|
match self.check_method {
|
2020-10-25 07:44:21 +13:00
|
|
|
CheckingMethod::Name => {
|
|
|
|
for i in &self.files_with_identical_names {
|
|
|
|
number_of_files += i.1.len() as u64;
|
|
|
|
number_of_groups += 1;
|
|
|
|
}
|
2022-12-21 20:44:26 +13:00
|
|
|
println!("Found {number_of_files} files in {number_of_groups} groups with same name(may have different content)",);
|
2020-10-25 07:44:21 +13:00
|
|
|
for (name, vector) in &self.files_with_identical_names {
|
|
|
|
println!("Name - {} - {} files ", name, vector.len());
|
|
|
|
for j in vector {
|
|
|
|
println!("{}", j.path.display());
|
|
|
|
}
|
|
|
|
println!();
|
|
|
|
}
|
|
|
|
}
|
2023-04-05 18:08:43 +12:00
|
|
|
CheckingMethod::SizeName => {
|
|
|
|
for i in &self.files_with_identical_size_names {
|
|
|
|
number_of_files += i.1.len() as u64;
|
|
|
|
number_of_groups += 1;
|
|
|
|
}
|
|
|
|
println!("Found {number_of_files} files in {number_of_groups} groups with same size and name(may have different content)",);
|
|
|
|
for ((size, name), vector) in &self.files_with_identical_size_names {
|
|
|
|
println!("Name - {}, {} - {} files ", name, format_size(*size, BINARY), vector.len());
|
|
|
|
for j in vector {
|
|
|
|
println!("{}", j.path.display());
|
|
|
|
}
|
|
|
|
println!();
|
|
|
|
}
|
|
|
|
}
|
2021-12-02 00:37:17 +13:00
|
|
|
CheckingMethod::Hash => {
|
2023-01-29 06:54:02 +13:00
|
|
|
for vector in self.files_with_identical_hashes.values() {
|
2020-09-10 22:42:44 +12:00
|
|
|
for j in vector {
|
2020-08-30 02:12:20 +12:00
|
|
|
number_of_files += j.len() as u64;
|
|
|
|
number_of_groups += 1;
|
|
|
|
}
|
|
|
|
}
|
2020-08-31 00:00:22 +12:00
|
|
|
println!(
|
2020-09-02 03:10:54 +12:00
|
|
|
"Found {} duplicated files in {} groups with same content which took {}:",
|
2020-08-31 00:00:22 +12:00
|
|
|
number_of_files,
|
|
|
|
number_of_groups,
|
2022-09-14 08:18:07 +12:00
|
|
|
format_size(self.information.lost_space_by_size, BINARY)
|
2020-08-31 00:00:22 +12:00
|
|
|
);
|
2020-09-17 23:35:11 +12:00
|
|
|
for (size, vector) in self.files_with_identical_hashes.iter().rev() {
|
2020-09-10 22:42:44 +12:00
|
|
|
for j in vector {
|
2022-09-14 08:18:07 +12:00
|
|
|
println!("Size - {} ({}) - {} files ", format_size(*size, BINARY), size, j.len());
|
2020-08-30 02:12:20 +12:00
|
|
|
for k in j {
|
2020-10-15 05:41:37 +13:00
|
|
|
println!("{}", k.path.display());
|
2020-08-30 02:12:20 +12:00
|
|
|
}
|
|
|
|
println!("----");
|
|
|
|
}
|
|
|
|
println!();
|
|
|
|
}
|
2020-08-28 04:57:56 +12:00
|
|
|
}
|
2020-09-25 08:25:24 +12:00
|
|
|
CheckingMethod::Size => {
|
2020-08-30 02:12:20 +12:00
|
|
|
for i in &self.files_with_identical_size {
|
|
|
|
number_of_files += i.1.len() as u64;
|
|
|
|
number_of_groups += 1;
|
|
|
|
}
|
2020-08-31 00:00:22 +12:00
|
|
|
println!(
|
|
|
|
"Found {} files in {} groups with same size(may have different content) which took {}:",
|
|
|
|
number_of_files,
|
|
|
|
number_of_groups,
|
2022-09-14 08:18:07 +12:00
|
|
|
format_size(self.information.lost_space_by_size, BINARY)
|
2020-08-31 00:00:22 +12:00
|
|
|
);
|
2020-09-17 23:35:11 +12:00
|
|
|
for (size, vector) in &self.files_with_identical_size {
|
2022-09-14 08:18:07 +12:00
|
|
|
println!("Size - {} ({}) - {} files ", format_size(*size, BINARY), size, vector.len());
|
2020-09-17 23:35:11 +12:00
|
|
|
for j in vector {
|
2020-10-15 05:41:37 +13:00
|
|
|
println!("{}", j.path.display());
|
2020-08-31 03:18:04 +12:00
|
|
|
}
|
|
|
|
println!();
|
|
|
|
}
|
2020-08-27 19:42:37 +12:00
|
|
|
}
|
2020-09-25 08:25:24 +12:00
|
|
|
CheckingMethod::None => {
|
|
|
|
panic!("Checking Method shouldn't be ever set to None");
|
2020-09-17 22:07:58 +12:00
|
|
|
}
|
2020-08-27 06:50:07 +12:00
|
|
|
}
|
2020-08-27 08:24:02 +12:00
|
|
|
}
|
2020-09-07 19:06:12 +12:00
|
|
|
}
|
|
|
|
|
2020-09-25 08:25:24 +12:00
|
|
|
/// Functions to remove slice(vector) of files with provided method
|
|
|
|
/// Returns size of removed elements, number of deleted and failed to delete files and modified warning list
|
2021-03-02 00:23:43 +13:00
|
|
|
fn delete_files(vector: &[FileEntry], delete_method: &DeleteMethod, text_messages: &mut Messages, dryrun: bool) -> (u64, usize, usize) {
|
2020-09-01 02:15:56 +12:00
|
|
|
assert!(vector.len() > 1, "Vector length must be bigger than 1(This should be done in previous steps).");
|
2020-09-12 01:52:06 +12:00
|
|
|
let mut gained_space: u64 = 0;
|
|
|
|
let mut removed_files: usize = 0;
|
|
|
|
let mut failed_to_remove_files: usize = 0;
|
2021-02-27 21:00:59 +13:00
|
|
|
let mut values = vector.iter().enumerate();
|
|
|
|
let q_index = match delete_method {
|
|
|
|
DeleteMethod::OneOldest | DeleteMethod::AllExceptNewest => values.max_by(|(_, l), (_, r)| l.modified_date.cmp(&r.modified_date)),
|
|
|
|
DeleteMethod::OneNewest | DeleteMethod::AllExceptOldest | DeleteMethod::HardLink => values.min_by(|(_, l), (_, r)| l.modified_date.cmp(&r.modified_date)),
|
|
|
|
DeleteMethod::None => values.next(),
|
|
|
|
};
|
2023-01-29 06:54:02 +13:00
|
|
|
let q_index = q_index.map_or(0, |t| t.0);
|
2021-02-27 21:00:59 +13:00
|
|
|
let n = match delete_method {
|
|
|
|
DeleteMethod::OneNewest | DeleteMethod::OneOldest => 1,
|
|
|
|
DeleteMethod::AllExceptNewest | DeleteMethod::AllExceptOldest | DeleteMethod::None | DeleteMethod::HardLink => usize::MAX,
|
|
|
|
};
|
|
|
|
for (index, file) in vector.iter().enumerate() {
|
|
|
|
if q_index == index {
|
|
|
|
continue;
|
2023-01-29 06:54:02 +13:00
|
|
|
}
|
|
|
|
if removed_files + failed_to_remove_files >= n {
|
2021-02-27 21:00:59 +13:00
|
|
|
break;
|
2021-02-06 05:59:34 +13:00
|
|
|
}
|
2021-03-02 00:23:43 +13:00
|
|
|
|
2021-02-27 21:00:59 +13:00
|
|
|
let r = match delete_method {
|
2021-03-02 00:23:43 +13:00
|
|
|
DeleteMethod::OneOldest | DeleteMethod::OneNewest | DeleteMethod::AllExceptOldest | DeleteMethod::AllExceptNewest => {
|
|
|
|
if dryrun {
|
|
|
|
Ok(Some(format!("Delete {}", file.path.display())))
|
|
|
|
} else {
|
|
|
|
fs::remove_file(&file.path).map(|_| None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DeleteMethod::HardLink => {
|
|
|
|
let src = &vector[q_index].path;
|
|
|
|
if dryrun {
|
|
|
|
Ok(Some(format!("Replace file {} with hard link to {}", file.path.display(), src.display())))
|
|
|
|
} else {
|
2021-07-30 17:16:35 +12:00
|
|
|
make_hard_link(src, &file.path).map(|_| None)
|
2021-03-02 00:23:43 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
DeleteMethod::None => Ok(None),
|
2021-02-27 21:00:59 +13:00
|
|
|
};
|
2021-03-02 00:23:43 +13:00
|
|
|
|
|
|
|
match r {
|
|
|
|
Err(e) => {
|
|
|
|
failed_to_remove_files += 1;
|
|
|
|
text_messages.warnings.push(format!("Failed to remove {} ({})", file.path.display(), e));
|
|
|
|
}
|
|
|
|
Ok(Some(msg)) => {
|
|
|
|
text_messages.messages.push(msg);
|
|
|
|
removed_files += 1;
|
|
|
|
gained_space += file.size;
|
|
|
|
}
|
|
|
|
Ok(None) => {
|
|
|
|
removed_files += 1;
|
|
|
|
gained_space += file.size;
|
|
|
|
}
|
2020-09-01 02:15:56 +12:00
|
|
|
}
|
2021-02-27 21:00:59 +13:00
|
|
|
}
|
2020-09-12 01:52:06 +12:00
|
|
|
(gained_space, removed_files, failed_to_remove_files)
|
2020-08-27 06:50:07 +12:00
|
|
|
}
|
2021-01-15 00:17:15 +13:00
|
|
|
|
2021-02-21 00:28:06 +13:00
|
|
|
#[cfg(target_family = "windows")]
|
|
|
|
fn filter_hard_links(vec_file_entry: &[FileEntry]) -> Vec<FileEntry> {
|
|
|
|
vec_file_entry.to_vec()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_family = "unix")]
|
|
|
|
fn filter_hard_links(vec_file_entry: &[FileEntry]) -> Vec<FileEntry> {
|
|
|
|
let mut inodes: HashSet<u64> = HashSet::with_capacity(vec_file_entry.len());
|
|
|
|
let mut identical: Vec<FileEntry> = Vec::with_capacity(vec_file_entry.len());
|
|
|
|
for f in vec_file_entry {
|
|
|
|
if let Ok(meta) = fs::metadata(&f.path) {
|
|
|
|
if !inodes.insert(meta.ino()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
identical.push(f.clone());
|
|
|
|
}
|
|
|
|
identical
|
|
|
|
}
|
|
|
|
|
2021-03-28 01:14:02 +13:00
|
|
|
pub fn make_hard_link(src: &Path, dst: &Path) -> io::Result<()> {
|
2021-02-12 07:04:23 +13:00
|
|
|
let dst_dir = dst.parent().ok_or_else(|| Error::new(ErrorKind::Other, "No parent"))?;
|
2022-04-09 03:17:16 +12:00
|
|
|
let temp = dst_dir.join(TEMP_HARDLINK_FILE);
|
|
|
|
fs::rename(dst, temp.as_path())?;
|
2021-02-12 07:04:23 +13:00
|
|
|
let result = fs::hard_link(src, dst);
|
|
|
|
if result.is_err() {
|
2022-04-09 03:17:16 +12:00
|
|
|
fs::rename(temp.as_path(), dst)?;
|
2021-02-12 07:04:23 +13:00
|
|
|
}
|
2022-04-09 03:17:16 +12:00
|
|
|
fs::remove_file(temp)?;
|
2021-02-12 07:04:23 +13:00
|
|
|
result
|
|
|
|
}
|
|
|
|
|
2021-12-02 00:37:17 +13:00
|
|
|
pub fn save_hashes_to_file(hashmap: &BTreeMap<String, FileEntry>, text_messages: &mut Messages, type_of_hash: &HashType, is_prehash: bool, minimal_cache_file_size: u64) {
|
2022-01-06 10:47:27 +13:00
|
|
|
if let Some(((file_handler, cache_file), (_json_file, _json_name))) = open_cache_folder(&get_file_hash_name(type_of_hash, is_prehash), true, false, &mut text_messages.warnings)
|
|
|
|
{
|
|
|
|
let mut writer = BufWriter::new(file_handler.unwrap()); // Unwrap cannot fail
|
2021-01-15 00:17:15 +13:00
|
|
|
|
2022-01-06 10:47:27 +13:00
|
|
|
let mut how_much = 0;
|
2021-01-15 00:17:15 +13:00
|
|
|
for file_entry in hashmap.values() {
|
2021-05-07 07:54:44 +12:00
|
|
|
if file_entry.size >= minimal_cache_file_size {
|
2021-01-15 00:17:15 +13:00
|
|
|
let string: String = format!("{}//{}//{}//{}", file_entry.path.display(), file_entry.size, file_entry.modified_date, file_entry.hash);
|
|
|
|
|
2022-12-21 20:44:26 +13:00
|
|
|
if let Err(e) = writeln!(writer, "{string}") {
|
2021-12-22 06:44:20 +13:00
|
|
|
text_messages
|
2022-01-06 10:47:27 +13:00
|
|
|
.warnings
|
2021-12-22 06:44:20 +13:00
|
|
|
.push(format!("Failed to save some data to cache file {}, reason {}", cache_file.display(), e));
|
2021-01-15 00:17:15 +13:00
|
|
|
return;
|
2022-01-06 10:47:27 +13:00
|
|
|
}
|
2023-01-29 06:54:02 +13:00
|
|
|
how_much += 1;
|
2021-01-15 00:17:15 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-20 10:35:07 +13:00
|
|
|
text_messages
|
|
|
|
.messages
|
|
|
|
.push(flc!("core_saving_to_cache", generate_translation_hashmap(vec![("number", how_much.to_string())])));
|
2021-02-04 06:11:04 +13:00
|
|
|
}
|
|
|
|
}
|
2022-06-01 03:52:55 +12:00
|
|
|
|
2021-12-02 00:37:17 +13:00
|
|
|
pub fn load_hashes_from_file(text_messages: &mut Messages, delete_outdated_cache: bool, type_of_hash: &HashType, is_prehash: bool) -> Option<BTreeMap<u64, Vec<FileEntry>>> {
|
2022-01-06 10:47:27 +13:00
|
|
|
if let Some(((file_handler, cache_file), (_json_file, _json_name))) =
|
|
|
|
open_cache_folder(&get_file_hash_name(type_of_hash, is_prehash), false, false, &mut text_messages.warnings)
|
|
|
|
{
|
|
|
|
// Unwrap could fail when failed to open cache file, but json would exists
|
2023-05-03 08:37:12 +12:00
|
|
|
let Some(file_handler) = file_handler else { return Default::default(); };
|
2021-01-15 00:17:15 +13:00
|
|
|
let reader = BufReader::new(file_handler);
|
|
|
|
|
|
|
|
let mut hashmap_loaded_entries: BTreeMap<u64, Vec<FileEntry>> = Default::default();
|
|
|
|
|
|
|
|
// Read the file line by line using the lines() iterator from std::io::BufRead.
|
|
|
|
for (index, line) in reader.lines().enumerate() {
|
|
|
|
let line = match line {
|
|
|
|
Ok(t) => t,
|
2021-11-15 03:53:55 +13:00
|
|
|
Err(e) => {
|
2021-12-22 06:44:20 +13:00
|
|
|
text_messages
|
|
|
|
.warnings
|
|
|
|
.push(format!("Failed to load line number {} from cache file {}, reason {}", index + 1, cache_file.display(), e));
|
2021-01-15 00:17:15 +13:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let uuu = line.split("//").collect::<Vec<&str>>();
|
|
|
|
if uuu.len() != 4 {
|
2021-12-22 06:44:20 +13:00
|
|
|
text_messages.warnings.push(format!(
|
|
|
|
"Found invalid data(too much or too low amount of data) in line {} - ({}) in cache file {}",
|
|
|
|
index + 1,
|
|
|
|
line,
|
|
|
|
cache_file.display()
|
|
|
|
));
|
2021-01-15 00:17:15 +13:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// Don't load cache data if destination file not exists
|
2021-12-01 00:45:09 +13:00
|
|
|
if !delete_outdated_cache || Path::new(uuu[0]).exists() {
|
2021-01-15 00:17:15 +13:00
|
|
|
let file_entry = FileEntry {
|
|
|
|
path: PathBuf::from(uuu[0]),
|
|
|
|
size: match uuu[1].parse::<u64>() {
|
|
|
|
Ok(t) => t,
|
2021-11-15 03:53:55 +13:00
|
|
|
Err(e) => {
|
2021-12-22 06:44:20 +13:00
|
|
|
text_messages.warnings.push(format!(
|
|
|
|
"Found invalid size value in line {} - ({}) in cache file {}, reason {}",
|
|
|
|
index + 1,
|
|
|
|
line,
|
|
|
|
cache_file.display(),
|
|
|
|
e
|
|
|
|
));
|
2021-01-15 00:17:15 +13:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
modified_date: match uuu[2].parse::<u64>() {
|
|
|
|
Ok(t) => t,
|
2021-11-15 03:53:55 +13:00
|
|
|
Err(e) => {
|
2021-12-22 06:44:20 +13:00
|
|
|
text_messages.warnings.push(format!(
|
|
|
|
"Found invalid modified date value in line {} - ({}) in cache file {}, reason {}",
|
|
|
|
index + 1,
|
|
|
|
line,
|
|
|
|
cache_file.display(),
|
|
|
|
e
|
|
|
|
));
|
2021-01-15 00:17:15 +13:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
hash: uuu[3].to_string(),
|
2022-01-02 09:07:20 +13:00
|
|
|
symlink_info: None,
|
2021-01-15 00:17:15 +13:00
|
|
|
};
|
2022-06-05 18:01:17 +12:00
|
|
|
hashmap_loaded_entries.entry(file_entry.size).or_insert_with(Vec::new).push(file_entry);
|
2021-01-15 00:17:15 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-20 10:35:07 +13:00
|
|
|
text_messages.messages.push(flc!(
|
|
|
|
"core_loading_from_cache",
|
2023-01-29 06:54:02 +13:00
|
|
|
generate_translation_hashmap(vec![("number", hashmap_loaded_entries.values().map(std::vec::Vec::len).sum::<usize>().to_string())])
|
2022-01-15 09:14:13 +13:00
|
|
|
));
|
2022-01-06 10:47:27 +13:00
|
|
|
|
2021-01-15 00:17:15 +13:00
|
|
|
return Some(hashmap_loaded_entries);
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
2021-02-12 07:04:23 +13:00
|
|
|
|
2022-01-06 10:47:27 +13:00
|
|
|
pub trait MyHasher {
|
|
|
|
fn update(&mut self, bytes: &[u8]);
|
|
|
|
fn finalize(&self) -> String;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn hash_calculation(buffer: &mut [u8], file_entry: &FileEntry, hash_type: &HashType, limit: u64) -> Result<String, String> {
|
|
|
|
let mut file_handler = match File::open(&file_entry.path) {
|
|
|
|
Ok(t) => t,
|
|
|
|
Err(e) => return Err(format!("Unable to check hash of file {}, reason {}", file_entry.path.display(), e)),
|
|
|
|
};
|
|
|
|
let hasher = &mut *hash_type.hasher();
|
|
|
|
let mut current_file_read_bytes: u64 = 0;
|
|
|
|
loop {
|
|
|
|
let n = match file_handler.read(buffer) {
|
|
|
|
Ok(0) => break,
|
|
|
|
Ok(t) => t,
|
|
|
|
Err(e) => return Err(format!("Error happened when checking hash of file {}, reason {}", file_entry.path.display(), e)),
|
|
|
|
};
|
|
|
|
|
|
|
|
current_file_read_bytes += n as u64;
|
|
|
|
hasher.update(&buffer[..n]);
|
|
|
|
|
|
|
|
if current_file_read_bytes >= limit {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(hasher.finalize())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_file_hash_name(type_of_hash: &HashType, is_prehash: bool) -> String {
|
|
|
|
let prehash_str = if is_prehash { "_prehash" } else { "" };
|
2022-12-21 20:44:26 +13:00
|
|
|
format!("cache_duplicates_{type_of_hash:?}{prehash_str}.txt")
|
2022-01-06 10:47:27 +13:00
|
|
|
}
|
|
|
|
|
2021-12-18 07:29:37 +13:00
|
|
|
impl MyHasher for blake3::Hasher {
|
|
|
|
fn update(&mut self, bytes: &[u8]) {
|
|
|
|
self.update(bytes);
|
|
|
|
}
|
|
|
|
fn finalize(&self) -> String {
|
|
|
|
self.finalize().to_hex().to_string()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MyHasher for crc32fast::Hasher {
|
|
|
|
fn update(&mut self, bytes: &[u8]) {
|
|
|
|
self.write(bytes);
|
|
|
|
}
|
|
|
|
fn finalize(&self) -> String {
|
|
|
|
self.finish().to_string()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-03 08:37:12 +12:00
|
|
|
impl MyHasher for Xxh3 {
|
2021-12-18 07:29:37 +13:00
|
|
|
fn update(&mut self, bytes: &[u8]) {
|
|
|
|
self.write(bytes);
|
|
|
|
}
|
|
|
|
fn finalize(&self) -> String {
|
|
|
|
self.finish().to_string()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-12 07:04:23 +13:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2022-01-02 09:07:20 +13:00
|
|
|
use std::fs::{read_dir, File, Metadata};
|
2021-02-22 22:52:21 +13:00
|
|
|
use std::io;
|
2021-02-12 07:04:23 +13:00
|
|
|
#[cfg(target_family = "windows")]
|
|
|
|
use std::os::fs::MetadataExt;
|
|
|
|
#[cfg(target_family = "unix")]
|
|
|
|
use std::os::unix::fs::MetadataExt;
|
|
|
|
|
2021-11-28 08:49:20 +13:00
|
|
|
use super::*;
|
|
|
|
|
2021-02-12 07:04:23 +13:00
|
|
|
#[cfg(target_family = "unix")]
|
|
|
|
fn assert_inode(before: &Metadata, after: &Metadata) {
|
|
|
|
assert_eq!(before.ino(), after.ino());
|
|
|
|
}
|
2021-11-28 08:57:10 +13:00
|
|
|
|
2021-02-12 07:04:23 +13:00
|
|
|
#[cfg(target_family = "windows")]
|
|
|
|
fn assert_inode(_: &Metadata, _: &Metadata) {}
|
|
|
|
|
|
|
|
#[test]
|
2021-02-22 22:52:21 +13:00
|
|
|
fn test_make_hard_link() -> io::Result<()> {
|
2021-02-12 07:04:23 +13:00
|
|
|
let dir = tempfile::Builder::new().tempdir()?;
|
|
|
|
let (src, dst) = (dir.path().join("a"), dir.path().join("b"));
|
|
|
|
File::create(&src)?;
|
|
|
|
let metadata = fs::metadata(&src)?;
|
|
|
|
File::create(&dst)?;
|
|
|
|
|
|
|
|
make_hard_link(&src, &dst)?;
|
|
|
|
|
|
|
|
assert_inode(&metadata, &fs::metadata(&dst)?);
|
|
|
|
assert_eq!(metadata.permissions(), fs::metadata(&dst)?.permissions());
|
|
|
|
assert_eq!(metadata.modified()?, fs::metadata(&dst)?.modified()?);
|
|
|
|
assert_inode(&metadata, &fs::metadata(&src)?);
|
|
|
|
assert_eq!(metadata.permissions(), fs::metadata(&src)?.permissions());
|
|
|
|
assert_eq!(metadata.modified()?, fs::metadata(&src)?.modified()?);
|
|
|
|
|
|
|
|
let mut actual = read_dir(&dir)?.map(|e| e.unwrap().path()).collect::<Vec<PathBuf>>();
|
2022-07-25 06:48:02 +12:00
|
|
|
actual.sort_unstable();
|
2021-02-12 07:04:23 +13:00
|
|
|
assert_eq!(vec![src, dst], actual);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-02-22 22:52:21 +13:00
|
|
|
fn test_make_hard_link_fails() -> io::Result<()> {
|
2021-02-12 07:04:23 +13:00
|
|
|
let dir = tempfile::Builder::new().tempdir()?;
|
|
|
|
let (src, dst) = (dir.path().join("a"), dir.path().join("b"));
|
|
|
|
File::create(&dst)?;
|
|
|
|
let metadata = fs::metadata(&dst)?;
|
|
|
|
|
|
|
|
assert!(make_hard_link(&src, &dst).is_err());
|
|
|
|
|
|
|
|
assert_inode(&metadata, &fs::metadata(&dst)?);
|
|
|
|
assert_eq!(metadata.permissions(), fs::metadata(&dst)?.permissions());
|
|
|
|
assert_eq!(metadata.modified()?, fs::metadata(&dst)?.modified()?);
|
|
|
|
|
|
|
|
assert_eq!(vec![dst], read_dir(&dir)?.map(|e| e.unwrap().path()).collect::<Vec<PathBuf>>());
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-02-21 00:28:06 +13:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_filter_hard_links_empty() {
|
|
|
|
let expected: Vec<FileEntry> = Default::default();
|
|
|
|
assert_eq!(expected, filter_hard_links(&[]));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_family = "unix")]
|
|
|
|
#[test]
|
2021-02-22 22:52:21 +13:00
|
|
|
fn test_filter_hard_links() -> io::Result<()> {
|
2021-02-21 00:28:06 +13:00
|
|
|
let dir = tempfile::Builder::new().tempdir()?;
|
|
|
|
let (src, dst) = (dir.path().join("a"), dir.path().join("b"));
|
|
|
|
File::create(&src)?;
|
|
|
|
fs::hard_link(src.clone(), dst.clone())?;
|
|
|
|
let e1 = FileEntry { path: src, ..Default::default() };
|
|
|
|
let e2 = FileEntry { path: dst, ..Default::default() };
|
|
|
|
let actual = filter_hard_links(&[e1.clone(), e2]);
|
|
|
|
assert_eq!(vec![e1], actual);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-02-22 22:52:21 +13:00
|
|
|
fn test_filter_hard_links_regular_files() -> io::Result<()> {
|
2021-02-21 00:28:06 +13:00
|
|
|
let dir = tempfile::Builder::new().tempdir()?;
|
|
|
|
let (src, dst) = (dir.path().join("a"), dir.path().join("b"));
|
|
|
|
File::create(&src)?;
|
|
|
|
File::create(&dst)?;
|
|
|
|
let e1 = FileEntry { path: src, ..Default::default() };
|
|
|
|
let e2 = FileEntry { path: dst, ..Default::default() };
|
|
|
|
let actual = filter_hard_links(&[e1.clone(), e2.clone()]);
|
|
|
|
assert_eq!(vec![e1, e2], actual);
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-02-22 22:52:21 +13:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_hash_calculation() -> io::Result<()> {
|
|
|
|
let dir = tempfile::Builder::new().tempdir()?;
|
|
|
|
let mut buf = [0u8; 1 << 10];
|
|
|
|
let src = dir.path().join("a");
|
|
|
|
let mut file = File::create(&src)?;
|
|
|
|
file.write_all(b"aa")?;
|
|
|
|
let e = FileEntry { path: src, ..Default::default() };
|
|
|
|
let r = hash_calculation(&mut buf, &e, &HashType::Blake3, 0).unwrap();
|
2021-12-02 08:09:04 +13:00
|
|
|
assert!(!r.is_empty());
|
2021-02-22 22:52:21 +13:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_hash_calculation_limit() -> io::Result<()> {
|
|
|
|
let dir = tempfile::Builder::new().tempdir()?;
|
|
|
|
let mut buf = [0u8; 1];
|
|
|
|
let src = dir.path().join("a");
|
|
|
|
let mut file = File::create(&src)?;
|
|
|
|
file.write_all(b"aa")?;
|
|
|
|
let e = FileEntry { path: src, ..Default::default() };
|
|
|
|
let r1 = hash_calculation(&mut buf, &e, &HashType::Blake3, 1).unwrap();
|
|
|
|
let r2 = hash_calculation(&mut buf, &e, &HashType::Blake3, 2).unwrap();
|
|
|
|
let r3 = hash_calculation(&mut buf, &e, &HashType::Blake3, u64::MAX).unwrap();
|
|
|
|
assert_ne!(r1, r2);
|
|
|
|
assert_eq!(r2, r3);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_hash_calculation_invalid_file() -> io::Result<()> {
|
|
|
|
let dir = tempfile::Builder::new().tempdir()?;
|
|
|
|
let mut buf = [0u8; 1 << 10];
|
|
|
|
let src = dir.path().join("a");
|
|
|
|
let e = FileEntry { path: src, ..Default::default() };
|
|
|
|
let r = hash_calculation(&mut buf, &e, &HashType::Blake3, 0).unwrap_err();
|
|
|
|
assert!(!r.is_empty());
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-02-12 07:04:23 +13:00
|
|
|
}
|