|
1 | | -use std::{fs::File, path::Path}; |
| 1 | +use std::{ |
| 2 | + fs::File, |
| 3 | + path::Path, |
| 4 | + process::{Command, Stdio}, |
| 5 | +}; |
2 | 6 |
|
3 | 7 | use reqwest::{blocking::ClientBuilder, Url}; |
4 | 8 |
|
| 9 | +use super::stdio::StdioUtils; |
| 10 | + |
5 | 11 | pub struct FileUtils; |
6 | 12 |
|
7 | 13 | impl FileUtils { |
@@ -40,20 +46,24 @@ impl FileUtils { |
40 | 46 |
|
41 | 47 | /// 递归地复制给定目录下所有文件到另一个文件夹中 |
42 | 48 | pub fn copy_dir_all(src: &Path, dst: &Path) -> Result<(), String> { |
43 | | - if src.is_dir() { |
44 | | - std::fs::create_dir_all(dst).map_err(|e| e.to_string())?; |
45 | | - for entry in std::fs::read_dir(src).map_err(|e| e.to_string())? { |
46 | | - let entry = entry.map_err(|e| e.to_string())?; |
47 | | - let path = entry.path(); |
48 | | - let dst_path = dst.join(path.file_name().unwrap()); |
49 | | - if path.is_dir() { |
50 | | - FileUtils::copy_dir_all(&path, &dst_path).map_err(|e| e.to_string())?; |
51 | | - } else { |
52 | | - std::fs::copy(&path, &dst_path).map_err(|e| e.to_string())?; |
53 | | - } |
54 | | - } |
55 | | - } else { |
56 | | - return Err(format!("No such source directory:{:?}", src)); |
| 49 | + let mut cmd = Command::new("cp"); |
| 50 | + cmd.arg("-r").arg("./").arg(dst); |
| 51 | + |
| 52 | + cmd.current_dir(src); |
| 53 | + |
| 54 | + // 创建子进程,执行命令 |
| 55 | + let proc: std::process::Child = cmd |
| 56 | + .stderr(Stdio::piped()) |
| 57 | + .spawn() |
| 58 | + .map_err(|e| e.to_string())?; |
| 59 | + let output = proc.wait_with_output().map_err(|e| e.to_string())?; |
| 60 | + |
| 61 | + if !output.status.success() { |
| 62 | + return Err(format!( |
| 63 | + "copy_dir_all failed, status: {:?}, stderr: {:?}", |
| 64 | + output.status, |
| 65 | + StdioUtils::tail_n_str(StdioUtils::stderr_to_lines(&output.stderr), 5) |
| 66 | + )); |
57 | 67 | } |
58 | 68 | Ok(()) |
59 | 69 | } |
|
0 commit comments