@@ -12,6 +12,7 @@ import (
1212 "regexp"
1313 "strings"
1414 "syscall"
15+ "time"
1516
1617 "github.com/goccy/go-json"
1718 "github.com/hedzr/progressbar"
@@ -515,3 +516,50 @@ func downloadOCILayer(ctx context.Context, registry, repository string, manifest
515516 }
516517 return binaryResp , sigResp , nil
517518}
519+
520+ // cleanTempFiles removes .tmp files in the InstallDir that haven't been accessed in over a day.
521+ func cleanInstallCache (installDir string ) error {
522+ const oneDay = 24 * time .Hour
523+ now := time .Now ()
524+
525+ entries , err := os .ReadDir (installDir )
526+ if err != nil {
527+ return errFileAccess .Wrap (err )
528+ }
529+
530+ for _ , entry := range entries {
531+ if entry .IsDir () || ! strings .HasSuffix (entry .Name (), ".tmp" ) {
532+ continue
533+ }
534+
535+ filePath := filepath .Join (installDir , entry .Name ())
536+ fileInfo , err := os .Stat (filePath )
537+ if err != nil {
538+ if verbosityLevel >= silentVerbosityWithErrors {
539+ fmt .Fprintf (os .Stderr , "Error accessing file info for %s: %v\n " , filePath , err )
540+ }
541+ continue
542+ }
543+ var atime time.Time
544+ if sysInfo , ok := fileInfo .Sys ().(* syscall.Stat_t ); ok {
545+ atime = time .Unix (sysInfo .Atim .Sec , sysInfo .Atim .Nsec )
546+ } else {
547+ if verbosityLevel >= extraVerbose {
548+ fmt .Fprintf (os .Stderr , "Warning: ATime not supported for %s, skipping cleanup\n " , filePath )
549+ }
550+ continue
551+ }
552+
553+ if now .Sub (atime ) > oneDay {
554+ if err := os .Remove (filePath ); err != nil {
555+ if verbosityLevel >= silentVerbosityWithErrors {
556+ fmt .Fprintf (os .Stderr , "Error removing old .tmp file %s: %v\n " , filePath , err )
557+ }
558+ } else if verbosityLevel >= extraVerbose {
559+ fmt .Printf ("Removed old .tmp file: %s\n " , filePath )
560+ }
561+ }
562+ }
563+
564+ return nil
565+ }
0 commit comments