-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclean.go
More file actions
782 lines (760 loc) · 26.8 KB
/
Copy pathclean.go
File metadata and controls
782 lines (760 loc) · 26.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
package main
import (
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"strconv"
"strings"
"sync"
)
// Clean levels.
const (
LevelSafe = "safe" // safe to delete, no rebuild cost
LevelModerate = "moderate" // rebuilds on next use, may be slow
LevelCautious = "cautious" // deleting forces re-download or re-login
)
// CleanItem is one rule-driven cleanup target.
type CleanItem struct {
ID string `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
Level string `json:"level"`
Paths []string `json:"paths"` // candidate paths (probed for existence)
Match string `json:"match,omitempty"` // "" | "glob:<pattern>" | "ext:<ext>" | "name:<name>"
MaxAgeDays int `json:"maxAgeDays,omitempty"` // >0: only entries older than N days
RequiresAdmin bool `json:"requiresAdmin"`
Exists bool `json:"exists"`
Size int64 `json:"size"`
FileCount int `json:"fileCount"`
Drive string `json:"drive"` // primary drive of the first existing path; "ALL" for multi-drive items
}
func homeDir() string {
h, _ := os.UserHomeDir()
return h
}
func localAppData() string {
if p := os.Getenv("LOCALAPPDATA"); p != "" {
return p
}
return filepath.Join(homeDir(), "AppData", "Local")
}
func systemRoot() string {
if p := os.Getenv("SystemRoot"); p != "" {
return p
}
return `C:\Windows`
}
// cleanItemDefs returns the full rule set. Rules whose paths don't exist are
// filtered out by CleanupItems.
func (a *App) cleanItemDefs() []CleanItem {
home := homeDir()
local := localAppData()
la := func(rel ...string) string { return filepath.Join(append([]string{local}, rel...)...) }
hp := func(rel ...string) string { return filepath.Join(append([]string{home}, rel...)...) }
win := func(rel ...string) string { return filepath.Join(append([]string{systemRoot()}, rel...)...) }
return []CleanItem{
{
ID: "recycle_bin", Name: "回收站",
Description: "清空所有磁盘的回收站",
Level: LevelSafe, Paths: a.recycleBinPaths(), Drive: "ALL",
},
{
ID: "temp_user", Name: "用户临时文件",
Description: "超过 7 天的 %TEMP% 临时文件",
Level: LevelSafe, Paths: []string{la("Temp")}, MaxAgeDays: 7,
},
{
ID: "temp_user_all", Name: "用户临时文件(全量)",
Description: "清理全部 %TEMP% 内容(正在使用的文件会跳过)",
Level: LevelSafe, Paths: []string{la("Temp")},
},
{
ID: "temp_windows", Name: "Windows 临时文件",
Description: "超过 7 天的 Windows\\Temp",
Level: LevelSafe, Paths: []string{win("Temp")}, MaxAgeDays: 7, RequiresAdmin: true,
},
{
ID: "crash_dumps", Name: "崩溃转储",
Description: "应用崩溃产生的 .dmp 转储文件",
Level: LevelSafe, Paths: []string{la("CrashDumps")}, Match: "ext:.dmp",
},
{
ID: "wer", Name: "Windows 错误报告",
Description: "ProgramData\\Microsoft\\Windows\\WER",
Level: LevelSafe, Paths: []string{filepath.Join(programData(), "Microsoft", "Windows", "WER")}, RequiresAdmin: true,
},
{
ID: "thumb_cache", Name: "缩略图缓存",
Description: "Explorer 缩略图缓存(重启资源管理器后生效)",
Level: LevelSafe, Paths: []string{la("Microsoft", "Windows", "Explorer")}, Match: "glob:thumbcache_*",
},
{
ID: "browser_cache", Name: "浏览器缓存",
Description: "Chrome / Edge 的页面缓存",
Level: LevelSafe,
Paths: []string{
la("Google", "Chrome", "User Data", "Default", "Cache"),
la("Google", "Chrome", "User Data", "Default", "Code Cache"),
la("Microsoft", "Edge", "User Data", "Default", "Cache"),
la("Microsoft", "Edge", "User Data", "Default", "Code Cache"),
},
},
{
ID: "dx_shader", Name: "DirectX 着色器缓存",
Description: "游戏与应用着色器缓存(重建后首次运行稍慢)",
Level: LevelModerate, Paths: []string{la("D3DSCache")},
},
{
ID: "windows_logs", Name: "Windows 日志",
Description: "超过 30 天的 Windows\\Logs",
Level: LevelModerate, Paths: []string{win("Logs")}, MaxAgeDays: 30, RequiresAdmin: true,
},
{
ID: "wu_downloads", Name: "Windows Update 下载缓存",
Description: "已下载的更新安装包(系统会重新下载)",
Level: LevelModerate, Paths: []string{win("SoftwareDistribution", "Download")}, RequiresAdmin: true,
},
{
ID: "delivery_opt", Name: "传递优化缓存",
Description: "Windows 更新点对点传输缓存",
Level: LevelModerate, Paths: []string{win("SoftwareDistribution", "DeliveryOptimization")}, RequiresAdmin: true,
},
{
ID: "prefetch", Name: "预读取缓存",
Description: "Windows\\Prefetch 启动预读",
Level: LevelModerate, Paths: []string{win("Prefetch")}, RequiresAdmin: true,
},
{
ID: "npm_cache", Name: "npm 缓存",
Description: "npm 包缓存(需要时重新下载)",
Level: LevelModerate, Paths: []string{la("npm-cache"), hp(".npm")},
},
{
ID: "pip_cache", Name: "pip 缓存",
Description: "pip 下载缓存(需要时重新下载)",
Level: LevelModerate, Paths: []string{la("pip", "Cache")},
},
{
ID: "go_cache", Name: "Go 模块缓存",
Description: "GOPATH 模块缓存(重新编译时重新下载)",
Level: LevelModerate, Paths: []string{hp("go", "pkg", "mod")},
},
{
ID: "nuget_cache", Name: "NuGet 缓存",
Description: "NuGet 包缓存(重新还原时重新下载)",
Level: LevelModerate, Paths: []string{hp(".nuget", "packages")},
},
{
ID: "codex_data", Name: ".codex 数据",
Description: "Codex CLI 的会话与数据(删除后需重新登录)",
Level: LevelCautious, Paths: []string{hp(".codex"), hp(".codex-ppt-skill")},
},
{
ID: "qoder_data", Name: "Qoder 全家桶数据",
Description: "Qoder CLI / 工作台 / 桌面的本地数据(删除后需重新登录)",
Level: LevelCautious,
Paths: []string{
hp(".qoderworkcn"), hp(".qoderwork"), hp(".qoder-cn"), hp(".qoder-cli"),
filepath.Join(roamingAppData(), "QoderCN"), filepath.Join(roamingAppData(), "QoderWork CN"),
},
},
{
ID: "claude_data", Name: ".claude 数据",
Description: "Claude Code 的会话、配置与插件(删除后需重新登录)",
Level: LevelCautious, Paths: []string{hp(".claude")},
},
{
ID: "claude_cli_node", Name: "Claude CLI Node 运行时",
Description: "claude-cli-nodejs 运行时缓存(删除后自动重新下载)",
Level: LevelModerate, Paths: []string{la("claude-cli-nodejs")},
},
{
ID: "mimocode_data", Name: ".mimocode 数据",
Description: "MimoCode 的本地数据",
Level: LevelCautious, Paths: []string{hp(".mimocode")},
},
{
ID: "cherry_studio", Name: "Cherry Studio 数据",
Description: "Cherry Studio 桌面端缓存与数据(删除后需重新配置)",
Level: LevelCautious,
Paths: []string{hp(".cherrystudio"), filepath.Join(roamingAppData(), "CherryStudio")},
},
{
ID: "kimi_data", Name: "Kimi 数据",
Description: "Kimi 桌面端 / kimi-code / kimi-work 的本地数据",
Level: LevelCautious,
Paths: []string{
filepath.Join(roamingAppData(), "kimi-desktop"), la("kimi-code"),
hp(".kimi-code"), hp(".kimi-webbridge"), hp(".kimi-work"),
},
},
{
ID: "cline_data", Name: "Cline 数据",
Description: "Cline 编程助手的本地数据(含 VS Code 扩展缓存)",
Level: LevelCautious,
Paths: []string{
hp(".cline"), hp(".agents"),
filepath.Join(roamingAppData(), "Code", "User", "globalStorage", "saoudrizwan.claude-dev"),
},
},
{
ID: "copilot_data", Name: "GitHub Copilot 数据",
Description: "Copilot CLI 与 VS Code Copilot 扩展数据",
Level: LevelCautious,
Paths: []string{
hp(".copilot"),
filepath.Join(roamingAppData(), "Code", "User", "globalStorage", "github.copilot-chat"),
},
},
{
ID: "coze_data", Name: "Coze / 扣子数据",
Description: "Coze 桌面端与 CLI 的本地数据(删除后需重新登录)",
Level: LevelCautious,
Paths: []string{hp(".coze"), filepath.Join(roamingAppData(), "Coze")},
},
{
ID: "doubao_data", Name: "豆包数据",
Description: "豆包桌面端与 CLI 的本地数据",
Level: LevelCautious,
Paths: []string{hp(".doubao"), filepath.Join(roamingAppData(), "Doubao")},
},
{
ID: "opencode_data", Name: "OpenCode 数据",
Description: "OpenCode 编程助手的本地数据",
Level: LevelCautious, Paths: []string{hp(".opencode")},
},
{
ID: "cc_switch", Name: "CC Switch 数据",
Description: "Claude Code 配置切换器数据",
Level: LevelCautious,
Paths: []string{
hp(".cc-switch"),
filepath.Join(roamingAppData(), "com.ccswitch.desktop"),
filepath.Join(localAppData(), "com.ccswitch.desktop"),
},
},
{
ID: "arkcli_data", Name: "arkcli 数据",
Description: "火山方舟 arkcli 工具数据",
Level: LevelCautious, Paths: []string{hp(".arkcli")},
},
{
ID: "dreamina_cli", Name: "即梦 AI (Dreamina)",
Description: "即梦 CLI 的本地数据",
Level: LevelCautious, Paths: []string{hp(".dreamina_cli")},
},
{
ID: "openchatcut", Name: "OpenChatCut 数据",
Description: "聊天截图 / 数据处理工具的本地数据",
Level: LevelCautious,
Paths: []string{hp(".openchatcut"), filepath.Join(roamingAppData(), "openchatcut")},
},
{
ID: "openai_desktop", Name: "OpenAI 桌面端数据",
Description: "OpenAI 桌面应用的本地数据",
Level: LevelCautious, Paths: []string{la("OpenAI")},
},
{
ID: "ai_canvas", Name: "AI CanvasPro 数据",
Description: "AI 画布工具的本地数据",
Level: LevelCautious,
Paths: []string{
filepath.Join(roamingAppData(), "AI CanvasPro"),
filepath.Join(localAppData(), "AI-CanvasPro"),
},
},
{
ID: "streamlit_cache", Name: "Streamlit 缓存",
Description: "Streamlit 应用缓存(重新运行自动重建)",
Level: LevelModerate, Paths: []string{hp(".streamlit")},
},
{
ID: "hf_cache", Name: "HuggingFace 模型缓存",
Description: "~/.cache/huggingface 下载的模型与数据集缓存(重新下载)",
Level: LevelModerate, Paths: []string{hp(".cache", "huggingface")},
},
{
ID: "cursor_data", Name: "Cursor 数据",
Description: "Cursor 编辑器的本地数据与索引",
Level: LevelCautious,
Paths: []string{hp(".cursor"), filepath.Join(roamingAppData(), "Cursor")},
},
{
ID: "windsurf_data", Name: "Windsurf / Codeium 数据",
Description: "Windsurf 编辑器与 Codeium 扩展数据",
Level: LevelCautious,
Paths: []string{
hp(".codeium"),
filepath.Join(roamingAppData(), "Windsurf"), filepath.Join(localAppData(), "Windsurf"),
},
},
{
ID: "trae_data", Name: "Trae 数据",
Description: "Trae 编辑器的本地数据",
Level: LevelCautious,
Paths: []string{filepath.Join(roamingAppData(), "Trae"), filepath.Join(localAppData(), "Trae")},
},
{
ID: "jan_data", Name: "Jan 数据",
Description: "Jan 本地 AI 客户端的模型与数据",
Level: LevelCautious,
Paths: []string{hp("jan"), filepath.Join(roamingAppData(), "jan")},
},
{
ID: "lmstudio", Name: "LM Studio 数据",
Description: "LM Studio 的模型与缓存(模型文件很大)",
Level: LevelCautious, Paths: []string{hp(".lmstudio")},
},
{
ID: "ollama", Name: "Ollama 模型",
Description: "Ollama 下载的本地模型(模型文件很大,删除需重新拉取)",
Level: LevelCautious, Paths: []string{hp(".ollama")},
},
{
ID: "chatbox", Name: "Chatbox 数据",
Description: "Chatbox AI 客户端的本地数据",
Level: LevelCautious, Paths: []string{filepath.Join(roamingAppData(), "chatbox")},
},
{
ID: "anythingllm", Name: "AnythingLLM 数据",
Description: "AnythingLLM 的本地知识库与数据",
Level: LevelCautious, Paths: []string{hp(".anythingllm")},
},
{
ID: "lobechat", Name: "LobeChat 数据",
Description: "LobeChat 桌面端本地数据",
Level: LevelCautious, Paths: []string{filepath.Join(roamingAppData(), "LobeChat")},
},
{
ID: "openwebui", Name: "Open WebUI 数据",
Description: "Open WebUI 本地数据",
Level: LevelCautious, Paths: []string{hp(".open-webui")},
},
{
ID: "nextchat", Name: "NextChat 数据",
Description: "NextChat 桌面端本地数据",
Level: LevelCautious, Paths: []string{filepath.Join(roamingAppData(), "NextChat")},
},
{
ID: "zed_data", Name: "Zed 数据",
Description: "Zed 编辑器的本地数据",
Level: LevelCautious, Paths: []string{filepath.Join(roamingAppData(), "Zed")},
},
{
ID: "n8n_data", Name: "n8n 数据",
Description: "n8n 自动化平台的本地数据",
Level: LevelCautious, Paths: []string{hp(".n8n")},
},
{
ID: "ultralytics", Name: "Ultralytics (YOLO)",
Description: "Ultralytics 的配置与设置缓存",
Level: LevelModerate, Paths: []string{filepath.Join(roamingAppData(), "Ultralytics")},
},
{
ID: "continue_ext", Name: "Continue 扩展数据",
Description: "VS Code Continue 扩展的本地数据",
Level: LevelCautious, Paths: []string{filepath.Join(roamingAppData(), "Continue")},
},
{
ID: "windows_old", Name: "Windows.old",
Description: "旧系统备份目录(删除不可恢复)",
Level: LevelCautious, Paths: []string{`C:\Windows.old`}, RequiresAdmin: true,
},
{
ID: "uv_cache", Name: "uv 包缓存",
Description: "Python uv 下载的包缓存(重新安装时重新下载)",
Level: LevelModerate, Paths: []string{la("uv")},
},
{
ID: "playwright_cache", Name: "Playwright 浏览器",
Description: "Playwright 下载的测试用浏览器(需要时重新下载)",
Level: LevelModerate, Paths: []string{la("ms-playwright")},
},
{
ID: "netease_cache", Name: "网易云音乐缓存",
Description: "NetEase 客户端缓存(重新播放时重新缓存)",
Level: LevelModerate, Paths: []string{la("NetEase")},
},
{
ID: "thunder_cache", Name: "迅雷下载缓存",
Description: "Thunder Network 客户端缓存(重新下载时重新缓存)",
Level: LevelModerate, Paths: []string{la("Thunder Network")},
},
{
ID: "douyin_cache", Name: "抖音/DouyinAR 缓存",
Description: "抖音相关应用缓存(重新使用时重建)",
Level: LevelModerate,
Paths: []string{
la("DouyinAR"),
filepath.Join(roamingAppData(), "douyin"),
},
},
{
// Blizzard game caches. Only the Cache/Logs subdirs are cleaned;
// game data dirs (e.g. Overwatch with seasonal/event data, settings)
// are left untouched so users never lose progress or config.
ID: "blizzard_cache", Name: "暴雪/战网缓存",
Description: "战网与暴雪游戏缓存(浏览器缓存/日志/错误报告,重新打开时重建;不删游戏本体与设置)",
Level: LevelModerate,
Paths: []string{
la("Battle.net", "BrowserCaches"),
la("Battle.net", "Cache"),
la("Battle.net", "Errors"),
la("Battle.net", "Logs"),
la("Battle.net", "CachedData.db"),
la("Blizzard Entertainment", "Telemetry"),
},
},
{
// Overwatch (and other Blizzard games) store seasonal/event data
// under %LOCALAPPDATA%\Blizzard Entertainment\<Game>\ as numbered
// folders (each patch/event adds one). We clean the numbered
// event-cache folders plus disposable subdirs, never game settings
// or the game root. Cleaned data re-downloads on next launch.
ID: "blizzard_game_cache", Name: "暴雪游戏活动数据(含守望先锋过往活动)",
Description: "守望先锋等暴雪游戏的过往活动/赛季缓存(数字文件夹,每次活动更新都会累积;删除后重新下载,游戏设置不受影响)",
Level: LevelCautious,
Paths: []string{
la("Blizzard Entertainment", "Overwatch", "Cache"),
la("Blizzard Entertainment", "Overwatch", "Logs"),
la("Blizzard Entertainment", "Overwatch", "Errors"),
la("Blizzard Entertainment", "Overwatch", "Saved"),
la("Blizzard Entertainment", "Diablo IV", "Cache"),
la("Blizzard Entertainment", "Diablo IV", "Logs"),
la("Blizzard Entertainment", "World of Warcraft", "Cache"),
la("Blizzard Entertainment", "World of Warcraft", "Logs"),
la("Blizzard Entertainment", "Hearthstone", "Cache"),
la("Blizzard Entertainment", "Hearthstone", "Logs"),
la("Blizzard Entertainment", "Call of Duty", "Cache"),
la("Blizzard Entertainment", "Call of Duty", "Logs"),
},
},
{
ID: "game_crash_dumps", Name: "游戏崩溃转储",
Description: "游戏与应用崩溃时的内存转储文件(无价值,可安全删除)",
Level: LevelSafe,
Paths: []string{
la("CrashDumps"),
},
},
{
ID: "steam_cache", Name: "Steam 缓存与日志",
Description: "Steam 客户端缓存与日志(游戏本体不受影响)",
Level: LevelModerate,
Paths: []string{
filepath.Join(roamingAppData(), "Steam", "logs"),
filepath.Join(roamingAppData(), "Steam", "htmlcache"),
filepath.Join(roamingAppData(), "Steam", "config", "htmlcache"),
filepath.Join(roamingAppData(), "Steam", "SteamAppData", "shadercache"),
// %LOCALAPPDATA%\Steam\htmlcache is the main cache dir on modern
// installs (415MB on a typical gamer machine).
la("Steam", "htmlcache"),
la("Steam", "logs"),
la("Steam", "config", "htmlcache"),
},
},
{
ID: "riot_cache", Name: "Riot 游戏缓存(LOL/瓦罗兰特)",
Description: "拳头游戏客户端的日志/崩溃报告/HTTP 缓存(游戏本体与配置不受影响)",
Level: LevelModerate,
Paths: []string{
la("Riot Games", "Riot Client", "Logs"),
la("Riot Games", "Riot Client", "Crashes"),
la("Riot Games", "Riot Client", "HttpCache"),
la("Riot Games", "League of Legends", "Logs"),
la("Riot Games", "League of Legends", "Crashes"),
la("Riot Games", "VALORANT", "Logs"),
la("Riot Games", "VALORANT", "Crashes"),
la("Riot Games", "VALORANT", "HttpCache"),
},
},
{
ID: "rockstar_cache", Name: "Rockstar 启动器缓存(GTA5)",
Description: "Rockstar 启动器的崩溃日志与缓存(游戏本体与存档不受影响)",
Level: LevelModerate,
Paths: []string{
la("Rockstar Games", "Launcher", "CrashLogs"),
la("Rockstar Games", "Launcher", "Logs"),
la("Rockstar Games", "Launcher", "Cache"),
la("Rockstar Games", "Launcher", "HttpCache"),
la("Rockstar Games", "GTAV Enhanced", "CrashLogs"),
},
},
{
ID: "epic_cache", Name: "Epic 游戏缓存",
Description: "Epic 启动器缓存与日志(游戏本体不受影响)",
Level: LevelModerate,
Paths: []string{
filepath.Join(roamingAppData(), "Epic", "EpicGamesLauncher", "Logs"),
la("Epic GamesLauncher", "Saved", "Logs"),
},
},
{
ID: "tencent_cache", Name: "腾讯系应用缓存",
Description: "Roaming\\Tencent 下的 QQ/微信等客户端缓存",
Level: LevelCautious, Paths: []string{filepath.Join(roamingAppData(), "Tencent")},
},
{
ID: "code_cache", Name: "VS Code 缓存",
Description: "VS Code 的缓存与日志(重新打开时重建)",
Level: LevelModerate,
Paths: []string{
filepath.Join(roamingAppData(), "Code", "Cache"),
filepath.Join(roamingAppData(), "Code", "CachedData"),
filepath.Join(roamingAppData(), "Code", "logs"),
filepath.Join(roamingAppData(), "Code", "Code Cache"),
},
},
{
ID: "vss_shadows", Name: "系统还原点(卷影副本)",
Description: "删除所有盘的系统还原点(不可恢复,无法移入回收站)",
Level: LevelCautious,
Paths: []string{`C:\System Volume Information`},
RequiresAdmin: true,
},
{
ID: "win_upgrade_residue", Name: "Windows 升级残留",
Description: "$WINDOWS.~BT / $Windows.~WS / ESD 升级残留文件(删除不可恢复)",
Level: LevelCautious,
Paths: []string{
`C:\$WINDOWS.~BT`,
`C:\$Windows.~WS`,
`C:\ESD`,
},
RequiresAdmin: true,
},
}
}
// roamingAppData returns the %APPDATA% directory.
func roamingAppData() string {
if p := os.Getenv("APPDATA"); p != "" {
return p
}
return filepath.Join(homeDir(), "AppData", "Roaming")
}
// recycleBinPaths returns the $Recycle.Bin path of every present fixed drive.
func (a *App) recycleBinPaths() []string {
paths := []string{}
for _, d := range a.GetDrives() {
paths = append(paths, d.Drive+`\$Recycle.Bin`)
}
return paths
}
// CleanupItems probes every cleanup rule and returns the ones that exist on
// this machine, with current sizes. Probing runs in parallel.
func (a *App) CleanupItems() []CleanItem {
defs := a.cleanItemDefs()
var wg sync.WaitGroup
for i := range defs {
wg.Add(1)
go func(idx int) {
defer wg.Done()
a.probeItem(&defs[idx])
}(i)
}
wg.Wait()
out := defs[:0]
for _, d := range defs {
if d.Exists {
out = append(out, d)
}
}
return out
}
// probeItem accumulates size/count for every existing path of a rule.
// Sizes are always computed live (never from the scan snapshot) so the
// cleanup center always reflects the current disk state, even if the
// snapshot is stale or the scan happened long ago.
func (a *App) probeItem(item *CleanItem) {
// System-level items whose primary path is unreadable by design
// (permission-denied), so os.Stat would hide them. Probe them specially.
switch item.ID {
case "vss_shadows":
a.probeVSS(item)
return
case "win_upgrade_residue":
item.Exists = false
for _, p := range item.Paths {
if _, err := os.Stat(p); err != nil {
continue // directory not present (or hidden); nothing to clean
}
item.Exists = true
if item.Drive == "" && len(p) > 1 {
item.Drive = strings.ToUpper(p[:1])
}
// Sizes come from a best-effort walk; permission errors are ignored.
s, fc := walkDirSize(p)
item.Size += s
item.FileCount += fc
}
return
case "blizzard_game_cache":
// Blizzard games (esp. Overwatch) accumulate numbered folders under
// %LOCALAPPDATA%\Blizzard Entertainment\<Game>\ — one per patch/event.
// These are event caches that re-download; settings live elsewhere.
// Also clean the classic Cache/Logs/Errors subdirs when present.
item.Exists = false
games := []string{"Overwatch", "Diablo IV", "World of Warcraft", "Hearthstone", "Call of Duty", "StarCraft II"}
for _, g := range games {
base := filepath.Join(localAppData(), "Blizzard Entertainment", g)
entries, err := os.ReadDir(base)
if err != nil {
continue
}
for _, e := range entries {
if !e.IsDir() {
continue
}
// Numbered folder = event/patch cache; also known cache dirs.
if isNumericDirName(e.Name()) || isBlizzardDisposableDir(e.Name()) {
p := filepath.Join(base, e.Name())
s, fc := walkDirSize(p)
item.Exists = true
item.Size += s
item.FileCount += fc
if item.Drive == "" {
item.Drive = "C"
}
}
}
}
return
}
for _, p := range item.Paths {
info, err := os.Stat(p)
if err != nil {
continue
}
item.Exists = true
if item.Drive == "" && len(p) > 1 {
item.Drive = strings.ToUpper(p[:1])
}
if info.IsDir() {
s, fc := walkDirSize(p)
item.Size += s
item.FileCount += fc
} else {
item.Size += info.Size()
item.FileCount++
}
}
}
// probeVSS fills a vss_shadows item. The System Volume Information directory
// is unreadable without SYSTEM privileges, so we rely on vssadmin (admin) to
// report the storage usage; without admin we still show the item with 0 size
// and let the RequiresAdmin badge explain it.
func (a *App) probeVSS(item *CleanItem) {
item.Exists = false
if _, err := os.Stat(`C:\System Volume Information`); err != nil {
return // no system restore volume at all
}
item.Exists = true
item.Drive = "C"
item.FileCount = 1
// Try vssadmin for a real size (needs elevation). Non-fatal.
if out, err := exec.Command("vssadmin", "list", "shadowstorage").CombinedOutput(); err == nil {
item.Size = parseVSSUsedBytes(string(out))
}
}
// parseVSSUsedBytes extracts the used space from `vssadmin list shadowstorage`
// output. The output is localized, so we match the "used" line and read the
// byte count in its parentheses, e.g.
// zh: "已使用的空间: 5.10 GB (5476081664 字节)"
// en: "Used Space: 1.50 GB (1610612736 bytes)"
// Only the used line is counted (max-space lines carry their own numbers).
func parseVSSUsedBytes(out string) int64 {
lines := strings.Split(out, "\n")
var total int64
for _, ln := range lines {
if !strings.Contains(ln, "(") {
continue
}
if strings.Contains(ln, "已使用的空间") || strings.Contains(ln, "Used Space") {
re := regexp.MustCompile(`\((\d+)\s*(?:字节|bytes)\)`)
if m := re.FindStringSubmatch(ln); len(m) == 2 {
n, _ := strconv.ParseInt(m[1], 10, 64)
total += n
}
}
}
return total
}
// dirSizeCached returns a directory's size from the scan cache, falling back
// to a live walk when the cache has no entry.
func (a *App) dirSizeCached(p string) (int64, int) {
if v, ok := a.scans.sizeMap.Load(p); ok {
di := v.(dirInfo)
return di.size, di.fileCount
}
return walkDirSize(p)
}
// walkDirSize recursively computes the size and file count of a directory
// using bounded concurrency. The semaphore is acquired inside the spawned
// goroutine so the dispatch path never blocks (avoids semaphore deadlock).
func walkDirSize(p string) (int64, int) {
sem := make(chan struct{}, runtime.NumCPU()*2)
var wg sync.WaitGroup
var mu sync.Mutex
var size int64
var files int
var walk func(dir string)
walk = func(dir string) {
entries, err := os.ReadDir(dir)
if err != nil {
return
}
for _, e := range entries {
if e.IsDir() {
sub := filepath.Join(dir, e.Name())
wg.Add(1)
go func() {
defer wg.Done()
sem <- struct{}{}
defer func() { <-sem }()
walk(sub)
}()
continue
}
if info, err := e.Info(); err == nil {
mu.Lock()
size += info.Size()
files++
mu.Unlock()
}
}
}
walk(p)
wg.Wait()
return size, files
}
func programData() string {
if p := os.Getenv("ProgramData"); p != "" {
return p
}
return `C:\ProgramData`
}
// isNumericDirName reports whether name consists only of digits — Blizzard
// games use numbered folders for per-patch/per-event data (e.g. Overwatch's
// "592095225" event cache).
func isNumericDirName(name string) bool {
if name == "" {
return false
}
for _, r := range name {
if r < '0' || r > '9' {
return false
}
}
return true
}
// isBlizzardDisposableDir reports whether a Blizzard game subdirectory is a
// disposable cache/log folder (safe to clean; re-downloads/rebuilds).
func isBlizzardDisposableDir(name string) bool {
switch strings.ToLower(name) {
case "cache", "logs", "errors", "saved", "telemetry", "crash":
return true
}
return false
}