Commit c1e60b6e authored by LE Manh Cuong's avatar LE Manh Cuong Committed by Brad Fitzpatrick

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í <mvdan@mvdan.cc>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarDaniel Martí <mvdan@mvdan.cc>
Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
parent 2bd767b1
...@@ -468,8 +468,10 @@ func UserHomeDir() (string, error) { ...@@ -468,8 +468,10 @@ func UserHomeDir() (string, error) {
} }
// On some geese the home directory is not always defined. // On some geese the home directory is not always defined.
switch runtime.GOOS { switch runtime.GOOS {
case "nacl", "android": case "nacl":
return "/", nil return "/", nil
case "android":
return "/sdcard", nil
case "darwin": case "darwin":
if runtime.GOARCH == "arm" || runtime.GOARCH == "arm64" { if runtime.GOARCH == "arm" || runtime.GOARCH == "arm64" {
return "/", nil return "/", nil
......
...@@ -26,12 +26,14 @@ func current() (*User, error) { ...@@ -26,12 +26,14 @@ func current() (*User, error) {
if err == nil { if err == nil {
return u, nil return u, nil
} }
homeDir, _ := os.UserHomeDir()
u = &User{ u = &User{
Uid: uid, Uid: uid,
Gid: currentGID(), Gid: currentGID(),
Username: os.Getenv("USER"), Username: os.Getenv("USER"),
Name: "", // ignored Name: "", // ignored
HomeDir: os.Getenv("HOME"), HomeDir: homeDir,
} }
// On NaCL and Android, return a dummy user instead of failing. // On NaCL and Android, return a dummy user instead of failing.
switch runtime.GOOS { switch runtime.GOOS {
...@@ -42,9 +44,6 @@ func current() (*User, error) { ...@@ -42,9 +44,6 @@ func current() (*User, error) {
if u.Username == "" { if u.Username == "" {
u.Username = "nacl" u.Username = "nacl"
} }
if u.HomeDir == "" {
u.HomeDir = "/"
}
case "android": case "android":
if u.Uid == "" { if u.Uid == "" {
u.Uid = "1" u.Uid = "1"
...@@ -52,9 +51,6 @@ func current() (*User, error) { ...@@ -52,9 +51,6 @@ func current() (*User, error) {
if u.Username == "" { if u.Username == "" {
u.Username = "android" u.Username = "android"
} }
if u.HomeDir == "" {
u.HomeDir = "/sdcard"
}
} }
// cgo isn't available, but if we found the minimum information // cgo isn't available, but if we found the minimum information
// without it, use it: // without it, use it:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment