From 778db848c6974cd6e4e175665472a10354f333f0 Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Fri, 12 Jul 2024 16:34:56 +0200 Subject: [PATCH] Fix daemon EnvVar dirs not being created on startup --- go/daemon/daemon.go | 4 ++++ go/daemon/env.go | 55 ++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 53 insertions(+), 6 deletions(-) diff --git a/go/daemon/daemon.go b/go/daemon/daemon.go index 1bf1139..160c34e 100644 --- a/go/daemon/daemon.go +++ b/go/daemon/daemon.go @@ -147,6 +147,10 @@ func NewDaemon( bootstrapFilePath = bootstrap.StateDirPath(d.opts.EnvVars.StateDirPath) ) + if err := d.opts.EnvVars.init(); err != nil { + return nil, fmt.Errorf("initializing daemon directories: %w", err) + } + currBootstrap, err := bootstrap.FromFile(bootstrapFilePath) if errors.Is(err, fs.ErrNotExist) { // daemon has never had a network created or joined diff --git a/go/daemon/env.go b/go/daemon/env.go index 6f13651..3da1b92 100644 --- a/go/daemon/env.go +++ b/go/daemon/env.go @@ -13,17 +13,62 @@ import ( "github.com/adrg/xdg" ) -// DEPRECATED -// // EnvVars are variables which are derived based on the environment which the // process is running in. -// -// TODO EnvVars should be private to this package. type EnvVars struct { RuntimeDirPath string StateDirPath string } +func (e EnvVars) init() error { + var errs []error + mkDir := func(path string) error { + { + parentPath := filepath.Dir(path) + parentInfo, err := os.Stat(parentPath) + if err != nil { + return fmt.Errorf("checking parent path %q: %w", parentPath, err) + } else if !parentInfo.IsDir() { + return fmt.Errorf("%q is not a directory", parentPath) + } + } + + info, err := os.Stat(path) + if errors.Is(err, fs.ErrNotExist) { + // fine + } else if err != nil { + return fmt.Errorf("checking path: %w", err) + } else if !info.IsDir() { + return fmt.Errorf("path is not a directory") + } else { + return nil + } + + if err := os.Mkdir(path, 0700); err != nil { + return fmt.Errorf("creating directory: %w", err) + } + return nil + } + + if err := mkDir(e.RuntimeDirPath); err != nil { + errs = append(errs, fmt.Errorf( + "creating runtime directory %q: %w", + e.RuntimeDirPath, + err, + )) + } + + if err := mkDir(e.StateDirPath); err != nil { + errs = append(errs, fmt.Errorf( + "creating state directory %q: %w", + e.StateDirPath, + err, + )) + } + + return errors.Join(errs...) +} + func getDefaultHTTPSocketDirPath() string { path, err := firstExistingDir( "/run", @@ -51,8 +96,6 @@ var HTTPSocketPath = sync.OnceValue(func() string { ) }) -// DEPRECATED -// // GetEnvVars will return the EnvVars of the current processes, as determined by // the process's environment. var GetEnvVars = sync.OnceValue(func() (v EnvVars) {