Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 deletions internal/lookup/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package root

import (
"os"
"path/filepath"

"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
Expand Down Expand Up @@ -59,18 +60,24 @@ func (r *Driver) Libraries() lookup.Locator {
// If configSearchPaths is specified, these paths are used as absolute paths,
// otherwise, /etc and /usr/share are searched.
func (r *Driver) Configs() lookup.Locator {
searchRoot := r.Root
searchPaths := []string{"/etc", "/usr/share"}
return lookup.NewFileLocator(r.configSearchOptions()...)
}

func (r *Driver) configSearchOptions() []lookup.Option {
if len(r.configSearchPaths) > 0 {
searchRoot = "/"
searchPaths = normalizeSearchPaths(r.configSearchPaths...)
return []lookup.Option{
lookup.WithLogger(r.logger),
lookup.WithRoot("/"),
lookup.WithSearchPaths(normalizeSearchPaths(r.configSearchPaths...)...),
}
}

return lookup.NewFileLocator(
searchPaths := []string{"/etc"}
searchPaths = append(searchPaths, xdgDataDirs()...)
return []lookup.Option{
lookup.WithLogger(r.logger),
lookup.WithRoot(searchRoot),
lookup.WithRoot(r.Root),
lookup.WithSearchPaths(searchPaths...),
)
}
}

// normalizeSearchPaths takes a list of paths and normalized these.
Expand All @@ -85,3 +92,13 @@ func normalizeSearchPaths(paths ...string) []string {
}
return normalized
}

// xdgDataDirs finds the paths as specified in the environment variable XDG_DATA_DIRS.
// See https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html.
func xdgDataDirs() []string {
if dirs, exists := os.LookupEnv("XDG_DATA_DIRS"); exists && dirs != "" {
return normalizeSearchPaths(dirs)
}

return []string{"/usr/local/share", "/usr/share"}
}