@@ -19,7 +19,6 @@ import (
1919 "github.com/ActiveState/cli/internal/installation/storage"
2020 "github.com/ActiveState/cli/internal/logging"
2121 configMediator "github.com/ActiveState/cli/internal/mediators/config"
22- "github.com/ActiveState/cli/internal/multilog"
2322 "github.com/ActiveState/cli/internal/sliceutils"
2423 "github.com/ActiveState/cli/internal/smartlink"
2524)
@@ -51,6 +50,8 @@ type artifactInfo struct {
5150 InUse bool `json:"inUse"`
5251 Size int64 `json:"size"`
5352 LastAccessTime int64 `json:"lastAccessTime"`
53+
54+ id strfmt.UUID // for convenience when removing stale artifacts; should NOT have json tag
5455}
5556
5657type ErrVolumeMismatch struct {
@@ -220,7 +221,10 @@ func (d *depot) DeployViaLink(id strfmt.UUID, relativeSrc, absoluteDest string)
220221 Files : files .RelativePaths (),
221222 RelativeSrc : relativeSrc ,
222223 })
223- d .recordUse (id )
224+ err = d .recordUse (id )
225+ if err != nil {
226+ return errs .Wrap (err , "Could not record artifact use" )
227+ }
224228
225229 return nil
226230}
@@ -278,26 +282,26 @@ func (d *depot) DeployViaCopy(id strfmt.UUID, relativeSrc, absoluteDest string)
278282 Files : files .RelativePaths (),
279283 RelativeSrc : relativeSrc ,
280284 })
281- d .recordUse (id )
285+ err = d .recordUse (id )
286+ if err != nil {
287+ return errs .Wrap (err , "Could not record artifact use" )
288+ }
282289
283290 return nil
284291}
285292
286- func (d * depot ) recordUse (id strfmt.UUID ) {
293+ func (d * depot ) recordUse (id strfmt.UUID ) error {
287294 // Ensure a cache entry for this artifact exists and then update its last access time.
288295 if _ , exists := d .config .Cache [id ]; ! exists {
289296 size , err := fileutils .GetDirSize (d .Path (id ))
290297 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" )
293299 }
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 }
298301 }
299302 d .config .Cache [id ].InUse = true
300303 d .config .Cache [id ].LastAccessTime = time .Now ().Unix ()
304+ return nil
301305}
302306
303307func (d * depot ) Undeploy (id strfmt.UUID , relativeSrc , path string ) error {
@@ -422,7 +426,10 @@ func (d *depot) Save() error {
422426 logging .Debug ("Artifact '%s' is no longer in use" , id .String ())
423427 }
424428 }
425- d .removeStaleArtifacts ()
429+ err := d .removeStaleArtifacts ()
430+ if err != nil {
431+ return errs .Wrap (err , "Could not remove stale artifacts" )
432+ }
426433
427434 // Write config file changes to disk
428435 configFile := filepath .Join (d .depotPath , depotFile )
@@ -468,36 +475,37 @@ func someFilesExist(filePaths []string, basePath string) bool {
468475
469476// removeStaleArtifacts iterates over all unused artifacts in the depot, sorts them by last access
470477// 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 {
476479 var totalSize int64
477- unusedArtifacts := make ([]* artifact , 0 )
480+ unusedArtifacts := make ([]* artifactInfo , 0 )
478481
479- for id , info := range d .config .Cache {
482+ for _ , info := range d .config .Cache {
480483 if ! info .InUse {
481484 totalSize += info .Size
482- unusedArtifacts = append (unusedArtifacts , & artifact { id : id , info : info } )
485+ unusedArtifacts = append (unusedArtifacts , info )
483486 }
484487 }
485488 logging .Debug ("There are %d unused artifacts totaling %.1f MB in size" , len (unusedArtifacts ), float64 (totalSize )/ float64 (MB ))
486489
487490 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
489492 })
490493
494+ var rerr error
491495 for _ , artifact := range unusedArtifacts {
492496 if totalSize <= d .cacheSize {
493497 break // done
494498 }
495- logging .Debug ("Removing cached artifact '%s', last accessed on %s" , artifact .id .String (), time .Unix (artifact .info .LastAccessTime , 0 ).Format (time .UnixDate ))
496499 if err := os .RemoveAll (d .Path (artifact .id )); err == nil {
497- totalSize -= artifact .info . Size
500+ totalSize -= artifact .Size
498501 } 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+ }
500507 }
501508 delete (d .config .Cache , artifact .id )
502509 }
510+ return rerr
503511}
0 commit comments