internals_windows.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package dockerfile
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "strings"
  7. "github.com/docker/docker/pkg/system"
  8. )
  9. // normaliseDest normalises the destination of a COPY/ADD command in a
  10. // platform semantically consistent way.
  11. func normaliseDest(cmdName, workingDir, requested string) (string, error) {
  12. dest := filepath.FromSlash(requested)
  13. endsInSlash := strings.HasSuffix(dest, string(os.PathSeparator))
  14. // We are guaranteed that the working directory is already consistent,
  15. // However, Windows also has, for now, the limitation that ADD/COPY can
  16. // only be done to the system drive, not any drives that might be present
  17. // as a result of a bind mount.
  18. //
  19. // So... if the path requested is Linux-style absolute (/foo or \\foo),
  20. // we assume it is the system drive. If it is a Windows-style absolute
  21. // (DRIVE:\\foo), error if DRIVE is not C. And finally, ensure we
  22. // strip any configured working directories drive letter so that it
  23. // can be subsequently legitimately converted to a Windows volume-style
  24. // pathname.
  25. // Not a typo - filepath.IsAbs, not system.IsAbs on this next check as
  26. // we only want to validate where the DriveColon part has been supplied.
  27. if filepath.IsAbs(dest) {
  28. if strings.ToUpper(string(dest[0])) != "C" {
  29. return "", fmt.Errorf("Windows does not support %s with a destinations not on the system drive (C:)", cmdName)
  30. }
  31. dest = dest[2:] // Strip the drive letter
  32. }
  33. // Cannot handle relative where WorkingDir is not the system drive.
  34. if len(workingDir) > 0 {
  35. if ((len(workingDir) > 1) && !system.IsAbs(workingDir[2:])) || (len(workingDir) == 1) {
  36. return "", fmt.Errorf("Current WorkingDir %s is not platform consistent", workingDir)
  37. }
  38. if !system.IsAbs(dest) {
  39. if string(workingDir[0]) != "C" {
  40. return "", fmt.Errorf("Windows does not support %s with relative paths when WORKDIR is not the system drive", cmdName)
  41. }
  42. dest = filepath.Join(string(os.PathSeparator), workingDir[2:], dest)
  43. // Make sure we preserve any trailing slash
  44. if endsInSlash {
  45. dest += string(os.PathSeparator)
  46. }
  47. }
  48. }
  49. return dest, nil
  50. }