Skip to content

Commit 5641cab

Browse files
authored
Merge pull request #7 from golang/master
glog: avoid calling user.Current() on windows (golang#69)
2 parents d78d2d2 + 9730314 commit 5641cab

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

glog_file.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
"fmt"
2727
"io"
2828
"os"
29-
"os/user"
3029
"path/filepath"
3130
"runtime"
3231
"strings"
@@ -68,9 +67,8 @@ func init() {
6867
host = shortHostname(h)
6968
}
7069

71-
current, err := user.Current()
72-
if err == nil {
73-
userName = current.Username
70+
if u := lookupUser(); u != "" {
71+
userName = u
7472
}
7573
// Sanitize userName since it is used to construct file paths.
7674
userName = strings.Map(func(r rune) rune {

glog_file_nonwindows.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//go:build !windows
2+
3+
package glog
4+
5+
import "os/user"
6+
7+
func lookupUser() string {
8+
if current, err := user.Current(); err == nil {
9+
return current.Username
10+
}
11+
return ""
12+
}

glog_file_windows.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//go:build windows
2+
3+
package glog
4+
5+
import (
6+
"syscall"
7+
)
8+
9+
// This follows the logic in the standard library's user.Current() function, except
10+
// that it leaves out the potentially expensive calls required to look up the user's
11+
// display name in Active Directory.
12+
func lookupUser() string {
13+
token, err := syscall.OpenCurrentProcessToken()
14+
if err != nil {
15+
return ""
16+
}
17+
defer token.Close()
18+
tokenUser, err := token.GetTokenUser()
19+
if err != nil {
20+
return ""
21+
}
22+
username, _, accountType, err := tokenUser.User.Sid.LookupAccount("")
23+
if err != nil {
24+
return ""
25+
}
26+
if accountType != syscall.SidTypeUser {
27+
return ""
28+
}
29+
return username
30+
}

0 commit comments

Comments
 (0)