From 27176cc6bb55b2e7d5d5685c5df36d67981e0135 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Mon, 11 Apr 2022 11:40:16 +0100 Subject: [PATCH] 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. --- fs/config/config.go | 38 ++++---------------------------------- 1 file changed, 4 insertions(+), 34 deletions(-) diff --git a/fs/config/config.go b/fs/config/config.go index 65780c367..0c84127e6 100644 --- a/fs/config/config.go +++ b/fs/config/config.go @@ -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")