我正在做一个文件夹镜像,当我删除一个文件或文件夹,我得到以下错误:
Running `target/debug/back-rust -b /home/michael/Downloads -s /home/michael/Documents/Michael/`
Watching /home/michael/Documents/Michael for changes
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Os { code: 2, kind: NotFound, message: "No such file or directory" }', src/main.rs:115:53
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
字符串
当我试图删除一个文件夹
这是我的代码:utils/remove_file.rs:
use std::fs;
use std::path::{Path, PathBuf};
pub fn remove_file(source_base_path: PathBuf, from_path: PathBuf, to_directory: PathBuf) {
let relative_path = from_path.strip_prefix(&source_base_path).unwrap();
let destination_path = to_directory.join(relative_path);
if destination_path.is_file() {
fs::remove_file(&destination_path).unwrap();
} else if destination_path.is_dir() {
fs::remove_dir_all(&destination_path).unwrap();
}
}
型
从路径可以是一个文件夹或文件,也可以是文件到文件夹到文件夹,但我不知道如何正确删除
main.rs:
if !deleted_files.is_empty() {
for file_path in deleted_files {
remove_file(
source_directory.clone(),
fs::canonicalize(file_path),
backup_directory.clone(),
);
}
}
型
1条答案
按热度按时间uplii1fm1#
这个问题是因为我
canonicalize
删除了一个文件,这使错误,与使path::PathBuf::from(file_path)
的错误已得到修复