Skip to content

Commit

Permalink
main: Fix cache directory removal if it's a mountpoint
Browse files Browse the repository at this point in the history
This fixes an issue where the cache directory cannot be removed if it's
a mountpoint.

This commit tries removing the cache directory but doesn't fail if
unsuccessful. Instead it tries removing the content of the cache
directory. Should this fail as well, distrobuilder will issue a warning.
The user is then responsible for cleaning up the cache directory.

Fixes lxc#716

Signed-off-by: Thomas Hipp <thomas.hipp@canonical.com>
  • Loading branch information
monstermunchkin committed Jun 13, 2023
1 parent d2667d6 commit a9512c5
Showing 1 changed file with 25 additions and 13 deletions.
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

0 comments on commit a9512c5

Please sign in to comment.