Skip to content

Commit a5f8048

Browse files
albertitogopherbot
authored andcommitted
acme/autocert: use standard functions to pick the cache directory
acme/autocert currently has ad-hoc logic to find a reasonable default for a cache directory. Since that logic was written (in 2017), new functions were added to the os package to provide that functionality (in Go 1.13, 2019-09): `os.UserCacheDir` and `os.UserHomeDir`. This patch replaces the ad-hoc logic with a call to `os.UserCacheDir`. The fallback to `/` is kept, since it may be relied upon in some environments. Change-Id: I3bf692ca670b87bf3d329e5d3684eee15ed374aa Reviewed-on: https://go-review.googlesource.com/c/crypto/+/440195 Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Reviewed-by: Roland Shoemaker <roland@golang.org> Auto-Submit: Sean Liao <sean@liao.dev> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Sean Liao <sean@liao.dev>
1 parent 958cde8 commit a5f8048

File tree

1 file changed

+6
-26
lines changed

1 file changed

+6
-26
lines changed

acme/autocert/listener.go

+6-26
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"net"
1111
"os"
1212
"path/filepath"
13-
"runtime"
1413
"time"
1514
)
1615

@@ -124,32 +123,13 @@ func (ln *listener) Close() error {
124123
return ln.tcpListener.Close()
125124
}
126125

127-
func homeDir() string {
128-
if runtime.GOOS == "windows" {
129-
return os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
130-
}
131-
if h := os.Getenv("HOME"); h != "" {
132-
return h
133-
}
134-
return "/"
135-
}
136-
137126
func cacheDir() string {
138127
const base = "golang-autocert"
139-
switch runtime.GOOS {
140-
case "darwin":
141-
return filepath.Join(homeDir(), "Library", "Caches", base)
142-
case "windows":
143-
for _, ev := range []string{"APPDATA", "CSIDL_APPDATA", "TEMP", "TMP"} {
144-
if v := os.Getenv(ev); v != "" {
145-
return filepath.Join(v, base)
146-
}
147-
}
148-
// Worst case:
149-
return filepath.Join(homeDir(), base)
150-
}
151-
if xdg := os.Getenv("XDG_CACHE_HOME"); xdg != "" {
152-
return filepath.Join(xdg, base)
128+
cache, err := os.UserCacheDir()
129+
if err != nil {
130+
// Fall back to the root directory.
131+
cache = "/.cache"
153132
}
154-
return filepath.Join(homeDir(), ".cache", base)
133+
134+
return filepath.Join(cache, base)
155135
}

0 commit comments

Comments
 (0)