| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package cache |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"crypto/sha256" |
| 10 |
"fmt" |
| 11 |
"hash" |
| 12 |
"io" |
| 13 |
"os" |
| 14 |
"strings" |
| 15 |
"sync" |
| 16 |
) |
| 17 |
|
| 18 |
var debugHash = false |
| 19 |
|
| 20 |
|
| 21 |
const HashSize = 32 |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
type Hash struct { |
| 26 |
h hash.Hash |
| 27 |
name string |
| 28 |
buf *bytes.Buffer |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
func stripExperiment(version string) string { |
| 34 |
if before, _, ok := strings.Cut(version, " X:"); ok { |
| 35 |
return before |
| 36 |
} |
| 37 |
return version |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
func Subkey(parent ActionID, desc string) ActionID { |
| 43 |
h := sha256.New() |
| 44 |
h.Write([]byte("subkey:")) |
| 45 |
h.Write(parent[:]) |
| 46 |
h.Write([]byte(desc)) |
| 47 |
var out ActionID |
| 48 |
h.Sum(out[:0]) |
| 49 |
if debugHash { |
| 50 |
fmt.Fprintf(os.Stderr, "HASH subkey %x %q = %x\n", parent, desc, out) |
| 51 |
} |
| 52 |
if verify { |
| 53 |
hashDebug.Lock() |
| 54 |
hashDebug.m[out] = fmt.Sprintf("subkey %x %q", parent, desc) |
| 55 |
hashDebug.Unlock() |
| 56 |
} |
| 57 |
return out |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
func NewHash(name string) *Hash { |
| 63 |
h := &Hash{h: sha256.New(), name: name} |
| 64 |
if debugHash { |
| 65 |
fmt.Fprintf(os.Stderr, "HASH[%s]\n", h.name) |
| 66 |
} |
| 67 |
if verify { |
| 68 |
h.buf = new(bytes.Buffer) |
| 69 |
} |
| 70 |
return h |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
func (h *Hash) Write(b []byte) (int, error) { |
| 75 |
if debugHash { |
| 76 |
fmt.Fprintf(os.Stderr, "HASH[%s]: %q\n", h.name, b) |
| 77 |
} |
| 78 |
if h.buf != nil { |
| 79 |
h.buf.Write(b) |
| 80 |
} |
| 81 |
return h.h.Write(b) |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
func (h *Hash) Sum() [HashSize]byte { |
| 86 |
var out [HashSize]byte |
| 87 |
h.h.Sum(out[:0]) |
| 88 |
if debugHash { |
| 89 |
fmt.Fprintf(os.Stderr, "HASH[%s]: %x\n", h.name, out) |
| 90 |
} |
| 91 |
if h.buf != nil { |
| 92 |
hashDebug.Lock() |
| 93 |
if hashDebug.m == nil { |
| 94 |
hashDebug.m = make(map[[HashSize]byte]string) |
| 95 |
} |
| 96 |
hashDebug.m[out] = h.buf.String() |
| 97 |
hashDebug.Unlock() |
| 98 |
} |
| 99 |
return out |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
var hashDebug struct { |
| 107 |
sync.Mutex |
| 108 |
m map[[HashSize]byte]string |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
func reverseHash(id [HashSize]byte) string { |
| 113 |
hashDebug.Lock() |
| 114 |
s := hashDebug.m[id] |
| 115 |
hashDebug.Unlock() |
| 116 |
return s |
| 117 |
} |
| 118 |
|
| 119 |
var hashFileCache struct { |
| 120 |
sync.Mutex |
| 121 |
m map[string][HashSize]byte |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
func FileHash(file string) ([HashSize]byte, error) { |
| 131 |
hashFileCache.Lock() |
| 132 |
out, ok := hashFileCache.m[file] |
| 133 |
hashFileCache.Unlock() |
| 134 |
|
| 135 |
if ok { |
| 136 |
return out, nil |
| 137 |
} |
| 138 |
|
| 139 |
h := sha256.New() |
| 140 |
f, err := os.Open(file) |
| 141 |
if err != nil { |
| 142 |
if debugHash { |
| 143 |
fmt.Fprintf(os.Stderr, "HASH %s: %v\n", file, err) |
| 144 |
} |
| 145 |
return [HashSize]byte{}, err |
| 146 |
} |
| 147 |
_, err = io.Copy(h, f) |
| 148 |
f.Close() |
| 149 |
if err != nil { |
| 150 |
if debugHash { |
| 151 |
fmt.Fprintf(os.Stderr, "HASH %s: %v\n", file, err) |
| 152 |
} |
| 153 |
return [HashSize]byte{}, err |
| 154 |
} |
| 155 |
h.Sum(out[:0]) |
| 156 |
if debugHash { |
| 157 |
fmt.Fprintf(os.Stderr, "HASH %s: %x\n", file, out) |
| 158 |
} |
| 159 |
|
| 160 |
SetFileHash(file, out) |
| 161 |
return out, nil |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
func SetFileHash(file string, sum [HashSize]byte) { |
| 166 |
hashFileCache.Lock() |
| 167 |
if hashFileCache.m == nil { |
| 168 |
hashFileCache.m = make(map[string][HashSize]byte) |
| 169 |
} |
| 170 |
hashFileCache.m[file] = sum |
| 171 |
hashFileCache.Unlock() |
| 172 |
} |