Browse Source

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 <github@gone.nl>
Sebastiaan van Stijn 1 year ago
parent
commit
90f37f48e2

+ 12 - 0
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) {

+ 0 - 10
pkg/containerfs/containerfs_unix.go

@@ -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)
-}

+ 0 - 15
pkg/containerfs/containerfs_windows.go

@@ -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)
-}