2022-01-02 09:07:20 +13:00
|
|
|
use std::fs;
|
2023-10-11 07:54:41 +13:00
|
|
|
|
2020-12-22 04:09:39 +13:00
|
|
|
use std::io::prelude::*;
|
|
|
|
|
2021-11-28 08:49:20 +13:00
|
|
|
use crossbeam_channel::Receiver;
|
2023-10-11 07:54:41 +13:00
|
|
|
use fun_time::fun_time;
|
2023-05-03 08:37:12 +12:00
|
|
|
use futures::channel::mpsc::UnboundedSender;
|
2023-10-11 07:54:41 +13:00
|
|
|
use log::debug;
|
2021-11-28 08:49:20 +13:00
|
|
|
|
2023-05-11 07:27:41 +12:00
|
|
|
use crate::common_dir_traversal::{Collect, DirTraversalBuilder, DirTraversalResult, ErrorType, FileEntry, ProgressData, ToolType};
|
2023-10-11 07:54:41 +13:00
|
|
|
use crate::common_tool::{CommonData, CommonToolData, DeleteMethod};
|
2020-12-22 04:09:39 +13:00
|
|
|
use crate::common_traits::*;
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
pub struct Info {
|
|
|
|
pub number_of_invalid_symlinks: usize,
|
|
|
|
}
|
2021-11-28 08:57:10 +13:00
|
|
|
|
2020-12-22 04:09:39 +13:00
|
|
|
pub struct InvalidSymlinks {
|
2023-10-05 19:06:47 +13:00
|
|
|
common_data: CommonToolData,
|
2020-12-22 04:09:39 +13:00
|
|
|
information: Info,
|
|
|
|
invalid_symlinks: Vec<FileEntry>,
|
|
|
|
}
|
|
|
|
impl InvalidSymlinks {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
2023-10-05 19:06:47 +13:00
|
|
|
common_data: CommonToolData::new(ToolType::InvalidSymlinks),
|
|
|
|
information: Info::default(),
|
2020-12-22 04:09:39 +13:00
|
|
|
invalid_symlinks: vec![],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-11 07:54:41 +13:00
|
|
|
#[fun_time(message = "find_invalid_links")]
|
2023-05-03 08:37:12 +12:00
|
|
|
pub fn find_invalid_links(&mut self, stop_receiver: Option<&Receiver<()>>, progress_sender: Option<&UnboundedSender<ProgressData>>) {
|
2023-10-05 19:06:47 +13:00
|
|
|
self.optimize_dirs_before_start();
|
2020-12-22 04:09:39 +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-12-22 04:09:39 +13:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
self.delete_files();
|
|
|
|
self.debug_print();
|
|
|
|
}
|
|
|
|
|
2023-10-11 07:54:41 +13:00
|
|
|
#[fun_time(message = "check_files")]
|
2023-05-03 08:37:12 +12:00
|
|
|
fn check_files(&mut self, stop_receiver: Option<&Receiver<()>>, progress_sender: Option<&UnboundedSender<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)
|
|
|
|
.collect(Collect::InvalidSymlinks)
|
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)
|
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.invalid_symlinks = grouped_file_entries.into_values().flatten().collect();
|
2022-01-02 09:07:20 +13:00
|
|
|
self.information.number_of_invalid_symlinks = self.invalid_symlinks.len();
|
2023-10-05 19:06:47 +13:00
|
|
|
self.common_data.text_messages.warnings.extend(warnings);
|
2023-10-11 07:54:41 +13:00
|
|
|
debug!("Found {} invalid symlinks.", self.information.number_of_invalid_symlinks);
|
2022-01-02 09:07:20 +13:00
|
|
|
true
|
2020-12-22 04:09:39 +13:00
|
|
|
}
|
2022-01-02 09:07:20 +13:00
|
|
|
DirTraversalResult::SuccessFolders { .. } => unreachable!(),
|
|
|
|
DirTraversalResult::Stopped => false,
|
2023-10-11 07:54:41 +13:00
|
|
|
}
|
2020-12-22 04:09:39 +13:00
|
|
|
}
|
|
|
|
|
2023-10-11 07:54:41 +13:00
|
|
|
#[fun_time(message = "delete_files")]
|
2020-12-22 04:09:39 +13:00
|
|
|
fn delete_files(&mut self) {
|
2023-10-11 07:54:41 +13:00
|
|
|
match self.common_data.delete_method {
|
2020-12-22 04:09:39 +13:00
|
|
|
DeleteMethod::Delete => {
|
|
|
|
for file_entry in &self.invalid_symlinks {
|
2022-01-02 09:07:20 +13:00
|
|
|
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-12-22 04:09:39 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DeleteMethod::None => {
|
|
|
|
//Just do nothing
|
|
|
|
}
|
2023-10-11 07:54:41 +13:00
|
|
|
_ => unreachable!(),
|
2020-12-22 04:09:39 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-28 08:57:10 +13:00
|
|
|
|
2020-12-22 04:09:39 +13:00
|
|
|
impl Default for InvalidSymlinks {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DebugPrint for InvalidSymlinks {
|
|
|
|
fn debug_print(&self) {
|
2023-10-11 07:54:41 +13:00
|
|
|
if !cfg!(debug_assertions) {
|
2020-12-22 04:09:39 +13:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
println!("---------------DEBUG PRINT---------------");
|
|
|
|
println!("Invalid symlinks list size - {}", self.invalid_symlinks.len());
|
2023-10-05 19:06:47 +13:00
|
|
|
self.debug_print_common();
|
2020-12-22 04:09:39 +13:00
|
|
|
println!("-----------------------------------------");
|
|
|
|
}
|
|
|
|
}
|
2021-11-28 08:57:10 +13:00
|
|
|
|
2023-10-11 07:54:41 +13:00
|
|
|
impl PrintResults for InvalidSymlinks {
|
|
|
|
fn write_results<T: Write>(&self, writer: &mut T) -> std::io::Result<()> {
|
2020-12-22 04:09:39 +13:00
|
|
|
if !self.invalid_symlinks.is_empty() {
|
2023-10-11 07:54:41 +13:00
|
|
|
writeln!(writer, "Found {} invalid symlinks.", self.information.number_of_invalid_symlinks)?;
|
2023-01-29 06:54:02 +13:00
|
|
|
for file_entry in &self.invalid_symlinks {
|
2020-12-22 04:09:39 +13:00
|
|
|
writeln!(
|
2021-01-09 23:52:43 +13:00
|
|
|
writer,
|
2020-12-22 04:09:39 +13:00
|
|
|
"{}\t\t{}\t\t{}",
|
2022-01-02 09:07:20 +13:00
|
|
|
file_entry.path.display(),
|
|
|
|
file_entry.symlink_info.clone().expect("invalid traversal result").destination_path.display(),
|
|
|
|
match file_entry.symlink_info.clone().expect("invalid traversal result").type_of_error {
|
2020-12-22 04:09:39 +13:00
|
|
|
ErrorType::InfiniteRecursion => "Infinite Recursion",
|
|
|
|
ErrorType::NonExistentFile => "Non Existent File",
|
|
|
|
}
|
2023-10-11 07:54:41 +13:00
|
|
|
)?;
|
2020-12-22 04:09:39 +13:00
|
|
|
}
|
|
|
|
} else {
|
2023-10-11 07:54:41 +13:00
|
|
|
write!(writer, "Not found any invalid symlinks.")?;
|
2020-12-22 04:09:39 +13:00
|
|
|
}
|
2021-11-28 08:57:10 +13:00
|
|
|
|
2023-10-11 07:54:41 +13:00
|
|
|
Ok(())
|
2020-12-22 04:09:39 +13:00
|
|
|
}
|
|
|
|
}
|
2023-10-05 19:06:47 +13:00
|
|
|
|
|
|
|
impl CommonData for InvalidSymlinks {
|
|
|
|
fn get_cd(&self) -> &CommonToolData {
|
|
|
|
&self.common_data
|
|
|
|
}
|
|
|
|
fn get_cd_mut(&mut self) -> &mut CommonToolData {
|
|
|
|
&mut self.common_data
|
|
|
|
}
|
|
|
|
}
|
2023-10-11 07:54:41 +13:00
|
|
|
|
|
|
|
impl InvalidSymlinks {
|
|
|
|
pub const fn get_invalid_symlinks(&self) -> &Vec<FileEntry> {
|
|
|
|
&self.invalid_symlinks
|
|
|
|
}
|
|
|
|
|
|
|
|
pub const fn get_information(&self) -> &Info {
|
|
|
|
&self.information
|
|
|
|
}
|
|
|
|
}
|