github.com/karrick/go@v0.0.0-20170817181416-d5b0ec858b37/src/os/user/lookup_stubs.go (about) 1 // Copyright 2011 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // +build !cgo,!windows,!plan9 android 6 7 package user 8 9 import ( 10 "errors" 11 "fmt" 12 "os" 13 "runtime" 14 "strconv" 15 ) 16 17 func init() { 18 groupImplemented = false 19 } 20 21 func current() (*User, error) { 22 u := &User{ 23 Uid: currentUID(), 24 Gid: currentGID(), 25 Username: os.Getenv("USER"), 26 Name: "", // ignored 27 HomeDir: os.Getenv("HOME"), 28 } 29 // On NaCL and Android, return a dummy user instead of failing. 30 switch runtime.GOOS { 31 case "nacl": 32 if u.Uid == "" { 33 u.Uid = "1" 34 } 35 if u.Username == "" { 36 u.Username = "nacl" 37 } 38 if u.HomeDir == "" { 39 u.HomeDir = "/" 40 } 41 case "android": 42 if u.Uid == "" { 43 u.Uid = "1" 44 } 45 if u.Username == "" { 46 u.Username = "android" 47 } 48 if u.HomeDir == "" { 49 u.HomeDir = "/sdcard" 50 } 51 } 52 // cgo isn't available, but if we found the minimum information 53 // without it, use it: 54 if u.Uid != "" && u.Username != "" && u.HomeDir != "" { 55 return u, nil 56 } 57 return u, fmt.Errorf("user: Current not implemented on %s/%s", runtime.GOOS, runtime.GOARCH) 58 } 59 60 func listGroups(*User) ([]string, error) { 61 if runtime.GOOS == "android" { 62 return nil, errors.New("user: GroupIds not implemented on Android") 63 } 64 return nil, errors.New("user: GroupIds requires cgo") 65 } 66 67 func currentUID() string { 68 if id := os.Getuid(); id >= 0 { 69 return strconv.Itoa(id) 70 } 71 // Note: Windows returns -1, but this file isn't used on 72 // Windows anyway, so this empty return path shouldn't be 73 // used. 74 return "" 75 } 76 77 func currentGID() string { 78 if id := os.Getgid(); id >= 0 { 79 return strconv.Itoa(id) 80 } 81 return "" 82 }