don't crash on weird directories

This commit is contained in:
Mylloon 2024-12-06 09:57:29 +01:00
parent 5bc8eba61d
commit 2b811359f4
Signed by: Anri
GPG key ID: A82D63DFF8D1317F

View file

@ -30,22 +30,21 @@ impl PartialOrd for FileNode {
} }
} }
#[once(time = 120)] #[once(time = 600)]
pub fn get_filetree( pub fn get_filetree(
initial_dir: &str, initial_dir: &str,
exclusion_list: &[String], exclusion_list: &[String],
exclusion_patterns: &[Regex], exclusion_patterns: &[Regex],
) -> FileNode { ) -> FileNode {
gen_filetree(initial_dir, exclusion_list, exclusion_patterns) gen_filetree(initial_dir, exclusion_list, exclusion_patterns).unwrap()
} }
fn gen_filetree( fn gen_filetree(
dir_path: &str, dir_path: &str,
exclusion_list: &[String], exclusion_list: &[String],
exclusion_patterns: &[Regex], exclusion_patterns: &[Regex],
) -> FileNode { ) -> Result<FileNode, std::io::Error> {
let mut children: Vec<FileNode> = std::fs::read_dir(dir_path) let mut children: Vec<FileNode> = std::fs::read_dir(dir_path)?
.unwrap()
.filter_map(Result::ok) .filter_map(Result::ok)
.filter_map(|entry| { .filter_map(|entry| {
let entry_path = entry.path(); let entry_path = entry.path();
@ -69,10 +68,14 @@ fn gen_filetree(
exclusion_list, exclusion_list,
exclusion_patterns, exclusion_patterns,
); );
if children_of_children.is_dir && children_of_children.children.is_empty() { if let Ok(coc) = children_of_children {
None if coc.is_dir && coc.children.is_empty() {
None
} else {
Some(coc)
}
} else { } else {
Some(children_of_children) None
} }
} }
}) })
@ -80,7 +83,7 @@ fn gen_filetree(
children.sort(); children.sort();
FileNode { Ok(FileNode {
name: Path::new(dir_path) name: Path::new(dir_path)
.file_name() .file_name()
.unwrap() .unwrap()
@ -88,7 +91,7 @@ fn gen_filetree(
.to_string(), .to_string(),
is_dir: true, is_dir: true,
children, children,
} })
} }
pub fn excluded(element: &str, exclusion_list: &[String], exclusion_patterns: &[Regex]) -> bool { pub fn excluded(element: &str, exclusion_list: &[String], exclusion_patterns: &[Regex]) -> bool {