diff --git a/cmds/portmaster-start/dirs.go b/cmds/portmaster-start/dirs.go new file mode 100644 index 00000000..e327963f --- /dev/null +++ b/cmds/portmaster-start/dirs.go @@ -0,0 +1,42 @@ +package main + +import ( + "fmt" + "log" + "os" + + "github.com/spf13/cobra" +) + +func init() { + rootCmd.AddCommand(cleanStructureCmd) +} + +var cleanStructureCmd = &cobra.Command{ + Use: "clean-structure", + Short: "Create and clean the required directory structure", + RunE: func(cmd *cobra.Command, args []string) error { + if err := ensureLoggingDir(); err != nil { + return err + } + return cleanAndEnsureExecDir() + }, +} + +func cleanAndEnsureExecDir() error { + execDir := dataRoot.ChildDir("exec", 0o777) + + // Clean up and remove exec dir. + err := os.RemoveAll(execDir.Path) + if err != nil { + log.Printf("WARNING: failed to fully remove exec dir (%q) for cleaning: %s", execDir.Path, err) + } + + // Re-create exec dir. + err = execDir.Ensure() + if err != nil { + return fmt.Errorf("failed to initialize exec dir (%q): %w", execDir.Path, err) + } + + return nil +} diff --git a/cmds/portmaster-start/main.go b/cmds/portmaster-start/main.go index 20f7a9f9..bd46cac8 100644 --- a/cmds/portmaster-start/main.go +++ b/cmds/portmaster-start/main.go @@ -51,7 +51,7 @@ var ( return err } - if err := configureLogging(); err != nil { + if err := ensureLoggingDir(); err != nil { return err } @@ -171,12 +171,12 @@ func configureRegistry(mustLoadIndex bool) error { return updateRegistryIndex(mustLoadIndex) } -func configureLogging() error { +func ensureLoggingDir() error { // set up logs root logsRoot = dataRoot.ChildDir("logs", 0777) err := logsRoot.Ensure() if err != nil { - return fmt.Errorf("failed to initialize logs root: %s", err) + return fmt.Errorf("failed to initialize logs root (%q): %s", logsRoot.Path, err) } // warn about CTRL-C on windows