diff.go 6.7 KB

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