Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使用cp -r来复制目录 #23

Merged
merged 2 commits into from
Feb 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 25 additions & 15 deletions src/utils/file.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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(())
}
Expand Down
Loading