path_windows.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. // +build windows
  2. package system
  3. import (
  4. "fmt"
  5. "path/filepath"
  6. "strings"
  7. )
  8. // DefaultPathEnv is deliberately empty on Windows as the default path will be set by
  9. // the container. Docker has no context of what the default path should be.
  10. const DefaultPathEnv = ""
  11. // CheckSystemDriveAndRemoveDriveLetter verifies and manipulates a Windows path.
  12. // This is used, for example, when validating a user provided path in docker cp.
  13. // If a drive letter is supplied, it must be the system drive. The drive letter
  14. // is always removed. Also, it translates it to OS semantics (IOW / to \). We
  15. // need the path in this syntax so that it can ultimately be contatenated with
  16. // a Windows long-path which doesn't support drive-letters. Examples:
  17. // C: --> Fail
  18. // C:\ --> \
  19. // a --> a
  20. // /a --> \a
  21. // d:\ --> Fail
  22. func CheckSystemDriveAndRemoveDriveLetter(path string) (string, error) {
  23. if len(path) == 2 && string(path[1]) == ":" {
  24. return "", fmt.Errorf("No relative path specified in %q", path)
  25. }
  26. if !filepath.IsAbs(path) || len(path) < 2 {
  27. return filepath.FromSlash(path), nil
  28. }
  29. if string(path[1]) == ":" && !strings.EqualFold(string(path[0]), "c") {
  30. return "", fmt.Errorf("The specified path is not on the system drive (C:)")
  31. }
  32. return filepath.FromSlash(path[2:]), nil
  33. }