-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move traversal and hashing out of main file
- Loading branch information
1 parent
07c5e54
commit 747f276
Showing
4 changed files
with
75 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
use crate::file_utils::FileInfo; | ||
use crate::file_utils::{calculate_hash, get_file_size}; | ||
use ignore::WalkBuilder; | ||
|
||
pub struct DirectoryTraversalOutput { | ||
pub file_infos: Vec<FileInfo>, | ||
pub dir_count: usize, | ||
pub max_depth: usize, | ||
} | ||
|
||
pub fn traverse_directory(dir: &str) -> DirectoryTraversalOutput { | ||
let mut dir_count = 1; | ||
let mut max_depth = 0; | ||
let file_infos: Vec<FileInfo> = WalkBuilder::new(dir) | ||
.hidden(true) | ||
.ignore(true) | ||
.git_ignore(true) | ||
.git_global(true) | ||
.git_exclude(true) | ||
.build() | ||
.filter_map(|e| e.ok()) | ||
.filter_map(|entry| { | ||
let depth = entry.depth(); | ||
if depth > max_depth { | ||
max_depth = depth; | ||
} | ||
if entry.file_type().map_or(false, |ft| ft.is_dir()) { | ||
dir_count += 1; | ||
} | ||
if entry.file_type().map_or(false, |ft| ft.is_file()) { | ||
let path = entry.into_path(); | ||
let size = get_file_size(&path); | ||
let hash = if size > 3 * 1024 * 1024 { | ||
// 3MB in bytes | ||
match calculate_hash(&path) { | ||
Ok(hash) => hash, | ||
Err(_) => String::new(), | ||
} | ||
} else { | ||
String::new() | ||
}; | ||
Some(FileInfo { path, size, hash }) | ||
} else { | ||
None | ||
} | ||
}) | ||
.collect(); | ||
|
||
DirectoryTraversalOutput { | ||
file_infos, | ||
dir_count, | ||
max_depth, | ||
} | ||
} |