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