Skip to content

Commit c1e60b6

Browse files
cuonglmbradfitz
authored andcommitted
os/user: use os.UserHomeDir for user.HomeDir
Using os.UserHomeDir for user.HomeDir helps us deduplicate the logic and keep the behavior consistent. Also make os.UserHomeDir return "/sdcard" in android. See: https://go-review.googlesource.com/c/go/+/37960/1/src/os/user/lookup_stubs.go#48 Fixes #31070 Change-Id: I521bad050bc5761ecc5c0085501374d2cf8e6897 Reviewed-on: https://go-review.googlesource.com/c/go/+/169540 Run-TryBot: Daniel Martí <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Daniel Martí <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent 2bd767b commit c1e60b6

File tree

2 files changed

+6
-8
lines changed

2 files changed

+6
-8
lines changed

src/os/file.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -468,8 +468,10 @@ func UserHomeDir() (string, error) {
468468
}
469469
// On some geese the home directory is not always defined.
470470
switch runtime.GOOS {
471-
case "nacl", "android":
471+
case "nacl":
472472
return "/", nil
473+
case "android":
474+
return "/sdcard", nil
473475
case "darwin":
474476
if runtime.GOARCH == "arm" || runtime.GOARCH == "arm64" {
475477
return "/", nil

src/os/user/lookup_stubs.go

+3-7
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@ func current() (*User, error) {
2626
if err == nil {
2727
return u, nil
2828
}
29+
30+
homeDir, _ := os.UserHomeDir()
2931
u = &User{
3032
Uid: uid,
3133
Gid: currentGID(),
3234
Username: os.Getenv("USER"),
3335
Name: "", // ignored
34-
HomeDir: os.Getenv("HOME"),
36+
HomeDir: homeDir,
3537
}
3638
// On NaCL and Android, return a dummy user instead of failing.
3739
switch runtime.GOOS {
@@ -42,19 +44,13 @@ func current() (*User, error) {
4244
if u.Username == "" {
4345
u.Username = "nacl"
4446
}
45-
if u.HomeDir == "" {
46-
u.HomeDir = "/"
47-
}
4847
case "android":
4948
if u.Uid == "" {
5049
u.Uid = "1"
5150
}
5251
if u.Username == "" {
5352
u.Username = "android"
5453
}
55-
if u.HomeDir == "" {
56-
u.HomeDir = "/sdcard"
57-
}
5854
}
5955
// cgo isn't available, but if we found the minimum information
6056
// without it, use it:

0 commit comments

Comments
 (0)