Skip to content

Commit

Permalink
Add buffered write/read (#186)
Browse files Browse the repository at this point in the history
  • Loading branch information
qarmin authored Jan 9, 2021
1 parent 3adbfdc commit 9975605
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 67 deletions.
13 changes: 7 additions & 6 deletions czkawka_core/src/big_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use humansize::{file_size_opts as options, FileSize};
use std::collections::BTreeMap;
use std::ffi::OsStr;
use std::fs::{File, Metadata};
use std::io::Write;
use std::io::{BufWriter, Write};
use std::path::PathBuf;
use std::sync::atomic::Ordering;
use std::sync::atomic::{AtomicBool, AtomicU64};
Expand Down Expand Up @@ -361,16 +361,17 @@ impl SaveResults for BigFile {
k => k.to_string(),
};

let mut file = match File::create(&file_name) {
let file_handler = match File::create(&file_name) {
Ok(t) => t,
Err(_) => {
self.text_messages.errors.push("Failed to create file ".to_string() + file_name.as_str());
return false;
}
};
let mut writer = BufWriter::new(file_handler);

if writeln!(
file,
writer,
"Results of searching {:?} with excluded directories {:?} and excluded items {:?}",
self.directories.included_directories, self.directories.excluded_directories, self.excluded_items.items
)
Expand All @@ -381,15 +382,15 @@ impl SaveResults for BigFile {
}

if self.information.number_of_real_files != 0 {
write!(file, "{} the biggest files.\n\n", self.information.number_of_real_files).unwrap();
write!(writer, "{} the biggest files.\n\n", self.information.number_of_real_files).unwrap();

for (size, files) in self.big_files.iter().rev() {
for file_entry in files {
writeln!(file, "{} ({}) - {}", size.file_size(options::BINARY).unwrap(), size, file_entry.path.display()).unwrap();
writeln!(writer, "{} ({}) - {}", size.file_size(options::BINARY).unwrap(), size, file_entry.path.display()).unwrap();
}
}
} else {
write!(file, "Not found any files.").unwrap();
write!(writer, "Not found any files.").unwrap();
}
Common::print_time(start_time, SystemTime::now(), "save_results_to_file".to_string());
true
Expand Down
38 changes: 20 additions & 18 deletions czkawka_core/src/duplicate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::common_items::ExcludedItems;
use crate::common_messages::Messages;
use crate::common_traits::*;
use rayon::prelude::*;
use std::io::BufWriter;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::Arc;
use std::thread::sleep;
Expand Down Expand Up @@ -904,16 +905,17 @@ impl SaveResults for DuplicateFinder {
k => k.to_string(),
};

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

if writeln!(
file,
writer,
"Results of searching {:?} with excluded directories {:?} and excluded items {:?}",
self.directories.included_directories, self.directories.excluded_directories, self.excluded_items.items
)
Expand All @@ -925,50 +927,50 @@ impl SaveResults for DuplicateFinder {
match self.check_method {
CheckingMethod::Name => {
if !self.files_with_identical_size.is_empty() {
writeln!(file, "-------------------------------------------------Files with same names-------------------------------------------------").unwrap();
writeln!(writer, "-------------------------------------------------Files with same names-------------------------------------------------").unwrap();
writeln!(
file,
writer,
"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() {
writeln!(file, "Name - {} - {} files ", name, vector.len()).unwrap();
writeln!(writer, "Name - {} - {} files ", name, vector.len()).unwrap();
for j in vector {
writeln!(file, "{}", j.path.display()).unwrap();
writeln!(writer, "{}", j.path.display()).unwrap();
}
writeln!(file).unwrap();
writeln!(writer).unwrap();
}
} else {
write!(file, "Not found any files with same names.").unwrap();
write!(writer, "Not found any files with same names.").unwrap();
}
}
CheckingMethod::Size => {
if !self.files_with_identical_size.is_empty() {
writeln!(file, "-------------------------------------------------Files with same size-------------------------------------------------").unwrap();
writeln!(writer, "-------------------------------------------------Files with same size-------------------------------------------------").unwrap();
writeln!(
file,
writer,
"Found {} duplicated files which in {} groups which takes {}.",
self.information.number_of_duplicated_files_by_size,
self.information.number_of_groups_by_size,
self.information.lost_space_by_size.file_size(options::BINARY).unwrap()
)
.unwrap();
for (size, vector) in self.files_with_identical_size.iter().rev() {
write!(file, "\n---- Size {} ({}) - {} files \n", size.file_size(options::BINARY).unwrap(), size, vector.len()).unwrap();
write!(writer, "\n---- Size {} ({}) - {} files \n", size.file_size(options::BINARY).unwrap(), size, vector.len()).unwrap();
for file_entry in vector {
writeln!(file, "{}", file_entry.path.display()).unwrap();
writeln!(writer, "{}", file_entry.path.display()).unwrap();
}
}
} else {
write!(file, "Not found any duplicates.").unwrap();
write!(writer, "Not found any duplicates.").unwrap();
}
}
CheckingMethod::Hash | CheckingMethod::HashMB => {
if !self.files_with_identical_hashes.is_empty() {
writeln!(file, "-------------------------------------------------Files with same hashes-------------------------------------------------").unwrap();
writeln!(writer, "-------------------------------------------------Files with same hashes-------------------------------------------------").unwrap();
writeln!(
file,
writer,
"Found {} duplicated files which in {} groups which takes {}.",
self.information.number_of_duplicated_files_by_hash,
self.information.number_of_groups_by_hash,
Expand All @@ -977,14 +979,14 @@ impl SaveResults for DuplicateFinder {
.unwrap();
for (size, vectors_vector) in self.files_with_identical_hashes.iter().rev() {
for vector in vectors_vector {
writeln!(file, "\n---- Size {} ({}) - {} files", size.file_size(options::BINARY).unwrap(), size, vector.len()).unwrap();
writeln!(writer, "\n---- Size {} ({}) - {} files", size.file_size(options::BINARY).unwrap(), size, vector.len()).unwrap();
for file_entry in vector {
writeln!(file, "{}", file_entry.path.display()).unwrap();
writeln!(writer, "{}", file_entry.path.display()).unwrap();
}
}
}
} else {
write!(file, "Not found any duplicates.").unwrap();
write!(writer, "Not found any duplicates.").unwrap();
}
}
CheckingMethod::None => {
Expand Down
12 changes: 7 additions & 5 deletions czkawka_core/src/empty_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::common_items::ExcludedItems;
use crate::common_messages::Messages;
use crate::common_traits::*;
use crossbeam_channel::Receiver;
use std::io::BufWriter;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::Arc;
use std::thread::sleep;
Expand Down Expand Up @@ -329,16 +330,17 @@ impl SaveResults for EmptyFiles {
k => k.to_string(),
};

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

if writeln!(
file,
writer,
"Results of searching {:?} with excluded directories {:?} and excluded items {:?}",
self.directories.included_directories, self.directories.excluded_directories, self.excluded_items.items
)
Expand All @@ -349,12 +351,12 @@ impl SaveResults for EmptyFiles {
}

if !self.empty_files.is_empty() {
writeln!(file, "Found {} empty files.", self.information.number_of_empty_files).unwrap();
writeln!(writer, "Found {} empty files.", self.information.number_of_empty_files).unwrap();
for file_entry in self.empty_files.iter() {
writeln!(file, "{}", file_entry.path.display()).unwrap();
writeln!(writer, "{}", file_entry.path.display()).unwrap();
}
} else {
write!(file, "Not found any empty files.").unwrap();
write!(writer, "Not found any empty files.").unwrap();
}
Common::print_time(start_time, SystemTime::now(), "save_results_to_file".to_string());
true
Expand Down
15 changes: 8 additions & 7 deletions czkawka_core/src/empty_folder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::common_traits::{DebugPrint, PrintResults, SaveResults};
use crossbeam_channel::Receiver;
use std::collections::BTreeMap;
use std::fs::{File, Metadata};
use std::io::Write;
use std::io::{BufWriter, Write};
use std::path::PathBuf;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::Arc;
Expand Down Expand Up @@ -333,27 +333,28 @@ impl SaveResults for EmptyFolder {
k => k.to_string(),
};

let mut file = match File::create(&file_name) {
let file_handler = match File::create(&file_name) {
Ok(t) => t,
Err(_) => {
self.text_messages.errors.push("Failed to create file ".to_string() + file_name.as_str());
return false;
}
};
let mut writer = BufWriter::new(file_handler);

if writeln!(file, "Results of searching {:?} with excluded directories {:?}", self.directories.included_directories, self.directories.excluded_directories).is_err() {
if writeln!(writer, "Results of searching {:?} with excluded directories {:?}", self.directories.included_directories, self.directories.excluded_directories).is_err() {
self.text_messages.errors.push("Failed to save results to file ".to_string() + file_name.as_str());
return false;
}

if !self.empty_folder_list.is_empty() {
writeln!(file, "-------------------------------------------------Empty folder list-------------------------------------------------").unwrap();
writeln!(file, "Found {} empty folders", self.information.number_of_empty_folders).unwrap();
writeln!(writer, "-------------------------------------------------Empty folder list-------------------------------------------------").unwrap();
writeln!(writer, "Found {} empty folders", self.information.number_of_empty_folders).unwrap();
for name in self.empty_folder_list.keys() {
writeln!(file, "{}", name.display()).unwrap();
writeln!(writer, "{}", name.display()).unwrap();
}
} else {
write!(file, "Not found any empty folders.").unwrap();
write!(writer, "Not found any empty folders.").unwrap();
}
Common::print_time(start_time, SystemTime::now(), "save_results_to_file".to_string());
true
Expand Down
12 changes: 7 additions & 5 deletions czkawka_core/src/invalid_symlinks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::common_items::ExcludedItems;
use crate::common_messages::Messages;
use crate::common_traits::*;
use crossbeam_channel::Receiver;
use std::io::BufWriter;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::Arc;
use std::thread::sleep;
Expand Down Expand Up @@ -376,16 +377,17 @@ impl SaveResults for InvalidSymlinks {
k => k.to_string(),
};

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

if writeln!(
file,
writer,
"Results of searching {:?} with excluded directories {:?} and excluded items {:?}",
self.directories.included_directories, self.directories.excluded_directories, self.excluded_items.items
)
Expand All @@ -396,10 +398,10 @@ impl SaveResults for InvalidSymlinks {
}

if !self.invalid_symlinks.is_empty() {
writeln!(file, "Found {} invalid symlinks.", self.information.number_of_invalid_symlinks).unwrap();
writeln!(writer, "Found {} invalid symlinks.", self.information.number_of_invalid_symlinks).unwrap();
for file_entry in self.invalid_symlinks.iter() {
writeln!(
file,
writer,
"{}\t\t{}\t\t{}",
file_entry.symlink_path.display(),
file_entry.destination_path.display(),
Expand All @@ -411,7 +413,7 @@ impl SaveResults for InvalidSymlinks {
.unwrap();
}
} else {
write!(file, "Not found any invalid symlinks.").unwrap();
write!(writer, "Not found any invalid symlinks.").unwrap();
}
Common::print_time(start_time, SystemTime::now(), "save_results_to_file".to_string());
true
Expand Down
12 changes: 7 additions & 5 deletions czkawka_core/src/same_music.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use audiotags::Tag;
use crossbeam_channel::Receiver;
use rayon::prelude::*;
use std::collections::HashMap;
use std::io::BufWriter;
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::Arc;
use std::thread::sleep;
Expand Down Expand Up @@ -664,16 +665,17 @@ impl SaveResults for SameMusic {
k => k.to_string(),
};

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

if writeln!(
file,
writer,
"Results of searching {:?} with excluded directories {:?} and excluded items {:?}",
self.directories.included_directories, self.directories.excluded_directories, self.excluded_items.items
)
Expand All @@ -684,12 +686,12 @@ impl SaveResults for SameMusic {
}

if !self.music_entries.is_empty() {
writeln!(file, "Found {} same music files.", self.information.number_of_music_entries).unwrap();
writeln!(writer, "Found {} same music files.", self.information.number_of_music_entries).unwrap();
for file_entry in self.music_entries.iter() {
writeln!(file, "{}", file_entry.path.display()).unwrap();
writeln!(writer, "{}", file_entry.path.display()).unwrap();
}
} else {
write!(file, "Not found any empty files.").unwrap();
write!(writer, "Not found any empty files.").unwrap();
}
Common::print_time(start_time, SystemTime::now(), "save_results_to_file".to_string());
true
Expand Down
Loading

0 comments on commit 9975605

Please sign in to comment.