copy_unix.go 976 B

123456789101112131415161718192021222324252627282930313233343536
  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) error {
  9. skipChownRoot, err := isExistingDirectory(destination)
  10. if err != nil {
  11. return err
  12. }
  13. // We Walk on the source rather than on the destination because we don't
  14. // want to change permissions on things we haven't created or modified.
  15. return filepath.Walk(source, func(fullpath string, info os.FileInfo, err error) error {
  16. // Do not alter the walk root iff. it existed before, as it doesn't fall under
  17. // the domain of "things we should chown".
  18. if skipChownRoot && source == fullpath {
  19. return nil
  20. }
  21. // Path is prefixed by source: substitute with destination instead.
  22. cleaned, err := filepath.Rel(source, fullpath)
  23. if err != nil {
  24. return err
  25. }
  26. fullpath = filepath.Join(destination, cleaned)
  27. return os.Lchown(fullpath, rootIDs.UID, rootIDs.GID)
  28. })
  29. }