Debian Codegraph @

cache/hash.go

raw symbols SCIP
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
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 // set when GODEBUG=gocachehash=1
19
20 // HashSize is the number of bytes in a hash.
21 const HashSize = 32
22
23 // A Hash provides access to the canonical hash function used to index the cache.
24 // The current implementation uses salted SHA256, but clients must not assume this.
25 type Hash struct {
26 h hash.Hash
27 name string // for debugging
28 buf *bytes.Buffer // for verify
29 }
30
31 // stripExperiment strips any GOEXPERIMENT configuration from the Go
32 // version string.
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 // Subkey returns an action ID corresponding to mixing a parent
41 // action ID with a string description of the subkey.
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 // NewHash returns a new Hash.
61 // The caller is expected to Write data to it and then call Sum.
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 // Write writes data to the running hash.
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 // Sum returns the hash of the data written previously.
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 // In GODEBUG=gocacheverify=1 mode,
103 // hashDebug holds the input to every computed hash ID,
104 // so that we can work backward from the ID involved in a
105 // cache entry mismatch to a description of what should be there.
106 var hashDebug struct {
107 sync.Mutex
108 m map[[HashSize]byte]string
109 }
110
111 // reverseHash returns the input used to compute the hash id.
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 // FileHash returns the hash of the named file.
125 // It caches repeated lookups for a given file,
126 // and the cache entry for a file can be initialized
127 // using SetFileHash.
128 // The hash used by FileHash is not the same as
129 // the hash used by NewHash.
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 // SetFileHash sets the hash returned by FileHash for file.
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 }