-
Notifications
You must be signed in to change notification settings - Fork 8
/
common.go
312 lines (284 loc) · 8.56 KB
/
common.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package sharedcache
import (
"context"
"fmt"
"path/filepath"
"strings"
"time"
"github.com/ARM-software/golang-utils/utils/collection"
"github.com/ARM-software/golang-utils/utils/commonerrors"
"github.com/ARM-software/golang-utils/utils/filesystem"
"github.com/ARM-software/golang-utils/utils/hashing"
"github.com/ARM-software/golang-utils/utils/parallelisation"
"github.com/ARM-software/golang-utils/utils/reflection"
)
const defaultCachedPackage = "cache.zip"
const hashFileDescriptor = ".hash"
// GenerateKey generates a key based on a list of key elements `elems`.
func GenerateKey(elems ...string) string {
hash := "ef46db3751d8e999" // xxhash(""). No point in calculating it for "" everytime
for _, elem := range elems {
hash = hashing.CalculateHash(hash+elem, hashing.HashXXHash)
}
return hash
}
func generateHashFileName(srcFile string) string {
return fmt.Sprintf("%v%v", filepath.Base(srcFile), hashFileDescriptor)
}
func getHash(ctx context.Context, fs filesystem.FS, src string, forceHashUpdate bool) (hash string, err error) {
err = parallelisation.DetermineContextError(ctx)
if err != nil {
return
}
hashFile := filepath.Join(filepath.Dir(src), generateHashFileName(src))
if fs.Exists(hashFile) && !forceHashUpdate {
fileContents, readErr := fs.ReadFile(hashFile)
if readErr == nil {
hash = string(fileContents)
if len(hash) == 16 { // if valid hash
// xxhash 64 bit will have 16 characters
return
}
}
}
hash, err = fs.FileHash(hashing.HashXXHash, src)
if err != nil {
return
}
_ = fs.WriteFile(hashFile, []byte(hash), 0775)
return
}
func isPathForDirectory(fs filesystem.FS, dst string) bool {
if ok, _ := fs.IsDir(dst); ok {
return true
}
if filepath.Ext(dst) == "" {
return true
}
return strings.HasSuffix(dst, string(fs.PathSeparator())) || strings.HasSuffix(dst, "/")
}
// TransferFiles transfers a file from a location `src` to another `dest` and ensures the integrity (i.e. hash validation) of what was copied across.
// `dest` can be a file or a directory. If not existent, it will be created on the fly. non-existent directory path should be terminated by a path separator i.e. / or \
func TransferFiles(ctx context.Context, fs filesystem.FS, dst, src string) (destFile string, err error) {
err = parallelisation.DetermineContextError(ctx)
if err != nil {
return
}
if !fs.Exists(src) {
err = fmt.Errorf("%w: source path does not exist [%v]", commonerrors.ErrNotFound, src)
return
}
if result, suberr := fs.IsFile(src); !result || suberr != nil {
err = fmt.Errorf("%w: source is expected to be a file [%v, (%v)]", commonerrors.ErrInvalid, src, suberr)
return
}
destDir := dst
destFile = dst
renameFile := false
if isPathForDirectory(fs, destFile) {
baseName := filepath.Base(src)
destFile = filepath.Join(destDir, baseName)
} else {
destDir = filepath.Dir(dst)
renameFile = true
}
// get hash of original file
hash1, err := getHash(ctx, fs, src, false)
if err != nil {
return
}
// download/upload from/to cache
err = fs.CopyWithContext(ctx, src, destDir)
if err != nil {
return
}
if renameFile {
err = fs.MoveWithContext(ctx, filepath.Join(destDir, filepath.Base(src)), destFile)
if err != nil {
return
}
}
// get hash of downloaded/uploaded file
hash2, err := getHash(ctx, fs, destFile, true)
if err != nil {
return
}
// check that hashes match
if !strings.EqualFold(hash1, hash2) {
_ = fs.Rm(destFile)
err = fmt.Errorf("%w: error occurred during file transfer (hash mismatch) [%v]", commonerrors.ErrInvalid, src)
return
}
return
}
// AbstractSharedCacheRepository defines an abstract cache repository.
type AbstractSharedCacheRepository struct {
cfg *Configuration
fs *filesystem.VFS
}
func (c *AbstractSharedCacheRepository) getCacheEntryPath(key string) string {
return filepath.Join(c.cfg.RemoteStoragePath, key)
}
func (c *AbstractSharedCacheRepository) createEntry(ctx context.Context, key string) (entryPath string, err error) {
err = parallelisation.DetermineContextError(ctx)
if err != nil {
return
}
entryPath = c.getCacheEntryPath(key)
// create remote directory if this is the first time
err = c.fs.MkDir(entryPath) // won't do anything if dir already exists
return
}
func (c *AbstractSharedCacheRepository) GenerateKey(elems ...string) string {
return GenerateKey(elems...)
}
func (c *AbstractSharedCacheRepository) RemoveEntry(ctx context.Context, key string) error {
err := parallelisation.DetermineContextError(ctx)
if err != nil {
return err
}
return c.fs.Rm(c.getCacheEntryPath(key))
}
func (c *AbstractSharedCacheRepository) GetEntries(ctx context.Context) (entries []string, err error) {
err = parallelisation.DetermineContextError(ctx)
if err != nil {
return
}
entries, err = c.fs.Ls(c.cfg.RemoteStoragePath)
if err != nil {
return
}
if reflection.IsEmpty(c.cfg.FilesystemItemsToIgnore) {
return
}
toIgnore := collection.ParseCommaSeparatedList(c.cfg.FilesystemItemsToIgnore)
entries, err = c.fs.ExcludeAll(entries, toIgnore...)
return
}
func (c *AbstractSharedCacheRepository) EntriesCount(ctx context.Context) (count int64, err error) {
files, err := c.GetEntries(ctx)
if err != nil {
return
}
count = int64(len(files))
return
}
func (c *AbstractSharedCacheRepository) setUpLocalDestination(ctx context.Context, dest string) (err error) {
// create localDir if necessary and remove old cache file(s)
err = parallelisation.DetermineContextError(ctx)
if err != nil {
return
}
err = c.fs.MkDir(dest)
if err != nil {
return
}
err = c.fs.CleanDir(dest)
if err != nil {
return
}
return
}
func (c *AbstractSharedCacheRepository) unpackPackageToLocalDestination(ctx context.Context, cachedPackagePath, dest string) (err error) {
err = parallelisation.DetermineContextError(ctx)
if err != nil {
return
}
// create temp location for cached package to be unzipped
tempDir, err := c.fs.TempDirInTempDir(tempDirPrefix)
if err != nil {
return
}
defer func() { _ = c.fs.Rm(tempDir) }()
// do the transfer to a temporary folder.
destZip, err := TransferFiles(ctx, c.fs, tempDir, cachedPackagePath)
defer func() { _ = c.fs.Rm(destZip) }()
if err != nil {
return
}
// unpack package into destination
_, err = c.fs.UnzipWithContext(ctx, destZip, dest)
return
}
func (c *AbstractSharedCacheRepository) getEntryAge(ctx context.Context, key string, getCachedPackageFromEntryPath func(ctx context.Context, key, entryDir string) (string, error)) (age time.Duration, err error) {
err = parallelisation.DetermineContextError(ctx)
if err != nil {
return
}
remoteDir := c.getCacheEntryPath(key)
remoteExists, err := c.fs.IsDir(remoteDir)
if err != nil {
return
}
if !remoteExists {
err = fmt.Errorf("no cache entry for key [%v]: %w", key, commonerrors.ErrNotFound)
return
}
dirTime, suberr := c.fs.StatTimes(remoteDir)
cachedPackage, err := getCachedPackageFromEntryPath(ctx, key, remoteDir)
if err != nil {
err = suberr
if err == nil && dirTime != nil {
age = time.Since(dirTime.ModTime())
}
return
}
packageTime, err := c.fs.StatTimes(cachedPackage)
if err != nil {
if suberr == nil && dirTime != nil {
err = suberr
age = time.Since(dirTime.ModTime())
return
}
return
}
age = time.Since(packageTime.ModTime())
return
}
func (c *AbstractSharedCacheRepository) setEntryAge(ctx context.Context, key string, age time.Duration, getCachedPackageFromEntryPath func(ctx context.Context, key, entryDir string) (string, error)) (err error) {
err = parallelisation.DetermineContextError(ctx)
if err != nil {
return
}
modTime := time.Now().Add(-age)
remoteDir := c.getCacheEntryPath(key)
remoteExists, err := c.fs.IsDir(remoteDir)
if err != nil {
return
}
if !remoteExists {
err = fmt.Errorf("no cache entry for key [%v]: %w", key, commonerrors.ErrNotFound)
return
}
err = c.fs.Chtimes(remoteDir, modTime, modTime)
if err != nil {
return
}
cachedPackage, err := getCachedPackageFromEntryPath(ctx, key, remoteDir)
if err != nil {
return
}
err = c.fs.Chtimes(cachedPackage, modTime, modTime)
return
}
func NewAbstractSharedCacheRepository(cfg *Configuration, fs filesystem.FS) (cache *AbstractSharedCacheRepository, err error) {
if cfg == nil {
err = fmt.Errorf("%w: missing configuration", commonerrors.ErrUndefined)
return
}
err = cfg.Validate()
if err != nil {
err = fmt.Errorf("%w: invalid configuration: %v", commonerrors.ErrInvalid, err.Error())
return
}
rawFs, ok := fs.(*filesystem.VFS)
if !ok {
err = fmt.Errorf("%w: to work properly, cache needs a VFS filesystem implementation", commonerrors.ErrNotImplemented)
return
}
cache = &AbstractSharedCacheRepository{
cfg: cfg,
fs: rawFs,
}
return
}