Skip to content

Commit ed7403b

Browse files
committed
detect mac temp caches as child dirs
1 parent 30d5f75 commit ed7403b

2 files changed

Lines changed: 200 additions & 8 deletions

File tree

crates/null-e-core/src/cleaners/system.rs

Lines changed: 100 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,72 @@ impl SystemCleaner {
4848
Ok(items)
4949
}
5050

51+
#[cfg(target_os = "macos")]
52+
fn detect_macos_child_dirs(
53+
&self,
54+
root: &std::path::Path,
55+
label_prefix: &str,
56+
subcategory: &str,
57+
icon: &'static str,
58+
description: &'static str,
59+
safe_to_delete: SafetyLevel,
60+
min_size: u64,
61+
) -> Result<Vec<CleanableItem>> {
62+
let mut items = Vec::new();
63+
64+
if !root.exists() {
65+
return Ok(items);
66+
}
67+
68+
let Ok(entries) = std::fs::read_dir(root) else {
69+
return Ok(items);
70+
};
71+
72+
for entry in entries.filter_map(|entry| entry.ok()) {
73+
let path = entry.path();
74+
if !path.is_dir() || std::fs::read_dir(&path).is_err() {
75+
continue;
76+
}
77+
78+
let (size, file_count) = calculate_dir_size(&path)?;
79+
if size < min_size {
80+
continue;
81+
}
82+
83+
let name = path
84+
.file_name()
85+
.map(|n| n.to_string_lossy().to_string())
86+
.unwrap_or_else(|| "unknown".to_string());
87+
88+
items.push(CleanableItem {
89+
name: format!("{label_prefix} ({name})"),
90+
category: "System".to_string(),
91+
subcategory: subcategory.to_string(),
92+
icon,
93+
path: path.clone(),
94+
size,
95+
file_count: Some(file_count),
96+
last_modified: get_mtime(&path),
97+
description: Cow::Borrowed(description),
98+
safe_to_delete,
99+
clean_command: None,
100+
});
101+
}
102+
103+
Ok(items)
104+
}
105+
106+
#[cfg(target_os = "macos")]
107+
fn macos_var_folders_dir(name: &str) -> Option<PathBuf> {
108+
let mut dir = std::env::temp_dir();
109+
if dir.file_name()?.to_str()? != "T" {
110+
return None;
111+
}
112+
dir.pop();
113+
dir.push(name);
114+
Some(dir)
115+
}
116+
51117
/// Detect Trash contents
52118
fn detect_trash(&self) -> Result<Vec<CleanableItem>> {
53119
let mut items = Vec::new();
@@ -202,10 +268,28 @@ impl SystemCleaner {
202268

203269
// Temp locations
204270
#[cfg(target_os = "macos")]
205-
let temp_paths = vec![
206-
self.home.join("Library/Caches/TemporaryItems"),
207-
std::env::temp_dir(),
208-
];
271+
{
272+
items.extend(self.detect_macos_child_dirs(
273+
&self.home.join("Library/Caches/TemporaryItems"),
274+
"Temp Files",
275+
"Temp",
276+
"🔥",
277+
"Temporary files. May contain files in use.",
278+
SafetyLevel::Caution,
279+
100_000_000,
280+
)?);
281+
items.extend(self.detect_macos_child_dirs(
282+
&std::env::temp_dir(),
283+
"Temp Files",
284+
"Temp",
285+
"🔥",
286+
"Temporary files. May contain files in use.",
287+
SafetyLevel::Caution,
288+
100_000_000,
289+
)?);
290+
291+
return Ok(items);
292+
}
209293

210294
#[cfg(target_os = "linux")]
211295
let temp_paths = vec![
@@ -336,6 +420,18 @@ impl SystemCleaner {
336420
}
337421
}
338422

423+
if let Some(var_folders_cache) = Self::macos_var_folders_dir("C") {
424+
items.extend(self.detect_macos_child_dirs(
425+
&var_folders_cache,
426+
"System Cache",
427+
"Caches",
428+
"🗄️",
429+
"Per-user macOS cache. Apps may recreate it.",
430+
SafetyLevel::SafeWithCost,
431+
100_000_000,
432+
)?);
433+
}
434+
339435
// Font caches
340436
let font_caches = [
341437
self.home.join("Library/Caches/com.apple.FontRegistry"),

src/cleaners/system.rs

Lines changed: 100 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,72 @@ impl SystemCleaner {
4848
Ok(items)
4949
}
5050

51+
#[cfg(target_os = "macos")]
52+
fn detect_macos_child_dirs(
53+
&self,
54+
root: &std::path::Path,
55+
label_prefix: &str,
56+
subcategory: &str,
57+
icon: &'static str,
58+
description: &'static str,
59+
safe_to_delete: SafetyLevel,
60+
min_size: u64,
61+
) -> Result<Vec<CleanableItem>> {
62+
let mut items = Vec::new();
63+
64+
if !root.exists() {
65+
return Ok(items);
66+
}
67+
68+
let Ok(entries) = std::fs::read_dir(root) else {
69+
return Ok(items);
70+
};
71+
72+
for entry in entries.filter_map(|entry| entry.ok()) {
73+
let path = entry.path();
74+
if !path.is_dir() || std::fs::read_dir(&path).is_err() {
75+
continue;
76+
}
77+
78+
let (size, file_count) = calculate_dir_size(&path)?;
79+
if size < min_size {
80+
continue;
81+
}
82+
83+
let name = path
84+
.file_name()
85+
.map(|n| n.to_string_lossy().to_string())
86+
.unwrap_or_else(|| "unknown".to_string());
87+
88+
items.push(CleanableItem {
89+
name: format!("{label_prefix} ({name})"),
90+
category: "System".to_string(),
91+
subcategory: subcategory.to_string(),
92+
icon,
93+
path: path.clone(),
94+
size,
95+
file_count: Some(file_count),
96+
last_modified: get_mtime(&path),
97+
description: Cow::Borrowed(description),
98+
safe_to_delete,
99+
clean_command: None,
100+
});
101+
}
102+
103+
Ok(items)
104+
}
105+
106+
#[cfg(target_os = "macos")]
107+
fn macos_var_folders_dir(name: &str) -> Option<PathBuf> {
108+
let mut dir = std::env::temp_dir();
109+
if dir.file_name()?.to_str()? != "T" {
110+
return None;
111+
}
112+
dir.pop();
113+
dir.push(name);
114+
Some(dir)
115+
}
116+
51117
/// Detect Trash contents
52118
fn detect_trash(&self) -> Result<Vec<CleanableItem>> {
53119
let mut items = Vec::new();
@@ -202,10 +268,28 @@ impl SystemCleaner {
202268

203269
// Temp locations
204270
#[cfg(target_os = "macos")]
205-
let temp_paths = vec![
206-
self.home.join("Library/Caches/TemporaryItems"),
207-
std::env::temp_dir(),
208-
];
271+
{
272+
items.extend(self.detect_macos_child_dirs(
273+
&self.home.join("Library/Caches/TemporaryItems"),
274+
"Temp Files",
275+
"Temp",
276+
"🔥",
277+
"Temporary files. May contain files in use.",
278+
SafetyLevel::Caution,
279+
100_000_000,
280+
)?);
281+
items.extend(self.detect_macos_child_dirs(
282+
&std::env::temp_dir(),
283+
"Temp Files",
284+
"Temp",
285+
"🔥",
286+
"Temporary files. May contain files in use.",
287+
SafetyLevel::Caution,
288+
100_000_000,
289+
)?);
290+
291+
return Ok(items);
292+
}
209293

210294
#[cfg(target_os = "linux")]
211295
let temp_paths = vec![
@@ -336,6 +420,18 @@ impl SystemCleaner {
336420
}
337421
}
338422

423+
if let Some(var_folders_cache) = Self::macos_var_folders_dir("C") {
424+
items.extend(self.detect_macos_child_dirs(
425+
&var_folders_cache,
426+
"System Cache",
427+
"Caches",
428+
"🗄️",
429+
"Per-user macOS cache. Apps may recreate it.",
430+
SafetyLevel::SafeWithCost,
431+
100_000_000,
432+
)?);
433+
}
434+
339435
// Font caches
340436
let font_caches = [
341437
self.home.join("Library/Caches/com.apple.FontRegistry"),

0 commit comments

Comments
 (0)