gitea-tea/vendor/github.com/adrg/xdg/base_dirs.go
crapStone c4e2db32b5 rewrote config file path search (#219)
added comment to clarify coding choices

added package xdg to vendor folder

rewrote config file path search

Co-authored-by: crapStone <crapstone01@gmail.com>
Reviewed-on: https://gitea.com/gitea/tea/pulls/219
Reviewed-by: 6543 <6543@noreply.gitea.io>
Reviewed-by: Norwin <noerw@noreply.gitea.io>
2020-10-06 13:06:47 +00:00

79 lines
2.0 KiB
Go

package xdg
import "os"
// XDG Base Directory environment variables.
var (
envDataHome = "XDG_DATA_HOME"
envDataDirs = "XDG_DATA_DIRS"
envConfigHome = "XDG_CONFIG_HOME"
envConfigDirs = "XDG_CONFIG_DIRS"
envCacheHome = "XDG_CACHE_HOME"
envRuntimeDir = "XDG_RUNTIME_DIR"
)
type baseDirectories struct {
dataHome string
data []string
configHome string
config []string
cacheHome string
runtime string
// Non-standard directories.
fonts []string
applications []string
}
func (bd baseDirectories) dataFile(relPath string) (string, error) {
return createPath(relPath, append([]string{bd.dataHome}, bd.data...))
}
func (bd baseDirectories) configFile(relPath string) (string, error) {
return createPath(relPath, append([]string{bd.configHome}, bd.config...))
}
func (bd baseDirectories) cacheFile(relPath string) (string, error) {
return createPath(relPath, []string{bd.cacheHome})
}
func (bd baseDirectories) runtimeFile(relPath string) (string, error) {
fi, err := os.Lstat(bd.runtime)
if err != nil {
if os.IsNotExist(err) {
return createPath(relPath, []string{bd.runtime})
}
return "", err
}
if fi.IsDir() {
// The runtime directory must be owned by the user.
if err = os.Chown(bd.runtime, os.Getuid(), os.Getgid()); err != nil {
return "", err
}
} else {
// For security reasons, the runtime directory cannot be a symlink.
if err = os.Remove(bd.runtime); err != nil {
return "", err
}
}
return createPath(relPath, []string{bd.runtime})
}
func (bd baseDirectories) searchDataFile(relPath string) (string, error) {
return searchFile(relPath, append([]string{bd.dataHome}, bd.data...))
}
func (bd baseDirectories) searchConfigFile(relPath string) (string, error) {
return searchFile(relPath, append([]string{bd.configHome}, bd.config...))
}
func (bd baseDirectories) searchCacheFile(relPath string) (string, error) {
return searchFile(relPath, []string{bd.cacheHome})
}
func (bd baseDirectories) searchRuntimeFile(relPath string) (string, error) {
return searchFile(relPath, []string{bd.runtime})
}