internals_unix.go 1.4 KB

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