diff.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package archive
  2. import (
  3. "fmt"
  4. "io"
  5. "io/ioutil"
  6. "os"
  7. "path/filepath"
  8. "strings"
  9. "syscall"
  10. "github.com/docker/docker/vendor/src/code.google.com/p/go/src/pkg/archive/tar"
  11. "github.com/docker/docker/pkg/pools"
  12. )
  13. // Linux device nodes are a bit weird due to backwards compat with 16 bit device nodes.
  14. // They are, from low to high: the lower 8 bits of the minor, then 12 bits of the major,
  15. // then the top 12 bits of the minor
  16. func mkdev(major int64, minor int64) uint32 {
  17. return uint32(((minor & 0xfff00) << 12) | ((major & 0xfff) << 8) | (minor & 0xff))
  18. }
  19. func UnpackLayer(dest string, layer ArchiveReader) error {
  20. tr := tar.NewReader(layer)
  21. trBuf := pools.BufioReader32KPool.Get(tr)
  22. defer pools.BufioReader32KPool.Put(trBuf)
  23. var dirs []*tar.Header
  24. aufsTempdir := ""
  25. aufsHardlinks := make(map[string]*tar.Header)
  26. // Iterate through the files in the archive.
  27. for {
  28. hdr, err := tr.Next()
  29. if err == io.EOF {
  30. // end of tar archive
  31. break
  32. }
  33. if err != nil {
  34. return err
  35. }
  36. // Normalize name, for safety and for a simple is-root check
  37. hdr.Name = filepath.Clean(hdr.Name)
  38. if !strings.HasSuffix(hdr.Name, "/") {
  39. // Not the root directory, ensure that the parent directory exists.
  40. // This happened in some tests where an image had a tarfile without any
  41. // parent directories.
  42. parent := filepath.Dir(hdr.Name)
  43. parentPath := filepath.Join(dest, parent)
  44. if _, err := os.Lstat(parentPath); err != nil && os.IsNotExist(err) {
  45. err = os.MkdirAll(parentPath, 0600)
  46. if err != nil {
  47. return err
  48. }
  49. }
  50. }
  51. // Skip AUFS metadata dirs
  52. if strings.HasPrefix(hdr.Name, ".wh..wh.") {
  53. // Regular files inside /.wh..wh.plnk can be used as hardlink targets
  54. // We don't want this directory, but we need the files in them so that
  55. // such hardlinks can be resolved.
  56. if strings.HasPrefix(hdr.Name, ".wh..wh.plnk") && hdr.Typeflag == tar.TypeReg {
  57. basename := filepath.Base(hdr.Name)
  58. aufsHardlinks[basename] = hdr
  59. if aufsTempdir == "" {
  60. if aufsTempdir, err = ioutil.TempDir("", "dockerplnk"); err != nil {
  61. return err
  62. }
  63. defer os.RemoveAll(aufsTempdir)
  64. }
  65. if err := createTarFile(filepath.Join(aufsTempdir, basename), dest, hdr, tr, true); err != nil {
  66. return err
  67. }
  68. }
  69. continue
  70. }
  71. path := filepath.Join(dest, hdr.Name)
  72. rel, err := filepath.Rel(dest, path)
  73. if err != nil {
  74. return err
  75. }
  76. if strings.HasPrefix(rel, "..") {
  77. return breakoutError(fmt.Errorf("%q is outside of %q", hdr.Name, dest))
  78. }
  79. base := filepath.Base(path)
  80. if strings.HasPrefix(base, ".wh.") {
  81. originalBase := base[len(".wh."):]
  82. originalPath := filepath.Join(filepath.Dir(path), originalBase)
  83. if err := os.RemoveAll(originalPath); err != nil {
  84. return err
  85. }
  86. } else {
  87. // If path exits we almost always just want to remove and replace it.
  88. // The only exception is when it is a directory *and* the file from
  89. // the layer is also a directory. Then we want to merge them (i.e.
  90. // just apply the metadata from the layer).
  91. if fi, err := os.Lstat(path); err == nil {
  92. if !(fi.IsDir() && hdr.Typeflag == tar.TypeDir) {
  93. if err := os.RemoveAll(path); err != nil {
  94. return err
  95. }
  96. }
  97. }
  98. trBuf.Reset(tr)
  99. srcData := io.Reader(trBuf)
  100. srcHdr := hdr
  101. // Hard links into /.wh..wh.plnk don't work, as we don't extract that directory, so
  102. // we manually retarget these into the temporary files we extracted them into
  103. if hdr.Typeflag == tar.TypeLink && strings.HasPrefix(filepath.Clean(hdr.Linkname), ".wh..wh.plnk") {
  104. linkBasename := filepath.Base(hdr.Linkname)
  105. srcHdr = aufsHardlinks[linkBasename]
  106. if srcHdr == nil {
  107. return fmt.Errorf("Invalid aufs hardlink")
  108. }
  109. tmpFile, err := os.Open(filepath.Join(aufsTempdir, linkBasename))
  110. if err != nil {
  111. return err
  112. }
  113. defer tmpFile.Close()
  114. srcData = tmpFile
  115. }
  116. if err := createTarFile(path, dest, srcHdr, srcData, true); err != nil {
  117. return err
  118. }
  119. // Directory mtimes must be handled at the end to avoid further
  120. // file creation in them to modify the directory mtime
  121. if hdr.Typeflag == tar.TypeDir {
  122. dirs = append(dirs, hdr)
  123. }
  124. }
  125. }
  126. for _, hdr := range dirs {
  127. path := filepath.Join(dest, hdr.Name)
  128. ts := []syscall.Timespec{timeToTimespec(hdr.AccessTime), timeToTimespec(hdr.ModTime)}
  129. if err := syscall.UtimesNano(path, ts); err != nil {
  130. return err
  131. }
  132. }
  133. return nil
  134. }
  135. // ApplyLayer parses a diff in the standard layer format from `layer`, and
  136. // applies it to the directory `dest`.
  137. func ApplyLayer(dest string, layer ArchiveReader) error {
  138. dest = filepath.Clean(dest)
  139. // We need to be able to set any perms
  140. oldmask := syscall.Umask(0)
  141. defer syscall.Umask(oldmask)
  142. layer, err := DecompressStream(layer)
  143. if err != nil {
  144. return err
  145. }
  146. return UnpackLayer(dest, layer)
  147. }