diff.go 4.7 KB

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