copy_unix.go 1.9 KB

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