diff.go 4.5 KB

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