Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

main: Fix cache directory removal if it's a mountpoint #720

Merged
merged 1 commit into from
Jun 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 25 additions & 13 deletions distrobuilder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,19 +238,36 @@ func main() {
}
}

func (c *cmdGlobal) cleanupCacheDirectory() {
// Try removing the entire cache directory.
err := os.RemoveAll(c.flagCacheDir)
if err == nil {
return
}

// Try removing the content of the cache directory if the directory itself cannot be removed.
err = filepath.Walk(c.flagCacheDir, func(path string, info os.FileInfo, err error) error {
if path == c.flagCacheDir {
return nil
}

return os.RemoveAll(path)
})
if err != nil {
c.logger.WithField("err", err).Warn("Failed cleaning up cache directory")
}
}

func (c *cmdGlobal) preRunBuild(cmd *cobra.Command, args []string) error {
// if an error is returned, disable the usage message
cmd.SilenceUsage = true

isRunningBuildDir := cmd.CalledAs() == "build-dir"

// Clean up cache directory before doing anything
err := os.RemoveAll(c.flagCacheDir)
if err != nil {
return fmt.Errorf("Failed removing cache directory: %w", err)
}
c.cleanupCacheDirectory()

err = os.Mkdir(c.flagCacheDir, 0755)
err := os.MkdirAll(c.flagCacheDir, 0755)
if err != nil {
return fmt.Errorf("Failed creating cache directory: %w", err)
}
Expand Down Expand Up @@ -422,18 +439,13 @@ func (c *cmdGlobal) preRunBuild(cmd *cobra.Command, args []string) error {
}

func (c *cmdGlobal) preRunPack(cmd *cobra.Command, args []string) error {
var err error

// if an error is returned, disable the usage message
cmd.SilenceUsage = true

// Clean up cache directory before doing anything
err = os.RemoveAll(c.flagCacheDir)
if err != nil {
return fmt.Errorf("Failed removing cache directory: %w", err)
}
c.cleanupCacheDirectory()

err = os.Mkdir(c.flagCacheDir, 0755)
err := os.MkdirAll(c.flagCacheDir, 0755)
if err != nil {
return fmt.Errorf("Failed creating cache directory: %w", err)
}
Expand Down Expand Up @@ -491,7 +503,7 @@ func (c *cmdGlobal) postRun(cmd *cobra.Command, args []string) error {
c.logger.Info("Removing cache directory")
}

_ = os.RemoveAll(c.flagCacheDir)
c.cleanupCacheDirectory()
}

// Clean up sources directory
Expand Down