@@ -4,6 +4,7 @@ package recorder
4
4
5
5
import (
6
6
"fmt"
7
+ "hash/crc64"
7
8
"os"
8
9
"path/filepath"
9
10
"strings"
@@ -16,12 +17,16 @@ import (
16
17
"github.com/bpineau/katafygio/pkg/event"
17
18
)
18
19
19
- var appFs = afero .NewOsFs ()
20
+ var (
21
+ appFs = afero .NewOsFs ()
22
+ crc64Table = crc64 .MakeTable (crc64 .ECMA )
23
+ )
20
24
21
25
// activeFiles will contain a list of active (present in cluster) objets; we'll
22
26
// use that to periodically find and garbage collect stale objets in the git repos
23
- // (ie. if some objects were delete from cluster while katafygio was not running).
24
- type activeFiles map [string ]bool
27
+ // (ie. if some objects were delete from cluster while katafygio was not running),
28
+ // and to skip already existing and unchanged files.
29
+ type activeFiles map [string ]uint64
25
30
26
31
// Listener receive events from controllers and save them to disk as yaml files
27
32
type Listener struct {
@@ -133,17 +138,22 @@ func (w *Listener) save(file string, data []byte) error {
133
138
return nil
134
139
}
135
140
141
+ csum := crc64 .Checksum (data , crc64Table )
142
+
143
+ w .activesLock .RLock ()
144
+ prevsum , ok := w .actives [w .relativePath (file )]
145
+ w .activesLock .RUnlock ()
146
+ if ok && prevsum == csum {
147
+ return nil
148
+ }
149
+
136
150
dir := filepath .Clean (filepath .Dir (file ))
137
151
138
152
err := appFs .MkdirAll (dir , 0700 )
139
153
if err != nil {
140
154
return fmt .Errorf ("can't create local directory %s: %v" , dir , err )
141
155
}
142
156
143
- w .activesLock .Lock ()
144
- w .actives [w .relativePath (file )] = true
145
- w .activesLock .Unlock ()
146
-
147
157
tmpf , err := afero .TempFile (appFs , "" , "katafygio" )
148
158
if err != nil {
149
159
return fmt .Errorf ("failed to create a temporary file: %v" , err )
@@ -162,6 +172,10 @@ func (w *Listener) save(file string, data []byte) error {
162
172
return fmt .Errorf ("failed to rename %s to %s: %v" , tmpf .Name (), file , err )
163
173
}
164
174
175
+ w .activesLock .Lock ()
176
+ w .actives [w .relativePath (file )] = csum
177
+ w .activesLock .Unlock ()
178
+
165
179
return nil
166
180
}
167
181
0 commit comments