Skip to content

Commit a5cdb0d

Browse files
committed
defaults/darwin: align paths with root and XDG conventions
1 parent 45e76e3 commit a5cdb0d

File tree

1 file changed

+61
-5
lines changed

1 file changed

+61
-5
lines changed

pkg/defaults/defaults_darwin.go

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@
1616

1717
// This is a dummy file to allow usage of library functions
1818
// on Darwin-based systems.
19-
// All functions and variables are empty/no-ops
19+
// Most functions and variables are stubs/no-ops
2020

2121
package defaults
2222

2323
import (
24+
"fmt"
2425
"os"
2526
"path/filepath"
2627

@@ -38,18 +39,24 @@ func CNIPath() string {
3839
}
3940

4041
func CNIRuntimeDir() (string, error) {
42+
if os.Geteuid() != 0 {
43+
return filepath.Join(xdgRuntimeDir(), "cni"), nil
44+
}
4145
return "/var/run/cni", nil
4246
}
4347

4448
func CNINetConfPath() string {
49+
if os.Geteuid() != 0 {
50+
return filepath.Join(xdgConfigHome(), "cni", "net.d")
51+
}
4552
return gocni.DefaultNetDir
4653
}
4754

4855
func DataRoot() string {
49-
if home, err := os.UserHomeDir(); err == nil && home != "" {
50-
return filepath.Join(home, ".local", "share", "nerdctl")
56+
if os.Geteuid() == 0 {
57+
return "/var/lib/nerdctl"
5158
}
52-
return "/var/lib/nerdctl"
59+
return filepath.Join(xdgDataHome(), "nerdctl")
5360
}
5461

5562
func CgroupManager() string {
@@ -61,17 +68,66 @@ func CgroupnsMode() string {
6168
}
6269

6370
func NerdctlTOML() string {
71+
if os.Geteuid() != 0 {
72+
return filepath.Join(xdgConfigHome(), "nerdctl", "nerdctl.toml")
73+
}
6474
return "/etc/nerdctl/nerdctl.toml"
6575
}
6676

6777
func HostsDirs() []string {
68-
return []string{}
78+
if os.Geteuid() != 0 {
79+
xch := xdgConfigHome()
80+
return []string{
81+
filepath.Join(xch, "containerd", "certs.d"),
82+
filepath.Join(xch, "docker", "certs.d"),
83+
}
84+
}
85+
return []string{"/etc/containerd/certs.d", "/etc/docker/certs.d"}
6986
}
7087

7188
func HostGatewayIP() string {
7289
return ""
7390
}
7491

7592
func CDISpecDirs() []string {
93+
if os.Geteuid() != 0 {
94+
return []string{
95+
filepath.Join(xdgConfigHome(), "cdi"),
96+
filepath.Join(xdgRuntimeDir(), "cdi"),
97+
}
98+
}
7699
return []string{"/etc/cdi", "/var/run/cdi"}
77100
}
101+
102+
func xdgConfigHome() string {
103+
if xch := os.Getenv("XDG_CONFIG_HOME"); xch != "" {
104+
return xch
105+
}
106+
if home := os.Getenv("HOME"); home != "" {
107+
return filepath.Join(home, ".config")
108+
}
109+
if home, err := os.UserHomeDir(); err == nil && home != "" {
110+
return filepath.Join(home, ".config")
111+
}
112+
return "/etc"
113+
}
114+
115+
func xdgDataHome() string {
116+
if xdh := os.Getenv("XDG_DATA_HOME"); xdh != "" {
117+
return xdh
118+
}
119+
if home := os.Getenv("HOME"); home != "" {
120+
return filepath.Join(home, ".local", "share")
121+
}
122+
if home, err := os.UserHomeDir(); err == nil && home != "" {
123+
return filepath.Join(home, ".local", "share")
124+
}
125+
return "/var/lib"
126+
}
127+
128+
func xdgRuntimeDir() string {
129+
if xdr := os.Getenv("XDG_RUNTIME_DIR"); xdr != "" {
130+
return xdr
131+
}
132+
return fmt.Sprintf("/run/user/%d", os.Geteuid())
133+
}

0 commit comments

Comments
 (0)