Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] is file_count correct ? #39

Open
nesteiner opened this issue Feb 4, 2024 · 0 comments
Open

[Question] is file_count correct ? #39

nesteiner opened this issue Feb 4, 2024 · 0 comments

Comments

@nesteiner
Copy link

hey, I meet a proble, here is a directory vue-frontend, and when calling cargo wipe node in the directory
image

but when I call the find . type f | wc -l
image

the file_counts are not the same at all,

I have some function that modify from your code,

pub fn dir_size(path: impl Into<PathBuf>) -> io::Result<DirInfo> {
    let mut visited = HashSet::new();
    dir_size_0(path, &mut visited)
}

fn dir_size_0(path: impl Into<PathBuf>, visited: &mut HashSet<String>) -> io::Result<DirInfo> {
    let mut dir_info = DirInfo::new(0, 0, 0);

    let targetpath: PathBuf = path.into();
    for entry in WalkDir::new(&targetpath).max_depth(1) {
        let entry = entry?;
        let filepath = entry.path().display().to_string();

        if filepath == "." || filepath == "./" || filepath == targetpath.display().to_string() {
            continue;
        }

        if entry.file_type().is_dir() && !visited.contains(&filepath) {
            visited.insert(filepath.clone());
            let child_dir_info = dir_size_0(&filepath, visited)?;
            dir_info.dir_count += child_dir_info.dir_count + 1;
            dir_info.file_count += child_dir_info.file_count;
            dir_info.size += child_dir_info.size;
        } else {
            let file = File::open(&filepath)?;
            let metadata = file.metadata()?;
            dir_info.file_count += 1;
            dir_info.size += metadata.len() as usize;
        }
        
    }

    Ok(dir_info)
}

and the test resuls is
image

I am a little confused now, what is the matter with my code, or your code ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant