From 55ea2688292557cbdf99f241bec9a40d4cc317a9 Mon Sep 17 00:00:00 2001 From: Evan Lezar Date: Mon, 24 Jun 2024 15:47:05 +0200 Subject: [PATCH] Add RelativeToRoot function to Driver This adds a function to return a path as a path relative to the specified driver root. Signed-off-by: Evan Lezar --- internal/lookup/root/root.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) 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(