This repository has been archived by the owner on Sep 6, 2021. It is now read-only.
forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource.go
188 lines (151 loc) · 4.19 KB
/
resource.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
package resource
import (
"fmt"
"os"
"github.com/cloudfoundry/bosh-cli/crypto"
crypto2 "github.com/cloudfoundry/bosh-utils/crypto"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
)
type ResourceImpl struct {
name string
fingerprint string
archivePath string
archiveDigest string
expectToExist bool
archive Archive
}
type duplicateError interface {
IsDuplicate() bool
}
func NewResource(name, fp string, archive Archive) *ResourceImpl {
return &ResourceImpl{
name: name,
fingerprint: fp,
archive: archive,
}
}
func NewExistingResource(name, fp, sha1 string) *ResourceImpl {
return &ResourceImpl{
name: name,
fingerprint: fp,
archiveDigest: sha1,
expectToExist: true,
}
}
func NewResourceWithBuiltArchive(name, fp, path, sha1 string) *ResourceImpl {
return &ResourceImpl{
name: name,
fingerprint: fp,
archivePath: path,
archiveDigest: sha1,
expectToExist: true,
}
}
func (r *ResourceImpl) Name() string { return r.name }
func (r *ResourceImpl) Fingerprint() string { return r.fingerprint }
func (r *ResourceImpl) ArchivePath() string {
if len(r.archivePath) == 0 {
errMsg := "Internal inconsistency: Resource '%s/%s' must be found or built before getting its archive path"
panic(fmt.Sprintf(errMsg, r.name, r.fingerprint))
}
return r.archivePath
}
func (r *ResourceImpl) ArchiveDigest() string {
if len(r.archiveDigest) == 0 {
errMsg := "Internal inconsistency: Resource '%s/%s' must be found or built before getting its archive SHA1"
panic(fmt.Sprintf(errMsg, r.name, r.fingerprint))
}
return r.archiveDigest
}
func (r *ResourceImpl) Build(devIndex, finalIndex ArchiveIndex) error {
if r.hasArchive() {
return nil
}
err := r.findAndAttach(devIndex, finalIndex, r.expectToExist)
if err != nil {
return err
}
if r.hasArchive() {
return nil
}
path, sha1, err := r.archive.Build(r.fingerprint)
if err != nil {
return err
}
newDevPath, newDevSHA1, err := devIndex.Add(r.name, r.fingerprint, path, sha1)
de, ok := err.(duplicateError)
if ok && de.IsDuplicate() {
return r.findAndAttach(devIndex, finalIndex, r.expectToExist)
}
if err != nil {
return err
}
r.attachArchive(newDevPath, newDevSHA1)
return nil
}
func (r *ResourceImpl) Finalize(finalIndex ArchiveIndex) error {
finalPath, finalSHA1, err := finalIndex.Find(r.name, r.fingerprint)
if err != nil {
return err
} else if len(finalPath) > 0 {
r.attachArchive(finalPath, finalSHA1)
return nil
}
_, _, err = finalIndex.Add(r.name, r.fingerprint, r.ArchivePath(), r.ArchiveDigest())
de, ok := err.(duplicateError)
if ok && de.IsDuplicate() {
return r.Finalize(finalIndex)
}
return err
}
func (r *ResourceImpl) RehashWithCalculator(calculator crypto.DigestCalculator, archiveFilePathReader crypto2.ArchiveDigestFilePathReader) (Resource, error) {
archiveFile, err := archiveFilePathReader.OpenFile(r.archivePath, os.O_RDONLY, 0)
if err != nil {
return &ResourceImpl{}, err
}
defer archiveFile.Close()
digest, err := crypto2.ParseMultipleDigest(r.archiveDigest)
if err != nil {
return &ResourceImpl{}, err
}
err = digest.Verify(archiveFile)
if err != nil {
return &ResourceImpl{}, err
}
newSHA, err := calculator.Calculate(r.archivePath)
return &ResourceImpl{
name: r.name,
fingerprint: r.fingerprint,
archivePath: r.archivePath,
archiveDigest: newSHA,
expectToExist: r.expectToExist,
archive: r.archive,
}, err
}
func (r *ResourceImpl) findAndAttach(devIndex, finalIndex ArchiveIndex, errIfNotFound bool) error {
devPath, devSHA1, err := devIndex.Find(r.name, r.fingerprint)
if err != nil {
return err
} else if len(devPath) > 0 {
r.attachArchive(devPath, devSHA1)
return nil
}
finalPath, finalSHA1, err := finalIndex.Find(r.name, r.fingerprint)
if err != nil {
return err
} else if len(finalPath) > 0 {
r.attachArchive(finalPath, finalSHA1)
return nil
}
if errIfNotFound {
return bosherr.Errorf("Expected to find '%s/%s'", r.name, r.fingerprint)
}
return nil
}
func (r *ResourceImpl) attachArchive(path, sha1 string) {
r.archivePath = path
r.archiveDigest = sha1
}
func (r *ResourceImpl) hasArchive() bool {
return len(r.archivePath) > 0 && len(r.archiveDigest) > 0
}