copy_unix.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. //go:build !windows
  2. // +build !windows
  3. package dockerfile // import "github.com/docker/docker/builder/dockerfile"
  4. import (
  5. "os"
  6. "path"
  7. "path/filepath"
  8. "strings"
  9. "github.com/docker/docker/pkg/idtools"
  10. )
  11. func fixPermissions(source, destination string, identity idtools.Identity, overrideSkip bool) error {
  12. var (
  13. skipChownRoot bool
  14. err error
  15. )
  16. if !overrideSkip {
  17. skipChownRoot, err = isExistingDirectory(destination)
  18. if err != nil {
  19. return err
  20. }
  21. }
  22. // We Walk on the source rather than on the destination because we don't
  23. // want to change permissions on things we haven't created or modified.
  24. return filepath.Walk(source, func(fullpath string, _ os.FileInfo, _ error) error {
  25. // Do not alter the walk root iff. it existed before, as it doesn't fall under
  26. // the domain of "things we should chown".
  27. if skipChownRoot && source == fullpath {
  28. return nil
  29. }
  30. // Path is prefixed by source: substitute with destination instead.
  31. cleaned, err := filepath.Rel(source, fullpath)
  32. if err != nil {
  33. return err
  34. }
  35. fullpath = filepath.Join(destination, cleaned)
  36. return os.Lchown(fullpath, identity.UID, identity.GID)
  37. })
  38. }
  39. // normalizeDest normalises the destination of a COPY/ADD command in a
  40. // platform semantically consistent way.
  41. func normalizeDest(workingDir, requested string) (string, error) {
  42. dest := filepath.FromSlash(requested)
  43. endsInSlash := strings.HasSuffix(dest, string(os.PathSeparator))
  44. if !path.IsAbs(requested) {
  45. dest = path.Join("/", filepath.ToSlash(workingDir), dest)
  46. // Make sure we preserve any trailing slash
  47. if endsInSlash {
  48. dest += "/"
  49. }
  50. }
  51. return dest, nil
  52. }
  53. func containsWildcards(name string) bool {
  54. for i := 0; i < len(name); i++ {
  55. ch := name[i]
  56. if ch == '\\' {
  57. i++
  58. } else if ch == '*' || ch == '?' || ch == '[' {
  59. return true
  60. }
  61. }
  62. return false
  63. }
  64. func validateCopySourcePath(imageSource *imageMount, origPath string) error {
  65. return nil
  66. }