archive_unix.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. // CanonicalTarNameForPath returns platform-specific filepath
  32. // to canonical posix-style path for tar archival. p is relative
  33. // path.
  34. func CanonicalTarNameForPath(p string) string {
  35. return p // already unix-style
  36. }
  37. // chmodTarEntry is used to adjust the file permissions used in tar header based
  38. // on the platform the archival is done.
  39. func chmodTarEntry(perm os.FileMode) os.FileMode {
  40. return perm // noop for unix as golang APIs provide perm bits correctly
  41. }
  42. // statUnix populates hdr from system-dependent fields of fi without performing
  43. // any OS lookups.
  44. func statUnix(fi os.FileInfo, hdr *tar.Header) error {
  45. s, ok := fi.Sys().(*syscall.Stat_t)
  46. if !ok {
  47. return nil
  48. }
  49. hdr.Uid = int(s.Uid)
  50. hdr.Gid = int(s.Gid)
  51. if s.Mode&unix.S_IFBLK != 0 ||
  52. s.Mode&unix.S_IFCHR != 0 {
  53. hdr.Devmajor = int64(unix.Major(uint64(s.Rdev))) //nolint: unconvert
  54. hdr.Devminor = int64(unix.Minor(uint64(s.Rdev))) //nolint: unconvert
  55. }
  56. return nil
  57. }
  58. func getInodeFromStat(stat interface{}) (inode uint64, err error) {
  59. s, ok := stat.(*syscall.Stat_t)
  60. if ok {
  61. inode = s.Ino
  62. }
  63. return
  64. }
  65. func getFileUIDGID(stat interface{}) (idtools.Identity, error) {
  66. s, ok := stat.(*syscall.Stat_t)
  67. if !ok {
  68. return idtools.Identity{}, errors.New("cannot convert stat value to syscall.Stat_t")
  69. }
  70. return idtools.Identity{UID: int(s.Uid), GID: int(s.Gid)}, nil
  71. }
  72. // handleTarTypeBlockCharFifo is an OS-specific helper function used by
  73. // createTarFile to handle the following types of header: Block; Char; Fifo
  74. func handleTarTypeBlockCharFifo(hdr *tar.Header, path string) error {
  75. mode := uint32(hdr.Mode & 07777)
  76. switch hdr.Typeflag {
  77. case tar.TypeBlock:
  78. mode |= unix.S_IFBLK
  79. case tar.TypeChar:
  80. mode |= unix.S_IFCHR
  81. case tar.TypeFifo:
  82. mode |= unix.S_IFIFO
  83. }
  84. err := system.Mknod(path, mode, int(system.Mkdev(hdr.Devmajor, hdr.Devminor)))
  85. if errors.Is(err, syscall.EPERM) && userns.RunningInUserNS() {
  86. // In most cases, cannot create a device if running in user namespace
  87. err = nil
  88. }
  89. return err
  90. }
  91. func handleLChmod(hdr *tar.Header, path string, hdrInfo os.FileInfo) error {
  92. if hdr.Typeflag == tar.TypeLink {
  93. if fi, err := os.Lstat(hdr.Linkname); err == nil && (fi.Mode()&os.ModeSymlink == 0) {
  94. if err := os.Chmod(path, hdrInfo.Mode()); err != nil {
  95. return err
  96. }
  97. }
  98. } else if hdr.Typeflag != tar.TypeSymlink {
  99. if err := os.Chmod(path, hdrInfo.Mode()); err != nil {
  100. return err
  101. }
  102. }
  103. return nil
  104. }