fix many things
This commit is contained in:
@@ -201,6 +201,81 @@ fn get_dir_stats(path: &Path, filter_days: Option<u64>) -> (u64, u32) {
|
||||
(size, count)
|
||||
}
|
||||
|
||||
pub async fn run_fast_clean(_is_simulation: bool) -> Result<String, String> {
|
||||
Ok("快速清理任务已成功模拟执行。".into())
|
||||
#[derive(Serialize)]
|
||||
pub struct CleanResult {
|
||||
pub total_freed: String,
|
||||
pub success_count: u32,
|
||||
pub fail_count: u32,
|
||||
}
|
||||
|
||||
pub async fn run_fast_clean(is_simulation: bool) -> Result<CleanResult, String> {
|
||||
if is_simulation {
|
||||
return Ok(CleanResult {
|
||||
total_freed: "0 B".into(),
|
||||
success_count: 0,
|
||||
fail_count: 0,
|
||||
});
|
||||
}
|
||||
|
||||
let mut success_count = 0;
|
||||
let mut fail_count = 0;
|
||||
let mut total_freed: u64 = 0;
|
||||
|
||||
let mut target_paths = Vec::new();
|
||||
if let Ok(t) = std::env::var("TEMP") { target_paths.push(t); }
|
||||
target_paths.push("C:\\Windows\\Temp".into());
|
||||
target_paths.push("C:\\Windows\\SoftwareDistribution\\Download".into());
|
||||
target_paths.push("C:\\Windows\\ServiceProfiles\\NetworkService\\AppData\\Local\\Microsoft\\Windows\\DeliveryOptimization".into());
|
||||
|
||||
for path_str in target_paths {
|
||||
let path = Path::new(&path_str);
|
||||
if path.exists() {
|
||||
let (freed, s, f) = clean_directory_contents(path);
|
||||
total_freed += freed;
|
||||
success_count += s;
|
||||
fail_count += f;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(CleanResult {
|
||||
total_freed: format_size(total_freed),
|
||||
success_count,
|
||||
fail_count,
|
||||
})
|
||||
}
|
||||
|
||||
fn clean_directory_contents(path: &Path) -> (u64, u32, u32) {
|
||||
let mut freed = 0;
|
||||
let mut success = 0;
|
||||
let mut fail = 0;
|
||||
|
||||
if let Ok(entries) = fs::read_dir(path) {
|
||||
for entry in entries.filter_map(|e| e.ok()) {
|
||||
let entry_path = entry.path();
|
||||
let metadata = entry.metadata();
|
||||
let size = metadata.as_ref().map(|m| m.len()).unwrap_or(0);
|
||||
|
||||
if entry_path.is_file() {
|
||||
if fs::remove_file(&entry_path).is_ok() {
|
||||
freed += size;
|
||||
success += 1;
|
||||
} else {
|
||||
fail += 1;
|
||||
}
|
||||
} else if entry_path.is_dir() {
|
||||
// 递归清理子目录
|
||||
let (f, s, fl) = clean_directory_contents(&entry_path);
|
||||
freed += f;
|
||||
success += s;
|
||||
fail += fl;
|
||||
// 尝试删除已清空的目录
|
||||
if fs::remove_dir(&entry_path).is_ok() {
|
||||
success += 1;
|
||||
} else {
|
||||
fail += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
(freed, success, fail)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user