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 <elezar@nvidia.com>
This commit is contained in:
Evan Lezar 2024-06-24 15:47:05 +02:00
parent aae3da88c3
commit 55ea268829

View File

@ -19,6 +19,7 @@ package root
import ( import (
"os" "os"
"path/filepath" "path/filepath"
"strings"
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger" "github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
"github.com/NVIDIA/nvidia-container-toolkit/internal/lookup" "github.com/NVIDIA/nvidia-container-toolkit/internal/lookup"
@ -47,6 +48,18 @@ func New(opts ...Option) *Driver {
return d 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. // Files returns a Locator for arbitrary driver files.
func (r *Driver) Files(opts ...lookup.Option) lookup.Locator { func (r *Driver) Files(opts ...lookup.Option) lookup.Locator {
return lookup.NewFileLocator( return lookup.NewFileLocator(