archive_unix.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //go:build !windows
  2. // +build !windows
  3. package archive // import "github.com/docker/docker/pkg/archive"
  4. import (
  5. "archive/tar"
  6. "errors"
  7. "os"
  8. "path/filepath"
  9. "strings"
  10. "syscall"
  11. "github.com/containerd/containerd/pkg/userns"
  12. "github.com/docker/docker/pkg/idtools"
  13. "github.com/docker/docker/pkg/system"
  14. "golang.org/x/sys/unix"
  15. )
  16. func init() {
  17. sysStat = statUnix
  18. }
  19. // fixVolumePathPrefix does platform specific processing to ensure that if
  20. // the path being passed in is not in a volume path format, convert it to one.
  21. func fixVolumePathPrefix(srcPath string) string {
  22. return srcPath
  23. }
  24. // getWalkRoot calculates the root path when performing a TarWithOptions.
  25. // We use a separate function as this is platform specific. On Linux, we
  26. // can't use filepath.Join(srcPath,include) because this will clean away
  27. // a trailing "." or "/" which may be important.
  28. func getWalkRoot(srcPath string, include string) string {
  29. return strings.TrimSuffix(srcPath, string(filepath.Separator)) + string(filepath.Separator) + include
  30. }
  31. // chmodTarEntry is used to adjust the file permissions used in tar header based
  32. // on the platform the archival is done.
  33. func chmodTarEntry(perm os.FileMode) os.FileMode {
  34. return perm // noop for unix as golang APIs provide perm bits correctly
  35. }
  36. // statUnix populates hdr from system-dependent fields of fi without performing
  37. // any OS lookups.
  38. func statUnix(fi os.FileInfo, hdr *tar.Header) error {
  39. s, ok := fi.Sys().(*syscall.Stat_t)
  40. if !ok {
  41. return nil
  42. }
  43. hdr.Uid = int(s.Uid)
  44. hdr.Gid = int(s.Gid)
  45. if s.Mode&unix.S_IFBLK != 0 ||
  46. s.Mode&unix.S_IFCHR != 0 {
  47. hdr.Devmajor = int64(unix.Major(uint64(s.Rdev))) //nolint: unconvert
  48. hdr.Devminor = int64(unix.Minor(uint64(s.Rdev))) //nolint: unconvert
  49. }
  50. return nil
  51. }
  52. func getInodeFromStat(stat interface{}) (inode uint64, err error) {
  53. s, ok := stat.(*syscall.Stat_t)
  54. if ok {
  55. inode = s.Ino
  56. }
  57. return
  58. }
  59. func getFileUIDGID(stat interface{}) (idtools.Identity, error) {
  60. s, ok := stat.(*syscall.Stat_t)
  61. if !ok {
  62. return idtools.Identity{}, errors.New("cannot convert stat value to syscall.Stat_t")
  63. }
  64. return idtools.Identity{UID: int(s.Uid), GID: int(s.Gid)}, nil
  65. }
  66. // handleTarTypeBlockCharFifo is an OS-specific helper function used by
  67. // createTarFile to handle the following types of header: Block; Char; Fifo
  68. func handleTarTypeBlockCharFifo(hdr *tar.Header, path string) error {
  69. mode := uint32(hdr.Mode & 07777)
  70. switch hdr.Typeflag {
  71. case tar.TypeBlock:
  72. mode |= unix.S_IFBLK
  73. case tar.TypeChar:
  74. mode |= unix.S_IFCHR
  75. case tar.TypeFifo:
  76. mode |= unix.S_IFIFO
  77. }
  78. err := system.Mknod(path, mode, int(system.Mkdev(hdr.Devmajor, hdr.Devminor)))
  79. if errors.Is(err, syscall.EPERM) && userns.RunningInUserNS() {
  80. // In most cases, cannot create a device if running in user namespace
  81. err = nil
  82. }
  83. return err
  84. }
  85. func handleLChmod(hdr *tar.Header, path string, hdrInfo os.FileInfo) error {
  86. if hdr.Typeflag == tar.TypeLink {
  87. if fi, err := os.Lstat(hdr.Linkname); err == nil && (fi.Mode()&os.ModeSymlink == 0) {
  88. if err := os.Chmod(path, hdrInfo.Mode()); err != nil {
  89. return err
  90. }
  91. }
  92. } else if hdr.Typeflag != tar.TypeSymlink {
  93. if err := os.Chmod(path, hdrInfo.Mode()); err != nil {
  94. return err
  95. }
  96. }
  97. return nil
  98. }