From 842221aab21df7631af8bac87446efb63bc1f67c Mon Sep 17 00:00:00 2001 From: Oliver Gugger Date: Mon, 8 Nov 2021 15:28:54 +0100 Subject: [PATCH] config: move directory creation to after path fixes Fixes #5927. This commit moves the code that attempts to create parent directories to the correct place _after_ we've adjusted all path values to point to the correct places. Before this commit the macaroon and tor paths would point to their default locations, even if the --lnddir flag was specified. --- config.go | 46 ++++++++++++++++++++-------------------------- 1 file changed, 20 insertions(+), 26 deletions(-) diff --git a/config.go b/config.go index c4c70540d..6666942ce 100644 --- a/config.go +++ b/config.go @@ -740,8 +740,8 @@ func ValidateConfig(cfg Config, interceptor signal.Interceptor, fileParser, } } - str := "Failed to create lnd directory: %v" - return mkErr(str, err) + str := "Failed to create lnd directory '%s': %v" + return mkErr(str, dir, err) } return nil @@ -809,24 +809,6 @@ func ValidateConfig(cfg Config, interceptor signal.Interceptor, fileParser, cfg.WalletUnlockPasswordFile, ) - // Create the lnd directory and all other sub directories if they don't - // already exist. This makes sure that directory trees are also created - // for files that point to outside of the lnddir. - dirs := []string{ - lndDir, cfg.DataDir, - cfg.LetsEncryptDir, cfg.Watchtower.TowerDir, - filepath.Dir(cfg.TLSCertPath), filepath.Dir(cfg.TLSKeyPath), - filepath.Dir(cfg.AdminMacPath), filepath.Dir(cfg.ReadMacPath), - filepath.Dir(cfg.InvoiceMacPath), - filepath.Dir(cfg.Tor.PrivateKeyPath), - filepath.Dir(cfg.Tor.WatchtowerKeyPath), - } - for _, dir := range dirs { - if err := makeDirectory(dir); err != nil { - return nil, err - } - } - // Ensure that the user didn't attempt to specify negative values for // any of the autopilot params. if cfg.Autopilot.MaxChannels < 0 { @@ -1307,12 +1289,6 @@ func ValidateConfig(cfg Config, interceptor signal.Interceptor, fileParser, lncfg.NormalizeNetwork(cfg.ActiveNetParams.Name), ) - // We need to make sure the default network directory exists for when we - // try to create our default macaroons there. - if err := makeDirectory(cfg.networkDir); err != nil { - return nil, err - } - // If a custom macaroon directory wasn't specified and the data // directory has changed from the default path, then we'll also update // the path for the macaroons to be generated. @@ -1332,6 +1308,24 @@ func ValidateConfig(cfg Config, interceptor signal.Interceptor, fileParser, ) } + // Create the lnd directory and all other sub-directories if they don't + // already exist. This makes sure that directory trees are also created + // for files that point to outside the lnddir. + dirs := []string{ + lndDir, cfg.DataDir, cfg.networkDir, + cfg.LetsEncryptDir, cfg.Watchtower.TowerDir, + filepath.Dir(cfg.TLSCertPath), filepath.Dir(cfg.TLSKeyPath), + filepath.Dir(cfg.AdminMacPath), filepath.Dir(cfg.ReadMacPath), + filepath.Dir(cfg.InvoiceMacPath), + filepath.Dir(cfg.Tor.PrivateKeyPath), + filepath.Dir(cfg.Tor.WatchtowerKeyPath), + } + for _, dir := range dirs { + if err := makeDirectory(dir); err != nil { + return nil, err + } + } + // Similarly, if a custom back up file path wasn't specified, then // we'll update the file location to match our set network directory. if cfg.BackupFilePath == "" {