Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Core cleanup/changes #1082

Merged
merged 8 commits into from
Oct 10, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Smaller Printing
  • Loading branch information
qarmin committed Oct 10, 2023
commit cb5e455ced24df9598919410d6b3355dd6039cae
87 changes: 37 additions & 50 deletions czkawka_cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
#![allow(clippy::needless_late_init)]

use std::process;

use clap::Parser;
use log::error;

use commands::Commands;
use czkawka_core::bad_extensions::BadExtensions;
use czkawka_core::big_file::{BigFile, SearchMode};
use czkawka_core::broken_files::BrokenFiles;
use czkawka_core::common::{set_number_of_threads, setup_logger};
use czkawka_core::common_tool::{CommonData, DeleteMethod};
#[allow(unused_imports)] // It is used in release for print_results().
#[allow(unused_imports)] // It is used in release for print_results_to_output().
use czkawka_core::common_traits::*;
use czkawka_core::duplicate::DuplicateFinder;
use czkawka_core::empty_files::EmptyFiles;
@@ -101,16 +100,14 @@ fn duplicates(duplicates: DuplicatesArgs) {
df.find_duplicates(None, None);

if let Some(file_name) = file_to_save.file_name() {
if !df.save_results_to_file(file_name) {
df.get_text_messages().print_messages();
process::exit(1);
if let Err(e) = df.print_results_to_file(file_name) {
error!("Failed to save results to file {e}");
}
}

if !cfg!(debug_assertions) {
df.print_results();
df.print_results_to_output();
}
df.print_results();
df.get_text_messages().print_messages();
}

@@ -140,14 +137,13 @@ fn empty_folders(empty_folders: EmptyFoldersArgs) {
ef.find_empty_folders(None, None);

if let Some(file_name) = file_to_save.file_name() {
if !ef.save_results_to_file(file_name) {
ef.get_text_messages().print_messages();
process::exit(1);
if let Err(e) = ef.print_results_to_file(file_name) {
error!("Failed to save results to file {e}");
}
}

if !cfg!(debug_assertions) {
ef.print_results();
ef.print_results_to_output();
}
ef.get_text_messages().print_messages();
}
@@ -190,14 +186,13 @@ fn biggest_files(biggest_files: BiggestFilesArgs) {
bf.find_big_files(None, None);

if let Some(file_name) = file_to_save.file_name() {
if !bf.save_results_to_file(file_name) {
bf.get_text_messages().print_messages();
process::exit(1);
if let Err(e) = bf.print_results_to_file(file_name) {
error!("Failed to save results to file {e}");
}
}

if !cfg!(debug_assertions) {
bf.print_results();
bf.print_results_to_output();
}
bf.get_text_messages().print_messages();
}
@@ -235,14 +230,13 @@ fn empty_files(empty_files: EmptyFilesArgs) {
ef.find_empty_files(None, None);

if let Some(file_name) = file_to_save.file_name() {
if !ef.save_results_to_file(file_name) {
ef.get_text_messages().print_messages();
process::exit(1);
if let Err(e) = ef.print_results_to_file(file_name) {
error!("Failed to save results to file {e}");
}
}

if !cfg!(debug_assertions) {
ef.print_results();
ef.print_results_to_output();
}
ef.get_text_messages().print_messages();
}
@@ -278,14 +272,13 @@ fn temporary(temporary: TemporaryArgs) {
tf.find_temporary_files(None, None);

if let Some(file_name) = file_to_save.file_name() {
if !tf.save_results_to_file(file_name) {
tf.get_text_messages().print_messages();
process::exit(1);
if let Err(e) = tf.print_results_to_file(file_name) {
error!("Failed to save results to file {e}");
}
}

if !cfg!(debug_assertions) {
tf.print_results();
tf.print_results_to_output();
}
tf.get_text_messages().print_messages();
}
@@ -329,14 +322,13 @@ fn similar_images(similar_images: SimilarImagesArgs) {
sf.find_similar_images(None, None);

if let Some(file_name) = file_to_save.file_name() {
if !sf.save_results_to_file(file_name) {
sf.get_text_messages().print_messages();
process::exit(1);
if let Err(e) = sf.print_results_to_file(file_name) {
error!("Failed to save results to file {e}");
}
}

if !cfg!(debug_assertions) {
sf.print_results();
sf.print_results_to_output();
}
sf.get_text_messages().print_messages();
}
@@ -378,14 +370,13 @@ fn same_music(same_music: SameMusicArgs) {
mf.find_same_music(None, None);

if let Some(file_name) = file_to_save.file_name() {
if !mf.save_results_to_file(file_name) {
mf.get_text_messages().print_messages();
process::exit(1);
if let Err(e) = mf.print_results_to_file(file_name) {
error!("Failed to save results to file {e}");
}
}

if !cfg!(debug_assertions) {
mf.print_results();
mf.print_results_to_output();
}
mf.get_text_messages().print_messages();
}
@@ -422,14 +413,13 @@ fn invalid_symlinks(invalid_symlinks: InvalidSymlinksArgs) {
ifs.find_invalid_links(None, None);

if let Some(file_name) = file_to_save.file_name() {
if !ifs.save_results_to_file(file_name) {
ifs.get_text_messages().print_messages();
process::exit(1);
if let Err(e) = ifs.print_results_to_file(file_name) {
error!("Failed to save results to file {e}");
}
}

if !cfg!(debug_assertions) {
ifs.print_results();
ifs.print_results_to_output();
}
ifs.get_text_messages().print_messages();
}
@@ -467,14 +457,13 @@ fn broken_files(broken_files: BrokenFilesArgs) {
br.find_broken_files(None, None);

if let Some(file_name) = file_to_save.file_name() {
if !br.save_results_to_file(file_name) {
br.get_text_messages().print_messages();
process::exit(1);
if let Err(e) = br.print_results_to_file(file_name) {
error!("Failed to save results to file {e}");
}
}

if !cfg!(debug_assertions) {
br.print_results();
br.print_results_to_output();
}
br.get_text_messages().print_messages();
}
@@ -513,14 +502,13 @@ fn similar_videos(similar_videos: SimilarVideosArgs) {
vr.find_similar_videos(None, None);

if let Some(file_name) = file_to_save.file_name() {
if !vr.save_results_to_file(file_name) {
vr.get_text_messages().print_messages();
process::exit(1);
if let Err(e) = vr.print_results_to_file(file_name) {
error!("Failed to save results to file {e}");
}
}

if !cfg!(debug_assertions) {
vr.print_results();
vr.print_results_to_output();
}
vr.get_text_messages().print_messages();
}
@@ -550,17 +538,16 @@ fn bad_extensions(bad_extensions: BadExtensionsArgs) {
#[cfg(target_family = "unix")]
be.set_exclude_other_filesystems(exclude_other_filesystems.exclude_other_filesystems);

be.find_bad_extensions_files(None, None);

if let Some(file_name) = file_to_save.file_name() {
if !be.save_results_to_file(file_name) {
be.get_text_messages().print_messages();
process::exit(1);
if let Err(e) = be.print_results_to_file(file_name) {
error!("Failed to save results to file {e}");
}
}

be.find_bad_extensions_files(None, None);

if !cfg!(debug_assertions) {
be.print_results();
be.print_results_to_output();
}
be.get_text_messages().print_messages();
}
53 changes: 8 additions & 45 deletions czkawka_core/src/bad_extensions.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use std::collections::{BTreeSet, HashMap};
use std::fs::File;
use std::io::prelude::*;
use std::io::BufWriter;
use std::mem;
use std::path::PathBuf;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
@@ -413,55 +411,20 @@ impl DebugPrint for BadExtensions {
}
}

impl SaveResults for BadExtensions {
#[fun_time(message = "save_results_to_file")]
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(),
};

let file_handler = match File::create(&file_name) {
Ok(t) => t,
Err(e) => {
self.common_data.text_messages.errors.push(format!("Failed to create file {file_name}, reason {e}"));
return false;
}
};
let mut writer = BufWriter::new(file_handler);

if let Err(e) = writeln!(
impl PrintResults for BadExtensions {
fn write_results<T: Write>(&self, writer: &mut T) -> std::io::Result<()> {
writeln!(
writer,
"Results of searching {:?} with excluded directories {:?} and excluded items {:?}",
self.common_data.directories.included_directories, self.common_data.directories.excluded_directories, self.common_data.excluded_items.items
) {
self.common_data
.text_messages
.errors
.push(format!("Failed to save results to file {file_name}, reason {e}"));
return false;
}

if !self.bad_extensions_files.is_empty() {
writeln!(writer, "Found {} files with invalid extension.", self.information.number_of_files_with_bad_extension).unwrap();
for file_entry in &self.bad_extensions_files {
writeln!(writer, "{} ----- {}", file_entry.path.display(), file_entry.proper_extensions).unwrap();
}
} else {
write!(writer, "Not found any files with invalid extension.").unwrap();
}
)?;
writeln!(writer, "Found {} files with invalid extension.\n", self.information.number_of_files_with_bad_extension)?;

true
}
}

impl PrintResults for BadExtensions {
#[fun_time(message = "print_results")]
fn print_results(&self) {
println!("Found {} files with invalid extension.\n", self.information.number_of_files_with_bad_extension);
for file_entry in &self.bad_extensions_files {
println!("{} ----- {}", file_entry.path.display(), file_entry.proper_extensions);
writeln!(writer, "{} ----- {}", file_entry.path.display(), file_entry.proper_extensions)?;
}

Ok(())
}
}

51 changes: 9 additions & 42 deletions czkawka_core/src/big_file.rs
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@ use rayon::prelude::*;
use crate::common::{check_folder_children, prepare_thread_handler_common, send_info_and_wait_for_ending_all_threads, split_path};
use crate::common_dir_traversal::{common_get_entry_data_metadata, common_read_dir, get_lowercase_name, get_modified_time, CheckingMethod, ProgressData, ToolType};
use crate::common_tool::{CommonData, CommonToolData, DeleteMethod};
use crate::common_traits::{DebugPrint, PrintResults, SaveResults};
use crate::common_traits::{DebugPrint, PrintResults};

#[derive(Clone, Debug)]
pub struct FileEntry {
@@ -248,61 +248,28 @@ impl DebugPrint for BigFile {
}
}

impl SaveResults for BigFile {
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(),
};

let file_handler = match File::create(&file_name) {
Ok(t) => t,
Err(e) => {
self.common_data.text_messages.errors.push(format!("Failed to create file {file_name}, reason {e}"));
return false;
}
};
let mut writer = BufWriter::new(file_handler);

if let Err(e) = writeln!(
impl PrintResults for BigFile {
fn write_results<T: Write>(&self, writer: &mut T) -> std::io::Result<()> {
writeln!(
writer,
"Results of searching {:?} with excluded directories {:?} and excluded items {:?}",
self.common_data.directories.included_directories, self.common_data.directories.excluded_directories, self.common_data.excluded_items.items
) {
self.common_data
.text_messages
.errors
.push(format!("Failed to save results to file {file_name}, reason {e}"));
return false;
}
)?;

if self.information.number_of_real_files != 0 {
if self.search_mode == SearchMode::BiggestFiles {
write!(writer, "{} the biggest files.\n\n", self.information.number_of_real_files).unwrap();
writeln!(writer, "{} the biggest files.\n\n", self.information.number_of_real_files)?;
} else {
write!(writer, "{} the smallest files.\n\n", self.information.number_of_real_files).unwrap();
writeln!(writer, "{} the smallest files.\n\n", self.information.number_of_real_files)?;
}
for (size, file_entry) in &self.big_files {
writeln!(writer, "{} ({}) - {}", format_size(*size, BINARY), size, file_entry.path.display()).unwrap();
writeln!(writer, "{} ({}) - {}", format_size(*size, BINARY), size, file_entry.path.display())?;
}
} else {
write!(writer, "Not found any files.").unwrap();
}

true
}
}

impl PrintResults for BigFile {
fn print_results(&self) {
if self.search_mode == SearchMode::BiggestFiles {
println!("{} the biggest files.\n\n", self.information.number_of_real_files);
} else {
println!("{} the smallest files.\n\n", self.information.number_of_real_files);
}
for (size, file_entry) in &self.big_files {
println!("{} ({}) - {}", format_size(*size, BINARY), size, file_entry.path.display());
}
Ok(())
}
}

Loading