diff.go 4.1 KB

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