-
Notifications
You must be signed in to change notification settings - Fork 393
/
hasher.go
94 lines (82 loc) · 1.55 KB
/
hasher.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package hasher
import (
"bufio"
"context"
"crypto/sha1"
"encoding/json"
"fmt"
"hash"
"io"
"os"
"github.com/earthly/earthly/ast/spec"
)
type Hasher struct {
h hash.Hash
}
func New() *Hasher {
return &Hasher{
h: sha1.New(),
}
}
func (h *Hasher) GetHash() []byte {
if h == nil {
return nil
}
return h.h.Sum(nil)
}
func (h *Hasher) HashCommand(cmd spec.Command) {
dt, err := json.Marshal(cmd)
if err != nil {
panic(fmt.Sprintf("failed to hash command: %s", err)) // shouldn't happen
}
h.HashBytes(dt)
}
func (h *Hasher) HashVersion(version spec.Version) {
dt, err := json.Marshal(version)
if err != nil {
panic(fmt.Sprintf("failed to hash version: %s", err)) // shouldn't happen
}
h.HashBytes(dt)
}
func (h *Hasher) HashString(s string) {
h.HashBytes([]byte(s))
}
func (h *Hasher) HashBytes(b []byte) {
h.h.Write([]byte(fmt.Sprintf("%d", len(b))))
h.h.Write(b)
}
func (h *Hasher) HashFile(ctx context.Context, src string) error {
stat, err := os.Stat(src)
if err != nil {
return err
}
h.HashString(fmt.Sprintf("mode: %d;", stat.Mode()))
h.HashString(fmt.Sprintf("size: %d;", stat.Size()))
f, err := os.Open(src)
if err != nil {
return err
}
defer f.Close()
readCh := make(chan error)
buf := make([]byte, 1024*1024)
r := bufio.NewReader(f)
for {
var n int
go func() {
var err error
n, err = r.Read(buf)
readCh <- err
}()
select {
case err := <-readCh:
if err == io.EOF {
return nil
} else if err != nil {
return err
}
h.h.Write(buf[:n])
case <-ctx.Done():
return ctx.Err()
}
}
}