archive_unix.go 3.3 KB

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