/builder/dockerfile/dispatchers_windows.go

https://gitlab.com/vectorci/docker-1 · Go · 65 lines · 37 code · 6 blank · 22 comment · 12 complexity · 51f30e479a264578935d798ce03953b5 MD5 · raw file

  1. package dockerfile
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "regexp"
  7. "strings"
  8. "github.com/docker/docker/pkg/system"
  9. )
  10. // normaliseWorkdir normalises a user requested working directory in a
  11. // platform sematically consistent way.
  12. func normaliseWorkdir(current string, requested string) (string, error) {
  13. if requested == "" {
  14. return "", fmt.Errorf("cannot normalise nothing")
  15. }
  16. current = filepath.FromSlash(current)
  17. requested = filepath.FromSlash(requested)
  18. // Target semantics is C:\somefolder, specifically in the format:
  19. // UPPERCASEDriveLetter-Colon-Backslash-FolderName. We are already
  20. // guaranteed that `current`, if set, is consistent. This allows us to
  21. // cope correctly with any of the following in a Dockerfile:
  22. // WORKDIR a --> C:\a
  23. // WORKDIR c:\\foo --> C:\foo
  24. // WORKDIR \\foo --> C:\foo
  25. // WORKDIR /foo --> C:\foo
  26. // WORKDIR c:\\foo \ WORKDIR bar --> C:\foo --> C:\foo\bar
  27. // WORKDIR C:/foo \ WORKDIR bar --> C:\foo --> C:\foo\bar
  28. // WORKDIR C:/foo \ WORKDIR \\bar --> C:\foo --> C:\bar
  29. // WORKDIR /foo \ WORKDIR c:/bar --> C:\foo --> C:\bar
  30. if len(current) == 0 || system.IsAbs(requested) {
  31. if (requested[0] == os.PathSeparator) ||
  32. (len(requested) > 1 && string(requested[1]) != ":") ||
  33. (len(requested) == 1) {
  34. requested = filepath.Join(`C:\`, requested)
  35. }
  36. } else {
  37. requested = filepath.Join(current, requested)
  38. }
  39. // Upper-case drive letter
  40. return (strings.ToUpper(string(requested[0])) + requested[1:]), nil
  41. }
  42. func errNotJSON(command, original string) error {
  43. // For Windows users, give a hint if it looks like it might contain
  44. // a path which hasn't been escaped such as ["c:\windows\system32\prog.exe", "-param"],
  45. // as JSON must be escaped. Unfortunate...
  46. //
  47. // Specifically looking for quote-driveletter-colon-backslash, there's no
  48. // double backslash and a [] pair. No, this is not perfect, but it doesn't
  49. // have to be. It's simply a hint to make life a little easier.
  50. extra := ""
  51. original = filepath.FromSlash(strings.ToLower(strings.Replace(strings.ToLower(original), strings.ToLower(command)+" ", "", -1)))
  52. if len(regexp.MustCompile(`"[a-z]:\\.*`).FindStringSubmatch(original)) > 0 &&
  53. !strings.Contains(original, `\\`) &&
  54. strings.Contains(original, "[") &&
  55. strings.Contains(original, "]") {
  56. extra = fmt.Sprintf(`. It looks like '%s' includes a file path without an escaped back-slash. JSON requires back-slashes to be escaped such as ["c:\\path\\to\\file.exe", "/parameter"]`, original)
  57. }
  58. return fmt.Errorf("%s requires the arguments to be in JSON form%s", command, extra)
  59. }