]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: use os.UserCacheDir for the default GOCACHE
authorDaniel Martí <mvdan@mvdan.cc>
Thu, 11 Oct 2018 14:13:22 +0000 (15:13 +0100)
committerDaniel Martí <mvdan@mvdan.cc>
Wed, 24 Oct 2018 12:56:25 +0000 (12:56 +0000)
This piece of code predates the addition of os.UserCacheDir, and it
looks like os.UserCacheDir was based on this piece of code.

The two behaved exactly the same, minus cmd/go's addition of AppData for
Windows XP in CL 87675. However, Go 1.11 dropped support for Windows XP,
so we can safely ignore that change now.

The only tweaks necessary are to return "off" if an error is
encountered, and to disable warnings if we're using "/.cache".

Change-Id: Ia00577d4575ce4870f7fb103eafaa4d2b630743e
Reviewed-on: https://go-review.googlesource.com/c/141538
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
src/cmd/go/internal/cache/default.go

index 02fc1e896f74d80b5a1564d16ce778b425a3cdb3..4a69bf2a443c989bdbcfb81e0f62ef9080094206 100644 (file)
@@ -9,7 +9,6 @@ import (
        "io/ioutil"
        "os"
        "path/filepath"
-       "runtime"
        "sync"
 )
 
@@ -78,52 +77,19 @@ func defaultDir() (string, bool) {
        }
 
        // Compute default location.
-       // TODO(rsc): This code belongs somewhere else,
-       // like maybe ioutil.CacheDir or os.CacheDir.
-       showWarnings := true
-       switch runtime.GOOS {
-       case "windows":
-               dir = os.Getenv("LocalAppData")
-               if dir == "" {
-                       // Fall back to %AppData%, the old name of
-                       // %LocalAppData% on Windows XP.
-                       dir = os.Getenv("AppData")
-               }
-               if dir == "" {
-                       return "off", true
-               }
-
-       case "darwin":
-               dir = os.Getenv("HOME")
-               if dir == "" {
-                       return "off", true
-               }
-               dir += "/Library/Caches"
-
-       case "plan9":
-               dir = os.Getenv("home")
-               if dir == "" {
-                       return "off", true
-               }
-               // 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"
+       dir, err := os.UserCacheDir()
+       if err != nil {
+               return "off", true
+       }
+       dir = filepath.Join(dir, "go-build")
 
-       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 == "" {
-                               return "off", true
-                       }
-                       if dir == "/" {
-                               // probably docker run with -u flag
-                               // https://golang.org/issue/26280
-                               showWarnings = false
-                       }
-                       dir += "/.cache"
-               }
+       // Do this after filepath.Join, so that the path has been cleaned.
+       showWarnings := true
+       switch dir {
+       case "/.cache/go-build":
+               // probably docker run with -u flag
+               // https://golang.org/issue/26280
+               showWarnings = false
        }
-       return filepath.Join(dir, "go-build"), showWarnings
+       return dir, showWarnings
 }