diff.go 4.4 KB

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