Remove empty directories when removing previously installed files am: 46459b08bb

Change-Id: Ie68b6b8f37b655fb3e52b2643fd6936007afcd33
This commit is contained in:
Dan Willemsen 2020-02-14 05:05:14 +00:00
commit fa974a0c29

View file

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