Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 1 | // Copyright 2017 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package build |
| 16 | |
| 17 | import ( |
Dan Willemsen | 1e775d7 | 2020-01-03 13:40:45 -0800 | [diff] [blame] | 18 | "bytes" |
Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 19 | "fmt" |
Rupert Shuttleworth | 755ceb0 | 2021-08-11 09:20:27 -0400 | [diff] [blame] | 20 | "io/fs" |
Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 21 | "io/ioutil" |
| 22 | "os" |
| 23 | "path/filepath" |
Dan Willemsen | 1e775d7 | 2020-01-03 13:40:45 -0800 | [diff] [blame] | 24 | "sort" |
Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 25 | "strings" |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 26 | |
| 27 | "android/soong/ui/metrics" |
Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 28 | ) |
| 29 | |
Rupert Shuttleworth | 1f304e6 | 2020-11-24 14:13:41 +0000 | [diff] [blame] | 30 | // Given a series of glob patterns, remove matching files and directories from the filesystem. |
| 31 | // For example, "malware*" would remove all files and directories in the current directory that begin with "malware". |
Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 32 | func removeGlobs(ctx Context, globs ...string) { |
| 33 | for _, glob := range globs { |
Rupert Shuttleworth | 1f304e6 | 2020-11-24 14:13:41 +0000 | [diff] [blame] | 34 | // Find files and directories that match this glob pattern. |
Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 35 | files, err := filepath.Glob(glob) |
| 36 | if err != nil { |
| 37 | // Only possible error is ErrBadPattern |
| 38 | panic(fmt.Errorf("%q: %s", glob, err)) |
| 39 | } |
| 40 | |
| 41 | for _, file := range files { |
| 42 | err = os.RemoveAll(file) |
| 43 | if err != nil { |
| 44 | ctx.Fatalf("Failed to remove file %q: %v", file, err) |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
Rupert Shuttleworth | 755ceb0 | 2021-08-11 09:20:27 -0400 | [diff] [blame] | 50 | // Based on https://stackoverflow.com/questions/28969455/how-to-properly-instantiate-os-filemode |
| 51 | // Because Go doesn't provide a nice way to set bits on a filemode |
| 52 | const ( |
| 53 | FILEMODE_READ = 04 |
| 54 | FILEMODE_WRITE = 02 |
| 55 | FILEMODE_EXECUTE = 01 |
| 56 | FILEMODE_USER_SHIFT = 6 |
| 57 | FILEMODE_USER_READ = FILEMODE_READ << FILEMODE_USER_SHIFT |
| 58 | FILEMODE_USER_WRITE = FILEMODE_WRITE << FILEMODE_USER_SHIFT |
| 59 | FILEMODE_USER_EXECUTE = FILEMODE_EXECUTE << FILEMODE_USER_SHIFT |
| 60 | ) |
| 61 | |
| 62 | // Ensures that files and directories in the out dir can be deleted. |
| 63 | // For example, Bazen can generate output directories where the write bit isn't set, causing 'm' clean' to fail. |
| 64 | func ensureOutDirRemovable(ctx Context, config Config) { |
| 65 | err := filepath.WalkDir(config.OutDir(), func(path string, d fs.DirEntry, err error) error { |
| 66 | if err != nil { |
| 67 | return err |
| 68 | } |
| 69 | if d.IsDir() { |
| 70 | info, err := d.Info() |
| 71 | if err != nil { |
| 72 | return err |
| 73 | } |
| 74 | // Equivalent to running chmod u+rwx on each directory |
| 75 | newMode := info.Mode() | FILEMODE_USER_READ | FILEMODE_USER_WRITE | FILEMODE_USER_EXECUTE |
| 76 | if err := os.Chmod(path, newMode); err != nil { |
| 77 | return err |
| 78 | } |
| 79 | } |
| 80 | // Continue walking the out dir... |
| 81 | return nil |
| 82 | }) |
| 83 | if err != nil && !os.IsNotExist(err) { |
| 84 | // Display the error, but don't crash. |
| 85 | ctx.Println(err.Error()) |
| 86 | } |
| 87 | } |
| 88 | |
Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 89 | // Remove everything under the out directory. Don't remove the out directory |
| 90 | // itself in case it's a symlink. |
Rupert Shuttleworth | 1f304e6 | 2020-11-24 14:13:41 +0000 | [diff] [blame] | 91 | func clean(ctx Context, config Config) { |
Rupert Shuttleworth | 755ceb0 | 2021-08-11 09:20:27 -0400 | [diff] [blame] | 92 | ensureOutDirRemovable(ctx, config) |
Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 93 | removeGlobs(ctx, filepath.Join(config.OutDir(), "*")) |
| 94 | ctx.Println("Entire build directory removed.") |
| 95 | } |
| 96 | |
Rupert Shuttleworth | 1f304e6 | 2020-11-24 14:13:41 +0000 | [diff] [blame] | 97 | // Remove everything in the data directory. |
| 98 | func dataClean(ctx Context, config Config) { |
Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 99 | removeGlobs(ctx, filepath.Join(config.ProductOut(), "data", "*")) |
Rupert Shuttleworth | 1f304e6 | 2020-11-24 14:13:41 +0000 | [diff] [blame] | 100 | ctx.Println("Entire data directory removed.") |
Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | // installClean deletes all of the installed files -- the intent is to remove |
| 104 | // files that may no longer be installed, either because the user previously |
| 105 | // installed them, or they were previously installed by default but no longer |
| 106 | // are. |
| 107 | // |
| 108 | // This is faster than a full clean, since we're not deleting the |
| 109 | // intermediates. Instead of recompiling, we can just copy the results. |
Rupert Shuttleworth | 1f304e6 | 2020-11-24 14:13:41 +0000 | [diff] [blame] | 110 | func installClean(ctx Context, config Config) { |
| 111 | dataClean(ctx, config) |
Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 112 | |
| 113 | if hostCrossOutPath := config.hostCrossOut(); hostCrossOutPath != "" { |
| 114 | hostCrossOut := func(path string) string { |
| 115 | return filepath.Join(hostCrossOutPath, path) |
| 116 | } |
| 117 | removeGlobs(ctx, |
| 118 | hostCrossOut("bin"), |
| 119 | hostCrossOut("coverage"), |
| 120 | hostCrossOut("lib*"), |
| 121 | hostCrossOut("nativetest*")) |
| 122 | } |
| 123 | |
| 124 | hostOutPath := config.HostOut() |
| 125 | hostOut := func(path string) string { |
| 126 | return filepath.Join(hostOutPath, path) |
| 127 | } |
| 128 | |
Colin Cross | 3e6f67a | 2020-10-09 19:11:22 -0700 | [diff] [blame] | 129 | hostCommonOut := func(path string) string { |
| 130 | return filepath.Join(config.hostOutRoot(), "common", path) |
| 131 | } |
| 132 | |
Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 133 | productOutPath := config.ProductOut() |
| 134 | productOut := func(path string) string { |
| 135 | return filepath.Join(productOutPath, path) |
| 136 | } |
| 137 | |
| 138 | // Host bin, frameworks, and lib* are intentionally omitted, since |
| 139 | // otherwise we'd have to rebuild any generated files created with |
| 140 | // those tools. |
| 141 | removeGlobs(ctx, |
Roland Levillain | e5f9ee5 | 2019-09-11 14:50:08 +0100 | [diff] [blame] | 142 | hostOut("apex"), |
Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 143 | hostOut("obj/NOTICE_FILES"), |
| 144 | hostOut("obj/PACKAGING"), |
| 145 | hostOut("coverage"), |
| 146 | hostOut("cts"), |
| 147 | hostOut("nativetest*"), |
| 148 | hostOut("sdk"), |
| 149 | hostOut("sdk_addon"), |
| 150 | hostOut("testcases"), |
| 151 | hostOut("vts"), |
Dan Shi | 984c129 | 2020-03-18 22:42:00 -0700 | [diff] [blame] | 152 | hostOut("vts10"), |
Dan Shi | 53f1a19 | 2019-11-26 10:02:53 -0800 | [diff] [blame] | 153 | hostOut("vts-core"), |
Colin Cross | 3e6f67a | 2020-10-09 19:11:22 -0700 | [diff] [blame] | 154 | hostCommonOut("obj/PACKAGING"), |
Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 155 | productOut("*.img"), |
Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 156 | productOut("*.zip"), |
Dan Willemsen | a18660d | 2017-06-01 14:23:36 -0700 | [diff] [blame] | 157 | productOut("android-info.txt"), |
Daniel Norman | b8e7f81 | 2020-05-07 16:39:36 -0700 | [diff] [blame] | 158 | productOut("misc_info.txt"), |
Steven Moreland | 0aabb11 | 2019-08-26 11:31:33 -0700 | [diff] [blame] | 159 | productOut("apex"), |
Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 160 | productOut("kernel"), |
Yo Chiang | d813f12 | 2021-01-22 00:16:47 +0800 | [diff] [blame] | 161 | productOut("kernel-*"), |
Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 162 | productOut("data"), |
| 163 | productOut("skin"), |
| 164 | productOut("obj/NOTICE_FILES"), |
| 165 | productOut("obj/PACKAGING"), |
Tom Cherry | 7803a01 | 2018-08-08 13:24:32 -0700 | [diff] [blame] | 166 | productOut("ramdisk"), |
Bowgo Tsai | 5145c2c | 2019-10-08 18:12:37 +0800 | [diff] [blame] | 167 | productOut("debug_ramdisk"), |
Petri Gynther | ac22956 | 2021-03-02 23:44:02 -0800 | [diff] [blame] | 168 | productOut("vendor_ramdisk"), |
Will McVicker | 4cee625 | 2020-03-19 11:57:11 -0700 | [diff] [blame] | 169 | productOut("vendor_debug_ramdisk"), |
Bowgo Tsai | 5145c2c | 2019-10-08 18:12:37 +0800 | [diff] [blame] | 170 | productOut("test_harness_ramdisk"), |
Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 171 | productOut("recovery"), |
| 172 | productOut("root"), |
| 173 | productOut("system"), |
| 174 | productOut("system_other"), |
| 175 | productOut("vendor"), |
Colin Cross | ce4c7cd | 2020-10-14 11:29:16 -0700 | [diff] [blame] | 176 | productOut("vendor_dlkm"), |
Jaekyun Seok | f6307cc | 2018-05-16 12:25:41 +0900 | [diff] [blame] | 177 | productOut("product"), |
Justin Yun | d5f6c82 | 2019-06-25 16:47:17 +0900 | [diff] [blame] | 178 | productOut("system_ext"), |
Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 179 | productOut("oem"), |
| 180 | productOut("obj/FAKE"), |
| 181 | productOut("breakpad"), |
| 182 | productOut("cache"), |
| 183 | productOut("coverage"), |
| 184 | productOut("installer"), |
| 185 | productOut("odm"), |
Colin Cross | ce4c7cd | 2020-10-14 11:29:16 -0700 | [diff] [blame] | 186 | productOut("odm_dlkm"), |
Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 187 | productOut("sysloader"), |
Colin Cross | f7bcd42 | 2021-04-27 19:45:25 -0700 | [diff] [blame] | 188 | productOut("testcases"), |
| 189 | productOut("symbols")) |
Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | // Since products and build variants (unfortunately) shared the same |
| 193 | // PRODUCT_OUT staging directory, things can get out of sync if different |
| 194 | // build configurations are built in the same tree. This function will |
Rupert Shuttleworth | 1f304e6 | 2020-11-24 14:13:41 +0000 | [diff] [blame] | 195 | // notice when the configuration has changed and call installClean to |
Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 196 | // remove the files necessary to keep things consistent. |
| 197 | func installCleanIfNecessary(ctx Context, config Config) { |
| 198 | configFile := config.DevicePreviousProductConfig() |
| 199 | prefix := "PREVIOUS_BUILD_CONFIG := " |
| 200 | suffix := "\n" |
Rupert Shuttleworth | 1f304e6 | 2020-11-24 14:13:41 +0000 | [diff] [blame] | 201 | currentConfig := prefix + config.TargetProduct() + "-" + config.TargetBuildVariant() + suffix |
Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 202 | |
Dan Willemsen | e0879fc | 2017-08-04 15:06:27 -0700 | [diff] [blame] | 203 | ensureDirectoriesExist(ctx, filepath.Dir(configFile)) |
| 204 | |
Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 205 | writeConfig := func() { |
Rupert Shuttleworth | 1f304e6 | 2020-11-24 14:13:41 +0000 | [diff] [blame] | 206 | err := ioutil.WriteFile(configFile, []byte(currentConfig), 0666) // a+rw |
Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 207 | if err != nil { |
| 208 | ctx.Fatalln("Failed to write product config:", err) |
| 209 | } |
| 210 | } |
| 211 | |
Rupert Shuttleworth | 1f304e6 | 2020-11-24 14:13:41 +0000 | [diff] [blame] | 212 | previousConfigBytes, err := ioutil.ReadFile(configFile) |
Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 213 | if err != nil { |
| 214 | if os.IsNotExist(err) { |
Rupert Shuttleworth | 1f304e6 | 2020-11-24 14:13:41 +0000 | [diff] [blame] | 215 | // Just write the new config file, no old config file to worry about. |
Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 216 | writeConfig() |
| 217 | return |
| 218 | } else { |
| 219 | ctx.Fatalln("Failed to read previous product config:", err) |
| 220 | } |
Rupert Shuttleworth | 1f304e6 | 2020-11-24 14:13:41 +0000 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | previousConfig := string(previousConfigBytes) |
| 224 | if previousConfig == currentConfig { |
| 225 | // Same config as before - nothing to clean. |
Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 226 | return |
| 227 | } |
| 228 | |
Rupert Shuttleworth | 1f304e6 | 2020-11-24 14:13:41 +0000 | [diff] [blame] | 229 | if config.Environment().IsEnvTrue("DISABLE_AUTO_INSTALLCLEAN") { |
| 230 | ctx.Println("DISABLE_AUTO_INSTALLCLEAN is set and true; skipping auto-clean. Your tree may be in an inconsistent state.") |
Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 231 | return |
| 232 | } |
| 233 | |
Nan Zhang | 17f2767 | 2018-12-12 16:01:49 -0800 | [diff] [blame] | 234 | ctx.BeginTrace(metrics.PrimaryNinja, "installclean") |
Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 235 | defer ctx.EndTrace() |
| 236 | |
Rupert Shuttleworth | 1f304e6 | 2020-11-24 14:13:41 +0000 | [diff] [blame] | 237 | previousProductAndVariant := strings.TrimPrefix(strings.TrimSuffix(previousConfig, suffix), prefix) |
| 238 | currentProductAndVariant := strings.TrimPrefix(strings.TrimSuffix(currentConfig, suffix), prefix) |
Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 239 | |
Rupert Shuttleworth | 1f304e6 | 2020-11-24 14:13:41 +0000 | [diff] [blame] | 240 | ctx.Printf("Build configuration changed: %q -> %q, forcing installclean\n", previousProductAndVariant, currentProductAndVariant) |
Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 241 | |
Rupert Shuttleworth | 1f304e6 | 2020-11-24 14:13:41 +0000 | [diff] [blame] | 242 | installClean(ctx, config) |
Dan Willemsen | f052f78 | 2017-05-18 15:29:04 -0700 | [diff] [blame] | 243 | |
| 244 | writeConfig() |
| 245 | } |
Dan Willemsen | 1e775d7 | 2020-01-03 13:40:45 -0800 | [diff] [blame] | 246 | |
| 247 | // cleanOldFiles takes an input file (with all paths relative to basePath), and removes files from |
| 248 | // the filesystem if they were removed from the input file since the last execution. |
Rupert Shuttleworth | 1f304e6 | 2020-11-24 14:13:41 +0000 | [diff] [blame] | 249 | func cleanOldFiles(ctx Context, basePath, newFile string) { |
| 250 | newFile = filepath.Join(basePath, newFile) |
| 251 | oldFile := newFile + ".previous" |
Dan Willemsen | 1e775d7 | 2020-01-03 13:40:45 -0800 | [diff] [blame] | 252 | |
Cole Faust | 521e951 | 2021-09-14 15:06:23 -0700 | [diff] [blame] | 253 | if _, err := os.Stat(newFile); os.IsNotExist(err) { |
| 254 | // If the file doesn't exist, assume no installed files exist either |
| 255 | return |
| 256 | } else if err != nil { |
Rupert Shuttleworth | 1f304e6 | 2020-11-24 14:13:41 +0000 | [diff] [blame] | 257 | ctx.Fatalf("Expected %q to be readable", newFile) |
Dan Willemsen | 1e775d7 | 2020-01-03 13:40:45 -0800 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | if _, err := os.Stat(oldFile); os.IsNotExist(err) { |
Rupert Shuttleworth | 1f304e6 | 2020-11-24 14:13:41 +0000 | [diff] [blame] | 261 | if err := os.Rename(newFile, oldFile); err != nil { |
| 262 | ctx.Fatalf("Failed to rename file list (%q->%q): %v", newFile, oldFile, err) |
Dan Willemsen | 1e775d7 | 2020-01-03 13:40:45 -0800 | [diff] [blame] | 263 | } |
| 264 | return |
| 265 | } |
| 266 | |
Rupert Shuttleworth | 1f304e6 | 2020-11-24 14:13:41 +0000 | [diff] [blame] | 267 | var newData, oldData []byte |
| 268 | if data, err := ioutil.ReadFile(newFile); err == nil { |
| 269 | newData = data |
Dan Willemsen | 1e775d7 | 2020-01-03 13:40:45 -0800 | [diff] [blame] | 270 | } else { |
Rupert Shuttleworth | 1f304e6 | 2020-11-24 14:13:41 +0000 | [diff] [blame] | 271 | ctx.Fatalf("Failed to read list of installable files (%q): %v", newFile, err) |
Dan Willemsen | 1e775d7 | 2020-01-03 13:40:45 -0800 | [diff] [blame] | 272 | } |
Rupert Shuttleworth | 1f304e6 | 2020-11-24 14:13:41 +0000 | [diff] [blame] | 273 | if data, err := ioutil.ReadFile(oldFile); err == nil { |
| 274 | oldData = data |
| 275 | } else { |
| 276 | ctx.Fatalf("Failed to read list of installable files (%q): %v", oldFile, err) |
| 277 | } |
| 278 | |
| 279 | // Common case: nothing has changed |
| 280 | if bytes.Equal(newData, oldData) { |
| 281 | return |
| 282 | } |
| 283 | |
| 284 | var newPaths, oldPaths []string |
| 285 | newPaths = strings.Fields(string(newData)) |
| 286 | oldPaths = strings.Fields(string(oldData)) |
Dan Willemsen | 1e775d7 | 2020-01-03 13:40:45 -0800 | [diff] [blame] | 287 | |
| 288 | // These should be mostly sorted by make already, but better make sure Go concurs |
| 289 | sort.Strings(newPaths) |
| 290 | sort.Strings(oldPaths) |
| 291 | |
| 292 | for len(oldPaths) > 0 { |
| 293 | if len(newPaths) > 0 { |
| 294 | if oldPaths[0] == newPaths[0] { |
| 295 | // Same file; continue |
| 296 | newPaths = newPaths[1:] |
| 297 | oldPaths = oldPaths[1:] |
| 298 | continue |
| 299 | } else if oldPaths[0] > newPaths[0] { |
| 300 | // New file; ignore |
| 301 | newPaths = newPaths[1:] |
| 302 | continue |
| 303 | } |
| 304 | } |
Rupert Shuttleworth | 1f304e6 | 2020-11-24 14:13:41 +0000 | [diff] [blame] | 305 | |
Dan Willemsen | 1e775d7 | 2020-01-03 13:40:45 -0800 | [diff] [blame] | 306 | // File only exists in the old list; remove if it exists |
Rupert Shuttleworth | 1f304e6 | 2020-11-24 14:13:41 +0000 | [diff] [blame] | 307 | oldPath := filepath.Join(basePath, oldPaths[0]) |
Dan Willemsen | 1e775d7 | 2020-01-03 13:40:45 -0800 | [diff] [blame] | 308 | oldPaths = oldPaths[1:] |
Rupert Shuttleworth | 1f304e6 | 2020-11-24 14:13:41 +0000 | [diff] [blame] | 309 | |
| 310 | if oldFile, err := os.Stat(oldPath); err == nil { |
| 311 | if oldFile.IsDir() { |
| 312 | if err := os.Remove(oldPath); err == nil { |
| 313 | ctx.Println("Removed directory that is no longer installed: ", oldPath) |
| 314 | cleanEmptyDirs(ctx, filepath.Dir(oldPath)) |
Dan Willemsen | 1e775d7 | 2020-01-03 13:40:45 -0800 | [diff] [blame] | 315 | } else { |
Rupert Shuttleworth | 1f304e6 | 2020-11-24 14:13:41 +0000 | [diff] [blame] | 316 | ctx.Println("Failed to remove directory that is no longer installed (%q): %v", oldPath, err) |
Dan Willemsen | 1e775d7 | 2020-01-03 13:40:45 -0800 | [diff] [blame] | 317 | ctx.Println("It's recommended to run `m installclean`") |
| 318 | } |
| 319 | } else { |
Rupert Shuttleworth | 1f304e6 | 2020-11-24 14:13:41 +0000 | [diff] [blame] | 320 | // Removing a file, not a directory. |
| 321 | if err := os.Remove(oldPath); err == nil { |
| 322 | ctx.Println("Removed file that is no longer installed: ", oldPath) |
| 323 | cleanEmptyDirs(ctx, filepath.Dir(oldPath)) |
Dan Willemsen | 1e775d7 | 2020-01-03 13:40:45 -0800 | [diff] [blame] | 324 | } else if !os.IsNotExist(err) { |
Rupert Shuttleworth | 1f304e6 | 2020-11-24 14:13:41 +0000 | [diff] [blame] | 325 | ctx.Fatalf("Failed to remove file that is no longer installed (%q): %v", oldPath, err) |
Dan Willemsen | 1e775d7 | 2020-01-03 13:40:45 -0800 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | // Use the new list as the base for the next build |
Rupert Shuttleworth | 1f304e6 | 2020-11-24 14:13:41 +0000 | [diff] [blame] | 332 | os.Rename(newFile, oldFile) |
Dan Willemsen | 1e775d7 | 2020-01-03 13:40:45 -0800 | [diff] [blame] | 333 | } |
Dan Willemsen | 46459b0 | 2020-02-13 14:37:15 -0800 | [diff] [blame] | 334 | |
Rupert Shuttleworth | 1f304e6 | 2020-11-24 14:13:41 +0000 | [diff] [blame] | 335 | // cleanEmptyDirs will delete a directory if it contains no files. |
| 336 | // If a deletion occurs, then it also recurses upwards to try and delete empty parent directories. |
Dan Willemsen | 46459b0 | 2020-02-13 14:37:15 -0800 | [diff] [blame] | 337 | func cleanEmptyDirs(ctx Context, dir string) { |
| 338 | files, err := ioutil.ReadDir(dir) |
Rupert Shuttleworth | 1f304e6 | 2020-11-24 14:13:41 +0000 | [diff] [blame] | 339 | if err != nil { |
| 340 | ctx.Println("Could not read directory while trying to clean empty dirs: ", dir) |
Dan Willemsen | 46459b0 | 2020-02-13 14:37:15 -0800 | [diff] [blame] | 341 | return |
| 342 | } |
Rupert Shuttleworth | 1f304e6 | 2020-11-24 14:13:41 +0000 | [diff] [blame] | 343 | if len(files) > 0 { |
| 344 | // Directory is not empty. |
| 345 | return |
Dan Willemsen | 46459b0 | 2020-02-13 14:37:15 -0800 | [diff] [blame] | 346 | } |
Rupert Shuttleworth | 1f304e6 | 2020-11-24 14:13:41 +0000 | [diff] [blame] | 347 | |
| 348 | if err := os.Remove(dir); err == nil { |
| 349 | ctx.Println("Removed empty directory (may no longer be installed?): ", dir) |
| 350 | } else { |
| 351 | ctx.Fatalf("Failed to remove empty directory (which may no longer be installed?) %q: (%v)", dir, err) |
| 352 | } |
| 353 | |
| 354 | // Try and delete empty parent directories too. |
Dan Willemsen | 46459b0 | 2020-02-13 14:37:15 -0800 | [diff] [blame] | 355 | cleanEmptyDirs(ctx, filepath.Dir(dir)) |
| 356 | } |