hash.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // Copyright 2017 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package cache
  5. import (
  6. "bytes"
  7. "crypto/sha256"
  8. "fmt"
  9. "hash"
  10. "io"
  11. "os"
  12. "sync"
  13. )
  14. var debugHash = false // set when GODEBUG=gocachehash=1
  15. // HashSize is the number of bytes in a hash.
  16. const HashSize = 32
  17. // A Hash provides access to the canonical hash function used to index the cache.
  18. // The current implementation uses salted SHA256, but clients must not assume this.
  19. type Hash struct {
  20. h hash.Hash
  21. name string // for debugging
  22. buf *bytes.Buffer // for verify
  23. }
  24. // hashSalt is a salt string added to the beginning of every hash
  25. // created by NewHash. Using the Staticcheck version makes sure that different
  26. // versions of the command do not address the same cache
  27. // entries, so that a bug in one version does not affect the execution
  28. // of other versions. This salt will result in additional ActionID files
  29. // in the cache, but not additional copies of the large output files,
  30. // which are still addressed by unsalted SHA256.
  31. var hashSalt []byte
  32. func SetSalt(b []byte) {
  33. hashSalt = b
  34. }
  35. // Subkey returns an action ID corresponding to mixing a parent
  36. // action ID with a string description of the subkey.
  37. func Subkey(parent ActionID, desc string) ActionID {
  38. h := sha256.New()
  39. h.Write([]byte("subkey:"))
  40. h.Write(parent[:])
  41. h.Write([]byte(desc))
  42. var out ActionID
  43. h.Sum(out[:0])
  44. if debugHash {
  45. fmt.Fprintf(os.Stderr, "HASH subkey %x %q = %x\n", parent, desc, out)
  46. }
  47. if verify {
  48. hashDebug.Lock()
  49. hashDebug.m[out] = fmt.Sprintf("subkey %x %q", parent, desc)
  50. hashDebug.Unlock()
  51. }
  52. return out
  53. }
  54. // NewHash returns a new Hash.
  55. // The caller is expected to Write data to it and then call Sum.
  56. func NewHash(name string) *Hash {
  57. h := &Hash{h: sha256.New(), name: name}
  58. if debugHash {
  59. fmt.Fprintf(os.Stderr, "HASH[%s]\n", h.name)
  60. }
  61. h.Write(hashSalt)
  62. if verify {
  63. h.buf = new(bytes.Buffer)
  64. }
  65. return h
  66. }
  67. // Write writes data to the running hash.
  68. func (h *Hash) Write(b []byte) (int, error) {
  69. if debugHash {
  70. fmt.Fprintf(os.Stderr, "HASH[%s]: %q\n", h.name, b)
  71. }
  72. if h.buf != nil {
  73. h.buf.Write(b)
  74. }
  75. return h.h.Write(b)
  76. }
  77. // Sum returns the hash of the data written previously.
  78. func (h *Hash) Sum() [HashSize]byte {
  79. var out [HashSize]byte
  80. h.h.Sum(out[:0])
  81. if debugHash {
  82. fmt.Fprintf(os.Stderr, "HASH[%s]: %x\n", h.name, out)
  83. }
  84. if h.buf != nil {
  85. hashDebug.Lock()
  86. if hashDebug.m == nil {
  87. hashDebug.m = make(map[[HashSize]byte]string)
  88. }
  89. hashDebug.m[out] = h.buf.String()
  90. hashDebug.Unlock()
  91. }
  92. return out
  93. }
  94. // In GODEBUG=gocacheverify=1 mode,
  95. // hashDebug holds the input to every computed hash ID,
  96. // so that we can work backward from the ID involved in a
  97. // cache entry mismatch to a description of what should be there.
  98. var hashDebug struct {
  99. sync.Mutex
  100. m map[[HashSize]byte]string
  101. }
  102. // reverseHash returns the input used to compute the hash id.
  103. func reverseHash(id [HashSize]byte) string {
  104. hashDebug.Lock()
  105. s := hashDebug.m[id]
  106. hashDebug.Unlock()
  107. return s
  108. }
  109. var hashFileCache struct {
  110. sync.Mutex
  111. m map[string][HashSize]byte
  112. }
  113. // FileHash returns the hash of the named file.
  114. // It caches repeated lookups for a given file,
  115. // and the cache entry for a file can be initialized
  116. // using SetFileHash.
  117. // The hash used by FileHash is not the same as
  118. // the hash used by NewHash.
  119. func FileHash(file string) ([HashSize]byte, error) {
  120. hashFileCache.Lock()
  121. out, ok := hashFileCache.m[file]
  122. hashFileCache.Unlock()
  123. if ok {
  124. return out, nil
  125. }
  126. h := sha256.New()
  127. f, err := os.Open(file)
  128. if err != nil {
  129. if debugHash {
  130. fmt.Fprintf(os.Stderr, "HASH %s: %v\n", file, err)
  131. }
  132. return [HashSize]byte{}, err
  133. }
  134. _, err = io.Copy(h, f)
  135. f.Close()
  136. if err != nil {
  137. if debugHash {
  138. fmt.Fprintf(os.Stderr, "HASH %s: %v\n", file, err)
  139. }
  140. return [HashSize]byte{}, err
  141. }
  142. h.Sum(out[:0])
  143. if debugHash {
  144. fmt.Fprintf(os.Stderr, "HASH %s: %x\n", file, out)
  145. }
  146. SetFileHash(file, out)
  147. return out, nil
  148. }
  149. // SetFileHash sets the hash returned by FileHash for file.
  150. func SetFileHash(file string, sum [HashSize]byte) {
  151. hashFileCache.Lock()
  152. if hashFileCache.m == nil {
  153. hashFileCache.m = make(map[string][HashSize]byte)
  154. }
  155. hashFileCache.m[file] = sum
  156. hashFileCache.Unlock()
  157. }