Skip to content

Commit

Permalink
fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangsan committed Nov 6, 2023
1 parent 4796750 commit 3e9392b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
11 changes: 7 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{
env,
fs::{self, File},
io::{stdin, Read, Write},
io::{stdin, Read, Write, stdout},
path::{Path, PathBuf},
};
pub mod error;
Expand Down Expand Up @@ -44,10 +44,10 @@ where

pub fn confirm() -> bool {
print!("Are you sure? [Y/n] ");
stdout().flush().unwrap();
let mut s: String = String::new();
stdin().read_line(&mut s).unwrap();
s.pop();
return s.eq("Y");
return s.eq("Y\n");
}
pub fn check_exist(f: String) -> Result<String, AppError> {
let to: PathBuf = PathBuf::from(env::var("tc").unwrap()).join(&f);
Expand Down Expand Up @@ -103,8 +103,11 @@ pub fn is_valid_path(p: &str) -> bool {
pub fn get_type(t: &PathBuf) -> String {
if t.is_dir() {
return "directory".to_string();
} else if t.is_file(){
return "file".to_string();
} else {
return "undefined type".to_string();
}
return "file".to_string();
}

pub fn proc_toml() -> Result<String, AppError> {
Expand Down
17 changes: 11 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,14 @@ fn move_to_trash(
continue;
}
if permanently {
let fty = get_type(&target);
match remove_file(&target) {
Ok(_) => {
info_log = format!(
"{} {} permanently deleted {} \"{}\"\n",
now,
&user,
get_type(&target),
fty,
target.display(),
);
}
Expand All @@ -160,7 +161,7 @@ fn move_to_trash(
"{} {} tried to permanently delete {} \"{}\" while an error occured: {} \n",
now,
&user,
get_type(&target),
fty,
target.display(),
e
);
Expand All @@ -171,13 +172,15 @@ fn move_to_trash(
} else {
match check_exist(target.file_name().unwrap().to_string_lossy().into_owned()) {
Ok(n) => {
let fty = get_type(&target);
match rename(&target, to.join(&n)) {
Ok(_) => {
info_log = format!(
"{} {} deleted {} \"{}\" => {}\n",
now,
&user,
get_type(&target),
// get_type(&target),
fty,
target.display(),
&n
);
Expand All @@ -193,7 +196,7 @@ fn move_to_trash(
"{} {} tried to delete {} \"{}\" while an error occured: {} \n",
now,
&user,
get_type(&target),
fty,
target.display(),
e
);
Expand Down Expand Up @@ -236,12 +239,11 @@ fn regret(mut log: &File, now: &str) -> Result<(), AppError> {
match fs::rename(&v[0], &v[1]) {
Ok(_) => {
log_info = format!(
"{} {} undid last opeation\n",
"{} {} undid last opeation successfully\n",
now,
env::var("USER").unwrap_or("default".to_string())
);
log.write_all(log_info.as_bytes())?;
remove_file(&path_last)?;
}
Err(e) => {
match e.kind() {
Expand All @@ -253,6 +255,7 @@ fn regret(mut log: &File, now: &str) -> Result<(), AppError> {
e.kind().to_string()
);
log.write_all(log_info.as_bytes())?;
// 发生NotFound错误,删除.last文件
remove_file(&path_last)?;
}
_ => {
Expand All @@ -269,6 +272,8 @@ fn regret(mut log: &File, now: &str) -> Result<(), AppError> {
}
}
}
// 全部成功恢复,删除.last文件
remove_file(&path_last)?;
Ok(())
}

Expand Down

0 comments on commit 3e9392b

Please sign in to comment.