archive_unix.go 3.2 KB

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