1
0
mirror of https://github.com/golang/go synced 2024-11-16 19:24:49 -07:00

cmd/go: add Shell.RemoveAll

There are quite a few places that perform their own command logging
and then use os.RemoveAll. Unify (nearly) all of these into
(*Shell).RemoveAll, like many of the other internal implementations of
basic shell operations.

Change-Id: I94a2cbd9dc150a4c94a4051c42ce8e86dcc736fd
Reviewed-on: https://go-review.googlesource.com/c/go/+/536099
Reviewed-by: Bryan Mills <bcmills@google.com>
Auto-Submit: Austin Clements <austin@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
Austin Clements 2023-10-17 15:05:32 -04:00 committed by Gopher Robot
parent 4c31dfe850
commit 5bddb52a0b
4 changed files with 44 additions and 48 deletions

View File

@ -162,33 +162,19 @@ func runClean(ctx context.Context, cmd *base.Command, args []string) {
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 {
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 {
if err := sh.RemoveAll(logFile); err != nil && !printedErrors {
printedErrors = true
base.Error(err)
}
}
}
}
if cleanTestcache && !cleanCache {
// Instead of walking through the entire cache looking for test results,
@ -236,15 +222,10 @@ func runClean(ctx context.Context, cmd *base.Command, args []string) {
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 {
if err := sh.RemoveAll(fuzzDir); err != nil {
base.Error(err)
}
}
}
}
var cleaned = map[*load.Package]bool{}
@ -363,13 +344,7 @@ func clean(p *load.Package) {
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)
}
}

View File

@ -2041,10 +2041,7 @@ func builderCleanTest(b *work.Builder, ctx context.Context, a *work.Action) erro
if cfg.BuildWork {
return nil
}
if cfg.BuildX {
b.Shell(a).ShowCmd("", "rm -r %s", a.Objdir)
}
os.RemoveAll(a.Objdir)
b.Shell(a).RemoveAll(a.Objdir)
return nil
}

View File

@ -1858,15 +1858,41 @@ var AllowInstall = func(*Action) error { return nil }
// 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
}
}
os.RemoveAll(a.Objdir)
return false
}
if show() {
sh.ShowCmd("", "rm -rf %s", strings.Join(paths, " "))
}
}
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'.

View File

@ -15,8 +15,6 @@ import (
//
// Shell tracks context related to running commands, and form a tree much like
// context.Context.
//
// TODO: Add a RemoveAll method. "rm -rf" is pretty common.
type Shell struct {
action *Action // nil for the root shell
*shellShared // per-Builder state shared across Shells