github.com/sirkon/goproxy@v1.4.8/internal/cache/default.go (about) 1 // Copyright 2017 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package cache 6 7 import ( 8 "fmt" 9 "io/ioutil" 10 "os" 11 "path/filepath" 12 "runtime" 13 "sync" 14 ) 15 16 // Default returns the default cache to use, or nil if no cache should be used. 17 func Default() *Cache { 18 defaultOnce.Do(initDefaultCache) 19 return defaultCache 20 } 21 22 var ( 23 defaultOnce sync.Once 24 defaultCache *Cache 25 ) 26 27 // cacheREADME is a message stored in a README in the cache directory. 28 // Because the cache lives outside the normal Go trees, we leave the 29 // README as a courtesy to explain where it came from. 30 const cacheREADME = `This directory holds cached build artifacts from the Go build system. 31 Run "go clean -cache" if the directory is getting too large. 32 See golang.org to learn more about Go. 33 ` 34 35 // initDefaultCache does the work of finding the default cache 36 // the first time Default is called. 37 func initDefaultCache() { 38 dir, showWarnings := defaultDir() 39 if dir == "off" { 40 return 41 } 42 if err := os.MkdirAll(dir, 0777); err != nil { 43 if showWarnings { 44 fmt.Fprintf(os.Stderr, "go: disabling cache (%s) due to initialization failure: %s\n", dir, err) 45 } 46 return 47 } 48 if _, err := os.Stat(filepath.Join(dir, "README")); err != nil { 49 // Best effort. 50 ioutil.WriteFile(filepath.Join(dir, "README"), []byte(cacheREADME), 0666) 51 } 52 53 c, err := Open(dir) 54 if err != nil { 55 if showWarnings { 56 fmt.Fprintf(os.Stderr, "go: disabling cache (%s) due to initialization failure: %s\n", dir, err) 57 } 58 return 59 } 60 defaultCache = c 61 } 62 63 // DefaultDir returns the effective GOCACHE setting. 64 // It returns "off" if the cache is disabled. 65 func DefaultDir() string { 66 dir, _ := defaultDir() 67 return dir 68 } 69 70 // defaultDir returns the effective GOCACHE setting. 71 // It returns "off" if the cache is disabled. 72 // The second return value reports whether warnings should 73 // be shown if the cache fails to initialize. 74 func defaultDir() (string, bool) { 75 dir := os.Getenv("GOCACHE") 76 if dir != "" { 77 return dir, true 78 } 79 80 // Compute default location. 81 // TODO(rsc): This code belongs somewhere else, 82 // like maybe ioutil.CacheDir or os.CacheDir. 83 showWarnings := true 84 switch runtime.GOOS { 85 case "windows": 86 dir = os.Getenv("LocalAppData") 87 if dir == "" { 88 // Fall back to %AppData%, the old name of 89 // %LocalAppData% on Windows XP. 90 dir = os.Getenv("AppData") 91 } 92 if dir == "" { 93 return "off", true 94 } 95 96 case "darwin": 97 dir = os.Getenv("HOME") 98 if dir == "" { 99 return "off", true 100 } 101 dir += "/Library/Caches" 102 103 case "plan9": 104 dir = os.Getenv("home") 105 if dir == "" { 106 return "off", true 107 } 108 // Plan 9 has no established per-user cache directory, 109 // but $home/lib/xyz is the usual equivalent of $HOME/.xyz on Unix. 110 dir += "/lib/cache" 111 112 default: // Unix 113 // https://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html 114 dir = os.Getenv("XDG_CACHE_HOME") 115 if dir == "" { 116 dir = os.Getenv("HOME") 117 if dir == "" { 118 return "off", true 119 } 120 if dir == "/" { 121 // probably docker run with -u flag 122 // https://golang.org/issue/26280 123 showWarnings = false 124 } 125 dir += "/.cache" 126 } 127 } 128 return filepath.Join(dir, "go-build"), showWarnings 129 }