stop stream

This commit is contained in:
Borodinov Ilya 2024-05-13 22:43:12 +03:00
parent 5fad11ec7b
commit fad33758fe
Signed by: noth
GPG key ID: 75503B2EF596D1BD

View file

@ -1,7 +1,6 @@
use std::path::Path;
use crate::ResultExt;
use futures::TryStreamExt;
use tokio::fs;
use tokio_stream::{wrappers::ReadDirStream, StreamExt};
@ -10,25 +9,20 @@ pub async fn remove_matching<F>(dir: &Path, predicate: F) -> std::io::Result<()>
where
F: Fn(&Path) -> bool,
{
ReadDirStream::new(fs::read_dir(dir).await?)
.try_filter_map(move |entry| {
let predicate = |e| predicate(e);
async move {
let path = entry.path();
if predicate(path.as_path()) {
if let Ok(metadata) = fs::metadata(&path).await {
let mut entries = fs::read_dir(dir).await?;
while let Some(entry) = entries.next_entry().await? {
let entry_path = entry.path();
if predicate(entry_path.as_path()) {
if let Ok(metadata) = fs::metadata(&entry_path).await {
if metadata.is_file() {
fs::remove_file(&path).await?;
fs::remove_file(&entry_path).await?;
} else {
fs::remove_dir_all(&path).await?;
fs::remove_dir_all(&entry_path).await?;
}
}
Ok(Some(()))
} else {
Ok(None)
}
}
})
.try_collect::<()>()
.await
Ok(())
}