diff.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. dest = filepath.Clean(dest)
  18. // We need to be able to set any perms
  19. oldmask, err := system.Umask(0)
  20. if err != nil {
  21. return err
  22. }
  23. defer system.Umask(oldmask) // ignore err, ErrNotSupportedPlatform
  24. layer, err = DecompressStream(layer)
  25. if err != nil {
  26. return err
  27. }
  28. tr := tar.NewReader(layer)
  29. trBuf := pools.BufioReader32KPool.Get(tr)
  30. defer pools.BufioReader32KPool.Put(trBuf)
  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. // Prevent symlink breakout
  82. if !strings.HasPrefix(path, dest) {
  83. return breakoutError(fmt.Errorf("%q is outside of %q", path, dest))
  84. }
  85. if strings.HasPrefix(base, ".wh.") {
  86. originalBase := base[len(".wh."):]
  87. originalPath := filepath.Join(filepath.Dir(path), originalBase)
  88. if err := os.RemoveAll(originalPath); err != nil {
  89. return err
  90. }
  91. } else {
  92. // If path exits we almost always just want to remove and replace it.
  93. // The only exception is when it is a directory *and* the file from
  94. // the layer is also a directory. Then we want to merge them (i.e.
  95. // just apply the metadata from the layer).
  96. if fi, err := os.Lstat(path); err == nil {
  97. if !(fi.IsDir() && hdr.Typeflag == tar.TypeDir) {
  98. if err := os.RemoveAll(path); err != nil {
  99. return err
  100. }
  101. }
  102. }
  103. trBuf.Reset(tr)
  104. srcData := io.Reader(trBuf)
  105. srcHdr := hdr
  106. // Hard links into /.wh..wh.plnk don't work, as we don't extract that directory, so
  107. // we manually retarget these into the temporary files we extracted them into
  108. if hdr.Typeflag == tar.TypeLink && strings.HasPrefix(filepath.Clean(hdr.Linkname), ".wh..wh.plnk") {
  109. linkBasename := filepath.Base(hdr.Linkname)
  110. srcHdr = aufsHardlinks[linkBasename]
  111. if srcHdr == nil {
  112. return fmt.Errorf("Invalid aufs hardlink")
  113. }
  114. tmpFile, err := os.Open(filepath.Join(aufsTempdir, linkBasename))
  115. if err != nil {
  116. return err
  117. }
  118. defer tmpFile.Close()
  119. srcData = tmpFile
  120. }
  121. if err := createTarFile(path, dest, srcHdr, srcData, true); err != nil {
  122. return err
  123. }
  124. // Directory mtimes must be handled at the end to avoid further
  125. // file creation in them to modify the directory mtime
  126. if hdr.Typeflag == tar.TypeDir {
  127. dirs = append(dirs, hdr)
  128. }
  129. }
  130. }
  131. for _, hdr := range dirs {
  132. path := filepath.Join(dest, hdr.Name)
  133. ts := []syscall.Timespec{timeToTimespec(hdr.AccessTime), timeToTimespec(hdr.ModTime)}
  134. if err := syscall.UtimesNano(path, ts); err != nil {
  135. return err
  136. }
  137. }
  138. return nil
  139. }