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