lazycontext.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package remotecontext
  2. import (
  3. "encoding/hex"
  4. "os"
  5. "strings"
  6. "github.com/docker/docker/builder"
  7. "github.com/docker/docker/pkg/containerfs"
  8. "github.com/docker/docker/pkg/pools"
  9. "github.com/pkg/errors"
  10. )
  11. // NewLazySource creates a new LazyContext. LazyContext defines a hashed build
  12. // context based on a root directory. Individual files are hashed first time
  13. // they are asked. It is not safe to call methods of LazyContext concurrently.
  14. func NewLazySource(root containerfs.ContainerFS) (builder.Source, error) {
  15. return &lazySource{
  16. root: root,
  17. sums: make(map[string]string),
  18. }, nil
  19. }
  20. type lazySource struct {
  21. root containerfs.ContainerFS
  22. sums map[string]string
  23. }
  24. func (c *lazySource) Root() containerfs.ContainerFS {
  25. return c.root
  26. }
  27. func (c *lazySource) Close() error {
  28. return nil
  29. }
  30. func (c *lazySource) Hash(path string) (string, error) {
  31. cleanPath, fullPath, err := normalize(path, c.root)
  32. if err != nil {
  33. return "", err
  34. }
  35. fi, err := c.root.Lstat(fullPath)
  36. if err != nil {
  37. return "", errors.WithStack(err)
  38. }
  39. relPath, err := Rel(c.root, fullPath)
  40. if err != nil {
  41. return "", errors.WithStack(convertPathError(err, cleanPath))
  42. }
  43. sum, ok := c.sums[relPath]
  44. if !ok {
  45. sum, err = c.prepareHash(relPath, fi)
  46. if err != nil {
  47. return "", err
  48. }
  49. }
  50. return sum, nil
  51. }
  52. func (c *lazySource) prepareHash(relPath string, fi os.FileInfo) (string, error) {
  53. p := c.root.Join(c.root.Path(), relPath)
  54. h, err := NewFileHash(p, relPath, fi)
  55. if err != nil {
  56. return "", errors.Wrapf(err, "failed to create hash for %s", relPath)
  57. }
  58. if fi.Mode().IsRegular() && fi.Size() > 0 {
  59. f, err := c.root.Open(p)
  60. if err != nil {
  61. return "", errors.Wrapf(err, "failed to open %s", relPath)
  62. }
  63. defer f.Close()
  64. if _, err := pools.Copy(h, f); err != nil {
  65. return "", errors.Wrapf(err, "failed to copy file data for %s", relPath)
  66. }
  67. }
  68. sum := hex.EncodeToString(h.Sum(nil))
  69. c.sums[relPath] = sum
  70. return sum, nil
  71. }
  72. // Rel makes a path relative to base path. Same as `filepath.Rel` but can also
  73. // handle UUID paths in windows.
  74. func Rel(basepath containerfs.ContainerFS, targpath string) (string, error) {
  75. // filepath.Rel can't handle UUID paths in windows
  76. if basepath.OS() == "windows" {
  77. pfx := basepath.Path() + `\`
  78. if strings.HasPrefix(targpath, pfx) {
  79. p := strings.TrimPrefix(targpath, pfx)
  80. if p == "" {
  81. p = "."
  82. }
  83. return p, nil
  84. }
  85. }
  86. return basepath.Rel(basepath.Path(), targpath)
  87. }