Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions disk_cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,13 +317,18 @@ 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
}
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
}
Expand Down
74 changes: 74 additions & 0 deletions disk_cleanup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 2 additions & 0 deletions docs/disk-cleanup.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down