| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package cache |
| 6 |
|
| 7 |
import ( |
| 8 |
"fmt" |
| 9 |
"log" |
| 10 |
"os" |
| 11 |
"path/filepath" |
| 12 |
"sync" |
| 13 |
) |
| 14 |
|
| 15 |
|
| 16 |
|
| 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 |
|
| 28 |
|
| 29 |
|
| 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 |
Run "go clean -fuzzcache" to delete the fuzz cache. |
| 33 |
See golang.org to learn more about Go. |
| 34 |
` |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
func initDefaultCache() { |
| 39 |
dir := DefaultDir() |
| 40 |
if dir == "off" { |
| 41 |
if defaultDirErr != nil { |
| 42 |
log.Fatalf("build cache is required, but could not be located: %v", defaultDirErr) |
| 43 |
} |
| 44 |
log.Fatalf("build cache is disabled by GOCACHE=off, but required as of Go 1.12") |
| 45 |
} |
| 46 |
if err := os.MkdirAll(dir, 0777); err != nil { |
| 47 |
log.Fatalf("failed to initialize build cache at %s: %s\n", dir, err) |
| 48 |
} |
| 49 |
if _, err := os.Stat(filepath.Join(dir, "README")); err != nil { |
| 50 |
|
| 51 |
os.WriteFile(filepath.Join(dir, "README"), []byte(cacheREADME), 0666) |
| 52 |
} |
| 53 |
|
| 54 |
c, err := Open(dir) |
| 55 |
if err != nil { |
| 56 |
log.Fatalf("failed to initialize build cache at %s: %s\n", dir, err) |
| 57 |
} |
| 58 |
defaultCache = c |
| 59 |
} |
| 60 |
|
| 61 |
var ( |
| 62 |
defaultDirOnce sync.Once |
| 63 |
defaultDir string |
| 64 |
defaultDirErr error |
| 65 |
) |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
func DefaultDir() string { |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
defaultDirOnce.Do(func() { |
| 76 |
|
| 77 |
|
| 78 |
defaultDir = os.Getenv("GOCACHE") |
| 79 |
if filepath.IsAbs(defaultDir) || defaultDir == "off" { |
| 80 |
return |
| 81 |
} |
| 82 |
if defaultDir != "" { |
| 83 |
defaultDir = "off" |
| 84 |
defaultDirErr = fmt.Errorf("GOCACHE is not an absolute path") |
| 85 |
return |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
dir, err := os.UserCacheDir() |
| 90 |
if err != nil { |
| 91 |
defaultDir = "off" |
| 92 |
defaultDirErr = fmt.Errorf("GOCACHE is not defined and %v", err) |
| 93 |
return |
| 94 |
} |
| 95 |
defaultDir = filepath.Join(dir, "go-build") |
| 96 |
}) |
| 97 |
|
| 98 |
return defaultDir |
| 99 |
} |