From 90f37f48e23a3da1069abe75b0c3f9877bcb3efd Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Tue, 2 Jan 2024 15:26:29 +0100 Subject: [PATCH] pkg/containerfs: unify CleanScopedPath implementation Use stdlib's filepath.VolumeName to get the volume-name (if present) instead of a self-crafted implementation, and unify the implementations for Windows and Unix. Signed-off-by: Sebastiaan van Stijn --- pkg/containerfs/containerfs.go | 12 ++++++++++++ pkg/containerfs/containerfs_unix.go | 10 ---------- pkg/containerfs/containerfs_windows.go | 15 --------------- 3 files changed, 12 insertions(+), 25 deletions(-) delete mode 100644 pkg/containerfs/containerfs_unix.go delete mode 100644 pkg/containerfs/containerfs_windows.go diff --git a/pkg/containerfs/containerfs.go b/pkg/containerfs/containerfs.go index e65f783a23..186c138f81 100644 --- a/pkg/containerfs/containerfs.go +++ b/pkg/containerfs/containerfs.go @@ -6,6 +6,18 @@ import ( "github.com/moby/sys/symlink" ) +// CleanScopedPath prepares the given path to be combined with a mount path or +// a drive-letter. On Windows, it removes any existing driveletter (e.g. "C:"). +// The returned path is always prefixed with a [filepath.Separator]. +func CleanScopedPath(path string) string { + if len(path) >= 2 { + if v := filepath.VolumeName(path); len(v) > 0 { + path = path[len(v):] + } + } + return filepath.Join(string(filepath.Separator), path) +} + // ResolveScopedPath evaluates the given path scoped to the root. // For example, if root=/a, and path=/b/c, then this function would return /a/b/c. func ResolveScopedPath(root, path string) (string, error) { diff --git a/pkg/containerfs/containerfs_unix.go b/pkg/containerfs/containerfs_unix.go deleted file mode 100644 index 1e0b8a99a6..0000000000 --- a/pkg/containerfs/containerfs_unix.go +++ /dev/null @@ -1,10 +0,0 @@ -//go:build !windows - -package containerfs // import "github.com/docker/docker/pkg/containerfs" - -import "path/filepath" - -// CleanScopedPath preappends a to combine with a mnt path. -func CleanScopedPath(path string) string { - return filepath.Join(string(filepath.Separator), path) -} diff --git a/pkg/containerfs/containerfs_windows.go b/pkg/containerfs/containerfs_windows.go deleted file mode 100644 index f4011d1203..0000000000 --- a/pkg/containerfs/containerfs_windows.go +++ /dev/null @@ -1,15 +0,0 @@ -package containerfs // import "github.com/docker/docker/pkg/containerfs" - -import "path/filepath" - -// CleanScopedPath removes the C:\ syntax, and prepares to combine -// with a volume path -func CleanScopedPath(path string) string { - if len(path) >= 2 { - c := path[0] - if path[1] == ':' && ('a' <= c && c <= 'z' || 'A' <= c && c <= 'Z') { - path = path[2:] - } - } - return filepath.Join(string(filepath.Separator), path) -}