Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions cmd/limactl/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,20 +114,24 @@ func knownLocations(ctx context.Context) (map[string]limatype.File, error) {

func locationsFromLimaYAML(y *limatype.LimaYAML) map[string]limatype.File {
locations := make(map[string]limatype.File)
for _, f := range y.Images {
locations[downloader.CacheKey(f.Location)] = f.File
if f.Kernel != nil {
locations[downloader.CacheKey(f.Kernel.Location)] = f.Kernel.File
// decompress=false and decompress=true are cached separately
// because the same URL may be used for both compressed and uncompressed files.
for _, decompress := range []bool{false, true} {
for _, f := range y.Images {
locations[downloader.CacheKey(f.Location, decompress)] = f.File
if f.Kernel != nil {
locations[downloader.CacheKey(f.Kernel.Location, decompress)] = f.Kernel.File
}
if f.Initrd != nil {
locations[downloader.CacheKey(f.Initrd.Location, decompress)] = *f.Initrd
}
}
if f.Initrd != nil {
locations[downloader.CacheKey(f.Initrd.Location)] = *f.Initrd
for _, f := range y.Containerd.Archives {
locations[downloader.CacheKey(f.Location, decompress)] = f
}
for _, f := range y.Firmware.Images {
locations[downloader.CacheKey(f.Location, decompress)] = f.File
}
}
for _, f := range y.Containerd.Archives {
locations[downloader.CacheKey(f.Location)] = f
}
for _, f := range y.Firmware.Images {
locations[downloader.CacheKey(f.Location)] = f.File
}
return locations
}
20 changes: 12 additions & 8 deletions pkg/downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func Download(ctx context.Context, local, remote string, opts ...Opt) (*Result,
return res, nil
}

shad := cacheDirectoryPath(o.cacheDir, remote)
shad := cacheDirectoryPath(o.cacheDir, remote, o.decompress)
if err := os.MkdirAll(shad, 0o700); err != nil {
return nil, err
}
Expand All @@ -255,7 +255,7 @@ func Download(ctx context.Context, local, remote string, opts ...Opt) (*Result,
// nil if the file was copied, nil, nil if the file is not in the cache or the
// cache needs update, or nil, error on fatal error.
func getCached(ctx context.Context, localPath, remote string, o options) (*Result, error) {
shad := cacheDirectoryPath(o.cacheDir, remote)
shad := cacheDirectoryPath(o.cacheDir, remote, o.decompress)
shadData := filepath.Join(shad, "data")
shadTime := filepath.Join(shad, "time")
shadType := filepath.Join(shad, "type")
Expand Down Expand Up @@ -301,7 +301,7 @@ func getCached(ctx context.Context, localPath, remote string, o options) (*Resul

// fetch downloads remote to the cache and copy the cached file to local path.
func fetch(ctx context.Context, localPath, remote string, o options) (*Result, error) {
shad := cacheDirectoryPath(o.cacheDir, remote)
shad := cacheDirectoryPath(o.cacheDir, remote, o.decompress)
shadData := filepath.Join(shad, "data")
shadTime := filepath.Join(shad, "time")
shadType := filepath.Join(shad, "type")
Expand Down Expand Up @@ -354,7 +354,7 @@ func Cached(remote string, opts ...Opt) (*Result, error) {
return nil, errors.New("local files are not cached")
}

shad := cacheDirectoryPath(o.cacheDir, remote)
shad := cacheDirectoryPath(o.cacheDir, remote, o.decompress)
shadData := filepath.Join(shad, "data")
shadTime := filepath.Join(shad, "time")
shadType := filepath.Join(shad, "type")
Expand Down Expand Up @@ -404,8 +404,8 @@ func Cached(remote string, opts ...Opt) (*Result, error) {
// - "data" file contains the data
// - "time" file contains the time (Last-Modified header)
// - "type" file contains the type (Content-Type header)
func cacheDirectoryPath(cacheDir, remote string) string {
return filepath.Join(cacheDir, "download", "by-url-sha256", CacheKey(remote))
func cacheDirectoryPath(cacheDir, remote string, decompress bool) string {
return filepath.Join(cacheDir, "download", "by-url-sha256", CacheKey(remote, decompress))
}

// cacheDigestPath returns the cache digest file path.
Expand Down Expand Up @@ -776,8 +776,12 @@ func CacheEntries(opts ...Opt) (map[string]string, error) {
}

// CacheKey returns the key for a cache entry of the remote URL.
func CacheKey(remote string) string {
return fmt.Sprintf("%x", sha256.Sum256([]byte(remote)))
func CacheKey(remote string, decompress bool) string {
k := fmt.Sprintf("%x", sha256.Sum256([]byte(remote)))
if decompress {
k += "+decomp"
}
return k
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(On second thought this might be rather confusing when the original data is not compressed)


// RemoveAllCacheDir removes the cache directory.
Expand Down