aboutsummaryrefslogtreecommitdiff
path: root/vendor/github.com/containers/storage/pkg/unshare/unshare.go
blob: 221c7e088908be644268c26a8d0139c743107536 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package unshare

import (
	"fmt"
	"os"
	"os/user"
	"sync"

	"github.com/pkg/errors"
	"github.com/sirupsen/logrus"
)

var (
	homeDirOnce sync.Once
	homeDirErr  error
	homeDir     string

	hasCapSysAdminOnce sync.Once
	hasCapSysAdminRet  bool
	hasCapSysAdminErr  error
)

// HomeDir returns the home directory for the current user.
func HomeDir() (string, error) {
	homeDirOnce.Do(func() {
		home := os.Getenv("HOME")
		if home == "" {
			usr, err := user.LookupId(fmt.Sprintf("%d", GetRootlessUID()))
			if err != nil {
				homeDir, homeDirErr = "", errors.Wrapf(err, "unable to resolve HOME directory")
				return
			}
			homeDir, homeDirErr = usr.HomeDir, nil
			return
		}
		homeDir, homeDirErr = home, nil
	})
	return homeDir, homeDirErr
}

func bailOnError(err error, format string, a ...interface{}) { // nolint: golint,goprintffuncname
	if err != nil {
		if format != "" {
			logrus.Errorf("%s: %v", fmt.Sprintf(format, a...), err)
		} else {
			logrus.Errorf("%v", err)
		}
		os.Exit(1)
	}
}