Skip to content

Commit

Permalink
retry removals of directories that have been recently archived b/c of…
Browse files Browse the repository at this point in the history
… windows bug

fix related to the following issue: golang/go#51442
  • Loading branch information
YrrepNoj committed Mar 13, 2023
1 parent d11e78e commit c8670f1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/internal/packager/sbom/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,10 @@ func Catalog(componentSBOMs map[string]*types.ComponentSBOM, imgList []string, t
return err
}

_ = os.RemoveAll(builder.tmpSBOMPath)
if err := utils.RetryRemoveAll(builder.tmpSBOMPath, 3); err != nil {
return fmt.Errorf("unable to remove the temporary SBOM directory: %w", err)
}

builder.spinner.Success()

return nil
Expand Down
4 changes: 2 additions & 2 deletions src/pkg/packager/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,8 @@ func (p *Packager) Create(baseDir string) error {
}

// Remove the deflated component directory
if err := os.RemoveAll(componentPaths.Base); err != nil {
message.Debugf("unable to remove the component directory (%s): %s", componentPaths.Base, err.Error())
if err := utils.RetryRemoveAll(componentPaths.Base, 3); err != nil {
return fmt.Errorf("unable to remove the component directory (%s): %w", componentPaths.Base, err)
}
}

Expand Down
16 changes: 16 additions & 0 deletions src/pkg/utils/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"path/filepath"
"regexp"
"strings"
"time"

"github.com/defenseunicorns/zarf/src/pkg/message"
"github.com/otiai10/copy"
Expand Down Expand Up @@ -253,3 +254,18 @@ func GetDirSize(path string) (int64, error) {

return dirSize, err
}

// RetryRemoveAll will attempt to remove an item or directory up to the given number of retries.
// NOTE: This function is necessary because of a Windows bug with removing files that have been recently used: https://github.com/golang/go/issues/51442
func RetryRemoveAll(path string, retries int) error {
for i := 0; i < retries; i++ {
err := os.RemoveAll(path)
if err == nil {
return nil
}

time.Sleep(500 * time.Millisecond)
}

return os.RemoveAll(path)
}

0 comments on commit c8670f1

Please sign in to comment.