2022-01-02 09:07:20 +13:00
|
|
|
use std::fs;
|
2023-10-11 07:54:41 +13:00
|
|
|
|
2020-09-27 08:50:16 +13:00
|
|
|
use std::io::prelude::*;
|
|
|
|
|
2023-12-04 00:06:42 +13:00
|
|
|
use crossbeam_channel::{Receiver, Sender};
|
2023-10-11 07:54:41 +13:00
|
|
|
use fun_time::fun_time;
|
|
|
|
use log::debug;
|
2021-11-28 08:49:20 +13:00
|
|
|
|
2023-05-11 07:27:41 +12:00
|
|
|
use crate::common_dir_traversal::{DirTraversalBuilder, DirTraversalResult, FileEntry, ProgressData, ToolType};
|
2023-10-11 07:54:41 +13:00
|
|
|
use crate::common_tool::{CommonData, CommonToolData, DeleteMethod};
|
2020-09-27 08:50:16 +13:00
|
|
|
use crate::common_traits::*;
|
|
|
|
|
2020-10-10 09:32:08 +13:00
|
|
|
#[derive(Default)]
|
2020-09-27 08:50:16 +13:00
|
|
|
pub struct Info {
|
|
|
|
pub number_of_empty_files: usize,
|
|
|
|
}
|
2021-11-28 08:57:10 +13:00
|
|
|
|
2020-09-27 08:50:16 +13:00
|
|
|
pub struct EmptyFiles {
|
2023-10-05 19:06:47 +13:00
|
|
|
common_data: CommonToolData,
|
2020-09-27 08:50:16 +13:00
|
|
|
information: Info,
|
|
|
|
empty_files: Vec<FileEntry>,
|
2023-10-05 19:06:47 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
impl CommonData for EmptyFiles {
|
|
|
|
fn get_cd(&self) -> &CommonToolData {
|
|
|
|
&self.common_data
|
|
|
|
}
|
|
|
|
fn get_cd_mut(&mut self) -> &mut CommonToolData {
|
|
|
|
&mut self.common_data
|
|
|
|
}
|
2020-09-27 08:50:16 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
impl EmptyFiles {
|
2020-10-10 09:32:08 +13:00
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
2023-10-05 19:06:47 +13:00
|
|
|
common_data: CommonToolData::new(ToolType::EmptyFiles),
|
|
|
|
information: Info::default(),
|
2020-09-27 08:50:16 +13:00
|
|
|
empty_files: vec![],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-15 04:48:57 +13:00
|
|
|
#[fun_time(message = "find_empty_files", level = "info")]
|
2023-12-04 00:06:42 +13:00
|
|
|
pub fn find_empty_files(&mut self, stop_receiver: Option<&Receiver<()>>, progress_sender: Option<&Sender<ProgressData>>) {
|
2023-10-05 19:06:47 +13:00
|
|
|
self.optimize_dirs_before_start();
|
2020-12-02 22:25:27 +13:00
|
|
|
if !self.check_files(stop_receiver, progress_sender) {
|
2023-10-05 19:06:47 +13:00
|
|
|
self.common_data.stopped_search = true;
|
2020-10-10 22:15:20 +13:00
|
|
|
return;
|
|
|
|
}
|
2020-09-27 08:50:16 +13:00
|
|
|
self.delete_files();
|
|
|
|
self.debug_print();
|
|
|
|
}
|
|
|
|
|
2023-10-15 04:48:57 +13:00
|
|
|
#[fun_time(message = "check_files", level = "debug")]
|
2023-12-04 00:06:42 +13:00
|
|
|
fn check_files(&mut self, stop_receiver: Option<&Receiver<()>>, progress_sender: Option<&Sender<ProgressData>>) -> bool {
|
2022-01-02 09:07:20 +13:00
|
|
|
let result = DirTraversalBuilder::new()
|
2023-10-05 19:06:47 +13:00
|
|
|
.root_dirs(self.common_data.directories.included_directories.clone())
|
2022-01-02 09:07:20 +13:00
|
|
|
.group_by(|_fe| ())
|
|
|
|
.stop_receiver(stop_receiver)
|
|
|
|
.progress_sender(progress_sender)
|
|
|
|
.minimal_file_size(0)
|
|
|
|
.maximal_file_size(0)
|
2023-10-05 19:06:47 +13:00
|
|
|
.directories(self.common_data.directories.clone())
|
|
|
|
.allowed_extensions(self.common_data.allowed_extensions.clone())
|
|
|
|
.excluded_items(self.common_data.excluded_items.clone())
|
|
|
|
.recursive_search(self.common_data.recursive_search)
|
2023-12-04 00:06:42 +13:00
|
|
|
.tool_type(self.common_data.tool_type)
|
2022-01-02 09:07:20 +13:00
|
|
|
.build()
|
|
|
|
.run();
|
2023-10-11 07:54:41 +13:00
|
|
|
|
|
|
|
match result {
|
2023-05-08 06:54:05 +12:00
|
|
|
DirTraversalResult::SuccessFiles { grouped_file_entries, warnings } => {
|
2023-10-11 07:54:41 +13:00
|
|
|
self.empty_files = grouped_file_entries.into_values().flatten().collect();
|
2022-01-02 09:07:20 +13:00
|
|
|
self.information.number_of_empty_files = self.empty_files.len();
|
2023-10-05 19:06:47 +13:00
|
|
|
self.common_data.text_messages.warnings.extend(warnings);
|
2023-05-08 06:54:05 +12:00
|
|
|
|
2023-10-11 07:54:41 +13:00
|
|
|
debug!("Found {} empty files.", self.information.number_of_empty_files);
|
|
|
|
|
2022-01-02 09:07:20 +13:00
|
|
|
true
|
|
|
|
}
|
|
|
|
DirTraversalResult::SuccessFolders { .. } => {
|
|
|
|
unreachable!()
|
2020-09-27 08:50:16 +13:00
|
|
|
}
|
2022-01-02 09:07:20 +13:00
|
|
|
DirTraversalResult::Stopped => false,
|
2023-10-11 07:54:41 +13:00
|
|
|
}
|
2020-09-27 08:50:16 +13:00
|
|
|
}
|
|
|
|
|
2023-10-15 04:48:57 +13:00
|
|
|
#[fun_time(message = "delete_files", level = "debug")]
|
2020-09-27 08:50:16 +13:00
|
|
|
fn delete_files(&mut self) {
|
2023-10-11 07:54:41 +13:00
|
|
|
match self.common_data.delete_method {
|
2020-09-27 08:50:16 +13:00
|
|
|
DeleteMethod::Delete => {
|
|
|
|
for file_entry in &self.empty_files {
|
|
|
|
if fs::remove_file(file_entry.path.clone()).is_err() {
|
2023-10-05 19:06:47 +13:00
|
|
|
self.common_data.text_messages.warnings.push(file_entry.path.display().to_string());
|
2020-09-27 08:50:16 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DeleteMethod::None => {
|
|
|
|
//Just do nothing
|
|
|
|
}
|
2023-10-11 07:54:41 +13:00
|
|
|
_ => {
|
|
|
|
unreachable!()
|
|
|
|
}
|
2020-09-27 08:50:16 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-28 08:57:10 +13:00
|
|
|
|
2020-09-27 08:50:16 +13:00
|
|
|
impl Default for EmptyFiles {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DebugPrint for EmptyFiles {
|
|
|
|
fn debug_print(&self) {
|
2023-10-11 07:54:41 +13:00
|
|
|
if !cfg!(debug_assertions) {
|
2020-09-27 08:50:16 +13:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
println!("---------------DEBUG PRINT---------------");
|
|
|
|
println!("Empty list size - {}", self.empty_files.len());
|
2023-10-05 19:06:47 +13:00
|
|
|
self.debug_print_common();
|
2020-09-27 08:50:16 +13:00
|
|
|
println!("-----------------------------------------");
|
|
|
|
}
|
|
|
|
}
|
2021-11-28 08:57:10 +13:00
|
|
|
|
2023-10-11 07:54:41 +13:00
|
|
|
impl PrintResults for EmptyFiles {
|
|
|
|
fn write_results<T: Write>(&self, writer: &mut T) -> std::io::Result<()> {
|
|
|
|
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 {:?}",
|
2023-12-04 09:18:31 +13:00
|
|
|
self.common_data.directories.included_directories,
|
|
|
|
self.common_data.directories.excluded_directories,
|
|
|
|
self.common_data.excluded_items.get_excluded_items()
|
2023-10-11 07:54:41 +13:00
|
|
|
)?;
|
2020-09-27 08:50:16 +13:00
|
|
|
|
|
|
|
if !self.empty_files.is_empty() {
|
2023-10-11 07:54:41 +13:00
|
|
|
writeln!(writer, "Found {} empty files.", self.information.number_of_empty_files)?;
|
2023-01-29 06:54:02 +13:00
|
|
|
for file_entry in &self.empty_files {
|
2023-10-11 07:54:41 +13:00
|
|
|
writeln!(writer, "{}", file_entry.path.display())?;
|
2020-09-27 08:50:16 +13:00
|
|
|
}
|
|
|
|
} else {
|
2023-10-11 07:54:41 +13:00
|
|
|
write!(writer, "Not found any empty files.")?;
|
2020-09-27 08:50:16 +13:00
|
|
|
}
|
2023-05-08 06:54:05 +12:00
|
|
|
|
2023-10-11 07:54:41 +13:00
|
|
|
Ok(())
|
2020-09-27 08:50:16 +13:00
|
|
|
}
|
2023-10-13 05:48:46 +13:00
|
|
|
|
|
|
|
fn save_results_to_file_as_json(&self, file_name: &str, pretty_print: bool) -> std::io::Result<()> {
|
|
|
|
self.save_results_to_file_as_json_internal(file_name, &self.empty_files, pretty_print)
|
|
|
|
}
|
2020-09-27 08:50:16 +13:00
|
|
|
}
|
2023-10-05 19:06:47 +13:00
|
|
|
|
|
|
|
impl EmptyFiles {
|
|
|
|
pub const fn get_empty_files(&self) -> &Vec<FileEntry> {
|
|
|
|
&self.empty_files
|
|
|
|
}
|
|
|
|
|
|
|
|
pub const fn get_information(&self) -> &Info {
|
|
|
|
&self.information
|
|
|
|
}
|
|
|
|
}
|