458 lines
18 KiB
Rust
458 lines
18 KiB
Rust
use std::fs::{File, Metadata};
|
|
use std::io::prelude::*;
|
|
use std::path::PathBuf;
|
|
use std::time::{Duration, SystemTime, UNIX_EPOCH};
|
|
use std::{fs, thread};
|
|
|
|
use crate::common::Common;
|
|
use crate::common_directory::Directories;
|
|
use crate::common_extensions::Extensions;
|
|
use crate::common_items::ExcludedItems;
|
|
use crate::common_messages::Messages;
|
|
use crate::common_traits::*;
|
|
use crossbeam_channel::Receiver;
|
|
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
|
|
use std::sync::Arc;
|
|
use std::thread::sleep;
|
|
|
|
#[derive(Debug)]
|
|
pub struct ProgressData {
|
|
pub current_stage: u8,
|
|
pub max_stage: u8,
|
|
pub files_checked: usize,
|
|
}
|
|
|
|
#[derive(Eq, PartialEq, Clone, Debug)]
|
|
pub enum DeleteMethod {
|
|
None,
|
|
Delete,
|
|
}
|
|
|
|
const MAX_NUMBER_OF_SYMLINK_JUMPS: i32 = 20;
|
|
|
|
#[derive(Clone)]
|
|
pub enum ErrorType {
|
|
InfiniteRecursion,
|
|
NonExistentFile,
|
|
}
|
|
|
|
#[derive(Clone)]
|
|
pub struct FileEntry {
|
|
pub symlink_path: PathBuf,
|
|
pub destination_path: PathBuf,
|
|
pub type_of_error: ErrorType,
|
|
pub modified_date: u64,
|
|
}
|
|
|
|
/// Info struck with helpful information's about results
|
|
#[derive(Default)]
|
|
pub struct Info {
|
|
pub number_of_checked_files: usize,
|
|
pub number_of_checked_folders: usize,
|
|
pub number_of_ignored_files: usize,
|
|
pub number_of_ignored_things: usize,
|
|
pub number_of_invalid_symlinks: usize,
|
|
pub number_of_removed_files: usize,
|
|
pub number_of_failed_to_remove_files: usize,
|
|
}
|
|
impl Info {
|
|
pub fn new() -> Self {
|
|
Default::default()
|
|
}
|
|
}
|
|
|
|
/// Struct with required information's to work
|
|
pub struct InvalidSymlinks {
|
|
text_messages: Messages,
|
|
information: Info,
|
|
invalid_symlinks: Vec<FileEntry>,
|
|
directories: Directories,
|
|
allowed_extensions: Extensions,
|
|
excluded_items: ExcludedItems,
|
|
recursive_search: bool,
|
|
delete_method: DeleteMethod,
|
|
stopped_search: bool,
|
|
}
|
|
|
|
impl InvalidSymlinks {
|
|
pub fn new() -> Self {
|
|
Self {
|
|
text_messages: Messages::new(),
|
|
information: Info::new(),
|
|
recursive_search: true,
|
|
allowed_extensions: Extensions::new(),
|
|
directories: Directories::new(),
|
|
excluded_items: ExcludedItems::new(),
|
|
invalid_symlinks: vec![],
|
|
delete_method: DeleteMethod::None,
|
|
stopped_search: false,
|
|
}
|
|
}
|
|
|
|
pub fn find_invalid_links(&mut self, stop_receiver: Option<&Receiver<()>>, progress_sender: Option<&futures::channel::mpsc::Sender<ProgressData>>) {
|
|
self.directories.optimize_directories(self.recursive_search, &mut self.text_messages);
|
|
if !self.check_files(stop_receiver, progress_sender) {
|
|
self.stopped_search = true;
|
|
return;
|
|
}
|
|
self.delete_files();
|
|
self.debug_print();
|
|
}
|
|
|
|
pub fn get_stopped_search(&self) -> bool {
|
|
self.stopped_search
|
|
}
|
|
|
|
pub const fn get_invalid_symlinks(&self) -> &Vec<FileEntry> {
|
|
&self.invalid_symlinks
|
|
}
|
|
|
|
pub const fn get_text_messages(&self) -> &Messages {
|
|
&self.text_messages
|
|
}
|
|
|
|
pub const fn get_information(&self) -> &Info {
|
|
&self.information
|
|
}
|
|
|
|
pub fn set_delete_method(&mut self, delete_method: DeleteMethod) {
|
|
self.delete_method = delete_method;
|
|
}
|
|
|
|
pub fn set_recursive_search(&mut self, recursive_search: bool) {
|
|
self.recursive_search = recursive_search;
|
|
}
|
|
|
|
pub fn set_included_directory(&mut self, included_directory: String) -> bool {
|
|
self.directories.set_included_directory(included_directory, &mut self.text_messages)
|
|
}
|
|
|
|
pub fn set_excluded_directory(&mut self, excluded_directory: String) {
|
|
self.directories.set_excluded_directory(excluded_directory, &mut self.text_messages);
|
|
}
|
|
pub fn set_allowed_extensions(&mut self, allowed_extensions: String) {
|
|
self.allowed_extensions.set_allowed_extensions(allowed_extensions, &mut self.text_messages);
|
|
}
|
|
|
|
pub fn set_excluded_items(&mut self, excluded_items: String) {
|
|
self.excluded_items.set_excluded_items(excluded_items, &mut self.text_messages);
|
|
}
|
|
|
|
/// Check files for any with size == 0
|
|
fn check_files(&mut self, stop_receiver: Option<&Receiver<()>>, progress_sender: Option<&futures::channel::mpsc::Sender<ProgressData>>) -> bool {
|
|
let start_time: SystemTime = SystemTime::now();
|
|
let mut folders_to_check: Vec<PathBuf> = Vec::with_capacity(1024 * 2); // This should be small enough too not see to big difference and big enough to store most of paths without needing to resize vector
|
|
|
|
// Add root folders for finding
|
|
for id in &self.directories.included_directories {
|
|
folders_to_check.push(id.clone());
|
|
}
|
|
self.information.number_of_checked_folders += folders_to_check.len();
|
|
|
|
//// PROGRESS THREAD START
|
|
const LOOP_DURATION: u32 = 200; //in ms
|
|
let progress_thread_run = Arc::new(AtomicBool::new(true));
|
|
|
|
let atomic_file_counter = Arc::new(AtomicUsize::new(0));
|
|
|
|
let progress_thread_handle;
|
|
if let Some(progress_sender) = progress_sender {
|
|
let mut progress_send = progress_sender.clone();
|
|
let progress_thread_run = progress_thread_run.clone();
|
|
let atomic_file_counter = atomic_file_counter.clone();
|
|
progress_thread_handle = thread::spawn(move || loop {
|
|
progress_send
|
|
.try_send(ProgressData {
|
|
current_stage: 0,
|
|
max_stage: 0,
|
|
files_checked: atomic_file_counter.load(Ordering::Relaxed) as usize,
|
|
})
|
|
.unwrap();
|
|
if !progress_thread_run.load(Ordering::Relaxed) {
|
|
break;
|
|
}
|
|
sleep(Duration::from_millis(LOOP_DURATION as u64));
|
|
});
|
|
} else {
|
|
progress_thread_handle = thread::spawn(|| {});
|
|
}
|
|
//// PROGRESS THREAD END
|
|
|
|
while !folders_to_check.is_empty() {
|
|
if stop_receiver.is_some() && stop_receiver.unwrap().try_recv().is_ok() {
|
|
// End thread which send info to gui
|
|
progress_thread_run.store(false, Ordering::Relaxed);
|
|
progress_thread_handle.join().unwrap();
|
|
return false;
|
|
}
|
|
let current_folder = folders_to_check.pop().unwrap();
|
|
|
|
// Read current dir, if permission are denied just go to next
|
|
let read_dir = match fs::read_dir(¤t_folder) {
|
|
Ok(t) => t,
|
|
Err(_) => {
|
|
self.text_messages.warnings.push(format!("Cannot open dir {}", current_folder.display()));
|
|
continue;
|
|
} // Permissions denied
|
|
};
|
|
|
|
// Check every sub folder/file/link etc.
|
|
'dir: for entry in read_dir {
|
|
let entry_data = match entry {
|
|
Ok(t) => t,
|
|
Err(_) => {
|
|
self.text_messages.warnings.push(format!("Cannot read entry in dir {}", current_folder.display()));
|
|
continue;
|
|
} //Permissions denied
|
|
};
|
|
let metadata: Metadata = match entry_data.metadata() {
|
|
Ok(t) => t,
|
|
Err(_) => {
|
|
self.text_messages.warnings.push(format!("Cannot read metadata in dir {}", current_folder.display()));
|
|
continue;
|
|
} //Permissions denied
|
|
};
|
|
if metadata.is_dir() {
|
|
self.information.number_of_checked_folders += 1;
|
|
|
|
if !self.recursive_search {
|
|
continue;
|
|
}
|
|
|
|
let next_folder = current_folder.join(entry_data.file_name());
|
|
if self.directories.is_excluded(&next_folder) || self.excluded_items.is_excluded(&next_folder) {
|
|
continue 'dir;
|
|
}
|
|
|
|
folders_to_check.push(next_folder);
|
|
} else if metadata.is_file() {
|
|
atomic_file_counter.fetch_add(1, Ordering::Relaxed);
|
|
} else if metadata.file_type().is_symlink() {
|
|
atomic_file_counter.fetch_add(1, Ordering::Relaxed);
|
|
let file_name_lowercase: String = match entry_data.file_name().into_string() {
|
|
Ok(t) => t,
|
|
Err(_) => continue,
|
|
}
|
|
.to_lowercase();
|
|
|
|
// Checking allowed extensions
|
|
if !self.allowed_extensions.file_extensions.is_empty() {
|
|
let allowed = self.allowed_extensions.file_extensions.iter().any(|e| file_name_lowercase.ends_with((".".to_string() + e.to_lowercase().as_str()).as_str()));
|
|
if !allowed {
|
|
// Not an allowed extension, ignore it.
|
|
self.information.number_of_ignored_files += 1;
|
|
continue 'dir;
|
|
}
|
|
}
|
|
|
|
// Checking files
|
|
let current_file_name = current_folder.join(entry_data.file_name());
|
|
if self.excluded_items.is_excluded(¤t_file_name) {
|
|
continue 'dir;
|
|
}
|
|
|
|
let mut destination_path = PathBuf::new();
|
|
let type_of_error;
|
|
|
|
match current_file_name.read_link() {
|
|
Ok(t) => {
|
|
destination_path.push(t);
|
|
let mut number_of_loop = 0;
|
|
let mut current_path = current_file_name.clone();
|
|
loop {
|
|
if number_of_loop == 0 && !current_path.exists() {
|
|
type_of_error = ErrorType::NonExistentFile;
|
|
break;
|
|
}
|
|
if number_of_loop == MAX_NUMBER_OF_SYMLINK_JUMPS {
|
|
type_of_error = ErrorType::InfiniteRecursion;
|
|
break;
|
|
}
|
|
|
|
current_path = match current_path.read_link() {
|
|
Ok(t) => t,
|
|
Err(_) => {
|
|
// Looks that some next symlinks are broken, but we do nothing with it
|
|
continue 'dir;
|
|
}
|
|
};
|
|
|
|
number_of_loop += 1;
|
|
}
|
|
}
|
|
Err(_) => {
|
|
// Failed to load info about it
|
|
type_of_error = ErrorType::NonExistentFile;
|
|
}
|
|
}
|
|
|
|
// Creating new file entry
|
|
let fe: FileEntry = FileEntry {
|
|
symlink_path: current_file_name.clone(),
|
|
destination_path,
|
|
type_of_error,
|
|
modified_date: match metadata.modified() {
|
|
Ok(t) => match t.duration_since(UNIX_EPOCH) {
|
|
Ok(d) => d.as_secs(),
|
|
Err(_) => {
|
|
self.text_messages.warnings.push(format!("File {} seems to be modified before Unix Epoch.", current_file_name.display()));
|
|
0
|
|
}
|
|
},
|
|
Err(_) => {
|
|
self.text_messages.warnings.push(format!("Unable to get modification date from file {}", current_file_name.display()));
|
|
continue;
|
|
} // Permissions Denied
|
|
},
|
|
};
|
|
|
|
// Adding files to Vector
|
|
self.invalid_symlinks.push(fe);
|
|
|
|
self.information.number_of_checked_files += 1;
|
|
} else {
|
|
// Not sure what exactly this is
|
|
self.information.number_of_ignored_things += 1;
|
|
}
|
|
}
|
|
}
|
|
self.information.number_of_invalid_symlinks = self.invalid_symlinks.len();
|
|
// End thread which send info to gui
|
|
progress_thread_run.store(false, Ordering::Relaxed);
|
|
progress_thread_handle.join().unwrap();
|
|
|
|
Common::print_time(start_time, SystemTime::now(), "check_files_size".to_string());
|
|
true
|
|
}
|
|
|
|
/// Function to delete files, from filed Vector
|
|
fn delete_files(&mut self) {
|
|
let start_time: SystemTime = SystemTime::now();
|
|
|
|
match self.delete_method {
|
|
DeleteMethod::Delete => {
|
|
for file_entry in &self.invalid_symlinks {
|
|
if fs::remove_file(file_entry.symlink_path.clone()).is_err() {
|
|
self.text_messages.warnings.push(file_entry.symlink_path.display().to_string());
|
|
}
|
|
}
|
|
}
|
|
DeleteMethod::None => {
|
|
//Just do nothing
|
|
}
|
|
}
|
|
|
|
Common::print_time(start_time, SystemTime::now(), "delete_files".to_string());
|
|
}
|
|
}
|
|
impl Default for InvalidSymlinks {
|
|
fn default() -> Self {
|
|
Self::new()
|
|
}
|
|
}
|
|
|
|
impl DebugPrint for InvalidSymlinks {
|
|
#[allow(dead_code)]
|
|
#[allow(unreachable_code)]
|
|
/// Debugging printing - only available on debug build
|
|
fn debug_print(&self) {
|
|
#[cfg(not(debug_assertions))]
|
|
{
|
|
return;
|
|
}
|
|
println!("---------------DEBUG PRINT---------------");
|
|
println!("### Information's");
|
|
|
|
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 checked files - {}", self.information.number_of_checked_files);
|
|
println!("Number of checked folders - {}", self.information.number_of_checked_folders);
|
|
println!("Number of ignored files - {}", self.information.number_of_ignored_files);
|
|
println!("Number of ignored things(like symbolic links) - {}", self.information.number_of_ignored_things);
|
|
println!("Number of removed files - {}", self.information.number_of_removed_files);
|
|
println!("Number of failed to remove files - {}", self.information.number_of_failed_to_remove_files);
|
|
|
|
println!("### Other");
|
|
|
|
println!("Invalid symlinks list size - {}", self.invalid_symlinks.len());
|
|
println!("Allowed extensions - {:?}", self.allowed_extensions.file_extensions);
|
|
println!("Excluded items - {:?}", self.excluded_items.items);
|
|
println!("Included directories - {:?}", self.directories.included_directories);
|
|
println!("Excluded directories - {:?}", self.directories.excluded_directories);
|
|
println!("Recursive search - {}", self.recursive_search.to_string());
|
|
println!("Delete Method - {:?}", self.delete_method);
|
|
println!("-----------------------------------------");
|
|
}
|
|
}
|
|
impl SaveResults for InvalidSymlinks {
|
|
fn save_results_to_file(&mut self, file_name: &str) -> bool {
|
|
let start_time: SystemTime = SystemTime::now();
|
|
let file_name: String = match file_name {
|
|
"" => "results.txt".to_string(),
|
|
k => k.to_string(),
|
|
};
|
|
|
|
let mut file = match File::create(&file_name) {
|
|
Ok(t) => t,
|
|
Err(_) => {
|
|
self.text_messages.errors.push(format!("Failed to create file {}", file_name));
|
|
return false;
|
|
}
|
|
};
|
|
|
|
if writeln!(
|
|
file,
|
|
"Results of searching {:?} with excluded directories {:?} and excluded items {:?}",
|
|
self.directories.included_directories, self.directories.excluded_directories, self.excluded_items.items
|
|
)
|
|
.is_err()
|
|
{
|
|
self.text_messages.errors.push(format!("Failed to save results to file {}", file_name));
|
|
return false;
|
|
}
|
|
|
|
if !self.invalid_symlinks.is_empty() {
|
|
writeln!(file, "Found {} invalid symlinks.", self.information.number_of_invalid_symlinks).unwrap();
|
|
for file_entry in self.invalid_symlinks.iter() {
|
|
writeln!(
|
|
file,
|
|
"{}\t\t{}\t\t{}",
|
|
file_entry.symlink_path.display(),
|
|
file_entry.destination_path.display(),
|
|
match file_entry.type_of_error {
|
|
ErrorType::InfiniteRecursion => "Infinite Recursion",
|
|
ErrorType::NonExistentFile => "Non Existent File",
|
|
}
|
|
)
|
|
.unwrap();
|
|
}
|
|
} else {
|
|
write!(file, "Not found any invalid symlinks.").unwrap();
|
|
}
|
|
Common::print_time(start_time, SystemTime::now(), "save_results_to_file".to_string());
|
|
true
|
|
}
|
|
}
|
|
impl PrintResults for InvalidSymlinks {
|
|
/// Print information's about duplicated entries
|
|
/// Only needed for CLI
|
|
fn print_results(&self) {
|
|
let start_time: SystemTime = SystemTime::now();
|
|
println!("Found {} invalid symlinks.\n", self.information.number_of_invalid_symlinks);
|
|
for file_entry in self.invalid_symlinks.iter() {
|
|
println!(
|
|
"{}\t\t{}\t\t{}",
|
|
file_entry.symlink_path.display(),
|
|
file_entry.destination_path.display(),
|
|
match file_entry.type_of_error {
|
|
ErrorType::InfiniteRecursion => "Infinite Recursion",
|
|
ErrorType::NonExistentFile => "Non Existent File",
|
|
}
|
|
);
|
|
}
|
|
|
|
Common::print_time(start_time, SystemTime::now(), "print_entries".to_string());
|
|
}
|
|
}
|