filehash.go 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package remotecontext // import "github.com/docker/docker/builder/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. var link string
  13. if fi.Mode()&os.ModeSymlink != 0 {
  14. var err error
  15. link, err = os.Readlink(path)
  16. if err != nil {
  17. return nil, err
  18. }
  19. }
  20. hdr, err := archive.FileInfoHeader(name, fi, link)
  21. if err != nil {
  22. return nil, err
  23. }
  24. if err := archive.ReadSecurityXattrToTarHeader(path, hdr); err != nil {
  25. return nil, err
  26. }
  27. tsh := &tarsumHash{hdr: hdr, Hash: sha256.New()}
  28. tsh.Reset() // initialize header
  29. return tsh, nil
  30. }
  31. type tarsumHash struct {
  32. hash.Hash
  33. hdr *tar.Header
  34. }
  35. // Reset resets the Hash to its initial state.
  36. func (tsh *tarsumHash) Reset() {
  37. // comply with hash.Hash and reset to the state hash had before any writes
  38. tsh.Hash.Reset()
  39. tarsum.WriteV1Header(tsh.hdr, tsh.Hash)
  40. }