archive.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package remotecontext // import "github.com/docker/docker/builder/remotecontext"
  2. import (
  3. "io"
  4. "os"
  5. "path/filepath"
  6. "github.com/docker/docker/builder"
  7. "github.com/docker/docker/pkg/archive"
  8. "github.com/docker/docker/pkg/chrootarchive"
  9. "github.com/docker/docker/pkg/containerfs"
  10. "github.com/docker/docker/pkg/longpath"
  11. "github.com/docker/docker/pkg/tarsum"
  12. "github.com/pkg/errors"
  13. )
  14. type archiveContext struct {
  15. root string
  16. sums tarsum.FileInfoSums
  17. }
  18. func (c *archiveContext) Close() error {
  19. return os.RemoveAll(c.root)
  20. }
  21. func convertPathError(err error, cleanpath string) error {
  22. if err, ok := err.(*os.PathError); ok {
  23. err.Path = cleanpath
  24. return err
  25. }
  26. return err
  27. }
  28. type modifiableContext interface {
  29. builder.Source
  30. // Remove deletes the entry specified by `path`.
  31. // It is usual for directory entries to delete all its subentries.
  32. Remove(path string) error
  33. }
  34. // FromArchive returns a build source from a tar stream.
  35. //
  36. // It extracts the tar stream to a temporary folder that is deleted as soon as
  37. // the Context is closed.
  38. // As the extraction happens, a tarsum is calculated for every file, and the set of
  39. // all those sums then becomes the source of truth for all operations on this Context.
  40. //
  41. // Closing tarStream has to be done by the caller.
  42. func FromArchive(tarStream io.Reader) (builder.Source, error) {
  43. root, err := longpath.MkdirTemp("", "docker-builder")
  44. if err != nil {
  45. return nil, err
  46. }
  47. // Assume local file system. Since it's coming from a tar file.
  48. tsc := &archiveContext{root: root}
  49. // Make sure we clean-up upon error. In the happy case the caller
  50. // is expected to manage the clean-up
  51. defer func() {
  52. if err != nil {
  53. tsc.Close()
  54. }
  55. }()
  56. decompressedStream, err := archive.DecompressStream(tarStream)
  57. if err != nil {
  58. return nil, err
  59. }
  60. sum, err := tarsum.NewTarSum(decompressedStream, true, tarsum.Version1)
  61. if err != nil {
  62. return nil, err
  63. }
  64. err = chrootarchive.Untar(sum, root, nil)
  65. if err != nil {
  66. return nil, err
  67. }
  68. tsc.sums = sum.GetSums()
  69. return tsc, nil
  70. }
  71. func (c *archiveContext) Root() string {
  72. return c.root
  73. }
  74. func (c *archiveContext) Remove(path string) error {
  75. _, fullpath, err := normalize(path, c.root)
  76. if err != nil {
  77. return err
  78. }
  79. return os.RemoveAll(fullpath)
  80. }
  81. func (c *archiveContext) Hash(path string) (string, error) {
  82. cleanpath, fullpath, err := normalize(path, c.root)
  83. if err != nil {
  84. return "", err
  85. }
  86. rel, err := filepath.Rel(c.root, fullpath)
  87. if err != nil {
  88. return "", convertPathError(err, cleanpath)
  89. }
  90. // Use the checksum of the followed path(not the possible symlink) because
  91. // this is the file that is actually copied.
  92. if tsInfo := c.sums.GetFile(filepath.ToSlash(rel)); tsInfo != nil {
  93. return tsInfo.Sum(), nil
  94. }
  95. // We set sum to path by default for the case where GetFile returns nil.
  96. // The usual case is if relative path is empty.
  97. return path, nil // backwards compat TODO: see if really needed
  98. }
  99. func normalize(path string, root string) (cleanPath, fullPath string, err error) {
  100. cleanPath = filepath.Clean(string(filepath.Separator) + path)[1:]
  101. fullPath, err = containerfs.ResolveScopedPath(root, path)
  102. if err != nil {
  103. return "", "", errors.Wrapf(err, "forbidden path outside the build context: %s (%s)", path, cleanPath)
  104. }
  105. return
  106. }