diff.go 4.5 KB

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