Remove empty directories when removing previously installed files

Apparently PackageManager gets confused when the apk disappears, but the
directory still exists.

Test: lunch aosp_arm-eng; m BasicDreams
      <remove BasicDreams from PRODUCT_PACKAGES>
      m nothing
      ls out/target/product/generic/system/app
Change-Id: I486c0ddadde55fad226049d3fb0f09af88117da0
This commit is contained in:
Dan Willemsen 2020-02-13 14:37:15 -08:00
parent 7d8ecd0bc1
commit 46459b08bb

View file

@ -237,6 +237,7 @@ func cleanOldFiles(ctx Context, basePath, file string) {
if fi.IsDir() {
if err := os.Remove(old); err == nil {
ctx.Println("Removed directory that is no longer installed: ", old)
cleanEmptyDirs(ctx, filepath.Dir(old))
} else {
ctx.Println("Failed to remove directory that is no longer installed (%q): %v", old, err)
ctx.Println("It's recommended to run `m installclean`")
@ -244,6 +245,7 @@ func cleanOldFiles(ctx Context, basePath, file string) {
} else {
if err := os.Remove(old); err == nil {
ctx.Println("Removed file that is no longer installed: ", old)
cleanEmptyDirs(ctx, filepath.Dir(old))
} else if !os.IsNotExist(err) {
ctx.Fatalf("Failed to remove file that is no longer installed (%q): %v", old, err)
}
@ -254,3 +256,16 @@ func cleanOldFiles(ctx Context, basePath, file string) {
// Use the new list as the base for the next build
os.Rename(file, oldFile)
}
func cleanEmptyDirs(ctx Context, dir string) {
files, err := ioutil.ReadDir(dir)
if err != nil || len(files) > 0 {
return
}
if err := os.Remove(dir); err == nil {
ctx.Println("Removed directory that is no longer installed: ", dir)
} else {
ctx.Fatalf("Failed to remove directory that is no longer installed (%q): %v", dir, err)
}
cleanEmptyDirs(ctx, filepath.Dir(dir))
}