@@ -44,6 +44,7 @@ const (
44
44
type Result struct {
45
45
Status Status
46
46
CachePath string // "/Users/foo/Library/Caches/lima/download/by-url-sha256/<SHA256_OF_URL>/data"
47
+ LastModified string
47
48
ValidatedDigest bool
48
49
}
49
50
@@ -175,7 +176,7 @@ func Download(ctx context.Context, local, remote string, opts ...Opt) (*Result,
175
176
}
176
177
177
178
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 {
179
180
return nil , err
180
181
}
181
182
res := & Result {
@@ -187,6 +188,7 @@ func Download(ctx context.Context, local, remote string, opts ...Opt) (*Result,
187
188
188
189
shad := cacheDirectoryPath (o .cacheDir , remote )
189
190
shadData := filepath .Join (shad , "data" )
191
+ shadTime := filepath .Join (shad , "time" )
190
192
shadDigest , err := cacheDigestPath (shad , o .expectedDigest )
191
193
if err != nil {
192
194
return nil , err
@@ -210,6 +212,7 @@ func Download(ctx context.Context, local, remote string, opts ...Opt) (*Result,
210
212
res := & Result {
211
213
Status : StatusUsedCache ,
212
214
CachePath : shadData ,
215
+ LastModified : shadTime ,
213
216
ValidatedDigest : o .expectedDigest != "" ,
214
217
}
215
218
return res , nil
@@ -224,7 +227,7 @@ func Download(ctx context.Context, local, remote string, opts ...Opt) (*Result,
224
227
if err := os .WriteFile (shadURL , []byte (remote ), 0o644 ); err != nil {
225
228
return nil , err
226
229
}
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 {
228
231
return nil , err
229
232
}
230
233
// 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,
239
242
res := & Result {
240
243
Status : StatusDownloaded ,
241
244
CachePath : shadData ,
245
+ LastModified : shadTime ,
242
246
ValidatedDigest : o .expectedDigest != "" ,
243
247
}
244
248
return res , nil
@@ -266,6 +270,7 @@ func Cached(remote string, opts ...Opt) (*Result, error) {
266
270
267
271
shad := cacheDirectoryPath (o .cacheDir , remote )
268
272
shadData := filepath .Join (shad , "data" )
273
+ shadTime := filepath .Join (shad , "time" )
269
274
shadDigest , err := cacheDigestPath (shad , o .expectedDigest )
270
275
if err != nil {
271
276
return nil , err
@@ -285,6 +290,7 @@ func Cached(remote string, opts ...Opt) (*Result, error) {
285
290
res := & Result {
286
291
Status : StatusUsedCache ,
287
292
CachePath : shadData ,
293
+ LastModified : shadTime ,
288
294
ValidatedDigest : o .expectedDigest != "" ,
289
295
}
290
296
return res , nil
@@ -293,6 +299,7 @@ func Cached(remote string, opts ...Opt) (*Result, error) {
293
299
// cacheDirectoryPath returns the cache subdirectory path.
294
300
// - "url" file contains the url
295
301
// - "data" file contains the data
302
+ // - "time" file contains the time (Last-Modified header)
296
303
func cacheDirectoryPath (cacheDir , remote string ) string {
297
304
return filepath .Join (cacheDir , "download" , "by-url-sha256" , fmt .Sprintf ("%x" , sha256 .Sum256 ([]byte (remote ))))
298
305
}
@@ -470,7 +477,7 @@ func validateLocalFileDigest(localPath string, expectedDigest digest.Digest) err
470
477
return nil
471
478
}
472
479
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 {
474
481
if localPath == "" {
475
482
return fmt .Errorf ("downloadHTTP: got empty localPath" )
476
483
}
@@ -489,6 +496,12 @@ func downloadHTTP(ctx context.Context, localPath, url, description string, expec
489
496
if err != nil {
490
497
return err
491
498
}
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
+ }
492
505
defer resp .Body .Close ()
493
506
bar , err := progressbar .New (resp .ContentLength )
494
507
if err != nil {
0 commit comments