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>
This commit is contained in:
Sebastiaan van Stijn 2024-01-02 15:26:29 +01:00
parent a5b4670c79
commit 90f37f48e2
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C
3 changed files with 12 additions and 25 deletions

View file

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

View file

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

View file

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