Skip to content

Commit 7a9bc14

Browse files
authored
Merge pull request #425 from jmbaur/discover-use-xdg
Use XDG_DATA_DIRS instead of hardcoding /usr/share
2 parents 29c0f82 + 5788e62 commit 7a9bc14

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

internal/lookup/root/root.go

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package root
1818

1919
import (
20+
"os"
2021
"path/filepath"
2122

2223
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
@@ -59,18 +60,24 @@ func (r *Driver) Libraries() lookup.Locator {
5960
// If configSearchPaths is specified, these paths are used as absolute paths,
6061
// otherwise, /etc and /usr/share are searched.
6162
func (r *Driver) Configs() lookup.Locator {
62-
searchRoot := r.Root
63-
searchPaths := []string{"/etc", "/usr/share"}
63+
return lookup.NewFileLocator(r.configSearchOptions()...)
64+
}
65+
66+
func (r *Driver) configSearchOptions() []lookup.Option {
6467
if len(r.configSearchPaths) > 0 {
65-
searchRoot = "/"
66-
searchPaths = normalizeSearchPaths(r.configSearchPaths...)
68+
return []lookup.Option{
69+
lookup.WithLogger(r.logger),
70+
lookup.WithRoot("/"),
71+
lookup.WithSearchPaths(normalizeSearchPaths(r.configSearchPaths...)...),
72+
}
6773
}
68-
69-
return lookup.NewFileLocator(
74+
searchPaths := []string{"/etc"}
75+
searchPaths = append(searchPaths, xdgDataDirs()...)
76+
return []lookup.Option{
7077
lookup.WithLogger(r.logger),
71-
lookup.WithRoot(searchRoot),
78+
lookup.WithRoot(r.Root),
7279
lookup.WithSearchPaths(searchPaths...),
73-
)
80+
}
7481
}
7582

7683
// normalizeSearchPaths takes a list of paths and normalized these.
@@ -85,3 +92,13 @@ func normalizeSearchPaths(paths ...string) []string {
8592
}
8693
return normalized
8794
}
95+
96+
// xdgDataDirs finds the paths as specified in the environment variable XDG_DATA_DIRS.
97+
// See https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html.
98+
func xdgDataDirs() []string {
99+
if dirs, exists := os.LookupEnv("XDG_DATA_DIRS"); exists && dirs != "" {
100+
return normalizeSearchPaths(dirs)
101+
}
102+
103+
return []string{"/usr/local/share", "/usr/share"}
104+
}

0 commit comments

Comments
 (0)