Skip to content

Commit

Permalink
dont chdir to dataDir
Browse files Browse the repository at this point in the history
  • Loading branch information
linkdata committed Jun 24, 2024
1 parent 71e2728 commit 19c5cf2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 21 deletions.
8 changes: 4 additions & 4 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ func logInfo(logger InfoLogger, msg, key, val string) {
// If User is set it then switches to that user and the users primary group.
// Note that this is not supported on Windows.
//
// If DataDir or DefaultDataDirSuffix is set, changes the current working
// directory. If DataDirMode is nonzero, the directory will be created
// if necessary.
// If DataDir or DefaultDataDirSuffix is set, calculates the absolute
// data directory path and sets DataDir. If DataDirMode is nonzero, the
// directory will be created if necessary.
//
// On a non-error return, CertDir and DataDir will be absolute paths or be empty,
// and ListenURL will be a printable and connectable URL like "http://localhost:80".
Expand All @@ -49,7 +49,7 @@ func (cfg *Config) Apply(logger InfoLogger) (l net.Listener, err error) {
logInfo(logger, "user switched", "user", cfg.User)
if cfg.DataDir, err = DefaultDataDir(cfg.DataDir, cfg.DefaultDataDirSuffix); err == nil {
if cfg.DataDir, err = UseDataDir(cfg.DataDir, cfg.DataDirMode); err == nil {
logInfo(logger, "using data directory", "dir", cfg.DataDir)
logInfo(logger, "data directory", "dir", cfg.DataDir)
}
}
}
Expand Down
22 changes: 5 additions & 17 deletions usedatadir.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ import (
"path/filepath"
)

// DataDirPermissions is the permissions UseDataDir()
// passes on to os.MkdirAll().
var DataDirPermissions = fs.FileMode(0750)

// DefaultDataDir returns dataDir if not empty, otherwise if
// defaultSuffix is not empty it returns the joined path
// of os.UserConfigDir() and defaultSuffix.
Expand All @@ -25,26 +21,18 @@ func DefaultDataDir(dataDir, defaultSuffix string) (string, error) {
return dataDir, err
}

func mkdirAll(dataDir string, mode fs.FileMode) (err error) {
if mode != 0 {
err = os.MkdirAll(dataDir, mode)
}
return
}

// UseDataDir does nothing if dataDir is empty, otherwise it expands
// environment variables and transforms it into an absolute path.
// Then, if mode is not zero, it creates the path if it does not exist.
// Finally, it finally changes the current directory to it.
// UseDataDir expands environment variables in dataDir and transforms
// it into an absolute path. Then, if mode is not zero, it creates
// the path if it does not exist. Does nothing if dataDir is empty.
//
// Returns the final path or an empty string if dataDir was empty.
func UseDataDir(dataDir string, mode fs.FileMode) (string, error) {
var err error
if dataDir != "" {
dataDir = os.ExpandEnv(dataDir)
if dataDir, err = filepath.Abs(dataDir); err == nil {
if err = mkdirAll(dataDir, mode); err == nil {
err = os.Chdir(dataDir)
if mode != 0 {
err = os.MkdirAll(dataDir, mode)
}
}
}
Expand Down

0 comments on commit 19c5cf2

Please sign in to comment.