archive_unix.go 3.2 KB

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