Skip to content

Commit c0e6d8e

Browse files
committed
downloader: Add CacheEntries(), CacheKey(), and RemoveAllCacheDir()
Signed-off-by: Norio Nomura <[email protected]>
1 parent c397481 commit c0e6d8e

File tree

2 files changed

+68
-35
lines changed

2 files changed

+68
-35
lines changed

cmd/limactl/prune.go

+15-34
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package main
22

33
import (
4-
"crypto/sha256"
5-
"fmt"
64
"os"
7-
"path/filepath"
85

6+
"github.com/lima-vm/lima/pkg/downloader"
97
"github.com/lima-vm/lima/pkg/limayaml"
108
"github.com/lima-vm/lima/pkg/store"
119
"github.com/lima-vm/lima/pkg/templatestore"
@@ -31,40 +29,27 @@ func pruneAction(cmd *cobra.Command, _ []string) error {
3129
if err != nil {
3230
return err
3331
}
34-
ucd, err := os.UserCacheDir()
35-
if err != nil {
36-
return err
37-
}
38-
cacheDir := filepath.Join(ucd, "lima")
39-
logrus.Infof("Pruning %q", cacheDir)
32+
opt := downloader.WithCache()
4033
if !keepReferred {
41-
return os.RemoveAll(cacheDir)
34+
return downloader.RemoveAllCacheDir(opt)
4235
}
4336

4437
// Prune downloads that are not used by any instances or templates
45-
downloadDir := filepath.Join(cacheDir, "download", "by-url-sha256")
46-
_, err = os.Stat(downloadDir)
47-
if err != nil {
48-
if os.IsNotExist(err) {
49-
return nil
50-
}
51-
return err
52-
}
53-
cacheEntries, err := os.ReadDir(downloadDir)
38+
cacheEntries, err := downloader.CacheEntries(opt)
5439
if err != nil {
5540
return err
5641
}
5742
knownLocations, err := knownLocations()
5843
if err != nil {
5944
return err
6045
}
61-
for _, entry := range cacheEntries {
62-
if file, exists := knownLocations[entry.Name()]; exists {
63-
logrus.Debugf("Keep %q caching %q", entry.Name(), file.Location)
46+
for cacheKey, cachePath := range cacheEntries {
47+
if file, exists := knownLocations[cacheKey]; exists {
48+
logrus.Debugf("Keep %q caching %q", cacheKey, file.Location)
6449
} else {
65-
logrus.Debug("Deleting ", entry.Name())
66-
if err := os.RemoveAll(filepath.Join(downloadDir, entry.Name())); err != nil {
67-
logrus.Warnf("Failed to delete %q: %v", entry.Name(), err)
50+
logrus.Debug("Deleting ", cacheKey)
51+
if err := os.RemoveAll(cachePath); err != nil {
52+
logrus.Warnf("Failed to delete %q: %v", cacheKey, err)
6853
return err
6954
}
7055
}
@@ -114,23 +99,19 @@ func knownLocations() (map[string]limayaml.File, error) {
11499
func locationsFromLimaYAML(y *limayaml.LimaYAML) map[string]limayaml.File {
115100
locations := make(map[string]limayaml.File)
116101
for _, f := range y.Images {
117-
locations[sha256OfURL(f.Location)] = f.File
102+
locations[downloader.CacheKey(f.Location)] = f.File
118103
if f.Kernel != nil {
119-
locations[sha256OfURL(f.Kernel.Location)] = f.Kernel.File
104+
locations[downloader.CacheKey(f.Kernel.Location)] = f.Kernel.File
120105
}
121106
if f.Initrd != nil {
122-
locations[sha256OfURL(f.Initrd.Location)] = *f.Initrd
107+
locations[downloader.CacheKey(f.Initrd.Location)] = *f.Initrd
123108
}
124109
}
125110
for _, f := range y.Containerd.Archives {
126-
locations[sha256OfURL(f.Location)] = f
111+
locations[downloader.CacheKey(f.Location)] = f
127112
}
128113
for _, f := range y.Firmware.Images {
129-
locations[sha256OfURL(f.Location)] = f.File
114+
locations[downloader.CacheKey(f.Location)] = f.File
130115
}
131116
return locations
132117
}
133-
134-
func sha256OfURL(url string) string {
135-
return fmt.Sprintf("%x", sha256.Sum256([]byte(url)))
136-
}

pkg/downloader/downloader.go

+53-1
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ func Cached(remote string, opts ...Opt) (*Result, error) {
341341
// - "time" file contains the time (Last-Modified header)
342342
// - "type" file contains the type (Content-Type header)
343343
func cacheDirectoryPath(cacheDir, remote string) string {
344-
return filepath.Join(cacheDir, "download", "by-url-sha256", fmt.Sprintf("%x", sha256.Sum256([]byte(remote))))
344+
return filepath.Join(cacheDir, "download", "by-url-sha256", CacheKey(remote))
345345
}
346346

347347
// cacheDigestPath returns the cache digest file path.
@@ -601,3 +601,55 @@ func downloadHTTP(ctx context.Context, localPath, lastModified, contentType, url
601601
}
602602
return os.Rename(localPathTmp, localPath)
603603
}
604+
605+
// CacheEntries returns a map of cache entries.
606+
// The key is the SHA256 of the URL.
607+
// The value is the path to the cache entry.
608+
func CacheEntries(opt ...Opt) (map[string]string, error) {
609+
entries := make(map[string]string)
610+
var o options
611+
for _, f := range opt {
612+
if err := f(&o); err != nil {
613+
return nil, err
614+
}
615+
}
616+
if o.cacheDir == "" {
617+
return entries, nil
618+
}
619+
downloadDir := filepath.Join(o.cacheDir, "download", "by-url-sha256")
620+
_, err := os.Stat(downloadDir)
621+
if err != nil {
622+
if os.IsNotExist(err) {
623+
return entries, nil
624+
}
625+
return nil, err
626+
}
627+
cacheEntries, err := os.ReadDir(downloadDir)
628+
if err != nil {
629+
return nil, err
630+
}
631+
for _, entry := range cacheEntries {
632+
entries[entry.Name()] = filepath.Join(downloadDir, entry.Name())
633+
}
634+
return entries, nil
635+
}
636+
637+
// CacheKey returns the key for a cache entry of the remote URL.
638+
func CacheKey(remote string) string {
639+
return fmt.Sprintf("%x", sha256.Sum256([]byte(remote)))
640+
}
641+
642+
// RemoveAllCacheDir removes the cache directory.
643+
func RemoveAllCacheDir(opt ...Opt) error {
644+
var o options
645+
for _, f := range opt {
646+
if err := f(&o); err != nil {
647+
return err
648+
}
649+
}
650+
if o.cacheDir == "" {
651+
return nil
652+
}
653+
logrus.Infof("Pruning %q", o.cacheDir)
654+
return os.RemoveAll(o.cacheDir)
655+
}

0 commit comments

Comments
 (0)