internals_unix.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. // +build !windows
  2. package dockerfile
  3. import (
  4. "os"
  5. "path/filepath"
  6. )
  7. func fixPermissions(source, destination string, uid, gid int, destExisted bool) error {
  8. // If the destination didn't already exist, or the destination isn't a
  9. // directory, then we should Lchown the destination. Otherwise, we shouldn't
  10. // Lchown the destination.
  11. destStat, err := os.Stat(destination)
  12. if err != nil {
  13. // This should *never* be reached, because the destination must've already
  14. // been created while untar-ing the context.
  15. return err
  16. }
  17. doChownDestination := !destExisted || !destStat.IsDir()
  18. // We Walk on the source rather than on the destination because we don't
  19. // want to change permissions on things we haven't created or modified.
  20. return filepath.Walk(source, func(fullpath string, info os.FileInfo, err error) error {
  21. // Do not alter the walk root iff. it existed before, as it doesn't fall under
  22. // the domain of "things we should chown".
  23. if !doChownDestination && (source == fullpath) {
  24. return nil
  25. }
  26. // Path is prefixed by source: substitute with destination instead.
  27. cleaned, err := filepath.Rel(source, fullpath)
  28. if err != nil {
  29. return err
  30. }
  31. fullpath = filepath.Join(destination, cleaned)
  32. return os.Lchown(fullpath, uid, gid)
  33. })
  34. }