forked from cloudfoundry/bosh-cli
/
dir_reader.go
285 lines (224 loc) · 7.11 KB
/
dir_reader.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package pkg
import (
"os"
"path/filepath"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshsys "github.com/cloudfoundry/bosh-utils/system"
"errors"
. "github.com/cloudfoundry/bosh-cli/release/pkg/manifest"
. "github.com/cloudfoundry/bosh-cli/release/resource"
)
type DirReaderImpl struct {
archiveFactory ArchiveFunc
srcDirPath string
blobsDirPath string
fs boshsys.FileSystem
}
var (
fileNotFoundError = errors.New("File Not Found")
)
func NewDirReaderImpl(
archiveFactory ArchiveFunc,
srcDirPath string,
blobsDirPath string,
fs boshsys.FileSystem,
) DirReaderImpl {
return DirReaderImpl{
archiveFactory: archiveFactory,
srcDirPath: srcDirPath,
blobsDirPath: blobsDirPath,
fs: fs,
}
}
func (r DirReaderImpl) Read(path string) (*Package, error) {
manifestLock, err := r.collectLock(path)
if err != nil {
return nil, bosherr.WrapErrorf(err, "Collecting package spec lock")
}
if manifestLock != nil {
resource := NewExistingResource(manifestLock.Name, manifestLock.Fingerprint, "")
return NewPackage(resource, manifestLock.Dependencies), nil
}
manifest, files, prepFiles, err := r.collectFiles(path)
if err != nil {
return nil, bosherr.WrapErrorf(err, "Collecting package files")
}
// Note that files do not include package's spec file,
// but rather specify dependencies as additional chunks for the fingerprint.
archive := r.archiveFactory(ArchiveFactoryArgs{Files: files, PrepFiles: prepFiles, Chunks: manifest.Dependencies})
fp, err := archive.Fingerprint()
if err != nil {
return nil, err
}
resource := NewResource(manifest.Name, fp, archive)
return NewPackage(resource, manifest.Dependencies), nil
}
func (r DirReaderImpl) collectLock(path string) (*ManifestLock, error) {
path = filepath.Join(path, "spec.lock")
if r.fs.FileExists(path) {
manifestLock, err := NewManifestLockFromPath(path, r.fs)
if err != nil {
return nil, err
}
return &manifestLock, nil
}
return nil, nil
}
func (r DirReaderImpl) collectFiles(path string) (Manifest, []File, []File, error) {
var files, prepFiles []File
specPath := filepath.Join(path, "spec")
manifest, err := NewManifestFromPath(specPath, r.fs)
if err != nil {
return Manifest{}, nil, nil, err
}
packagingPath := filepath.Join(path, "packaging")
files, err = r.checkAndFilterDir(packagingPath, path)
if err != nil {
if err == fileNotFoundError {
return manifest, nil, nil, bosherr.Errorf(
"Expected to find '%s' for package '%s'", packagingPath, manifest.Name)
}
return manifest, nil, nil, bosherr.Errorf("Unexpected error occurred: %s", err)
}
prePackagingPath := filepath.Join(path, "pre_packaging")
prepFiles, err = r.checkAndFilterDir(prePackagingPath, path) //can proceed if there is no pre_packaging
if err != nil && err != fileNotFoundError {
return manifest, nil, nil, bosherr.Errorf("Unexpected error occurred: %s", err)
}
files = append(files, prepFiles...)
filesByRelPath, err := r.applyFilesPattern(manifest)
if err != nil {
return manifest, nil, nil, err
}
excludedFiles, err := r.applyExcludedFilesPattern(manifest)
if err != nil {
return manifest, nil, nil, err
}
for _, excludedFile := range excludedFiles {
delete(filesByRelPath, excludedFile.RelativePath)
}
for _, specialFileName := range []string{"packaging", "pre_packaging"} {
if _, ok := filesByRelPath[specialFileName]; ok {
errMsg := "Expected special '%s' file to not be included via 'files' key for package '%s'"
return manifest, nil, nil, bosherr.Errorf(errMsg, specialFileName, manifest.Name)
}
}
for _, file := range filesByRelPath {
files = append(files, file)
}
return manifest, files, prepFiles, nil
}
func (r DirReaderImpl) applyFilesPattern(manifest Manifest) (map[string]File, error) {
filesByRelPath := map[string]File{}
for _, glob := range manifest.Files {
matchingFilesFound := false
srcDirMatches, err := r.fs.RecursiveGlob(filepath.Join(r.srcDirPath, glob))
if err != nil {
return map[string]File{}, bosherr.WrapErrorf(err, "Listing package files in src")
}
for _, path := range srcDirMatches {
isPackageableFile, err := r.isPackageableFile(path)
if err != nil {
return map[string]File{}, bosherr.WrapErrorf(err, "Checking file packageability")
}
if isPackageableFile {
matchingFilesFound = true
file := NewFile(path, r.srcDirPath)
if _, found := filesByRelPath[file.RelativePath]; !found {
filesByRelPath[file.RelativePath] = file
}
}
}
blobsDirMatches, err := r.fs.RecursiveGlob(filepath.Join(r.blobsDirPath, glob))
if err != nil {
return map[string]File{}, bosherr.WrapErrorf(err, "Listing package files in blobs")
}
for _, path := range blobsDirMatches {
isPackageableFile, err := r.isPackageableFile(path)
if err != nil {
return map[string]File{}, bosherr.WrapErrorf(err, "Checking file packageability")
}
if isPackageableFile {
matchingFilesFound = true
file := NewFile(path, r.blobsDirPath)
if _, found := filesByRelPath[file.RelativePath]; !found {
filesByRelPath[file.RelativePath] = file
}
}
}
if !matchingFilesFound {
return nil, bosherr.Errorf("Missing files for pattern '%s'", glob)
}
}
return filesByRelPath, nil
}
func (r DirReaderImpl) applyExcludedFilesPattern(manifest Manifest) ([]File, error) {
var excludedFiles []File
for _, glob := range manifest.ExcludedFiles {
srcDirMatches, err := r.fs.RecursiveGlob(filepath.Join(r.srcDirPath, glob))
if err != nil {
return []File{}, bosherr.WrapErrorf(err, "Listing package excluded files in src")
}
for _, path := range srcDirMatches {
file := NewFile(path, r.srcDirPath)
excludedFiles = append(excludedFiles, file)
}
blobsDirMatches, err := r.fs.RecursiveGlob(filepath.Join(r.blobsDirPath, glob))
if err != nil {
return []File{}, bosherr.WrapErrorf(err, "Listing package excluded files in blobs")
}
for _, path := range blobsDirMatches {
file := NewFile(path, r.blobsDirPath)
excludedFiles = append(excludedFiles, file)
}
}
return excludedFiles, nil
}
func (r DirReaderImpl) checkAndFilterDir(packagePath, path string) ([]File, error) {
var files []File
if r.fs.FileExists(packagePath) {
isPackageableFile, err := r.isPackageableFile(packagePath)
if err != nil {
return nil, err
}
if isPackageableFile {
file := NewFile(packagePath, path)
file.ExcludeMode = true
files = append(files, file)
}
return files, nil
}
return []File{}, fileNotFoundError
}
func (r DirReaderImpl) isPackageableFile(path string) (bool, error) {
info, err := r.fs.Lstat(path)
if err != nil {
return false, err
}
if info.IsDir() {
return false, nil
}
fullSrcDirPath, err := r.fs.ReadAndFollowLink(r.srcDirPath)
if err != nil {
return false, err
}
fileIsSymlink := info.Mode()&os.ModeSymlink != 0
if !fileIsSymlink {
fullPath, err := r.fs.ReadAndFollowLink(path)
if err != nil {
return false, err
}
relPath, err := filepath.Rel(r.srcDirPath, path)
if err != nil {
return false, err
}
fullRelPath, err := filepath.Rel(fullSrcDirPath, fullPath)
if err != nil {
return false, err
}
if relPath != fullRelPath {
return false, nil
}
}
return true, nil
}