2022-01-02 09:07:20 +13:00
|
|
|
use std::fs;
|
2020-12-22 04:09:39 +13:00
|
|
|
use std::io::prelude::*;
|
2023-12-17 11:21:09 +13:00
|
|
|
use std::path::{Path, PathBuf};
|
2020-12-22 04:09:39 +13:00
|
|
|
|
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;
|
2023-12-17 11:21:09 +13:00
|
|
|
use serde::{Deserialize, Serialize};
|
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
|
|
|
|
2023-12-17 11:21:09 +13:00
|
|
|
const MAX_NUMBER_OF_SYMLINK_JUMPS: i32 = 20;
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
|
|
|
|
pub struct SymlinkInfo {
|
|
|
|
pub destination_path: PathBuf,
|
|
|
|
pub type_of_error: ErrorType,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
|
|
|
pub struct SymlinksFileEntry {
|
|
|
|
pub path: PathBuf,
|
|
|
|
pub size: u64,
|
|
|
|
pub modified_date: u64,
|
|
|
|
pub symlink_info: SymlinkInfo,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ResultEntry for SymlinksFileEntry {
|
|
|
|
fn get_path(&self) -> &Path {
|
|
|
|
&self.path
|
|
|
|
}
|
|
|
|
fn get_modified_date(&self) -> u64 {
|
|
|
|
self.modified_date
|
|
|
|
}
|
|
|
|
fn get_size(&self) -> u64 {
|
|
|
|
self.size
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FileEntry {
|
|
|
|
fn into_symlinks_entry(self, symlink_info: SymlinkInfo) -> SymlinksFileEntry {
|
|
|
|
SymlinksFileEntry {
|
|
|
|
size: self.size,
|
|
|
|
path: self.path,
|
|
|
|
modified_date: self.modified_date,
|
|
|
|
|
|
|
|
symlink_info,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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,
|
2023-12-17 11:21:09 +13:00
|
|
|
invalid_symlinks: Vec<SymlinksFileEntry>,
|
2020-12-22 04:09:39 +13:00
|
|
|
}
|
|
|
|
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-15 04:48:57 +13:00
|
|
|
#[fun_time(message = "find_invalid_links", level = "info")]
|
2023-12-04 00:06:42 +13:00
|
|
|
pub fn find_invalid_links(&mut self, stop_receiver: Option<&Receiver<()>>, progress_sender: Option<&Sender<ProgressData>>) {
|
2024-02-15 05:45:25 +13:00
|
|
|
self.prepare_items();
|
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-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-12-17 11:21:09 +13:00
|
|
|
.common_data(&self.common_data)
|
2022-01-02 09:07:20 +13:00
|
|
|
.group_by(|_fe| ())
|
|
|
|
.stop_receiver(stop_receiver)
|
|
|
|
.progress_sender(progress_sender)
|
|
|
|
.collect(Collect::InvalidSymlinks)
|
|
|
|
.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-12-17 11:21:09 +13:00
|
|
|
self.invalid_symlinks = grouped_file_entries
|
|
|
|
.into_values()
|
|
|
|
.flatten()
|
|
|
|
.filter_map(|e| {
|
|
|
|
let Some((destination_path, type_of_error)) = Self::check_invalid_symlinks(&e.path) else {
|
|
|
|
return None;
|
|
|
|
};
|
|
|
|
Some(e.into_symlinks_entry(SymlinkInfo { destination_path, type_of_error }))
|
|
|
|
})
|
|
|
|
.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::Stopped => false,
|
2023-10-11 07:54:41 +13:00
|
|
|
}
|
2020-12-22 04:09:39 +13:00
|
|
|
}
|
|
|
|
|
2023-12-17 11:21:09 +13:00
|
|
|
fn check_invalid_symlinks(current_file_name: &Path) -> Option<(PathBuf, ErrorType)> {
|
|
|
|
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.to_path_buf();
|
|
|
|
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(_inspected) => {
|
|
|
|
// Looks that some next symlinks are broken, but we do nothing with it - TODO why they are broken
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
number_of_loop += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(_inspected) => {
|
|
|
|
// Failed to load info about it
|
|
|
|
type_of_error = ErrorType::NonExistentFile;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Some((destination_path, type_of_error))
|
|
|
|
}
|
|
|
|
|
2023-10-15 04:48:57 +13:00
|
|
|
#[fun_time(message = "delete_files", level = "debug")]
|
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 {
|
2023-12-17 11:21:09 +13:00
|
|
|
if fs::remove_file(&file_entry.path).is_err() {
|
2023-12-08 07:38:41 +13:00
|
|
|
self.common_data.text_messages.warnings.push(file_entry.path.to_string_lossy().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,
|
2023-12-08 07:38:41 +13:00
|
|
|
"{:?}\t\t{:?}\t\t{}",
|
|
|
|
file_entry.path,
|
2023-12-17 11:21:09 +13:00
|
|
|
file_entry.symlink_info.destination_path,
|
|
|
|
match file_entry.symlink_info.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-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.invalid_symlinks, pretty_print)
|
|
|
|
}
|
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 {
|
2023-12-17 11:21:09 +13:00
|
|
|
pub const fn get_invalid_symlinks(&self) -> &Vec<SymlinksFileEntry> {
|
2023-10-11 07:54:41 +13:00
|
|
|
&self.invalid_symlinks
|
|
|
|
}
|
|
|
|
|
|
|
|
pub const fn get_information(&self) -> &Info {
|
|
|
|
&self.information
|
|
|
|
}
|
|
|
|
}
|