-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from TC999/dev
合并更改
- Loading branch information
Showing
7 changed files
with
145 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,3 +23,6 @@ Cargo.lock | |
# Added by cargo | ||
|
||
/target | ||
|
||
# 日志文件 | ||
*.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,31 @@ | ||
use std::fs; | ||
use std::path::Path; | ||
use crate::logger; | ||
|
||
pub fn delete_folder(folder_path: &str) -> Result<(), String> { | ||
println!("Attempting to delete folder: {}", folder_path); | ||
println!("尝试删除文件夹: {}", folder_path); | ||
logger::log_info(&format!("尝试删除文件夹: {}", folder_path)); | ||
|
||
let path = Path::new(folder_path); | ||
|
||
if !path.exists() { | ||
return Err("Folder does not exist.".to_string()); | ||
println!("文件夹不存在."); | ||
let error_msg = "文件夹不存在.".to_string(); | ||
logger::log_error(&error_msg); | ||
return Err(error_msg); | ||
} | ||
|
||
if path.is_dir() { | ||
fs::remove_dir_all(path).map_err(|e| e.to_string()) | ||
fs::remove_dir_all(path).map_err(|e| { | ||
println!("删除失败: {}", e); | ||
let error_msg = format!("删除失败: {}", e); | ||
logger::log_error(&error_msg); | ||
error_msg | ||
}) | ||
} else { | ||
Err("The path is not a directory.".to_string()) | ||
println!("路径不是目录."); | ||
let error_msg = "路径不是目录.".to_string(); | ||
logger::log_error(&error_msg); | ||
Err(error_msg) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
use simplelog::{Config, LevelFilter, SimpleLogger, WriteLogger}; | ||
use std::fs::File; | ||
|
||
pub fn init_logger(log_to_file: bool) { | ||
if log_to_file { | ||
let _ = WriteLogger::init( | ||
LevelFilter::Info, | ||
Config::default(), | ||
File::create("appdata_cleaner.log").expect("无法创建日志文件"), | ||
); | ||
} else { | ||
let _ = SimpleLogger::init(LevelFilter::Info, Config::default()); | ||
} | ||
} | ||
|
||
pub fn log_info(message: &str) { | ||
log::info!("{}", message); | ||
} | ||
|
||
pub fn log_error(message: &str) { | ||
log::error!("{}", message); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,62 @@ | ||
use std::sync::mpsc::Sender; | ||
use std::thread; | ||
use std::{fs, path::PathBuf}; | ||
use std::path::Path; | ||
|
||
use dirs_next as dirs; | ||
use std::fs; | ||
use crate::logger; // 引入日志模块 | ||
|
||
pub fn scan_appdata(tx: std::sync::mpsc::Sender<(String, u64)>) { | ||
if let Some(appdata_dir) = dirs::data_dir() { | ||
// 正确使用 std::fs::read_dir 获取文件夹内容 | ||
let entries = | ||
fs::read_dir(appdata_dir).unwrap_or_else(|_| fs::read_dir("/dev/null").unwrap()); | ||
pub fn scan_appdata(tx: Sender<(String, u64)>, folder_type: &str) { | ||
println!("开始扫描 {} 类型的文件夹", folder_type); | ||
// 记录日志 | ||
logger::log_info(&format!("开始扫描 {} 类型的文件夹", folder_type)); | ||
|
||
for entry in entries { | ||
if let Ok(entry) = entry { | ||
let path = entry.path(); | ||
if path.is_dir() { | ||
// 获取文件夹的名称 | ||
let folder_name = path | ||
.file_name() | ||
.and_then(|os_str| os_str.to_str()) | ||
.unwrap_or("<未知文件夹>") | ||
.to_owned(); | ||
// 根据 folder_type 确定要扫描的目录 | ||
let appdata_dir = match folder_type { | ||
"Roaming" => dirs::data_dir(), | ||
"Local" => dirs::cache_dir(), | ||
"LocalLow" => Some(PathBuf::from("C:/Users/Default/AppData/LocalLow")), // 手动设置路径 | ||
_ => None, | ||
}; | ||
|
||
let size = calculate_folder_size(&path); // 计算文件夹大小 | ||
tx.send((folder_name, size)).unwrap(); | ||
// 如果找到有效的目录,开始扫描 | ||
if let Some(appdata_dir) = appdata_dir { | ||
thread::spawn(move || { | ||
if let Ok(entries) = fs::read_dir(&appdata_dir) { | ||
for entry in entries.flatten() { | ||
if let Ok(metadata) = entry.metadata() { | ||
if metadata.is_dir() { | ||
let folder_name = entry.file_name().to_string_lossy().to_string(); | ||
let size = calculate_folder_size(&entry.path()); | ||
// 发送文件夹大小数据 | ||
tx.send((folder_name, size)).unwrap(); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
} | ||
|
||
fn calculate_folder_size(folder: &std::path::Path) -> u64 { | ||
// 计算文件夹的总大小(递归) | ||
fn calculate_folder_size(folder: &Path) -> u64 { | ||
let mut size = 0; | ||
|
||
// 遍历文件夹中的所有条目 | ||
if let Ok(entries) = fs::read_dir(folder) { | ||
for entry in entries.flatten() { | ||
if let Ok(metadata) = entry.metadata() { | ||
size += metadata.len(); | ||
let path = entry.path(); | ||
if path.is_dir() { | ||
// 递归计算子文件夹的大小 | ||
size += calculate_folder_size(&path); | ||
} else if path.is_file() { | ||
// 计算文件大小 | ||
if let Ok(metadata) = entry.metadata() { | ||
size += metadata.len(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
size | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters