From 1b2721022800111007e2bbee04e73d94c769202f Mon Sep 17 00:00:00 2001 From: sunjinshuai <9484484+sunjinshuai@users.noreply.github.com> Date: Mon, 26 Oct 2026 11:12:04 +0800 Subject: [PATCH] fix: preserve installed bundles during disk cleanup --- disk_cleanup.go | 5 +++ disk_cleanup_test.go | 74 ++++++++++++++++++++++++++++++++++++++++++++ docs/disk-cleanup.md | 2 ++ 3 files changed, 81 insertions(+) diff --git a/disk_cleanup.go b/disk_cleanup.go index f27b899..a88c812 100644 --- a/disk_cleanup.go +++ b/disk_cleanup.go @@ -317,6 +317,7 @@ func diskCleanupTargets(dataDirectory, categoryID string) ([]string, error) { func findNamedDirectories(root, name string, excludedAncestors map[string]bool) ([]string, error) { var targets []string + bundleDirectory := filepath.Join(root, "Containers", "Bundle") err := filepath.WalkDir(root, func(current string, entry os.DirEntry, walkErr error) error { if walkErr != nil { return walkErr @@ -324,6 +325,10 @@ func findNamedDirectories(root, name string, excludedAncestors map[string]bool) if !entry.IsDir() || current == root { return nil } + // Packaged resources are durable even when named Caches, Logs, or tmp. + if current == bundleDirectory { + return filepath.SkipDir + } if isProtectedCleanupSubtree(entry.Name()) { return filepath.SkipDir } diff --git a/disk_cleanup_test.go b/disk_cleanup_test.go index f071560..4e1e3bd 100644 --- a/disk_cleanup_test.go +++ b/disk_cleanup_test.go @@ -142,6 +142,80 @@ func TestDiskCleanupPlanAndCleanStayInsideAllowlist(t *testing.T) { } } +func TestDiskCleanupPreservesInstalledBundles(t *testing.T) { + for _, test := range []struct { + categoryID string + name string + }{ + {"caches", "Caches"}, + {"logs", "Logs"}, + {"temporary", "tmp"}, + } { + t.Run(test.categoryID, func(t *testing.T) { + dataDirectory := filepath.Join(t.TempDir(), "data") + bundleDirectory := filepath.Join(dataDirectory, "Containers", "Bundle", "Application") + appDirectory := filepath.Join(bundleDirectory, "APP", "Example.app") + var resources []string + for _, directory := range []string{ + appDirectory, + filepath.Join(appDirectory, "PlugIns", "Example.appex"), + filepath.Join(appDirectory, "Frameworks", "Example.framework"), + } { + resource := filepath.Join(directory, test.name, "keep.bin") + writeTestFile(t, resource, 4096) + resources = append(resources, resource) + } + cleanupDirectory := filepath.Join(dataDirectory, "Containers", "Data", "Application", "DATA", "Library", test.name) + if test.categoryID == "temporary" { + cleanupDirectory = filepath.Join(dataDirectory, "Containers", "Data", "Application", "DATA", "tmp") + } + cleanupFile := filepath.Join(cleanupDirectory, "remove.bin") + writeTestFile(t, cleanupFile, 8192) + cleanupBytes, err := allocatedSize(cleanupDirectory) + if err != nil { + t.Fatal(err) + } + bundleBytes, err := allocatedSize(bundleDirectory) + if err != nil { + t.Fatal(err) + } + + plan, err := diskCleanupPlanAt("TEST-UDID", dataDirectory) + if err != nil { + t.Fatal(err) + } + if plan.CleanableBytes != cleanupBytes { + t.Errorf("cleanable bytes = %d, want only disposable data: %d", plan.CleanableBytes, cleanupBytes) + } + for _, category := range plan.Categories { + if category.ID == test.categoryID && category.Targets != 1 { + t.Errorf("cleanup targets = %d, want only the app data directory", category.Targets) + } + } + for _, storage := range plan.Storage { + if storage.ID == "installed-apps" && storage.Bytes != bundleBytes { + t.Errorf("installed app bytes = %d, want %d", storage.Bytes, bundleBytes) + } + } + + if _, err := cleanDiskAt("TEST-UDID", dataDirectory, []string{test.categoryID}); err != nil { + t.Fatal(err) + } + for _, resource := range resources { + if _, err := os.Stat(resource); err != nil { + t.Errorf("cleanup touched installed bundle resource %s: %v", resource, err) + } + } + if _, err := os.Stat(cleanupFile); !os.IsNotExist(err) { + t.Errorf("disposable file was not removed: %v", err) + } + if info, err := os.Stat(cleanupDirectory); err != nil || !info.IsDir() { + t.Errorf("cleanup must preserve the disposable directory: %v", err) + } + }) + } +} + func TestDiskCleanupRejectsIntermediateSymlink(t *testing.T) { root := t.TempDir() dataDirectory := filepath.Join(root, "data") diff --git a/docs/disk-cleanup.md b/docs/disk-cleanup.md index 29c35c8..a7b2dfc 100644 --- a/docs/disk-cleanup.md +++ b/docs/disk-cleanup.md @@ -115,6 +115,8 @@ allowing runtime paths into a cleanup plan. - Name-based discovery skips Documents, media, and MobileAsset trees so a user-created folder named `Caches`, `Logs`, or `tmp` is never enough to make durable content eligible for cleanup. +- Installed bundle storage (`Containers/Bundle`) is skipped entirely, including + packaged resources named `Caches`, `Logs`, or `tmp` in apps, extensions, and frameworks. - Downloaded language cleanup uses one exact MobileAsset catalog path; required Siri, speech, and voice catalogs are rejected by selection validation. - Only directory contents are removed; required container directories remain.