Skip to content

Commit

Permalink
Treat XDG_CONFIG_HOME and $HOME/.config the same way
Browse files Browse the repository at this point in the history
Currently we handle XDG_CONFIG_HOME and the fallback $HOME/.config
differently. We create the later if it does not exist and verify that
it is owned by the current user. This patch makes XDG_CONFIG_HOME
follow the same pattern.

Signed-off-by: Daniel J Walsh <[email protected]>
  • Loading branch information
rhatdan committed Sep 4, 2024
1 parent 465c38f commit f74b71b
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions pkg/homedir/homedir_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,39 @@ func isWriteableOnlyByOwner(perm os.FileMode) bool {
return (perm & 0o722) == 0o700
}

// GetConfigHome returns XDG_CONFIG_HOME.
// ConfigHome returns XDG_CONFIG_HOME.
// ConfigHome returns $HOME/.config and nil error if XDG_CONFIG_HOME is not set.
// Verifies ownership of config directory matches the current process or
// returns error.
// See also https://standards.freedesktop.org/basedir-spec/latest/ar01s03.html
func GetConfigHome() (string, error) {
rootlessConfigHomeDirOnce.Do(func() {
tmpDir := os.Getenv("XDG_CONFIG_HOME")
if tmpDir == "" {
home := Get()
resolvedHome, err := filepath.EvalSymlinks(home)
if err != nil {
rootlessConfigHomeDirError = fmt.Errorf("cannot resolve %s: %w", home, err)
return
}
tmpDir = filepath.Join(resolvedHome, ".config")
}
st, err := os.Stat(tmpDir)
if err == nil {
if int(st.Sys().(*syscall.Stat_t).Uid) == os.Geteuid() {
rootlessConfigHomeDir = tmpDir
} else {
rootlessConfigHomeDirError = fmt.Errorf("path %q exists and it is not owned by the current user", tmpDir)
return
}
}
})

return rootlessConfigHomeDir, rootlessConfigHomeDirError
}

// GetConfigHome returns XDG_CONFIG_HOME. (Deprecated)
// GetConfigHome returns $HOME/.config and nil error if XDG_CONFIG_HOME is not set.
//
// See also https://standards.freedesktop.org/basedir-spec/latest/ar01s03.html
func GetConfigHome() (string, error) {
rootlessConfigHomeDirOnce.Do(func() {
Expand Down

0 comments on commit f74b71b

Please sign in to comment.