Skip to content

Commit ec85797

Browse files
committed
downloader: CacheKey: include decompression flag
The caches are now created separately for compressed and decompressed contents. When a decompressed content is cached and a compressed content is requested, the downloader now correctly returns the compressed content. - URLSHA/data: unmodified original data - URLSHA/sha256.digest: digest of the original data - URLSHA+decomp/data: decompressed data - URLSHA+decomp/sha256.digest: digest of the *original* (i.e., compressed) data Caching a decompressed content does not automatically cache the original compressed content. Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
1 parent 331bd2d commit ec85797

File tree

2 files changed

+28
-20
lines changed

2 files changed

+28
-20
lines changed

cmd/limactl/prune.go

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -114,20 +114,24 @@ func knownLocations(ctx context.Context) (map[string]limatype.File, error) {
114114

115115
func locationsFromLimaYAML(y *limatype.LimaYAML) map[string]limatype.File {
116116
locations := make(map[string]limatype.File)
117-
for _, f := range y.Images {
118-
locations[downloader.CacheKey(f.Location)] = f.File
119-
if f.Kernel != nil {
120-
locations[downloader.CacheKey(f.Kernel.Location)] = f.Kernel.File
117+
// decompress=false and decompress=true are cached separately
118+
// because the same URL may be used for both compressed and uncompressed files.
119+
for _, decompress := range []bool{false, true} {
120+
for _, f := range y.Images {
121+
locations[downloader.CacheKey(f.Location, decompress)] = f.File
122+
if f.Kernel != nil {
123+
locations[downloader.CacheKey(f.Kernel.Location, decompress)] = f.Kernel.File
124+
}
125+
if f.Initrd != nil {
126+
locations[downloader.CacheKey(f.Initrd.Location, decompress)] = *f.Initrd
127+
}
121128
}
122-
if f.Initrd != nil {
123-
locations[downloader.CacheKey(f.Initrd.Location)] = *f.Initrd
129+
for _, f := range y.Containerd.Archives {
130+
locations[downloader.CacheKey(f.Location, decompress)] = f
131+
}
132+
for _, f := range y.Firmware.Images {
133+
locations[downloader.CacheKey(f.Location, decompress)] = f.File
124134
}
125-
}
126-
for _, f := range y.Containerd.Archives {
127-
locations[downloader.CacheKey(f.Location)] = f
128-
}
129-
for _, f := range y.Firmware.Images {
130-
locations[downloader.CacheKey(f.Location)] = f.File
131135
}
132136
return locations
133137
}

pkg/downloader/downloader.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ func Download(ctx context.Context, local, remote string, opts ...Opt) (*Result,
230230
return res, nil
231231
}
232232

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

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

357-
shad := cacheDirectoryPath(o.cacheDir, remote)
357+
shad := cacheDirectoryPath(o.cacheDir, remote, o.decompress)
358358
shadData := filepath.Join(shad, "data")
359359
shadTime := filepath.Join(shad, "time")
360360
shadType := filepath.Join(shad, "type")
@@ -404,8 +404,8 @@ func Cached(remote string, opts ...Opt) (*Result, error) {
404404
// - "data" file contains the data
405405
// - "time" file contains the time (Last-Modified header)
406406
// - "type" file contains the type (Content-Type header)
407-
func cacheDirectoryPath(cacheDir, remote string) string {
408-
return filepath.Join(cacheDir, "download", "by-url-sha256", CacheKey(remote))
407+
func cacheDirectoryPath(cacheDir, remote string, decompress bool) string {
408+
return filepath.Join(cacheDir, "download", "by-url-sha256", CacheKey(remote, decompress))
409409
}
410410

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

778778
// CacheKey returns the key for a cache entry of the remote URL.
779-
func CacheKey(remote string) string {
780-
return fmt.Sprintf("%x", sha256.Sum256([]byte(remote)))
779+
func CacheKey(remote string, decompress bool) string {
780+
k := fmt.Sprintf("%x", sha256.Sum256([]byte(remote)))
781+
if decompress {
782+
k += "+decomp"
783+
}
784+
return k
781785
}
782786

783787
// RemoveAllCacheDir removes the cache directory.

0 commit comments

Comments
 (0)