copy_unix.go 2.0 KB

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