tarsum.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. package remotecontext
  2. import (
  3. "fmt"
  4. "os"
  5. "sync"
  6. iradix "github.com/hashicorp/go-immutable-radix"
  7. "github.com/docker/docker/pkg/containerfs"
  8. "github.com/pkg/errors"
  9. "github.com/tonistiigi/fsutil"
  10. )
  11. type hashed interface {
  12. Hash() string
  13. }
  14. // CachableSource is a source that contains cache records for its contents
  15. type CachableSource struct {
  16. mu sync.Mutex
  17. root containerfs.ContainerFS
  18. tree *iradix.Tree
  19. txn *iradix.Txn
  20. }
  21. // NewCachableSource creates new CachableSource
  22. func NewCachableSource(root string) *CachableSource {
  23. ts := &CachableSource{
  24. tree: iradix.New(),
  25. root: containerfs.NewLocalContainerFS(root),
  26. }
  27. return ts
  28. }
  29. // MarshalBinary marshals current cache information to a byte array
  30. func (cs *CachableSource) MarshalBinary() ([]byte, error) {
  31. b := TarsumBackup{Hashes: make(map[string]string)}
  32. root := cs.getRoot()
  33. root.Walk(func(k []byte, v interface{}) bool {
  34. b.Hashes[string(k)] = v.(*fileInfo).sum
  35. return false
  36. })
  37. return b.Marshal()
  38. }
  39. // UnmarshalBinary decodes cache information for presented byte array
  40. func (cs *CachableSource) UnmarshalBinary(data []byte) error {
  41. var b TarsumBackup
  42. if err := b.Unmarshal(data); err != nil {
  43. return err
  44. }
  45. txn := iradix.New().Txn()
  46. for p, v := range b.Hashes {
  47. txn.Insert([]byte(p), &fileInfo{sum: v})
  48. }
  49. cs.mu.Lock()
  50. defer cs.mu.Unlock()
  51. cs.tree = txn.Commit()
  52. return nil
  53. }
  54. // Scan rescans the cache information from the file system
  55. func (cs *CachableSource) Scan() error {
  56. lc, err := NewLazySource(cs.root)
  57. if err != nil {
  58. return err
  59. }
  60. txn := iradix.New().Txn()
  61. err = cs.root.Walk(cs.root.Path(), func(path string, info os.FileInfo, err error) error {
  62. if err != nil {
  63. return errors.Wrapf(err, "failed to walk %s", path)
  64. }
  65. rel, err := Rel(cs.root, path)
  66. if err != nil {
  67. return err
  68. }
  69. h, err := lc.Hash(rel)
  70. if err != nil {
  71. return err
  72. }
  73. txn.Insert([]byte(rel), &fileInfo{sum: h})
  74. return nil
  75. })
  76. if err != nil {
  77. return err
  78. }
  79. cs.mu.Lock()
  80. defer cs.mu.Unlock()
  81. cs.tree = txn.Commit()
  82. return nil
  83. }
  84. // HandleChange notifies the source about a modification operation
  85. func (cs *CachableSource) HandleChange(kind fsutil.ChangeKind, p string, fi os.FileInfo, err error) (retErr error) {
  86. cs.mu.Lock()
  87. if cs.txn == nil {
  88. cs.txn = cs.tree.Txn()
  89. }
  90. if kind == fsutil.ChangeKindDelete {
  91. cs.txn.Delete([]byte(p))
  92. cs.mu.Unlock()
  93. return
  94. }
  95. h, ok := fi.(hashed)
  96. if !ok {
  97. cs.mu.Unlock()
  98. return errors.Errorf("invalid fileinfo: %s", p)
  99. }
  100. hfi := &fileInfo{
  101. sum: h.Hash(),
  102. }
  103. cs.txn.Insert([]byte(p), hfi)
  104. cs.mu.Unlock()
  105. return nil
  106. }
  107. func (cs *CachableSource) getRoot() *iradix.Node {
  108. cs.mu.Lock()
  109. if cs.txn != nil {
  110. cs.tree = cs.txn.Commit()
  111. cs.txn = nil
  112. }
  113. t := cs.tree
  114. cs.mu.Unlock()
  115. return t.Root()
  116. }
  117. // Close closes the source
  118. func (cs *CachableSource) Close() error {
  119. return nil
  120. }
  121. func (cs *CachableSource) normalize(path string) (cleanpath, fullpath string, err error) {
  122. cleanpath = cs.root.Clean(string(cs.root.Separator()) + path)[1:]
  123. fullpath, err = cs.root.ResolveScopedPath(path, true)
  124. if err != nil {
  125. return "", "", fmt.Errorf("Forbidden path outside the context: %s (%s)", path, fullpath)
  126. }
  127. _, err = cs.root.Lstat(fullpath)
  128. if err != nil {
  129. return "", "", convertPathError(err, path)
  130. }
  131. return
  132. }
  133. // Hash returns a hash for a single file in the source
  134. func (cs *CachableSource) Hash(path string) (string, error) {
  135. n := cs.getRoot()
  136. // TODO: check this for symlinks
  137. v, ok := n.Get([]byte(path))
  138. if !ok {
  139. return path, nil
  140. }
  141. return v.(*fileInfo).sum, nil
  142. }
  143. // Root returns a root directory for the source
  144. func (cs *CachableSource) Root() containerfs.ContainerFS {
  145. return cs.root
  146. }
  147. type fileInfo struct {
  148. sum string
  149. }
  150. func (fi *fileInfo) Hash() string {
  151. return fi.sum
  152. }