-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfilemanager.go
489 lines (397 loc) · 11.4 KB
/
filemanager.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
// Copyright 2024 qbee.io
//
// 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.
//
// SPDX-License-Identifier: Apache-2.0
package client
import (
"context"
"crypto/sha256"
"encoding/hex"
"fmt"
"io"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
)
// FileManager provides implementation of common batch operations inside qbee's file manager.
type FileManager struct {
// client is the FileManager client.
client *Client
// deleteMissing deletes files on the remote that are not present locally.
deleteMissing bool
// dryRun does not perform any changes.
dryRun bool
// excludes removes local paths from being considered
excludes []regexp.Regexp
// includes includes only local paths that match the pattern
includes []regexp.Regexp
// remoteFiles is a map of the remote files.
remoteFiles map[string]File
// localFiles is a map of the local files.
localFiles map[string]File
}
// NewFileManager returns a new FileManager.
func NewFileManager() *FileManager {
return &FileManager{
remoteFiles: make(map[string]File),
localFiles: make(map[string]File),
}
}
// WithClient sets the client
func (m *FileManager) WithClient(client *Client) *FileManager {
m.client = client
return m
}
// WithDelete sets the delete flag
func (m *FileManager) WithDelete(del bool) *FileManager {
m.deleteMissing = del
return m
}
// WithDryRun sets the dryrun flag
func (m *FileManager) WithDryRun(dryrun bool) *FileManager {
m.dryRun = dryrun
return m
}
// GetLocalSnapshot returns the snapshot of the local files.
func (m *FileManager) GetLocalSnapshot() map[string]File {
return m.localFiles
}
// GetRemoteSnapshot returns the snapshot of the remote files.
func (m *FileManager) GetRemoteSnapshot() map[string]File {
return m.remoteFiles
}
// WithExcludes sets the excludes
func (m *FileManager) WithExcludes(excludes string) *FileManager {
m.excludes = preparePathMatchList(excludes)
return m
}
// WithIncludes sets the includes
func (m *FileManager) WithIncludes(includes string) *FileManager {
m.includes = preparePathMatchList(includes)
return m
}
// preparePathMatchList prepares a list of regular expressions from a comma-separated list of patterns.
func preparePathMatchList(patterns string) []regexp.Regexp {
patternsList := strings.Split(patterns, ",")
pathMatchList := make([]regexp.Regexp, 0)
for _, pathPattern := range patternsList {
pathPattern = strings.TrimLeft(pathPattern, "/")
pathPattern = regexp.QuoteMeta(pathPattern)
pathRE := regexp.MustCompile(pathPattern)
pathMatchList = append(pathMatchList, *pathRE)
}
return pathMatchList
}
// Sync synchronizes the local directory with the FileManager.
func (m *FileManager) Sync(ctx context.Context, source, dest string) error {
if err := m.SnapshotLocal(source); err != nil {
return err
}
if err := m.SnapshotRemote(ctx, dest); err != nil {
return err
}
updates, err := m.filterUploads()
if err != nil {
return err
}
for _, uploadFile := range updates {
if uploadFile.IsDir {
continue
}
if err := m.upload(ctx, uploadFile, dest); err != nil {
return err
}
}
if m.deleteMissing {
// remove all files that from remote that are present locally
for remoteRelativeName := range m.remoteFiles {
if _, ok := m.localFiles[remoteRelativeName]; ok {
delete(m.remoteFiles, remoteRelativeName)
}
}
// delete all remaining files
return m.deleteRemoteRecursive()
}
return nil
}
// Remove all files under provided remotePath.
//
// If recursive flag is set to true and remotePath points to a directory,
// this method will remove all nested files and directories recursively.
// Otherwise, it will return an error when deleting a non-empty directory.
func (m *FileManager) Remove(ctx context.Context, remotePath string, recursive bool) error {
// Add the top directory to the list of files to delete
m.remoteFiles[remotePath] = File{
Name: remotePath,
Path: remotePath,
IsDir: true,
}
if recursive {
if err := m.SnapshotRemote(ctx, remotePath); err != nil {
return err
}
}
return m.deleteRemoteRecursive()
}
// List files under provided remotePath.
func (m *FileManager) List(ctx context.Context, remotePath string) error {
if err := m.SnapshotRemote(ctx, remotePath); err != nil {
return err
}
for _, remoteFile := range sortFileMap(m.remoteFiles, false) {
fmt.Println(remoteFile.Path)
}
return nil
}
// deleteRemoteRecursive deletes all discovered remote files in the FileManager.
func (m *FileManager) deleteRemoteRecursive() error {
deletes := sortFileMap(m.remoteFiles, true)
for _, remoteFile := range deletes {
if err := m.deleteRemote(remoteFile); err != nil {
return err
}
}
return nil
}
// sortFileMap returns a slice of files sorted by path
func sortFileMap(fileMap map[string]File, reverse bool) []File {
files := []File{}
for _, file := range fileMap {
files = append(files, file)
}
sort.Slice(files, func(i, j int) bool {
if reverse {
return files[i].Path > files[j].Path
}
return files[i].Path < files[j].Path
})
return files
}
// filterUploads returns the files that need to be uploaded.
func (m *FileManager) filterUploads() (map[string]File, error) {
uploads := make(map[string]File)
for localRelativeName, localFile := range m.localFiles {
if localFile.IsDir {
continue
}
remoteFile, ok := m.remoteFiles[localRelativeName]
if !ok {
uploads[localRelativeName] = localFile
continue
}
if localFile.Size != remoteFile.Size {
uploads[localRelativeName] = localFile
continue
}
if localDigest, err := getFileDigest(localFile.Path); err != nil {
return nil, err
} else if localDigest != remoteFile.Digest {
uploads[localRelativeName] = localFile
}
}
return uploads, nil
}
// SnapshotLocal populates the localFiles map with the files in the local directory.
func (m *FileManager) SnapshotLocal(localPath string) error {
m.localFiles = make(map[string]File)
stat, err := os.Stat(localPath)
if err != nil {
return err
}
if !stat.IsDir() {
return fmt.Errorf("path %s is not a directory", localPath)
}
err = filepath.Walk(localPath, func(path string, stat os.FileInfo, err error) error {
if err != nil {
return err
}
fileName, err := filepath.Rel(localPath, path)
if err != nil {
return err
}
if m.matchExcludes(fileName) && !m.matchIncludes(localPath, fileName) {
return nil
}
fileName = filepath.ToSlash(fileName)
m.localFiles[fileName] = File{
Name: fileName,
Path: path,
Size: int(stat.Size()),
IsDir: stat.IsDir(),
}
return nil
})
if err != nil {
return err
}
return nil
}
// matchExcludes determines if a path matches any of the exclude filters.
func (m *FileManager) matchExcludes(path string) bool {
if len(m.excludes) == 0 {
return false
}
return matchRElist(path, m.excludes)
}
// matchIncludes determines if a path matches any of the include filters.
func (m *FileManager) matchIncludes(basePath, path string) bool {
if len(m.includes) == 0 {
return false
}
if !matchRElist(path, m.includes) {
return false
}
// add directories to the list of files
dirNameRelative := filepath.Dir(path)
subDirPaths := strings.Split(dirNameRelative, string(filepath.Separator))
for i := range subDirPaths {
subDirPath := filepath.Join(subDirPaths[:i+1]...)
subDirPathKey := filepath.ToSlash(subDirPath)
if _, ok := m.localFiles[subDirPathKey]; ok {
continue
}
absolutePath := filepath.Join(basePath, subDirPath)
fileInfo, err := os.Stat(absolutePath)
// this should never happen
if os.IsNotExist(err) {
return false
}
m.localFiles[subDirPathKey] = File{
Name: subDirPathKey,
IsDir: true,
Path: absolutePath,
Size: int(fileInfo.Size()),
}
}
return true
}
// matchRElist determines if a pattern matches a list of regular expressions.
func matchRElist(pattern string, list []regexp.Regexp) bool {
if len(list) == 0 {
return false
}
for _, re := range list {
if re.MatchString(pattern) {
return true
}
}
return false
}
// SnapshotRemote populates the remoteFiles map with the files in the FileManager.
func (m *FileManager) SnapshotRemote(ctx context.Context, remotePath string) error {
m.remoteFiles = make(map[string]File)
searchPath := fmt.Sprintf("^%s/.*", remotePath)
trimPrefix := remotePath + "/"
// Search for the root directory
if remotePath == "/" {
searchPath = fmt.Sprintf("^%s.*", remotePath)
trimPrefix = remotePath
}
query := ListQuery{
ItemsPerPage: 1000,
Offset: 0,
Search: ListSearch{
Path: searchPath,
},
}
for {
files, err := m.client.ListFiles(ctx, query)
if err != nil {
return err
}
for _, file := range files.Items {
remoteRelativeName := strings.TrimPrefix(file.Path, trimPrefix)
m.remoteFiles[remoteRelativeName] = file
}
query.Offset += query.ItemsPerPage
if query.Offset > files.Total {
break
}
}
return nil
}
// upload uploads the file to the FileManager.
func (m *FileManager) upload(ctx context.Context, file File, destPath string) error {
// We do not upload directories
if file.IsDir {
return nil
}
baseName := filepath.Base(file.Name)
dirName := filepath.ToSlash(filepath.Dir(file.Name))
destinationPath := filepath.ToSlash(filepath.Join(destPath, dirName))
fmt.Printf("Uploading %s to %s\n", file.Path, destinationPath)
if m.dryRun {
return nil
}
reader, err := os.Open(file.Path)
if err != nil {
return err
}
defer reader.Close()
if err := m.client.UploadFileReplace(ctx, destinationPath, baseName, true, reader); err != nil {
return err
}
return nil
}
// deleteRemote deletes the remote file.
func (m *FileManager) deleteRemote(remoteFile File) error {
fmt.Printf("Deleting %s\n", remoteFile.Path)
if m.dryRun {
return nil
}
return m.client.DeleteFile(context.Background(), remoteFile.Path)
}
// getFileDigest returns the sha256 digest of the given file.
func getFileDigest(src string) (string, error) {
fp, err := os.Open(src)
if err != nil {
return "", fmt.Errorf("error opening file %s: %w", src, err)
}
defer fp.Close()
if _, err = fp.Stat(); err != nil {
return "", fmt.Errorf("error getting file metadata %s: %w", src, err)
}
digest := sha256.New()
if _, err = io.Copy(digest, fp); err != nil {
return "", fmt.Errorf("error calculating file checksum %s: %w", src, err)
}
return hex.EncodeToString(digest.Sum(nil)), nil
}
// UploadFile uploads a file to the FileManager.
func (m *FileManager) UploadFile(ctx context.Context, remotePath, localPath string, overwrite bool) error {
reader, err := os.Open(localPath)
if err != nil {
return err
}
defer reader.Close()
return m.client.UploadFileReplace(ctx, remotePath, localPath, overwrite, reader)
}
// DownloadFile downloads a file from the FileManager.
func (m *FileManager) DownloadFile(ctx context.Context, remotePath, localPath string) error {
writer, err := os.Create(localPath)
if err != nil {
return err
}
defer writer.Close()
reader, err := m.client.DownloadFile(ctx, remotePath)
if err != nil {
return err
}
if _, err := io.Copy(writer, reader); err != nil {
return err
}
return nil
}