2015-12-10 14:35:53 +00:00
|
|
|
// +build windows
|
|
|
|
|
|
|
|
package system
|
|
|
|
|
2016-04-27 02:26:12 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CheckSystemDriveAndRemoveDriveLetter verifies and manipulates a Windows path.
|
|
|
|
// This is used, for example, when validating a user provided path in docker cp.
|
|
|
|
// If a drive letter is supplied, it must be the system drive. The drive letter
|
|
|
|
// is always removed. Also, it translates it to OS semantics (IOW / to \). We
|
2017-05-21 23:24:07 +00:00
|
|
|
// need the path in this syntax so that it can ultimately be concatenated with
|
2016-04-27 02:26:12 +00:00
|
|
|
// a Windows long-path which doesn't support drive-letters. Examples:
|
|
|
|
// C: --> Fail
|
|
|
|
// C:\ --> \
|
|
|
|
// a --> a
|
|
|
|
// /a --> \a
|
|
|
|
// d:\ --> Fail
|
|
|
|
func CheckSystemDriveAndRemoveDriveLetter(path string) (string, error) {
|
|
|
|
if len(path) == 2 && string(path[1]) == ":" {
|
|
|
|
return "", fmt.Errorf("No relative path specified in %q", path)
|
|
|
|
}
|
|
|
|
if !filepath.IsAbs(path) || len(path) < 2 {
|
|
|
|
return filepath.FromSlash(path), nil
|
|
|
|
}
|
|
|
|
if string(path[1]) == ":" && !strings.EqualFold(string(path[0]), "c") {
|
|
|
|
return "", fmt.Errorf("The specified path is not on the system drive (C:)")
|
|
|
|
}
|
|
|
|
return filepath.FromSlash(path[2:]), nil
|
|
|
|
}
|