diff --git a/cmd/limactl/prune.go b/cmd/limactl/prune.go index bce9427f6bc..bbe2f56101c 100644 --- a/cmd/limactl/prune.go +++ b/cmd/limactl/prune.go @@ -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 } diff --git a/pkg/downloader/downloader.go b/pkg/downloader/downloader.go index 71101981d4b..047f197bf89 100644 --- a/pkg/downloader/downloader.go +++ b/pkg/downloader/downloader.go @@ -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 } @@ -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") @@ -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") @@ -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") @@ -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. @@ -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 } // RemoveAllCacheDir removes the cache directory.