@@ -19,7 +19,6 @@ import (
19
19
"github.com/ActiveState/cli/internal/installation/storage"
20
20
"github.com/ActiveState/cli/internal/logging"
21
21
configMediator "github.com/ActiveState/cli/internal/mediators/config"
22
- "github.com/ActiveState/cli/internal/multilog"
23
22
"github.com/ActiveState/cli/internal/sliceutils"
24
23
"github.com/ActiveState/cli/internal/smartlink"
25
24
)
@@ -51,6 +50,8 @@ type artifactInfo struct {
51
50
InUse bool `json:"inUse"`
52
51
Size int64 `json:"size"`
53
52
LastAccessTime int64 `json:"lastAccessTime"`
53
+
54
+ id strfmt.UUID // for convenience when removing stale artifacts; should NOT have json tag
54
55
}
55
56
56
57
type ErrVolumeMismatch struct {
@@ -220,7 +221,10 @@ func (d *depot) DeployViaLink(id strfmt.UUID, relativeSrc, absoluteDest string)
220
221
Files : files .RelativePaths (),
221
222
RelativeSrc : relativeSrc ,
222
223
})
223
- d .recordUse (id )
224
+ err = d .recordUse (id )
225
+ if err != nil {
226
+ return errs .Wrap (err , "Could not record artifact use" )
227
+ }
224
228
225
229
return nil
226
230
}
@@ -278,26 +282,26 @@ func (d *depot) DeployViaCopy(id strfmt.UUID, relativeSrc, absoluteDest string)
278
282
Files : files .RelativePaths (),
279
283
RelativeSrc : relativeSrc ,
280
284
})
281
- d .recordUse (id )
285
+ err = d .recordUse (id )
286
+ if err != nil {
287
+ return errs .Wrap (err , "Could not record artifact use" )
288
+ }
282
289
283
290
return nil
284
291
}
285
292
286
- func (d * depot ) recordUse (id strfmt.UUID ) {
293
+ func (d * depot ) recordUse (id strfmt.UUID ) error {
287
294
// Ensure a cache entry for this artifact exists and then update its last access time.
288
295
if _ , exists := d .config .Cache [id ]; ! exists {
289
296
size , err := fileutils .GetDirSize (d .Path (id ))
290
297
if err != nil {
291
- multilog .Error ("Could not get artifact size on disk: %v" , err )
292
- size = 0
298
+ return errs .Wrap (err , "Could not get artifact size on disk" )
293
299
}
294
- logging .Debug ("Recording artifact '%s' with size %.1f MB" , id .String (), float64 (size )/ float64 (MB ))
295
- d .config .Cache [id ] = & artifactInfo {Size : size }
296
- } else {
297
- logging .Debug ("Recording use of artifact '%s'" , id .String ())
300
+ d .config .Cache [id ] = & artifactInfo {Size : size , id : id }
298
301
}
299
302
d .config .Cache [id ].InUse = true
300
303
d .config .Cache [id ].LastAccessTime = time .Now ().Unix ()
304
+ return nil
301
305
}
302
306
303
307
func (d * depot ) Undeploy (id strfmt.UUID , relativeSrc , path string ) error {
@@ -422,7 +426,10 @@ func (d *depot) Save() error {
422
426
logging .Debug ("Artifact '%s' is no longer in use" , id .String ())
423
427
}
424
428
}
425
- d .removeStaleArtifacts ()
429
+ err := d .removeStaleArtifacts ()
430
+ if err != nil {
431
+ return errs .Wrap (err , "Could not remove stale artifacts" )
432
+ }
426
433
427
434
// Write config file changes to disk
428
435
configFile := filepath .Join (d .depotPath , depotFile )
@@ -468,36 +475,37 @@ func someFilesExist(filePaths []string, basePath string) bool {
468
475
469
476
// removeStaleArtifacts iterates over all unused artifacts in the depot, sorts them by last access
470
477
// time, and removes them until the size of cached artifacts is under the limit.
471
- func (d * depot ) removeStaleArtifacts () {
472
- type artifact struct {
473
- id strfmt.UUID
474
- info * artifactInfo
475
- }
478
+ func (d * depot ) removeStaleArtifacts () error {
476
479
var totalSize int64
477
- unusedArtifacts := make ([]* artifact , 0 )
480
+ unusedArtifacts := make ([]* artifactInfo , 0 )
478
481
479
- for id , info := range d .config .Cache {
482
+ for _ , info := range d .config .Cache {
480
483
if ! info .InUse {
481
484
totalSize += info .Size
482
- unusedArtifacts = append (unusedArtifacts , & artifact { id : id , info : info } )
485
+ unusedArtifacts = append (unusedArtifacts , info )
483
486
}
484
487
}
485
488
logging .Debug ("There are %d unused artifacts totaling %.1f MB in size" , len (unusedArtifacts ), float64 (totalSize )/ float64 (MB ))
486
489
487
490
sort .Slice (unusedArtifacts , func (i , j int ) bool {
488
- return unusedArtifacts [i ].info . LastAccessTime < unusedArtifacts [j ]. info .LastAccessTime
491
+ return unusedArtifacts [i ].LastAccessTime < unusedArtifacts [j ].LastAccessTime
489
492
})
490
493
494
+ var rerr error
491
495
for _ , artifact := range unusedArtifacts {
492
496
if totalSize <= d .cacheSize {
493
497
break // done
494
498
}
495
- logging .Debug ("Removing cached artifact '%s', last accessed on %s" , artifact .id .String (), time .Unix (artifact .info .LastAccessTime , 0 ).Format (time .UnixDate ))
496
499
if err := os .RemoveAll (d .Path (artifact .id )); err == nil {
497
- totalSize -= artifact .info . Size
500
+ totalSize -= artifact .Size
498
501
} else {
499
- multilog .Error ("Could not delete old artifact: %v" , err )
502
+ if err := errs .Wrap (err , "Could not delete old artifact" ); rerr == nil {
503
+ rerr = err
504
+ } else {
505
+ rerr = errs .Pack (rerr , err )
506
+ }
500
507
}
501
508
delete (d .config .Cache , artifact .id )
502
509
}
510
+ return rerr
503
511
}
0 commit comments