forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fs_index.go
244 lines (193 loc) · 5.22 KB
/
fs_index.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
package index
import (
"fmt"
"path"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshsys "github.com/cloudfoundry/bosh-utils/system"
"gopkg.in/yaml.v2"
)
type FSIndex struct {
name string
dirPath string
reporter Reporter
blobs IndexBlobs
fs boshsys.FileSystem
useSubdir bool
expectsBlobstoreIDs bool
}
type indexEntry struct {
Key string
Version string
BlobstoreID string
SHA1 string
}
/*
---
builds:
70b9ea8efb83b882021792517b1164550b41bc27:
version: 70b9ea8efb83b882021792517b1164550b41bc27
sha1: b514a8cc194278d5c5b96d552cdbe0cade1ed719
format-version: '2'
---
builds:
88a17b61a5b892c9c59884ea5dee04ba6e116bb2:
version: 88a17b61a5b892c9c59884ea5dee04ba6e116bb2
sha1: 98eca647ddf3b24ae62fce44ea8a8e3526c3e53c
blobstore_id: cd26953d-b811-4127-b512-6f48639bd069
format-version: '2'
*/
type fsIndexSchema struct {
Builds map[string]fsIndexSchema_Entry `yaml:"builds"`
FormatVersion string `yaml:"format-version"`
}
type fsIndexSchema_Entry struct {
Version string `yaml:"version"`
BlobstoreID string `yaml:"blobstore_id,omitempty"`
SHA1 string `yaml:"sha1"`
}
func NewFSIndex(
name string,
dirPath string,
useSubdir bool,
expectsBlobstoreIDs bool,
reporter Reporter,
blobs IndexBlobs,
fs boshsys.FileSystem,
) FSIndex {
return FSIndex{
name: name,
dirPath: dirPath,
reporter: reporter,
blobs: blobs,
fs: fs,
useSubdir: useSubdir,
expectsBlobstoreIDs: expectsBlobstoreIDs,
}
}
func (i FSIndex) Find(name, fingerprint string) (string, string, error) {
if len(name) == 0 {
return "", "", bosherr.Error("Expected non-empty name")
}
if len(fingerprint) == 0 {
return "", "", bosherr.Error("Expected non-empty fingerprint")
}
entries, err := i.entries(name)
if err != nil {
return "", "", err
}
for _, entry := range entries {
if entry.Version == fingerprint {
blobName := fmt.Sprintf("%s/%s", name, entry.Version)
blobPath, err := i.blobs.Get(blobName, entry.BlobstoreID, entry.SHA1)
if err != nil {
return "", "", err
}
return blobPath, entry.SHA1, nil
}
}
return "", "", nil
}
func (i FSIndex) Add(name, fingerprint, path, sha1 string) (string, string, error) {
if len(name) == 0 {
return "", "", bosherr.Error("Expected non-empty name")
}
if len(fingerprint) == 0 {
return "", "", bosherr.Error("Expected non-empty fingerprint")
}
if len(path) == 0 {
return "", "", bosherr.Error("Expected non-empty archive path")
}
if len(sha1) == 0 {
return "", "", bosherr.Error("Expected non-empty archive SHA1")
}
entries, err := i.entries(name)
if err != nil {
return "", "", err
}
for _, entry := range entries {
if entry.Version == fingerprint {
errMsg := "Trying to add duplicate index entry '%s/%s' and SHA1 '%s' (conflicts with '%#v')"
return "", "", bosherr.Errorf(errMsg, name, fingerprint, sha1, entry)
}
}
entry := indexEntry{
Key: fingerprint,
Version: fingerprint,
SHA1: sha1,
}
desc := fmt.Sprintf("%s/%s", name, fingerprint)
i.reporter.IndexEntryStartedAdding(i.name, desc)
blobID, blobPath, err := i.blobs.Add(path, sha1)
if err != nil {
i.reporter.IndexEntryFinishedAdding(i.name, desc, err)
return "", "", err
}
entry.BlobstoreID = blobID
entries = append(entries, entry)
err = i.save(name, entries)
if err != nil {
i.reporter.IndexEntryFinishedAdding(i.name, desc, err)
return "", "", err
}
i.reporter.IndexEntryFinishedAdding(i.name, desc, nil)
return blobPath, sha1, nil
}
func (i FSIndex) entries(name string) ([]indexEntry, error) {
indexPath := i.indexPath(name)
if !i.fs.FileExists(indexPath) {
return nil, nil
}
bytes, err := i.fs.ReadFile(indexPath)
if err != nil {
return nil, bosherr.WrapErrorf(err, "Reading index")
}
var schema fsIndexSchema
err = yaml.Unmarshal(bytes, &schema)
if err != nil {
return nil, bosherr.WrapError(err, "Unmarshalling index")
}
var entries []indexEntry
for versionKey, entry := range schema.Builds {
entries = append(entries, indexEntry{
Key: versionKey,
Version: entry.Version,
BlobstoreID: entry.BlobstoreID,
SHA1: entry.SHA1,
})
}
return entries, nil
}
func (i FSIndex) save(name string, entries []indexEntry) error {
schema := fsIndexSchema{
Builds: map[string]fsIndexSchema_Entry{},
FormatVersion: "2",
}
for _, entry := range entries {
if i.expectsBlobstoreIDs && len(entry.BlobstoreID) == 0 {
return bosherr.Errorf("Internal inconsistency: entry must include blob ID '%#v'", entry)
} else if !i.expectsBlobstoreIDs && len(entry.BlobstoreID) > 0 {
return bosherr.Errorf("Internal inconsistency: entry must not include blob ID '%#v'", entry)
}
schema.Builds[entry.Key] = fsIndexSchema_Entry{
Version: entry.Version,
BlobstoreID: entry.BlobstoreID,
SHA1: entry.SHA1,
}
}
bytes, err := yaml.Marshal(schema)
if err != nil {
return bosherr.WrapError(err, "Marshalling index")
}
indexPath := i.indexPath(name)
err = i.fs.WriteFile(indexPath, bytes)
if err != nil {
return bosherr.WrapErrorf(err, "Writing index")
}
return nil
}
func (i FSIndex) indexPath(name string) string {
if i.useSubdir {
return path.Join(i.dirPath, name, "index.yml")
}
return path.Join(i.dirPath, "index.yml")
}