forked from bufbuild/buf
/
cas_module_cacher.go
265 lines (252 loc) · 7.25 KB
/
cas_module_cacher.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// Copyright 2020-2023 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package bufmodulecache
import (
"context"
"errors"
"fmt"
"io"
"strings"
"github.com/bufbuild/buf/private/bufpkg/bufmodule"
"github.com/bufbuild/buf/private/bufpkg/bufmodule/bufmoduleref"
"github.com/bufbuild/buf/private/pkg/manifest"
"github.com/bufbuild/buf/private/pkg/normalpath"
"github.com/bufbuild/buf/private/pkg/storage"
"go.uber.org/multierr"
"go.uber.org/zap"
)
// subdirectories under ~/.cache/buf/v2/{remote}/{owner}/{repo}
const (
blobsDir = "blobs"
commitsDir = "commits"
)
type casModuleCacher struct {
logger *zap.Logger
bucket storage.ReadWriteBucket
}
func (c *casModuleCacher) GetModule(
ctx context.Context,
modulePin bufmoduleref.ModulePin,
) (_ bufmodule.Module, retErr error) {
moduleBasedir := normalpath.Join(modulePin.Remote(), modulePin.Owner(), modulePin.Repository())
manifestDigestStr := modulePin.Digest()
if manifestDigestStr == "" {
// Attempt to look up manifest digest from commit
commitPath := normalpath.Join(moduleBasedir, commitsDir, modulePin.Commit())
manifestDigestBytes, err := c.loadPath(ctx, commitPath)
if err != nil {
return nil, err
}
manifestDigestStr = string(manifestDigestBytes)
}
manifestDigest, err := manifest.NewDigestFromString(manifestDigestStr)
if err != nil {
return nil, err
}
manifestFromCache, err := c.readManifest(ctx, moduleBasedir, *manifestDigest)
if err != nil {
return nil, err
}
digests := manifestFromCache.Digests()
blobs := make([]manifest.Blob, len(digests))
for i, digest := range digests {
blob, err := c.readBlob(ctx, moduleBasedir, digest)
if err != nil {
return nil, err
}
blobs[i] = blob
}
blobSet, err := manifest.NewBlobSet(ctx, blobs)
if err != nil {
return nil, err
}
return bufmodule.NewModuleForManifestAndBlobSet(
ctx,
manifestFromCache,
blobSet,
bufmodule.ModuleWithModuleIdentityAndCommit(
modulePin,
modulePin.Commit(),
),
)
}
func (c *casModuleCacher) PutModule(
ctx context.Context,
modulePin bufmoduleref.ModulePin,
module bufmodule.Module,
) (retErr error) {
moduleManifest := module.Manifest()
if moduleManifest == nil {
return fmt.Errorf("manifest must be non-nil")
}
manifestBlob, err := moduleManifest.Blob()
if err != nil {
return err
}
manifestDigest := manifestBlob.Digest()
if manifestDigest == nil {
return errors.New("empty manifest digest")
}
if modulePinDigestEncoded := modulePin.Digest(); modulePinDigestEncoded != "" {
modulePinDigest, err := manifest.NewDigestFromString(modulePinDigestEncoded)
if err != nil {
return fmt.Errorf("invalid module pin digest %q: %w", modulePinDigestEncoded, err)
}
if !manifestDigest.Equal(*modulePinDigest) {
return fmt.Errorf("manifest digest mismatch: pin=%q, module=%q", modulePinDigest.String(), manifestDigest.String())
}
}
moduleBasedir := normalpath.Join(modulePin.Remote(), modulePin.Owner(), modulePin.Repository())
// Write blobs
for _, digest := range moduleManifest.Digests() {
blobDigestStr := digest.String()
blob, found := module.BlobSet().BlobFor(blobDigestStr)
if !found {
paths, _ := moduleManifest.PathsFor(blobDigestStr)
return fmt.Errorf("blob not found for digest=%q (paths=%v)", blobDigestStr, paths)
}
if err := c.writeBlob(ctx, moduleBasedir, blob); err != nil {
return err
}
}
// Write manifest
if err := c.writeBlob(ctx, moduleBasedir, manifestBlob); err != nil {
return err
}
// Write commit
commitPath := normalpath.Join(moduleBasedir, commitsDir, modulePin.Commit())
if err := c.atomicWrite(ctx, strings.NewReader(manifestBlob.Digest().String()), commitPath); err != nil {
return err
}
return nil
}
func (c *casModuleCacher) readBlob(
ctx context.Context,
moduleBasedir string,
digest manifest.Digest,
) (_ manifest.Blob, retErr error) {
hexDigest := digest.Hex()
blobPath := normalpath.Join(moduleBasedir, blobsDir, hexDigest[:2], hexDigest[2:])
contents, err := c.loadPath(ctx, blobPath)
if err != nil {
return nil, err
}
blob, err := manifest.NewMemoryBlob(digest, contents, manifest.MemoryBlobWithDigestValidation())
if err != nil {
return nil, fmt.Errorf("failed to create blob from path %s: %w", blobPath, err)
}
return blob, nil
}
func (c *casModuleCacher) validateBlob(
ctx context.Context,
moduleBasedir string,
digest *manifest.Digest,
) (bool, error) {
hexDigest := digest.Hex()
blobPath := normalpath.Join(moduleBasedir, blobsDir, hexDigest[:2], hexDigest[2:])
f, err := c.bucket.Get(ctx, blobPath)
if err != nil {
return false, err
}
defer func() {
if err := f.Close(); err != nil {
c.logger.Debug("err closing blob", zap.Error(err))
}
}()
digester, err := manifest.NewDigester(digest.Type())
if err != nil {
return false, err
}
cacheDigest, err := digester.Digest(f)
if err != nil {
return false, err
}
return digest.Equal(*cacheDigest), nil
}
func (c *casModuleCacher) readManifest(
ctx context.Context,
moduleBasedir string,
manifestDigest manifest.Digest,
) (_ *manifest.Manifest, retErr error) {
blob, err := c.readBlob(ctx, moduleBasedir, manifestDigest)
if err != nil {
return nil, err
}
f, err := blob.Open(ctx)
if err != nil {
return nil, err
}
defer func() {
retErr = multierr.Append(retErr, f.Close())
}()
moduleManifest, err := manifest.NewFromReader(f)
if err != nil {
return nil, fmt.Errorf("failed to read manifest %s: %w", manifestDigest.String(), err)
}
return moduleManifest, nil
}
func (c *casModuleCacher) writeBlob(
ctx context.Context,
moduleBasedir string,
blob manifest.Blob,
) (retErr error) {
// Avoid unnecessary write if the blob is already written to disk
valid, err := c.validateBlob(ctx, moduleBasedir, blob.Digest())
if err == nil && valid {
return nil
}
if !storage.IsNotExist(err) {
c.logger.Debug(
"repairing cache entry",
zap.String("basedir", moduleBasedir),
zap.String("digest", blob.Digest().String()),
)
}
contents, err := blob.Open(ctx)
if err != nil {
return err
}
defer func() {
retErr = multierr.Append(retErr, contents.Close())
}()
hexDigest := blob.Digest().Hex()
blobPath := normalpath.Join(moduleBasedir, blobsDir, hexDigest[:2], hexDigest[2:])
return c.atomicWrite(ctx, contents, blobPath)
}
func (c *casModuleCacher) atomicWrite(ctx context.Context, contents io.Reader, path string) (retErr error) {
f, err := c.bucket.Put(ctx, path, storage.PutWithAtomic())
if err != nil {
return err
}
defer func() {
retErr = multierr.Append(retErr, f.Close())
}()
if _, err := io.Copy(f, contents); err != nil {
return err
}
return nil
}
func (c *casModuleCacher) loadPath(
ctx context.Context,
path string,
) (_ []byte, retErr error) {
f, err := c.bucket.Get(ctx, path)
if err != nil {
return nil, err
}
defer func() {
retErr = multierr.Append(retErr, f.Close())
}()
return io.ReadAll(f)
}