opendal/services/fs/
delete.rs1use super::core::*;
19use crate::raw::*;
20use crate::*;
21use std::sync::Arc;
22
23pub struct FsDeleter {
24 core: Arc<FsCore>,
25}
26
27impl FsDeleter {
28 pub fn new(core: Arc<FsCore>) -> Self {
29 Self { core }
30 }
31}
32
33impl oio::OneShotDelete for FsDeleter {
34 async fn delete_once(&self, path: String, _: OpDelete) -> Result<()> {
35 let p = self.core.root.join(path.trim_end_matches('/'));
36
37 let meta = tokio::fs::metadata(&p).await;
38
39 match meta {
40 Ok(meta) => {
41 if meta.is_dir() {
42 tokio::fs::remove_dir(&p).await.map_err(new_std_io_error)?;
43 } else {
44 tokio::fs::remove_file(&p).await.map_err(new_std_io_error)?;
45 }
46
47 Ok(())
48 }
49 Err(err) if err.kind() == std::io::ErrorKind::NotFound => Ok(()),
50 Err(err) => Err(new_std_io_error(err)),
51 }
52 }
53}
54
55impl oio::BlockingOneShotDelete for FsDeleter {
56 fn blocking_delete_once(&self, path: String, _: OpDelete) -> Result<()> {
57 let p = self.core.root.join(path.trim_end_matches('/'));
58
59 let meta = std::fs::metadata(&p);
60
61 match meta {
62 Ok(meta) => {
63 if meta.is_dir() {
64 std::fs::remove_dir(&p).map_err(new_std_io_error)?;
65 } else {
66 std::fs::remove_file(&p).map_err(new_std_io_error)?;
67 }
68
69 Ok(())
70 }
71 Err(err) if err.kind() == std::io::ErrorKind::NotFound => Ok(()),
72 Err(err) => Err(new_std_io_error(err)),
73 }
74 }
75}