copy_unix.go 2.0 KB

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