I'm creating a tool to find certain files, and I'm using WalkDir to iterate over every file from root. But when I try to pull the metadata from the file (Using fs::metadata), it says that there is "no such file or directory."
Here's my code:
use std::fs::metadata;
use walkdir::{DirEntry, WalkDir};
fn is_not_hidden(entry: &DirEntry) -> bool {
entry
.file_name()
.to_str()
.map(|s| entry.depth() == 0 || !s.starts_with("."))
.unwrap_or(false)
}
pub fn find() {
WalkDir::new("/")
.into_iter()
.filter_entry(|e| is_not_hidden(e))
.filter_map(|v| v.ok())
.for_each(|x| {
let data = metadata(x.path()).unwrap();
println!("{:?}", data);
});
}
And here's the full error message:
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Os { code: 2, kind: NotFound, message: "No such file or directory" }', src/ezfile.rs:18:41
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Any help would be greatly appreciated :)
metadata
's return value and print the path in case ofErr
? It's possible that you're hitting some special kind of file which isn't expected to be read, like something under/proc
. – Cerberusprintln!("{:?}", x.path())
in yourfor_each
and you should be able to see what the last attempted path was before the panic. – Herohtar