Skip to content

Commit 4cf4d20

Browse files
committed
Treat XDG_CONFIG_HOME and $HOME/.config the same way
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]>
1 parent 465c38f commit 4cf4d20

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

pkg/homedir/homedir_unix.go

+16-16
Original file line numberDiff line numberDiff line change
@@ -103,32 +103,32 @@ func isWriteableOnlyByOwner(perm os.FileMode) bool {
103103

104104
// GetConfigHome returns XDG_CONFIG_HOME.
105105
// GetConfigHome returns $HOME/.config and nil error if XDG_CONFIG_HOME is not set.
106-
//
106+
// As a side effect GetConfigHome will attempt to create the ConfigHome dir
107+
// if it does not exist.
107108
// See also https://standards.freedesktop.org/basedir-spec/latest/ar01s03.html
108109
func GetConfigHome() (string, error) {
109110
rootlessConfigHomeDirOnce.Do(func() {
110-
cfgHomeDir := os.Getenv("XDG_CONFIG_HOME")
111-
if cfgHomeDir == "" {
111+
tmpDir := os.Getenv("XDG_CONFIG_HOME")
112+
if tmpDir == "" {
112113
home := Get()
113114
resolvedHome, err := filepath.EvalSymlinks(home)
114115
if err != nil {
115116
rootlessConfigHomeDirError = fmt.Errorf("cannot resolve %s: %w", home, err)
116117
return
117118
}
118-
tmpDir := filepath.Join(resolvedHome, ".config")
119-
_ = os.MkdirAll(tmpDir, 0o700)
120-
st, err := os.Stat(tmpDir)
121-
if err != nil {
122-
rootlessConfigHomeDirError = err
123-
return
124-
} else if int(st.Sys().(*syscall.Stat_t).Uid) == os.Geteuid() {
125-
cfgHomeDir = tmpDir
126-
} else {
127-
rootlessConfigHomeDirError = fmt.Errorf("path %q exists and it is not owned by the current user", tmpDir)
128-
return
129-
}
119+
tmpDir = filepath.Join(resolvedHome, ".config")
120+
}
121+
_ = os.MkdirAll(tmpDir, 0o700)
122+
st, err := os.Stat(tmpDir)
123+
if err != nil {
124+
rootlessConfigHomeDirError = err
125+
return
126+
} else if int(st.Sys().(*syscall.Stat_t).Uid) == os.Geteuid() {
127+
rootlessConfigHomeDir = tmpDir
128+
} else {
129+
rootlessConfigHomeDirError = fmt.Errorf("path %q exists and it is not owned by the current user", tmpDir)
130+
return
130131
}
131-
rootlessConfigHomeDir = cfgHomeDir
132132
})
133133

134134
return rootlessConfigHomeDir, rootlessConfigHomeDirError

0 commit comments

Comments
 (0)