diff --git a/src/utils/file.rs b/src/utils/file.rs index f646fbc..911e8fe 100644 --- a/src/utils/file.rs +++ b/src/utils/file.rs @@ -1,7 +1,13 @@ -use std::{fs::File, path::Path}; +use std::{ + fs::File, + path::Path, + process::{Command, Stdio}, +}; use reqwest::{blocking::ClientBuilder, Url}; +use super::stdio::StdioUtils; + pub struct FileUtils; impl FileUtils { @@ -40,20 +46,24 @@ impl FileUtils { /// 递归地复制给定目录下所有文件到另一个文件夹中 pub fn copy_dir_all(src: &Path, dst: &Path) -> Result<(), String> { - if src.is_dir() { - std::fs::create_dir_all(dst).map_err(|e| e.to_string())?; - for entry in std::fs::read_dir(src).map_err(|e| e.to_string())? { - let entry = entry.map_err(|e| e.to_string())?; - let path = entry.path(); - let dst_path = dst.join(path.file_name().unwrap()); - if path.is_dir() { - FileUtils::copy_dir_all(&path, &dst_path).map_err(|e| e.to_string())?; - } else { - std::fs::copy(&path, &dst_path).map_err(|e| e.to_string())?; - } - } - } else { - return Err(format!("No such source directory:{:?}", src)); + let mut cmd = Command::new("cp"); + cmd.arg("-r").arg("./").arg(dst); + + cmd.current_dir(src); + + // 创建子进程,执行命令 + let proc: std::process::Child = cmd + .stderr(Stdio::piped()) + .spawn() + .map_err(|e| e.to_string())?; + let output = proc.wait_with_output().map_err(|e| e.to_string())?; + + if !output.status.success() { + return Err(format!( + "copy_dir_all failed, status: {:?}, stderr: {:?}", + output.status, + StdioUtils::tail_n_str(StdioUtils::stderr_to_lines(&output.stderr), 5) + )); } Ok(()) }