From a38385f3b5033a7757e000af5ecd83874bdfba42 Mon Sep 17 00:00:00 2001 From: Igor Katson Date: Fri, 13 Sep 2024 13:04:37 +0100 Subject: [PATCH] use walkdir create in another place instead of custom code --- crates/librqbit/src/create_torrent_file.rs | 29 ++++++---------------- 1 file changed, 7 insertions(+), 22 deletions(-) diff --git a/crates/librqbit/src/create_torrent_file.rs b/crates/librqbit/src/create_torrent_file.rs index dcd6e58..caecaef 100644 --- a/crates/librqbit/src/create_torrent_file.rs +++ b/crates/librqbit/src/create_torrent_file.rs @@ -20,28 +20,13 @@ pub struct CreateTorrentOptions<'a> { } fn walk_dir_find_paths(dir: &Path, out: &mut Vec>) -> anyhow::Result<()> { - let mut stack = vec![Cow::Borrowed(dir)]; - while let Some(dir) = stack.pop() { - let rd = std::fs::read_dir(&dir).with_context(|| format!("error reading {:?}", dir))?; - for element in rd { - let element = - element.with_context(|| format!("error reading DirEntry from {:?}", dir))?; - let ft = element.file_type().with_context(|| { - format!( - "error determining filetype of DirEntry {:?} while reading {:?}", - element.file_name(), - dir - ) - })?; - - let full_path = Cow::Owned(dir.join(element.file_name())); - if ft.is_dir() { - stack.push(full_path); - } else { - out.push(full_path); - } - } - } + out.extend( + walkdir::WalkDir::new(dir) + .into_iter() + .filter_map(|e| e.ok()) + .filter(|e| e.file_type().is_file()) + .map(|e| e.path().to_owned().into()), + ); Ok(()) }