copy_unix.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // +build !windows
  2. package dockerfile
  3. import (
  4. "os"
  5. "path/filepath"
  6. "github.com/docker/docker/pkg/idtools"
  7. )
  8. func fixPermissions(source, destination string, rootIDs idtools.IDPair, overrideSkip bool) error {
  9. var (
  10. skipChownRoot bool
  11. err error
  12. )
  13. if !overrideSkip {
  14. skipChownRoot, err = isExistingDirectory(destination)
  15. if err != nil {
  16. return err
  17. }
  18. }
  19. // We Walk on the source rather than on the destination because we don't
  20. // want to change permissions on things we haven't created or modified.
  21. return filepath.Walk(source, func(fullpath string, info os.FileInfo, err error) error {
  22. // Do not alter the walk root iff. it existed before, as it doesn't fall under
  23. // the domain of "things we should chown".
  24. if skipChownRoot && source == fullpath {
  25. return nil
  26. }
  27. // Path is prefixed by source: substitute with destination instead.
  28. cleaned, err := filepath.Rel(source, fullpath)
  29. if err != nil {
  30. return err
  31. }
  32. fullpath = filepath.Join(destination, cleaned)
  33. return os.Lchown(fullpath, rootIDs.UID, rootIDs.GID)
  34. })
  35. }