diff.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package archive
  2. import (
  3. "fmt"
  4. "io"
  5. "io/ioutil"
  6. "os"
  7. "path/filepath"
  8. "strings"
  9. "syscall"
  10. "github.com/dotcloud/docker/vendor/src/code.google.com/p/go/src/pkg/archive/tar"
  11. )
  12. // Linux device nodes are a bit weird due to backwards compat with 16 bit device nodes.
  13. // They are, from low to high: the lower 8 bits of the minor, then 12 bits of the major,
  14. // then the top 12 bits of the minor
  15. func mkdev(major int64, minor int64) uint32 {
  16. return uint32(((minor & 0xfff00) << 12) | ((major & 0xfff) << 8) | (minor & 0xff))
  17. }
  18. // ApplyLayer parses a diff in the standard layer format from `layer`, and
  19. // applies it to the directory `dest`.
  20. func ApplyLayer(dest string, layer ArchiveReader) error {
  21. // We need to be able to set any perms
  22. oldmask := syscall.Umask(0)
  23. defer syscall.Umask(oldmask)
  24. layer, err := DecompressStream(layer)
  25. if err != nil {
  26. return err
  27. }
  28. tr := tar.NewReader(layer)
  29. var dirs []*tar.Header
  30. aufsTempdir := ""
  31. aufsHardlinks := make(map[string]*tar.Header)
  32. // Iterate through the files in the archive.
  33. for {
  34. hdr, err := tr.Next()
  35. if err == io.EOF {
  36. // end of tar archive
  37. break
  38. }
  39. if err != nil {
  40. return err
  41. }
  42. // Normalize name, for safety and for a simple is-root check
  43. hdr.Name = filepath.Clean(hdr.Name)
  44. if !strings.HasSuffix(hdr.Name, "/") {
  45. // Not the root directory, ensure that the parent directory exists.
  46. // This happened in some tests where an image had a tarfile without any
  47. // parent directories.
  48. parent := filepath.Dir(hdr.Name)
  49. parentPath := filepath.Join(dest, parent)
  50. if _, err := os.Lstat(parentPath); err != nil && os.IsNotExist(err) {
  51. err = os.MkdirAll(parentPath, 0600)
  52. if err != nil {
  53. return err
  54. }
  55. }
  56. }
  57. // Skip AUFS metadata dirs
  58. if strings.HasPrefix(hdr.Name, ".wh..wh.") {
  59. // Regular files inside /.wh..wh.plnk can be used as hardlink targets
  60. // We don't want this directory, but we need the files in them so that
  61. // such hardlinks can be resolved.
  62. if strings.HasPrefix(hdr.Name, ".wh..wh.plnk") && hdr.Typeflag == tar.TypeReg {
  63. basename := filepath.Base(hdr.Name)
  64. aufsHardlinks[basename] = hdr
  65. if aufsTempdir == "" {
  66. if aufsTempdir, err = ioutil.TempDir("", "dockerplnk"); err != nil {
  67. return err
  68. }
  69. defer os.RemoveAll(aufsTempdir)
  70. }
  71. if err := createTarFile(filepath.Join(aufsTempdir, basename), dest, hdr, tr, true); err != nil {
  72. return err
  73. }
  74. }
  75. continue
  76. }
  77. path := filepath.Join(dest, hdr.Name)
  78. base := filepath.Base(path)
  79. if strings.HasPrefix(base, ".wh.") {
  80. originalBase := base[len(".wh."):]
  81. originalPath := filepath.Join(filepath.Dir(path), originalBase)
  82. if err := os.RemoveAll(originalPath); err != nil {
  83. return err
  84. }
  85. } else {
  86. // If path exits we almost always just want to remove and replace it.
  87. // The only exception is when it is a directory *and* the file from
  88. // the layer is also a directory. Then we want to merge them (i.e.
  89. // just apply the metadata from the layer).
  90. if fi, err := os.Lstat(path); err == nil {
  91. if !(fi.IsDir() && hdr.Typeflag == tar.TypeDir) {
  92. if err := os.RemoveAll(path); err != nil {
  93. return err
  94. }
  95. }
  96. }
  97. srcData := io.Reader(tr)
  98. srcHdr := hdr
  99. // Hard links into /.wh..wh.plnk don't work, as we don't extract that directory, so
  100. // we manually retarget these into the temporary files we extracted them into
  101. if hdr.Typeflag == tar.TypeLink && strings.HasPrefix(filepath.Clean(hdr.Linkname), ".wh..wh.plnk") {
  102. linkBasename := filepath.Base(hdr.Linkname)
  103. srcHdr = aufsHardlinks[linkBasename]
  104. if srcHdr == nil {
  105. return fmt.Errorf("Invalid aufs hardlink")
  106. }
  107. tmpFile, err := os.Open(filepath.Join(aufsTempdir, linkBasename))
  108. if err != nil {
  109. return err
  110. }
  111. defer tmpFile.Close()
  112. srcData = tmpFile
  113. }
  114. if err := createTarFile(path, dest, srcHdr, srcData, true); err != nil {
  115. return err
  116. }
  117. // Directory mtimes must be handled at the end to avoid further
  118. // file creation in them to modify the directory mtime
  119. if hdr.Typeflag == tar.TypeDir {
  120. dirs = append(dirs, hdr)
  121. }
  122. }
  123. }
  124. for _, hdr := range dirs {
  125. path := filepath.Join(dest, hdr.Name)
  126. ts := []syscall.Timespec{timeToTimespec(hdr.AccessTime), timeToTimespec(hdr.ModTime)}
  127. if err := syscall.UtimesNano(path, ts); err != nil {
  128. return err
  129. }
  130. }
  131. return nil
  132. }