tarsum.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. package builder
  2. import (
  3. "fmt"
  4. "io"
  5. "os"
  6. "path/filepath"
  7. "github.com/docker/docker/pkg/archive"
  8. "github.com/docker/docker/pkg/chrootarchive"
  9. "github.com/docker/docker/pkg/ioutils"
  10. "github.com/docker/docker/pkg/symlink"
  11. "github.com/docker/docker/pkg/tarsum"
  12. )
  13. type tarSumContext struct {
  14. root string
  15. sums tarsum.FileInfoSums
  16. }
  17. func (c *tarSumContext) Close() error {
  18. return os.RemoveAll(c.root)
  19. }
  20. func convertPathError(err error, cleanpath string) error {
  21. if err, ok := err.(*os.PathError); ok {
  22. err.Path = cleanpath
  23. return err
  24. }
  25. return err
  26. }
  27. func (c *tarSumContext) Open(path string) (io.ReadCloser, error) {
  28. cleanpath, fullpath, err := c.normalize(path)
  29. if err != nil {
  30. return nil, err
  31. }
  32. r, err := os.Open(fullpath)
  33. if err != nil {
  34. return nil, convertPathError(err, cleanpath)
  35. }
  36. return r, nil
  37. }
  38. func (c *tarSumContext) Stat(path string) (string, FileInfo, error) {
  39. cleanpath, fullpath, err := c.normalize(path)
  40. if err != nil {
  41. return "", nil, err
  42. }
  43. st, err := os.Lstat(fullpath)
  44. if err != nil {
  45. return "", nil, convertPathError(err, cleanpath)
  46. }
  47. rel, err := filepath.Rel(c.root, fullpath)
  48. if err != nil {
  49. return "", nil, convertPathError(err, cleanpath)
  50. }
  51. // We set sum to path by default for the case where GetFile returns nil.
  52. // The usual case is if relative path is empty.
  53. sum := path
  54. // Use the checksum of the followed path(not the possible symlink) because
  55. // this is the file that is actually copied.
  56. if tsInfo := c.sums.GetFile(filepath.ToSlash(rel)); tsInfo != nil {
  57. sum = tsInfo.Sum()
  58. }
  59. fi := &HashedFileInfo{PathFileInfo{st, fullpath, filepath.Base(cleanpath)}, sum}
  60. return rel, fi, nil
  61. }
  62. // MakeTarSumContext returns a build Context from a tar stream.
  63. //
  64. // It extracts the tar stream to a temporary folder that is deleted as soon as
  65. // the Context is closed.
  66. // As the extraction happens, a tarsum is calculated for every file, and the set of
  67. // all those sums then becomes the source of truth for all operations on this Context.
  68. //
  69. // Closing tarStream has to be done by the caller.
  70. func MakeTarSumContext(tarStream io.Reader) (ModifiableContext, error) {
  71. root, err := ioutils.TempDir("", "docker-builder")
  72. if err != nil {
  73. return nil, err
  74. }
  75. tsc := &tarSumContext{root: root}
  76. // Make sure we clean-up upon error. In the happy case the caller
  77. // is expected to manage the clean-up
  78. defer func() {
  79. if err != nil {
  80. tsc.Close()
  81. }
  82. }()
  83. decompressedStream, err := archive.DecompressStream(tarStream)
  84. if err != nil {
  85. return nil, err
  86. }
  87. sum, err := tarsum.NewTarSum(decompressedStream, true, tarsum.Version1)
  88. if err != nil {
  89. return nil, err
  90. }
  91. err = chrootarchive.Untar(sum, root, nil)
  92. if err != nil {
  93. return nil, err
  94. }
  95. tsc.sums = sum.GetSums()
  96. return tsc, nil
  97. }
  98. func (c *tarSumContext) normalize(path string) (cleanpath, fullpath string, err error) {
  99. cleanpath = filepath.Clean(string(os.PathSeparator) + path)[1:]
  100. fullpath, err = symlink.FollowSymlinkInScope(filepath.Join(c.root, path), c.root)
  101. if err != nil {
  102. return "", "", fmt.Errorf("Forbidden path outside the build context: %s (%s)", path, fullpath)
  103. }
  104. _, err = os.Lstat(fullpath)
  105. if err != nil {
  106. return "", "", convertPathError(err, path)
  107. }
  108. return
  109. }
  110. func (c *tarSumContext) Walk(root string, walkFn WalkFunc) error {
  111. root = filepath.Join(c.root, filepath.Join(string(filepath.Separator), root))
  112. return filepath.Walk(root, func(fullpath string, info os.FileInfo, err error) error {
  113. rel, err := filepath.Rel(c.root, fullpath)
  114. if err != nil {
  115. return err
  116. }
  117. if rel == "." {
  118. return nil
  119. }
  120. sum := rel
  121. if tsInfo := c.sums.GetFile(filepath.ToSlash(rel)); tsInfo != nil {
  122. sum = tsInfo.Sum()
  123. }
  124. fi := &HashedFileInfo{PathFileInfo{FileInfo: info, FilePath: fullpath}, sum}
  125. if err := walkFn(rel, fi, nil); err != nil {
  126. return err
  127. }
  128. return nil
  129. })
  130. }
  131. func (c *tarSumContext) Remove(path string) error {
  132. _, fullpath, err := c.normalize(path)
  133. if err != nil {
  134. return err
  135. }
  136. return os.RemoveAll(fullpath)
  137. }