archive_unix.go 3.0 KB

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