archive_unix.go 3.2 KB

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