file_operation/copy/async/
fn.rs1use crate::*;
2
3pub async fn async_copy_file(src: &str, dest: &str) -> Result<(), Error> {
14 tokio::fs::copy(src, dest).await?;
15 Ok(())
16}
17
18pub async fn async_copy_dir_files(src_dir: &str, dest_dir: &str) -> Result<(), Error> {
29 let src_path: &Path = Path::new(src_dir);
30 let dest_path: &Path = Path::new(dest_dir);
31 if dest_path.exists()
32 && let Some(dest_path_str) = dest_path.to_str()
33 {
34 if dest_path.is_file() {
35 delete_file(dest_path_str)?;
36 }
37 if dest_path.is_dir() {
38 delete_dir(dest_path_str)?;
39 }
40 }
41 tokio::fs::create_dir_all(dest_path).await?;
42 let mut tasks: Vec<JoinHandle<Result<(), Error>>> = Vec::new();
43 let mut read_dir: ReadDir = tokio::fs::read_dir(src_path).await?;
44 while let Some(entry) = read_dir.next_entry().await? {
45 let file_name: OsString = entry.file_name();
46 let src_file_path: PathBuf = entry.path();
47 let mut dest_file_path: PathBuf = PathBuf::from(dest_path);
48 dest_file_path.push(file_name);
49 if src_file_path.is_dir() {
50 let src_file_path_str: String = src_file_path.to_str().unwrap().to_string();
51 let dest_file_path_str: String = dest_file_path.to_str().unwrap().to_string();
52 tasks.push(spawn(async move {
53 async_copy_file(&src_file_path_str, &dest_file_path_str).await
54 }));
55 } else if src_file_path.is_file() {
56 let src_file_path_str: String = src_file_path.to_str().unwrap().to_string();
57 let dest_file_path_str: String = dest_file_path.to_str().unwrap().to_string();
58 tasks.push(spawn(async move {
59 async_copy_file(&src_file_path_str, &dest_file_path_str).await
60 }));
61 }
62 }
63 for task in tasks {
64 task.await??;
65 }
66 Ok(())
67}