archive_unix.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //go:build !windows
  2. package archive // import "github.com/docker/docker/pkg/archive"
  3. import (
  4. "archive/tar"
  5. "errors"
  6. "os"
  7. "path/filepath"
  8. "runtime"
  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. // Devmajor and Devminor are only needed for special devices.
  40. // In FreeBSD, RDev for regular files is -1 (unless overridden by FS):
  41. // https://cgit.freebsd.org/src/tree/sys/kern/vfs_default.c?h=stable/13#n1531
  42. // (NODEV is -1: https://cgit.freebsd.org/src/tree/sys/sys/param.h?h=stable/13#n241).
  43. // ZFS in particular does not override the default:
  44. // https://cgit.freebsd.org/src/tree/sys/contrib/openzfs/module/os/freebsd/zfs/zfs_vnops_os.c?h=stable/13#n2027
  45. // Since `Stat_t.Rdev` is uint64, the cast turns -1 into (2^64 - 1).
  46. // Such large values cannot be encoded in a tar header.
  47. if runtime.GOOS == "freebsd" && hdr.Typeflag != tar.TypeBlock && hdr.Typeflag != tar.TypeChar {
  48. return nil
  49. }
  50. s, ok := fi.Sys().(*syscall.Stat_t)
  51. if !ok {
  52. return nil
  53. }
  54. hdr.Uid = int(s.Uid)
  55. hdr.Gid = int(s.Gid)
  56. if s.Mode&unix.S_IFBLK != 0 ||
  57. s.Mode&unix.S_IFCHR != 0 {
  58. hdr.Devmajor = int64(unix.Major(uint64(s.Rdev))) //nolint: unconvert
  59. hdr.Devminor = int64(unix.Minor(uint64(s.Rdev))) //nolint: unconvert
  60. }
  61. return nil
  62. }
  63. func getInodeFromStat(stat interface{}) (inode uint64, err error) {
  64. s, ok := stat.(*syscall.Stat_t)
  65. if ok {
  66. inode = s.Ino
  67. }
  68. return
  69. }
  70. func getFileUIDGID(stat interface{}) (idtools.Identity, error) {
  71. s, ok := stat.(*syscall.Stat_t)
  72. if !ok {
  73. return idtools.Identity{}, errors.New("cannot convert stat value to syscall.Stat_t")
  74. }
  75. return idtools.Identity{UID: int(s.Uid), GID: int(s.Gid)}, nil
  76. }
  77. // handleTarTypeBlockCharFifo is an OS-specific helper function used by
  78. // createTarFile to handle the following types of header: Block; Char; Fifo
  79. func handleTarTypeBlockCharFifo(hdr *tar.Header, path string) error {
  80. mode := uint32(hdr.Mode & 0o7777)
  81. switch hdr.Typeflag {
  82. case tar.TypeBlock:
  83. mode |= unix.S_IFBLK
  84. case tar.TypeChar:
  85. mode |= unix.S_IFCHR
  86. case tar.TypeFifo:
  87. mode |= unix.S_IFIFO
  88. }
  89. err := system.Mknod(path, mode, int(system.Mkdev(hdr.Devmajor, hdr.Devminor)))
  90. if errors.Is(err, syscall.EPERM) && userns.RunningInUserNS() {
  91. // In most cases, cannot create a device if running in user namespace
  92. err = nil
  93. }
  94. return err
  95. }
  96. func handleLChmod(hdr *tar.Header, path string, hdrInfo os.FileInfo) error {
  97. if hdr.Typeflag == tar.TypeLink {
  98. if fi, err := os.Lstat(hdr.Linkname); err == nil && (fi.Mode()&os.ModeSymlink == 0) {
  99. if err := os.Chmod(path, hdrInfo.Mode()); err != nil {
  100. return err
  101. }
  102. }
  103. } else if hdr.Typeflag != tar.TypeSymlink {
  104. if err := os.Chmod(path, hdrInfo.Mode()); err != nil {
  105. return err
  106. }
  107. }
  108. return nil
  109. }