subdirs, _ := filepath.Glob(filepath.Join(str.QuoteGlob(dir), "[0-9a-f][0-9a-f]"))
printedErrors := false
if len(subdirs) > 0 {
- if cfg.BuildN || cfg.BuildX {
- sh.ShowCmd("", "rm -r %s", strings.Join(subdirs, " "))
- }
- if !cfg.BuildN {
- for _, d := range subdirs {
- // Only print the first error - there may be many.
- // This also mimics what os.RemoveAll(dir) would do.
- if err := os.RemoveAll(d); err != nil && !printedErrors {
- printedErrors = true
- base.Error(err)
- }
- }
+ if err := sh.RemoveAll(subdirs...); err != nil && !printedErrors {
+ printedErrors = true
+ base.Error(err)
}
}
logFile := filepath.Join(dir, "log.txt")
- if cfg.BuildN || cfg.BuildX {
- sh.ShowCmd("", "rm -f %s", logFile)
- }
- if !cfg.BuildN {
- if err := os.RemoveAll(logFile); err != nil && !printedErrors {
- printedErrors = true
- base.Error(err)
- }
+ if err := sh.RemoveAll(logFile); err != nil && !printedErrors {
+ printedErrors = true
+ base.Error(err)
}
}
}
if cleanFuzzcache {
fuzzDir := cache.Default().FuzzDir()
- if cfg.BuildN || cfg.BuildX {
- sh.ShowCmd("", "rm -rf %s", fuzzDir)
- }
- if !cfg.BuildN {
- if err := os.RemoveAll(fuzzDir); err != nil {
- base.Error(err)
- }
+ if err := sh.RemoveAll(fuzzDir); err != nil {
+ base.Error(err)
}
}
}
if dir.IsDir() {
// TODO: Remove once Makefiles are forgotten.
if cleanDir[name] {
- if cfg.BuildN || cfg.BuildX {
- sh.ShowCmd(p.Dir, "rm -r %s", name)
- if cfg.BuildN {
- continue
- }
- }
- if err := os.RemoveAll(filepath.Join(p.Dir, name)); err != nil {
+ if err := sh.RemoveAll(filepath.Join(p.Dir, name)); err != nil {
base.Error(err)
}
}
// this keeps the intermediate objects from hitting the disk.
func (b *Builder) cleanup(a *Action) {
if !cfg.BuildWork {
- if cfg.BuildX {
- // Don't say we are removing the directory if
- // we never created it.
- if _, err := os.Stat(a.Objdir); err == nil || cfg.BuildN {
- b.Shell(a).ShowCmd("", "rm -r %s", a.Objdir)
+ b.Shell(a).RemoveAll(a.Objdir)
+ }
+}
+
+// RemoveAll is like 'rm -rf'. It attempts to remove all paths even if there's
+// an error, and returns the first error.
+func (sh *Shell) RemoveAll(paths ...string) error {
+ if cfg.BuildN || cfg.BuildX {
+ // Don't say we are removing the directory if we never created it.
+ show := func() bool {
+ for _, path := range paths {
+ if _, ok := sh.mkdirCache.Get(path); ok {
+ return true
+ }
+ if _, err := os.Stat(path); !os.IsNotExist(err) {
+ return true
+ }
}
+ return false
+ }
+ if show() {
+ sh.ShowCmd("", "rm -rf %s", strings.Join(paths, " "))
}
- os.RemoveAll(a.Objdir)
}
+ if cfg.BuildN {
+ return nil
+ }
+
+ var err error
+ for _, path := range paths {
+ if err2 := os.RemoveAll(path); err2 != nil && err == nil {
+ err = err2
+ }
+ }
+ return err
}
// moveOrCopyFile is like 'mv src dst' or 'cp src dst'.