diff --git a/internal/lookup/root/root.go b/internal/lookup/root/root.go index 1d5e6bd7..d0c83701 100644 --- a/internal/lookup/root/root.go +++ b/internal/lookup/root/root.go @@ -19,6 +19,7 @@ package root import ( "os" "path/filepath" + "strings" "github.com/NVIDIA/nvidia-container-toolkit/internal/logger" "github.com/NVIDIA/nvidia-container-toolkit/internal/lookup" @@ -47,6 +48,18 @@ func New(opts ...Option) *Driver { return d } +// RelativeToRoot returns the specified path relative to the driver root. +func (r *Driver) RelativeToRoot(path string) string { + if r.Root == "" || r.Root == "/" { + return path + } + if !filepath.IsAbs(path) { + return path + } + + return strings.TrimPrefix(path, r.Root) +} + // Files returns a Locator for arbitrary driver files. func (r *Driver) Files(opts ...lookup.Option) lookup.Locator { return lookup.NewFileLocator(