diff.go 4.4 KB

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