ruvector_memopt/windows/
performance.rs1use std::process::Command;
4use std::path::PathBuf;
5
6pub fn cleanup_temp_files() -> Result<String, String> {
8 let temp_dirs = [
9 std::env::var("TEMP").unwrap_or_default(),
10 std::env::var("TMP").unwrap_or_default(),
11 ];
12
13 let mut total_freed = 0u64;
14 let mut files_deleted = 0u32;
15
16 for dir in &temp_dirs {
17 if dir.is_empty() { continue; }
18 if let Ok(entries) = std::fs::read_dir(dir) {
19 for entry in entries.flatten() {
20 if let Ok(metadata) = entry.metadata() {
21 let size = metadata.len();
22 if std::fs::remove_file(entry.path()).is_ok() ||
23 std::fs::remove_dir_all(entry.path()).is_ok() {
24 total_freed += size;
25 files_deleted += 1;
26 }
27 }
28 }
29 }
30 }
31
32 Ok(format!("Deleted {} items\nFreed: {:.1} MB",
33 files_deleted, total_freed as f64 / 1024.0 / 1024.0))
34}
35
36pub fn flush_dns() -> Result<String, String> {
38 let output = Command::new("ipconfig")
39 .args(["/flushdns"])
40 .output()
41 .map_err(|e| e.to_string())?;
42
43 if output.status.success() {
44 Ok("DNS cache flushed".to_string())
45 } else {
46 Err("Failed to flush DNS".to_string())
47 }
48}
49
50pub fn set_high_performance() -> Result<String, String> {
52 let output = Command::new("powercfg")
53 .args(["/setactive", "8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c"])
54 .output()
55 .map_err(|e| e.to_string())?;
56
57 if output.status.success() {
58 Ok("High Performance mode activated".to_string())
59 } else {
60 Err("Need admin rights for power plan".to_string())
61 }
62}
63
64pub fn cleanup_thumbnails() -> Result<String, String> {
66 let mut thumb_path = PathBuf::from(std::env::var("USERPROFILE").unwrap_or_default());
67 thumb_path.push("AppData");
68 thumb_path.push("Local");
69 thumb_path.push("Microsoft");
70 thumb_path.push("Windows");
71 thumb_path.push("Explorer");
72
73 let mut deleted = 0u32;
74
75 if let Ok(entries) = std::fs::read_dir(&thumb_path) {
76 for entry in entries.flatten() {
77 let name = entry.file_name().to_string_lossy().to_string();
78 if name.starts_with("thumbcache_") {
79 if std::fs::remove_file(entry.path()).is_ok() {
80 deleted += 1;
81 }
82 }
83 }
84 }
85
86 Ok(format!("Cleared {} thumbnail caches", deleted))
87}