Skip to content

Commit

Permalink
homedir: revamp
Browse files Browse the repository at this point in the history
Back in the day os/user package was relied on libc functions to parse
/etc/passwd and /etc/group. This was fixed in Go 1.11 by [1] and
subsequent fixes, so there is no more need to parse /etc/passwd.

This assumes that users know when and how to use osusergo build tag
(since this is not something very new, most are).

It might make sense to note in the changelog / release notes that the
static_build tag is no longer supported, and osusergo tag should be
used instead.

[1] https://go-review.googlesource.com/c/go/+/92456

Updates: da6051a
Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
  • Loading branch information
kolyshkin committed Feb 1, 2022
1 parent 6b744b9 commit 230c6f2
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 76 deletions.
29 changes: 13 additions & 16 deletions homedir.go
Expand Up @@ -2,27 +2,24 @@ package dbus

import (
"os"
"sync"
)

var (
homeDir string
homeDirLock sync.Mutex
"os/user"
)

// Get returns the home directory of the current user, which is usually the
// value of HOME environment variable. In case it is not set or empty, os/user
// package is used.
//
// If linking statically with cgo enabled against glibc, make sure the
// osusergo build tag is used.
//
// If needing to do nss lookups, do not disable cgo or set osusergo.
func getHomeDir() string {
homeDirLock.Lock()
defer homeDirLock.Unlock()

homeDir := os.Getenv("HOME")
if homeDir != "" {
return homeDir
}

homeDir = os.Getenv("HOME")
if homeDir != "" {
return homeDir
if u, err := user.Current(); err == nil {
return u.HomeDir
}

homeDir = lookupHomeDir()
return homeDir
return "/"
}
15 changes: 0 additions & 15 deletions homedir_dynamic.go

This file was deleted.

45 changes: 0 additions & 45 deletions homedir_static.go

This file was deleted.

0 comments on commit 230c6f2

Please sign in to comment.