Skip to content

Commit 50bd1c4

Browse files
committed
os: have UserCacheDir return an error on failure
Previously, it would return an empty string if it could not determine the user's cache directory. Return an error instead. Change-Id: I74f00b1ad3858efa3fe2700c599271ebfe5764b6 Reviewed-on: https://go-review.googlesource.com/120757 Run-TryBot: Andrew Bonventre <[email protected]> Reviewed-by: Josh Bleecher Snyder <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 4f172e7 commit 50bd1c4

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

Diff for: api/go1.11.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ pkg net/http/httptrace, type ClientTrace struct, Got1xxResponse func(int, textpr
451451
pkg os, const ModeIrregular = 524288
452452
pkg os, const ModeIrregular FileMode
453453
pkg os, const ModeType = 2399666176
454-
pkg os, func UserCacheDir() string
454+
pkg os, func UserCacheDir() (string, error)
455455
pkg os/signal, func Ignored(os.Signal) bool
456456
pkg regexp/syntax, method (Op) String() string
457457
pkg runtime/trace, func IsEnabled() bool

Diff for: src/os/file.go

+9-6
Original file line numberDiff line numberDiff line change
@@ -342,25 +342,28 @@ func TempDir() string {
342342
// On Plan 9, it returns $home/lib/cache.
343343
//
344344
// If the location cannot be determined (for example, $HOME is not defined),
345-
// then it will return an empty string.
346-
func UserCacheDir() string {
345+
// then it will return an error.
346+
func UserCacheDir() (string, error) {
347347
var dir string
348348

349349
switch runtime.GOOS {
350350
case "windows":
351351
dir = Getenv("LocalAppData")
352+
if dir == "" {
353+
return "", errors.New("%LocalAppData% is not defined")
354+
}
352355

353356
case "darwin":
354357
dir = Getenv("HOME")
355358
if dir == "" {
356-
return ""
359+
return "", errors.New("$HOME is not defined")
357360
}
358361
dir += "/Library/Caches"
359362

360363
case "plan9":
361364
dir = Getenv("home")
362365
if dir == "" {
363-
return ""
366+
return "", errors.New("$home is not defined")
364367
}
365368
dir += "/lib/cache"
366369

@@ -369,13 +372,13 @@ func UserCacheDir() string {
369372
if dir == "" {
370373
dir = Getenv("HOME")
371374
if dir == "" {
372-
return ""
375+
return "", errors.New("neither $XDG_CACHE_HOME nor $HOME are defined")
373376
}
374377
dir += "/.cache"
375378
}
376379
}
377380

378-
return dir
381+
return dir, nil
379382
}
380383

381384
// Chmod changes the mode of the named file to mode.

0 commit comments

Comments
 (0)