filehash.go 773 B

12345678910111213141516171819202122232425262728293031323334
  1. package remotecontext
  2. import (
  3. "archive/tar"
  4. "crypto/sha256"
  5. "hash"
  6. "os"
  7. "github.com/docker/docker/pkg/archive"
  8. "github.com/docker/docker/pkg/tarsum"
  9. )
  10. // NewFileHash returns new hash that is used for the builder cache keys
  11. func NewFileHash(path, name string, fi os.FileInfo) (hash.Hash, error) {
  12. hdr, err := archive.FileInfoHeader(path, name, fi)
  13. if err != nil {
  14. return nil, err
  15. }
  16. tsh := &tarsumHash{hdr: hdr, Hash: sha256.New()}
  17. tsh.Reset() // initialize header
  18. return tsh, nil
  19. }
  20. type tarsumHash struct {
  21. hash.Hash
  22. hdr *tar.Header
  23. }
  24. // Reset resets the Hash to its initial state.
  25. func (tsh *tarsumHash) Reset() {
  26. // comply with hash.Hash and reset to the state hash had before any writes
  27. tsh.Hash.Reset()
  28. tarsum.WriteV1Header(tsh.hdr, tsh.Hash)
  29. }