Skip to content

Commit 5524c02

Browse files
committed
Store time of Last-Modified in cache dir
Signed-off-by: Anders F Björklund <[email protected]>
1 parent 7295d3b commit 5524c02

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

pkg/downloader/downloader.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ const (
4444
type Result struct {
4545
Status Status
4646
CachePath string // "/Users/foo/Library/Caches/lima/download/by-url-sha256/<SHA256_OF_URL>/data"
47+
LastModified string
4748
ValidatedDigest bool
4849
}
4950

@@ -175,7 +176,7 @@ func Download(ctx context.Context, local, remote string, opts ...Opt) (*Result,
175176
}
176177

177178
if o.cacheDir == "" {
178-
if err := downloadHTTP(ctx, localPath, remote, o.description, o.expectedDigest); err != nil {
179+
if err := downloadHTTP(ctx, localPath, "", remote, o.description, o.expectedDigest); err != nil {
179180
return nil, err
180181
}
181182
res := &Result{
@@ -187,6 +188,7 @@ func Download(ctx context.Context, local, remote string, opts ...Opt) (*Result,
187188

188189
shad := cacheDirectoryPath(o.cacheDir, remote)
189190
shadData := filepath.Join(shad, "data")
191+
shadTime := filepath.Join(shad, "time")
190192
shadDigest, err := cacheDigestPath(shad, o.expectedDigest)
191193
if err != nil {
192194
return nil, err
@@ -210,6 +212,7 @@ func Download(ctx context.Context, local, remote string, opts ...Opt) (*Result,
210212
res := &Result{
211213
Status: StatusUsedCache,
212214
CachePath: shadData,
215+
LastModified: shadTime,
213216
ValidatedDigest: o.expectedDigest != "",
214217
}
215218
return res, nil
@@ -224,7 +227,7 @@ func Download(ctx context.Context, local, remote string, opts ...Opt) (*Result,
224227
if err := os.WriteFile(shadURL, []byte(remote), 0o644); err != nil {
225228
return nil, err
226229
}
227-
if err := downloadHTTP(ctx, shadData, remote, o.description, o.expectedDigest); err != nil {
230+
if err := downloadHTTP(ctx, shadData, shadTime, remote, o.description, o.expectedDigest); err != nil {
228231
return nil, err
229232
}
230233
// no need to pass the digest to copyLocal(), as we already verified the digest
@@ -239,6 +242,7 @@ func Download(ctx context.Context, local, remote string, opts ...Opt) (*Result,
239242
res := &Result{
240243
Status: StatusDownloaded,
241244
CachePath: shadData,
245+
LastModified: shadTime,
242246
ValidatedDigest: o.expectedDigest != "",
243247
}
244248
return res, nil
@@ -266,6 +270,7 @@ func Cached(remote string, opts ...Opt) (*Result, error) {
266270

267271
shad := cacheDirectoryPath(o.cacheDir, remote)
268272
shadData := filepath.Join(shad, "data")
273+
shadTime := filepath.Join(shad, "time")
269274
shadDigest, err := cacheDigestPath(shad, o.expectedDigest)
270275
if err != nil {
271276
return nil, err
@@ -285,6 +290,7 @@ func Cached(remote string, opts ...Opt) (*Result, error) {
285290
res := &Result{
286291
Status: StatusUsedCache,
287292
CachePath: shadData,
293+
LastModified: shadTime,
288294
ValidatedDigest: o.expectedDigest != "",
289295
}
290296
return res, nil
@@ -293,6 +299,7 @@ func Cached(remote string, opts ...Opt) (*Result, error) {
293299
// cacheDirectoryPath returns the cache subdirectory path.
294300
// - "url" file contains the url
295301
// - "data" file contains the data
302+
// - "time" file contains the time (Last-Modified header)
296303
func cacheDirectoryPath(cacheDir, remote string) string {
297304
return filepath.Join(cacheDir, "download", "by-url-sha256", fmt.Sprintf("%x", sha256.Sum256([]byte(remote))))
298305
}
@@ -470,7 +477,7 @@ func validateLocalFileDigest(localPath string, expectedDigest digest.Digest) err
470477
return nil
471478
}
472479

473-
func downloadHTTP(ctx context.Context, localPath, url, description string, expectedDigest digest.Digest) error {
480+
func downloadHTTP(ctx context.Context, localPath, lastModified, url, description string, expectedDigest digest.Digest) error {
474481
if localPath == "" {
475482
return fmt.Errorf("downloadHTTP: got empty localPath")
476483
}
@@ -489,6 +496,12 @@ func downloadHTTP(ctx context.Context, localPath, url, description string, expec
489496
if err != nil {
490497
return err
491498
}
499+
if lastModified != "" {
500+
lm := resp.Header.Get("Last-Modified")
501+
if err := os.WriteFile(lastModified, []byte(lm), 0o644); err != nil {
502+
return err
503+
}
504+
}
492505
defer resp.Body.Close()
493506
bar, err := progressbar.New(resp.ContentLength)
494507
if err != nil {

0 commit comments

Comments
 (0)