-
Notifications
You must be signed in to change notification settings - Fork 122
/
cloner.go
170 lines (147 loc) · 3.68 KB
/
cloner.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
// Copyright 2021-2024 Zenauth Ltd.
// SPDX-License-Identifier: Apache-2.0
package blob
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"io/fs"
"path/filepath"
"strings"
"go.uber.org/multierr"
"go.uber.org/zap"
"gocloud.dev/blob"
"github.com/cerbos/cerbos/internal/util"
)
// clonerFS represents file system interface that used by the Cloner.
type clonerFS interface {
fs.FS
Remove(name string) error
Create(name string) (io.WriteCloser, error)
MkdirAll(path string, perm fs.FileMode) error
}
type infoType map[string][]byte
type Cloner struct {
log *zap.SugaredLogger
bucket *blob.Bucket
fsys clonerFS
info infoType // map[path]eTag
}
// NewCloner creates an object to clone the bucket and saves
// supported files in the fsys.
func NewCloner(bucket *blob.Bucket, fsys clonerFS) (*Cloner, error) {
c := &Cloner{
bucket: bucket,
log: zap.S().Named("blob.cloner"),
fsys: fsys,
}
info, err := c.calculateInfo()
c.log.Debugf("Checkout dir contains (%d) files", len(info))
if err != nil {
return nil, err
}
c.info = info
return c, nil
}
type fileInfo struct {
file string
etag []byte
}
type CloneResult struct {
updateOrAdd []fileInfo
delete []string
failuresCount int
}
func (cr *CloneResult) isEmpty() bool {
return cr == nil || (len(cr.updateOrAdd) == 0 && len(cr.delete) == 0)
}
func (cr *CloneResult) failures() int {
if cr == nil {
return 0
}
return cr.failuresCount
}
func (c *Cloner) Clone(ctx context.Context) (*CloneResult, error) {
iter := c.bucket.List(nil)
info := make(infoType, len(c.info))
cr := new(CloneResult)
for {
obj, err := iter.Next(ctx)
if errors.Is(err, io.EOF) {
break
}
if err != nil {
c.log.Errorw("Failed to get next item", "error", err)
return nil, fmt.Errorf("failed to get next object in the bucket: %w", err)
}
file := strings.TrimPrefix(obj.Key, "/")
eTag := obj.MD5
if util.FileType(file) == util.FileTypeNotIndexed {
continue
}
info[file] = eTag
if eTag != nil && bytes.Equal(eTag, c.info[file]) {
continue
}
if err = c.downloadToFile(ctx, obj.Key, file); err != nil {
c.log.Errorw("Failed to download file", "error", err, "file", file)
cr.failuresCount++
} else {
cr.updateOrAdd = append(cr.updateOrAdd, fileInfo{file: file, etag: eTag})
}
}
for key := range c.info {
if _, ok := info[key]; !ok {
c.log.Debugw("Removing file", "file", key)
err := c.fsys.Remove(key)
if err != nil {
return nil, err
}
cr.delete = append(cr.delete, key)
}
}
c.info = info
return cr, nil
}
func (c *Cloner) downloadToFile(ctx context.Context, key, file string) (err error) {
// Create the directories in the path
dir := filepath.Dir(file)
if err = c.fsys.MkdirAll(dir, 0o775); err != nil { //nolint:mnd
return fmt.Errorf("failed to make dir %q: %w", dir, err)
}
// Set up the local file
fd, err := c.fsys.Create(file)
if err != nil {
return fmt.Errorf("failed to create a file %q: %w", file, err)
}
defer multierr.AppendInvoke(&err, multierr.Close(fd))
r, err := c.bucket.NewReader(ctx, key, nil)
if err != nil {
return fmt.Errorf("failed to create a reader for the object %q: %w", key, err)
}
// defer multierr.AppendInvoke(&err, multierr.Close(r))
defer r.Close()
if _, err = io.Copy(fd, r); err != nil {
return fmt.Errorf("failed to read the object %q: %w", key, err)
}
return nil
}
func (c *Cloner) calculateInfo() (infoType, error) {
result := make(infoType)
err := fs.WalkDir(c.fsys, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() || !util.IsSupportedFileType(path) {
return nil
}
result[path] = nil
return nil
})
if err != nil {
return nil, err
}
return result, nil
}