archive_unix.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 seperate 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{}) (nlink uint32, 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. nlink = uint32(s.Nlink)
  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 major(device uint64) uint64 {
  51. return (device >> 8) & 0xfff
  52. }
  53. func minor(device uint64) uint64 {
  54. return (device & 0xff) | ((device >> 12) & 0xfff00)
  55. }
  56. // handleTarTypeBlockCharFifo is an OS-specific helper function used by
  57. // createTarFile to handle the following types of header: Block; Char; Fifo
  58. func handleTarTypeBlockCharFifo(hdr *tar.Header, path string) error {
  59. mode := uint32(hdr.Mode & 07777)
  60. switch hdr.Typeflag {
  61. case tar.TypeBlock:
  62. mode |= syscall.S_IFBLK
  63. case tar.TypeChar:
  64. mode |= syscall.S_IFCHR
  65. case tar.TypeFifo:
  66. mode |= syscall.S_IFIFO
  67. }
  68. if err := system.Mknod(path, mode, int(system.Mkdev(hdr.Devmajor, hdr.Devminor))); err != nil {
  69. return err
  70. }
  71. return nil
  72. }
  73. func handleLChmod(hdr *tar.Header, path string, hdrInfo os.FileInfo) error {
  74. if hdr.Typeflag == tar.TypeLink {
  75. if fi, err := os.Lstat(hdr.Linkname); err == nil && (fi.Mode()&os.ModeSymlink == 0) {
  76. if err := os.Chmod(path, hdrInfo.Mode()); err != nil {
  77. return err
  78. }
  79. }
  80. } else if hdr.Typeflag != tar.TypeSymlink {
  81. if err := os.Chmod(path, hdrInfo.Mode()); err != nil {
  82. return err
  83. }
  84. }
  85. return nil
  86. }