archive_unix.go 3.2 KB

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