archive_unix.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // +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. "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 {
  30. return p // 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(unix.Major(uint64(s.Rdev))) // nolint: unconvert
  44. hdr.Devminor = int64(unix.Minor(uint64(s.Rdev))) // nolint: unconvert
  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 = s.Ino
  53. }
  54. return
  55. }
  56. func getFileUIDGID(stat interface{}) (idtools.Identity, error) {
  57. s, ok := stat.(*syscall.Stat_t)
  58. if !ok {
  59. return idtools.Identity{}, errors.New("cannot convert stat value to syscall.Stat_t")
  60. }
  61. return idtools.Identity{UID: int(s.Uid), GID: int(s.Gid)}, nil
  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 |= unix.S_IFBLK
  74. case tar.TypeChar:
  75. mode |= unix.S_IFCHR
  76. case tar.TypeFifo:
  77. mode |= unix.S_IFIFO
  78. }
  79. return system.Mknod(path, mode, int(system.Mkdev(hdr.Devmajor, hdr.Devminor)))
  80. }
  81. func handleLChmod(hdr *tar.Header, path string, hdrInfo os.FileInfo) error {
  82. if hdr.Typeflag == tar.TypeLink {
  83. if fi, err := os.Lstat(hdr.Linkname); err == nil && (fi.Mode()&os.ModeSymlink == 0) {
  84. if err := os.Chmod(path, hdrInfo.Mode()); err != nil {
  85. return err
  86. }
  87. }
  88. } else if hdr.Typeflag != tar.TypeSymlink {
  89. if err := os.Chmod(path, hdrInfo.Mode()); err != nil {
  90. return err
  91. }
  92. }
  93. return nil
  94. }