stop stream
This commit is contained in:
parent
5fad11ec7b
commit
fad33758fe
1 changed files with 13 additions and 19 deletions
|
@ -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 {
|
||||
if metadata.is_file() {
|
||||
fs::remove_file(&path).await?;
|
||||
} else {
|
||||
fs::remove_dir_all(&path).await?;
|
||||
}
|
||||
}
|
||||
Ok(Some(()))
|
||||
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(&entry_path).await?;
|
||||
} else {
|
||||
Ok(None)
|
||||
fs::remove_dir_all(&entry_path).await?;
|
||||
}
|
||||
}
|
||||
})
|
||||
.try_collect::<()>()
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue