forked from bufbuild/buf
/
file_set.go
158 lines (144 loc) · 4.49 KB
/
file_set.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
// Copyright 2020-2024 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 bufcas
import (
"context"
"fmt"
"sort"
"github.com/alis-exchange/buf/private/pkg/storage"
"go.uber.org/multierr"
)
// FileSet is a pair of a Manifest and its associated BlobSet.
//
// This can be read and written from and to a storage.Bucket.
//
// The Manifest is guaranteed to exactly correlate with the Blobs in the BlobSet,
// that is the Digests of the FileNodes in the Manifest will exactly match the
// Digests in the Blobs.
type FileSet interface {
// Manifest returns the associated Manifest.
Manifest() Manifest
// BlobSet returns the associated BlobSet.
BlobSet() BlobSet
// Protect against creation of a FileSet outside of this package, as we
// do very careful validation.
isFileSet()
}
// NewFileSet returns a new FileSet.
//
// Validation is done to ensure the Manifest exactly matches the BlobSet.
func NewFileSet(manifest Manifest, blobSet BlobSet) (FileSet, error) {
manifestDigestStringMap := make(map[string]struct{})
blobDigestStringMap := make(map[string]struct{})
for _, fileNode := range manifest.FileNodes() {
manifestDigestStringMap[fileNode.Digest().String()] = struct{}{}
}
for _, blob := range blobSet.Blobs() {
blobDigestStringMap[blob.Digest().String()] = struct{}{}
}
var onlyInManifest []string
var onlyInBlobSet []string
for manifestDigestString := range manifestDigestStringMap {
if _, ok := blobDigestStringMap[manifestDigestString]; !ok {
onlyInManifest = append(onlyInManifest, manifestDigestString)
}
}
for blobDigestString := range blobDigestStringMap {
if _, ok := manifestDigestStringMap[blobDigestString]; !ok {
onlyInBlobSet = append(onlyInBlobSet, blobDigestString)
}
}
if len(onlyInManifest) > 0 || len(onlyInBlobSet) > 0 {
sort.Strings(onlyInManifest)
sort.Strings(onlyInBlobSet)
return nil, fmt.Errorf("mismatched Manifest and BlobSet at FileSet construction, digests only in Manifest: [%v], digests only in BlobSet: [%v]", onlyInManifest, onlyInBlobSet)
}
return newFileSet(manifest, blobSet), nil
}
// NewFileSetForBucket returns a new FileSet for the given ReadBucket.
func NewFileSetForBucket(ctx context.Context, bucket storage.ReadBucket) (FileSet, error) {
var fileNodes []FileNode
var blobs []Blob
if err := storage.WalkReadObjects(
ctx,
bucket,
"",
func(readObject storage.ReadObject) error {
blob, err := NewBlobForContent(readObject)
if err != nil {
return fmt.Errorf("error creating Blob for file %q: %w", readObject.Path(), err)
}
fileNode, err := NewFileNode(readObject.Path(), blob.Digest())
if err != nil {
return fmt.Errorf("error creating FileNode for file %q: %w", readObject.Path(), err)
}
fileNodes = append(fileNodes, fileNode)
blobs = append(blobs, blob)
return nil
},
); err != nil {
return nil, err
}
manifest, err := NewManifest(fileNodes)
if err != nil {
return nil, err
}
blobSet, err := NewBlobSet(blobs)
if err != nil {
return nil, err
}
return newFileSet(
manifest,
blobSet,
), nil
}
// PutFileSetToBucket writes the FileSet to the given WriteBucket.
func PutFileSetToBucket(
ctx context.Context,
fileSet FileSet,
bucket storage.WriteBucket,
) error {
for _, fileNode := range fileSet.Manifest().FileNodes() {
writeObjectCloser, err := bucket.Put(ctx, fileNode.Path(), storage.PutWithAtomic())
if err != nil {
return err
}
blob := fileSet.BlobSet().GetBlob(fileNode.Digest())
if _, err := writeObjectCloser.Write(blob.Content()); err != nil {
return multierr.Append(err, writeObjectCloser.Close())
}
if err := writeObjectCloser.Close(); err != nil {
return err
}
}
return nil
}
// *** PRIVATE ****
type fileSet struct {
manifest Manifest
blobSet BlobSet
}
func newFileSet(manifest Manifest, blobSet BlobSet) *fileSet {
return &fileSet{
manifest: manifest,
blobSet: blobSet,
}
}
func (f *fileSet) Manifest() Manifest {
return f.manifest
}
func (f *fileSet) BlobSet() BlobSet {
return f.blobSet
}
func (*fileSet) isFileSet() {}