archive_unix.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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, ta *tarAppender, name string, stat interface{}) (inode uint64, 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. inode = uint64(s.Ino)
  42. // Currently go does not fill in the major/minors
  43. if s.Mode&syscall.S_IFBLK != 0 ||
  44. s.Mode&syscall.S_IFCHR != 0 {
  45. hdr.Devmajor = int64(major(uint64(s.Rdev)))
  46. hdr.Devminor = int64(minor(uint64(s.Rdev)))
  47. }
  48. return
  49. }
  50. func getFileUIDGID(stat interface{}) (int, int, error) {
  51. s, ok := stat.(*syscall.Stat_t)
  52. if !ok {
  53. return -1, -1, errors.New("cannot convert stat value to syscall.Stat_t")
  54. }
  55. return int(s.Uid), int(s.Gid), nil
  56. }
  57. func major(device uint64) uint64 {
  58. return (device >> 8) & 0xfff
  59. }
  60. func minor(device uint64) uint64 {
  61. return (device & 0xff) | ((device >> 12) & 0xfff00)
  62. }
  63. // handleTarTypeBlockCharFifo is an OS-specific helper function used by
  64. // createTarFile to handle the following types of header: Block; Char; Fifo
  65. func handleTarTypeBlockCharFifo(hdr *tar.Header, path string) error {
  66. if rsystem.RunningInUserNS() {
  67. // cannot create a device if running in user namespace
  68. return nil
  69. }
  70. mode := uint32(hdr.Mode & 07777)
  71. switch hdr.Typeflag {
  72. case tar.TypeBlock:
  73. mode |= syscall.S_IFBLK
  74. case tar.TypeChar:
  75. mode |= syscall.S_IFCHR
  76. case tar.TypeFifo:
  77. mode |= syscall.S_IFIFO
  78. }
  79. if err := system.Mknod(path, mode, int(system.Mkdev(hdr.Devmajor, hdr.Devminor))); err != nil {
  80. return err
  81. }
  82. return nil
  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. }