diff.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. package archive
  2. import (
  3. "archive/tar"
  4. "fmt"
  5. "io"
  6. "io/ioutil"
  7. "os"
  8. "path/filepath"
  9. "runtime"
  10. "strings"
  11. "syscall"
  12. "github.com/Sirupsen/logrus"
  13. "github.com/docker/docker/pkg/pools"
  14. "github.com/docker/docker/pkg/system"
  15. )
  16. func UnpackLayer(dest string, layer ArchiveReader) (size int64, err error) {
  17. tr := tar.NewReader(layer)
  18. trBuf := pools.BufioReader32KPool.Get(tr)
  19. defer pools.BufioReader32KPool.Put(trBuf)
  20. var dirs []*tar.Header
  21. aufsTempdir := ""
  22. aufsHardlinks := make(map[string]*tar.Header)
  23. // Iterate through the files in the archive.
  24. for {
  25. hdr, err := tr.Next()
  26. if err == io.EOF {
  27. // end of tar archive
  28. break
  29. }
  30. if err != nil {
  31. return 0, err
  32. }
  33. size += hdr.Size
  34. // Normalize name, for safety and for a simple is-root check
  35. hdr.Name = filepath.Clean(hdr.Name)
  36. // Windows does not support filenames with colons in them. Ignore
  37. // these files. This is not a problem though (although it might
  38. // appear that it is). Let's suppose a client is running docker pull.
  39. // The daemon it points to is Windows. Would it make sense for the
  40. // client to be doing a docker pull Ubuntu for example (which has files
  41. // with colons in the name under /usr/share/man/man3)? No, absolutely
  42. // not as it would really only make sense that they were pulling a
  43. // Windows image. However, for development, it is necessary to be able
  44. // to pull Linux images which are in the repository.
  45. //
  46. // TODO Windows. Once the registry is aware of what images are Windows-
  47. // specific or Linux-specific, this warning should be changed to an error
  48. // to cater for the situation where someone does manage to upload a Linux
  49. // image but have it tagged as Windows inadvertantly.
  50. if runtime.GOOS == "windows" {
  51. if strings.Contains(hdr.Name, ":") {
  52. logrus.Warnf("Windows: Ignoring %s (is this a Linux image?)", hdr.Name)
  53. continue
  54. }
  55. }
  56. // Note as these operations are platform specific, so must the slash be.
  57. if !strings.HasSuffix(hdr.Name, string(os.PathSeparator)) {
  58. // Not the root directory, ensure that the parent directory exists.
  59. // This happened in some tests where an image had a tarfile without any
  60. // parent directories.
  61. parent := filepath.Dir(hdr.Name)
  62. parentPath := filepath.Join(dest, parent)
  63. if _, err := os.Lstat(parentPath); err != nil && os.IsNotExist(err) {
  64. err = system.MkdirAll(parentPath, 0600)
  65. if err != nil {
  66. return 0, err
  67. }
  68. }
  69. }
  70. // Skip AUFS metadata dirs
  71. if strings.HasPrefix(hdr.Name, ".wh..wh.") {
  72. // Regular files inside /.wh..wh.plnk can be used as hardlink targets
  73. // We don't want this directory, but we need the files in them so that
  74. // such hardlinks can be resolved.
  75. if strings.HasPrefix(hdr.Name, ".wh..wh.plnk") && hdr.Typeflag == tar.TypeReg {
  76. basename := filepath.Base(hdr.Name)
  77. aufsHardlinks[basename] = hdr
  78. if aufsTempdir == "" {
  79. if aufsTempdir, err = ioutil.TempDir("", "dockerplnk"); err != nil {
  80. return 0, err
  81. }
  82. defer os.RemoveAll(aufsTempdir)
  83. }
  84. if err := createTarFile(filepath.Join(aufsTempdir, basename), dest, hdr, tr, true, nil); err != nil {
  85. return 0, err
  86. }
  87. }
  88. continue
  89. }
  90. path := filepath.Join(dest, hdr.Name)
  91. rel, err := filepath.Rel(dest, path)
  92. if err != nil {
  93. return 0, err
  94. }
  95. // Note as these operations are platform specific, so must the slash be.
  96. if strings.HasPrefix(rel, ".."+string(os.PathSeparator)) {
  97. return 0, breakoutError(fmt.Errorf("%q is outside of %q", hdr.Name, dest))
  98. }
  99. base := filepath.Base(path)
  100. if strings.HasPrefix(base, ".wh.") {
  101. originalBase := base[len(".wh."):]
  102. originalPath := filepath.Join(filepath.Dir(path), originalBase)
  103. if err := os.RemoveAll(originalPath); err != nil {
  104. return 0, err
  105. }
  106. } else {
  107. // If path exits we almost always just want to remove and replace it.
  108. // The only exception is when it is a directory *and* the file from
  109. // the layer is also a directory. Then we want to merge them (i.e.
  110. // just apply the metadata from the layer).
  111. if fi, err := os.Lstat(path); err == nil {
  112. if !(fi.IsDir() && hdr.Typeflag == tar.TypeDir) {
  113. if err := os.RemoveAll(path); err != nil {
  114. return 0, err
  115. }
  116. }
  117. }
  118. trBuf.Reset(tr)
  119. srcData := io.Reader(trBuf)
  120. srcHdr := hdr
  121. // Hard links into /.wh..wh.plnk don't work, as we don't extract that directory, so
  122. // we manually retarget these into the temporary files we extracted them into
  123. if hdr.Typeflag == tar.TypeLink && strings.HasPrefix(filepath.Clean(hdr.Linkname), ".wh..wh.plnk") {
  124. linkBasename := filepath.Base(hdr.Linkname)
  125. srcHdr = aufsHardlinks[linkBasename]
  126. if srcHdr == nil {
  127. return 0, fmt.Errorf("Invalid aufs hardlink")
  128. }
  129. tmpFile, err := os.Open(filepath.Join(aufsTempdir, linkBasename))
  130. if err != nil {
  131. return 0, err
  132. }
  133. defer tmpFile.Close()
  134. srcData = tmpFile
  135. }
  136. if err := createTarFile(path, dest, srcHdr, srcData, true, nil); err != nil {
  137. return 0, err
  138. }
  139. // Directory mtimes must be handled at the end to avoid further
  140. // file creation in them to modify the directory mtime
  141. if hdr.Typeflag == tar.TypeDir {
  142. dirs = append(dirs, hdr)
  143. }
  144. }
  145. }
  146. for _, hdr := range dirs {
  147. path := filepath.Join(dest, hdr.Name)
  148. ts := []syscall.Timespec{timeToTimespec(hdr.AccessTime), timeToTimespec(hdr.ModTime)}
  149. if err := syscall.UtimesNano(path, ts); err != nil {
  150. return 0, err
  151. }
  152. }
  153. return size, nil
  154. }
  155. // ApplyLayer parses a diff in the standard layer format from `layer`, and
  156. // applies it to the directory `dest`. Returns the size in bytes of the
  157. // contents of the layer.
  158. func ApplyLayer(dest string, layer ArchiveReader) (int64, error) {
  159. dest = filepath.Clean(dest)
  160. // We need to be able to set any perms
  161. oldmask, err := system.Umask(0)
  162. if err != nil {
  163. return 0, err
  164. }
  165. defer system.Umask(oldmask) // ignore err, ErrNotSupportedPlatform
  166. layer, err = DecompressStream(layer)
  167. if err != nil {
  168. return 0, err
  169. }
  170. return UnpackLayer(dest, layer)
  171. }