diff.go 4.4 KB

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