@@ -159,27 +159,26 @@ func (s *Storage) RemoveAllButCurrent(artifact sourcev1.Artifact) ([]string, err
159
159
160
160
// getGarbageFiles returns all files that need to be garbage collected for the given artifact.
161
161
// Garbage files are determined based on the below flow:
162
- // 1. collect all files with an expired ttl
162
+ // 1. collect all artifact files with an expired ttl
163
163
// 2. if we satisfy maxItemsToBeRetained, then return
164
- // 3. else, remove all files till the latest n files remain, where n=maxItemsToBeRetained
165
- func (s * Storage ) getGarbageFiles (artifact sourcev1.Artifact , totalCountLimit , maxItemsToBeRetained int , ttl time.Duration ) ([]string , error ) {
164
+ // 3. else, collect all artifact files till the latest n files remain, where n=maxItemsToBeRetained
165
+ func (s * Storage ) getGarbageFiles (artifact sourcev1.Artifact , totalCountLimit , maxItemsToBeRetained int , ttl time.Duration ) (garbageFiles []string , _ error ) {
166
166
localPath := s .LocalPath (artifact )
167
167
dir := filepath .Dir (localPath )
168
- garbageFiles := []string {}
169
- filesWithCreatedTs := make (map [time.Time ]string )
168
+ artifactFilesWithCreatedTs := make (map [time.Time ]string )
170
169
// sortedPaths contain all files sorted according to their created ts.
171
170
sortedPaths := []string {}
172
171
now := time .Now ().UTC ()
173
- totalFiles := 0
172
+ totalArtifactFiles := 0
174
173
var errors []string
175
174
creationTimestamps := []time.Time {}
176
175
_ = filepath .WalkDir (dir , func (path string , d fs.DirEntry , err error ) error {
177
176
if err != nil {
178
177
errors = append (errors , err .Error ())
179
178
return nil
180
179
}
181
- if totalFiles >= totalCountLimit {
182
- return fmt .Errorf ("reached file walking limit, already walked over: %d" , totalFiles )
180
+ if totalArtifactFiles >= totalCountLimit {
181
+ return fmt .Errorf ("reached file walking limit, already walked over: %d" , totalArtifactFiles )
183
182
}
184
183
info , err := d .Info ()
185
184
if err != nil {
@@ -189,14 +188,16 @@ func (s *Storage) getGarbageFiles(artifact sourcev1.Artifact, totalCountLimit, m
189
188
createdAt := info .ModTime ().UTC ()
190
189
diff := now .Sub (createdAt )
191
190
// Compare the time difference between now and the time at which the file was created
192
- // with the provided TTL. Delete if the difference is greater than the TTL.
191
+ // with the provided TTL. Delete if the difference is greater than the TTL. Since the
192
+ // below logic just deals with determining if an artifact needs to be garbage collected,
193
+ // we avoid all lock files, adding them at the end to the list of garbage files.
193
194
expired := diff > ttl
194
- if ! info .IsDir () && info .Mode ()& os .ModeSymlink != os .ModeSymlink {
195
+ if ! info .IsDir () && info .Mode ()& os .ModeSymlink != os .ModeSymlink && filepath . Ext ( path ) != ".lock" {
195
196
if path != localPath && expired {
196
197
garbageFiles = append (garbageFiles , path )
197
198
}
198
- totalFiles += 1
199
- filesWithCreatedTs [createdAt ] = path
199
+ totalArtifactFiles += 1
200
+ artifactFilesWithCreatedTs [createdAt ] = path
200
201
creationTimestamps = append (creationTimestamps , createdAt )
201
202
}
202
203
return nil
@@ -208,14 +209,14 @@ func (s *Storage) getGarbageFiles(artifact sourcev1.Artifact, totalCountLimit, m
208
209
209
210
// We already collected enough garbage files to satisfy the no. of max
210
211
// items that are supposed to be retained, so exit early.
211
- if totalFiles - len (garbageFiles ) < maxItemsToBeRetained {
212
+ if totalArtifactFiles - len (garbageFiles ) < maxItemsToBeRetained {
212
213
return garbageFiles , nil
213
214
}
214
215
215
216
// sort all timestamps in an ascending order.
216
217
sort .Slice (creationTimestamps , func (i , j int ) bool { return creationTimestamps [i ].Before (creationTimestamps [j ]) })
217
218
for _ , ts := range creationTimestamps {
218
- path , ok := filesWithCreatedTs [ts ]
219
+ path , ok := artifactFilesWithCreatedTs [ts ]
219
220
if ! ok {
220
221
return garbageFiles , fmt .Errorf ("failed to fetch file for created ts: %v" , ts )
221
222
}
@@ -225,7 +226,7 @@ func (s *Storage) getGarbageFiles(artifact sourcev1.Artifact, totalCountLimit, m
225
226
var collected int
226
227
noOfGarbageFiles := len (garbageFiles )
227
228
for _ , path := range sortedPaths {
228
- if path != localPath && ! stringInSlice (path , garbageFiles ) {
229
+ if path != localPath && filepath . Ext ( path ) != ".lock" && ! stringInSlice (path , garbageFiles ) {
229
230
// If we previously collected a few garbage files with an expired ttl, then take that into account
230
231
// when checking whether we need to remove more files to satisfy the max no. of items allowed
231
232
// in the filesystem, along with the no. of files already removed in this loop.
@@ -271,6 +272,17 @@ func (s *Storage) GarbageCollect(ctx context.Context, artifact sourcev1.Artifact
271
272
} else {
272
273
deleted = append (deleted , file )
273
274
}
275
+ // If a lock file exists for this garbage artifact, remove that too.
276
+ lockFile := file + ".lock"
277
+ if _ , err = os .Lstat (lockFile ); err == nil {
278
+ err = os .Remove (lockFile )
279
+ if err != nil {
280
+ errors = append (errors , err )
281
+ } else {
282
+ deleted = append (deleted , lockFile )
283
+ }
284
+
285
+ }
274
286
}
275
287
}
276
288
if len (errors ) > 0 {
0 commit comments