Skip to content

Commit

Permalink
Refactor: Compress arguemnts to one object
Browse files Browse the repository at this point in the history
Several arguments were passed around the dirwalker file. Compress them
into a single struct.
  • Loading branch information
bootandy committed Jul 16, 2021
1 parent f6e36ab commit f219a75
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 59 deletions.
83 changes: 28 additions & 55 deletions src/dirwalker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,25 @@ use std::fs::DirEntry;

use crate::platform::get_metadata;

pub fn walk_it(
dirs: HashSet<PathBuf>,
ignore_directories: HashSet<PathBuf>,
allowed_filesystems: HashSet<u64>,
use_apparent_size: bool,
by_filecount: bool,
ignore_hidden: bool,
) -> (Vec<Node>, bool) {
pub struct WalkData {
pub ignore_directories: HashSet<PathBuf>,
pub allowed_filesystems: HashSet<u64>,
pub use_apparent_size: bool,
pub by_filecount: bool,
pub ignore_hidden: bool,
}

pub fn walk_it(dirs: HashSet<PathBuf>, walk_data: WalkData) -> (Vec<Node>, bool) {
let permissions_flag = AtomicBool::new(false);

let top_level_nodes: Vec<_> = dirs
.into_iter()
.filter_map(|d| {
let n = walk(
d,
&permissions_flag,
&ignore_directories,
&allowed_filesystems,
use_apparent_size,
by_filecount,
ignore_hidden,
);
let n = walk(d, &permissions_flag, &walk_data);
match n {
Some(n) => {
let mut inodes: HashSet<(u64, u64)> = HashSet::new();
clean_inodes(n, &mut inodes, use_apparent_size)
clean_inodes(n, &mut inodes, walk_data.use_apparent_size)
}
None => None,
}
Expand Down Expand Up @@ -78,36 +71,23 @@ fn clean_inodes(
});
}

fn ignore_file(
entry: &DirEntry,
ignore_hidden: bool,
ignore_directories: &HashSet<PathBuf>,
allowed_filesystems: &HashSet<u64>,
) -> bool {
fn ignore_file(entry: &DirEntry, walk_data: &WalkData) -> bool {
let is_dot_file = entry.file_name().to_str().unwrap_or("").starts_with('.');
let is_ignored_path = ignore_directories.contains(&entry.path());
let is_ignored_path = walk_data.ignore_directories.contains(&entry.path());

if !allowed_filesystems.is_empty() {
if !walk_data.allowed_filesystems.is_empty() {
let size_inode_device = get_metadata(&entry.path(), false);

if let Some((_size, Some((_id, dev)))) = size_inode_device {
if !allowed_filesystems.contains(&dev) {
if !walk_data.allowed_filesystems.contains(&dev) {
return true;
}
}
}
(is_dot_file && ignore_hidden) || is_ignored_path
(is_dot_file && walk_data.ignore_hidden) || is_ignored_path
}

fn walk(
dir: PathBuf,
permissions_flag: &AtomicBool,
ignore_directories: &HashSet<PathBuf>,
allowed_filesystems: &HashSet<u64>,
use_apparent_size: bool,
by_filecount: bool,
ignore_hidden: bool,
) -> Option<Node> {
fn walk(dir: PathBuf, permissions_flag: &AtomicBool, walk_data: &WalkData) -> Option<Node> {
let mut children = vec![];

if let Ok(entries) = fs::read_dir(dir.clone()) {
Expand All @@ -122,30 +102,17 @@ fn walk(

// return walk(entry.path(), permissions_flag, ignore_directories, allowed_filesystems, use_apparent_size, by_filecount, ignore_hidden);

if !ignore_file(
&entry,
ignore_hidden,
&ignore_directories,
&allowed_filesystems,
) {
if !ignore_file(&entry, walk_data) {
if let Ok(data) = entry.file_type() {
if data.is_dir() && !data.is_symlink() {
return walk(
entry.path(),
permissions_flag,
ignore_directories,
allowed_filesystems,
use_apparent_size,
by_filecount,
ignore_hidden,
);
return walk(entry.path(), permissions_flag, walk_data);
}
return build_node(
entry.path(),
vec![],
use_apparent_size,
walk_data.use_apparent_size,
data.is_symlink(),
by_filecount,
walk_data.by_filecount,
);
}
}
Expand All @@ -158,7 +125,13 @@ fn walk(
} else {
permissions_flag.store(true, atomic::Ordering::Relaxed);
}
build_node(dir, children, use_apparent_size, false, by_filecount)
build_node(
dir,
children,
walk_data.use_apparent_size,
false,
walk_data.by_filecount,
)
}

mod tests {
Expand Down
10 changes: 6 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::collections::HashSet;
use self::display::draw_it;
use clap::{App, AppSettings, Arg};
use dirwalker::walk_it;
use dirwalker::WalkData;
use filter::{get_biggest, get_by_depth};
use std::cmp::max;
use std::path::PathBuf;
Expand Down Expand Up @@ -214,14 +215,15 @@ fn main() {
.flat_map(|x| simplified_dirs.iter().map(move |d| d.join(x.clone())))
.collect();

let (nodes, errors) = walk_it(
simplified_dirs,
ignored_full_path,
let walk_data = WalkData {
ignore_directories: ignored_full_path,
allowed_filesystems,
use_apparent_size,
by_filecount,
ignore_hidden,
);
};

let (nodes, errors) = walk_it(simplified_dirs, walk_data);

let tree = {
match depth {
Expand Down

0 comments on commit f219a75

Please sign in to comment.