/
cache.go
168 lines (128 loc) · 2.72 KB
/
cache.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package cc
import (
"bufio"
"context"
"errors"
"io"
"os"
"os/exec"
"path/filepath"
"github.com/hashicorp/go-hclog"
"github.com/klauspost/compress/zstd"
"github.com/mr-tron/base58"
"golang.org/x/crypto/blake2b"
)
type Cache struct {
root string
path string
}
func NewCache(root, path string) (*Cache, error) {
return nil, io.EOF
return &Cache{root: root, path: path}, nil
}
var ErrNotCacheable = errors.New("not cacheable")
func (c *Cache) CalculateCacheInfo(ctx context.Context, L hclog.Logger, args []string) (string, string, error) {
op, err := Analyze(args)
if err != nil {
return "", "", err
}
err = op.Cachable()
if err != nil {
return "", "", err
}
newArgs := op.PreprocessArgs()
execPath, err := LookPath(newArgs[0], c.path)
cmd := exec.CommandContext(ctx, execPath, newArgs[1:]...)
out, err := cmd.StdoutPipe()
if err != nil {
return "", "", err
}
err = cmd.Start()
if err != nil {
return "", "", err
}
br := bufio.NewReaderSize(out, 1024*1024)
h, _ := blake2b.New256(nil)
outer:
for {
line, err := br.ReadSlice('\n')
if err != nil {
switch err {
case bufio.ErrBufferFull:
// ok, process line as is.
case io.EOF:
break outer
default:
L.Error("observed buffering error", "error", err)
out.Close()
break outer
}
}
// Skip any cpp line markers because they contain file names
// that won't be portable
if len(line) > 1 && line[0] == '#' {
continue
}
h.Write(line)
}
err = cmd.Wait()
if err != nil {
L.Error("error detected running preprocessing", "args", newArgs, "error", err)
return "", "", err
} else {
L.Info("executed preprocessor", "args", newArgs)
}
h.Write(op.Nonce())
return op.Output(), base58.Encode(h.Sum(nil)), nil
}
func (c *Cache) Retrieve(info, output string) (bool, error) {
path := filepath.Join(c.root, info)
i, err := os.Open(path)
if err != nil {
return false, nil
}
defer i.Close()
zi, err := zstd.NewReader(i)
if err != nil {
return false, nil
}
defer zi.Close()
o, err := os.Create(output)
if err != nil {
return false, nil
}
defer o.Close()
_, err = io.Copy(o, zi)
if err != nil {
return false, nil
}
return true, nil
}
func (c *Cache) Store(info, output string) ([]byte, error) {
path := filepath.Join(c.root, info)
err := os.MkdirAll(filepath.Dir(path), 0755)
if err != nil {
return nil, err
}
i, err := os.Open(output)
if err != nil {
return nil, nil
}
defer i.Close()
o, err := os.Create(path)
if err != nil {
return nil, nil
}
defer o.Close()
zo, err := zstd.NewWriter(o)
if err != nil {
return nil, nil
}
defer zo.Close()
h, _ := blake2b.New256(nil)
_, err = io.Copy(zo, io.TeeReader(i, h))
if err != nil {
return nil, nil
}
return h.Sum(nil), nil
}