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