diff.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. "github.com/docker/docker/pkg/system"
  13. )
  14. // ApplyLayer parses a diff in the standard layer format from `layer`, and
  15. // applies it to the directory `dest`.
  16. func ApplyLayer(dest string, layer ArchiveReader) error {
  17. // We need to be able to set any perms
  18. oldmask, err := system.Umask(0)
  19. if err != nil {
  20. return err
  21. }
  22. defer system.Umask(oldmask) // ignore err, ErrNotSupportedPlatform
  23. layer, err = DecompressStream(layer)
  24. if err != nil {
  25. return err
  26. }
  27. tr := tar.NewReader(layer)
  28. trBuf := pools.BufioReader32KPool.Get(tr)
  29. defer pools.BufioReader32KPool.Put(trBuf)
  30. var dirs []*tar.Header
  31. aufsTempdir := ""
  32. aufsHardlinks := make(map[string]*tar.Header)
  33. // Iterate through the files in the archive.
  34. for {
  35. hdr, err := tr.Next()
  36. if err == io.EOF {
  37. // end of tar archive
  38. break
  39. }
  40. if err != nil {
  41. return err
  42. }
  43. // Normalize name, for safety and for a simple is-root check
  44. hdr.Name = filepath.Clean(hdr.Name)
  45. if !strings.HasSuffix(hdr.Name, "/") {
  46. // Not the root directory, ensure that the parent directory exists.
  47. // This happened in some tests where an image had a tarfile without any
  48. // parent directories.
  49. parent := filepath.Dir(hdr.Name)
  50. parentPath := filepath.Join(dest, parent)
  51. if _, err := os.Lstat(parentPath); err != nil && os.IsNotExist(err) {
  52. err = os.MkdirAll(parentPath, 0600)
  53. if err != nil {
  54. return err
  55. }
  56. }
  57. }
  58. // Skip AUFS metadata dirs
  59. if strings.HasPrefix(hdr.Name, ".wh..wh.") {
  60. // Regular files inside /.wh..wh.plnk can be used as hardlink targets
  61. // We don't want this directory, but we need the files in them so that
  62. // such hardlinks can be resolved.
  63. if strings.HasPrefix(hdr.Name, ".wh..wh.plnk") && hdr.Typeflag == tar.TypeReg {
  64. basename := filepath.Base(hdr.Name)
  65. aufsHardlinks[basename] = hdr
  66. if aufsTempdir == "" {
  67. if aufsTempdir, err = ioutil.TempDir("", "dockerplnk"); err != nil {
  68. return err
  69. }
  70. defer os.RemoveAll(aufsTempdir)
  71. }
  72. if err := createTarFile(filepath.Join(aufsTempdir, basename), dest, hdr, tr, true); err != nil {
  73. return err
  74. }
  75. }
  76. continue
  77. }
  78. path := filepath.Join(dest, hdr.Name)
  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. }