1
mirror of https://github.com/rclone/rclone synced 2024-12-20 10:25:56 +01:00

config: use os.UserCacheDir from go stdlib to find cache dir #6095

When this code was originally implemented os.UserCacheDir wasn't
public so this used a copy of the code. This commit replaces that now
out of date copy with a call to the now public stdlib function.
This commit is contained in:
Nick Craig-Wood 2022-04-11 11:40:16 +01:00
parent f1e4b7da7b
commit 27176cc6bb

View File

@ -674,41 +674,11 @@ func Dump() error {
}
// makeCacheDir returns a directory to use for caching.
//
// Code borrowed from go stdlib until it is made public
func makeCacheDir() (dir string) {
// Compute default location.
switch runtime.GOOS {
case "windows":
dir = os.Getenv("LocalAppData")
case "darwin":
dir = os.Getenv("HOME")
if dir != "" {
dir += "/Library/Caches"
}
case "plan9":
dir = os.Getenv("home")
if dir != "" {
// Plan 9 has no established per-user cache directory,
// but $home/lib/xyz is the usual equivalent of $HOME/.xyz on Unix.
dir += "/lib/cache"
}
default: // Unix
// https://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
dir = os.Getenv("XDG_CACHE_HOME")
if dir == "" {
dir = os.Getenv("HOME")
if dir != "" {
dir += "/.cache"
}
}
}
// if no dir found then use TempDir - we will have a cachedir!
if dir == "" {
dir, err := os.UserCacheDir()
if err != nil || dir == "" {
fs.Debugf(nil, "Failed to find user cache dir, using temporary directory: %v", err)
// if no dir found then use TempDir - we will have a cachedir!
dir = os.TempDir()
}
return filepath.Join(dir, "rclone")