tarsum.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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(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. if err := chrootarchive.Untar(sum, root, nil); err != nil {
  92. return nil, err
  93. }
  94. tsc.sums = sum.GetSums()
  95. return tsc, nil
  96. }
  97. func (c *tarSumContext) normalize(path string) (cleanpath, fullpath string, err error) {
  98. cleanpath = filepath.Clean(string(os.PathSeparator) + path)[1:]
  99. fullpath, err = symlink.FollowSymlinkInScope(filepath.Join(c.root, path), c.root)
  100. if err != nil {
  101. return "", "", fmt.Errorf("Forbidden path outside the build context: %s (%s)", path, fullpath)
  102. }
  103. _, err = os.Lstat(fullpath)
  104. if err != nil {
  105. return "", "", convertPathError(err, path)
  106. }
  107. return
  108. }
  109. func (c *tarSumContext) Walk(root string, walkFn WalkFunc) error {
  110. root = filepath.Join(c.root, filepath.Join(string(filepath.Separator), root))
  111. return filepath.Walk(root, func(fullpath string, info os.FileInfo, err error) error {
  112. rel, err := filepath.Rel(c.root, fullpath)
  113. if err != nil {
  114. return err
  115. }
  116. if rel == "." {
  117. return nil
  118. }
  119. sum := rel
  120. if tsInfo := c.sums.GetFile(filepath.ToSlash(rel)); tsInfo != nil {
  121. sum = tsInfo.Sum()
  122. }
  123. fi := &HashedFileInfo{PathFileInfo{FileInfo: info, FilePath: fullpath}, sum}
  124. if err := walkFn(rel, fi, nil); err != nil {
  125. return err
  126. }
  127. return nil
  128. })
  129. }
  130. func (c *tarSumContext) Remove(path string) error {
  131. _, fullpath, err := c.normalize(path)
  132. if err != nil {
  133. return err
  134. }
  135. return os.RemoveAll(fullpath)
  136. }