diff.go 7.8 KB

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