( ctx context.Context, query *Query, url string, resp *http.Response, )
| 320 | } |
| 321 | |
| 322 | func writeHTTPStateSnapshot( |
| 323 | ctx context.Context, |
| 324 | query *Query, |
| 325 | url string, |
| 326 | resp *http.Response, |
| 327 | ) (_ bkcache.ImmutableRef, _ digest.Digest, _ string, _ string, rerr error) { |
| 328 | bkref, err := query.SnapshotManager().New(ctx, nil, |
| 329 | bkcache.WithRecordType(bkclient.UsageRecordTypeRegular), |
| 330 | bkcache.WithDescription(fmt.Sprintf("http state %s", url)), |
| 331 | ) |
| 332 | if err != nil { |
| 333 | return nil, "", "", "", err |
| 334 | } |
| 335 | defer func() { |
| 336 | if rerr != nil && bkref != nil { |
| 337 | _ = bkref.Release(context.WithoutCancel(ctx)) |
| 338 | } |
| 339 | }() |
| 340 | |
| 341 | h := sha256.New() |
| 342 | lastModified := resp.Header.Get("Last-Modified") |
| 343 | err = MountRef(ctx, bkref, func(out string, _ *mount.Mount) error { |
| 344 | dest := filepath.Join(out, httpStateCanonicalPath) |
| 345 | f, err := os.OpenFile(dest, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.FileMode(httpStateCanonicalPermissions)) |
| 346 | if err != nil { |
| 347 | return err |
| 348 | } |
| 349 | if _, err := io.Copy(io.MultiWriter(f, h), resp.Body); err != nil { |
| 350 | _ = f.Close() |
| 351 | return err |
| 352 | } |
| 353 | if err := f.Close(); err != nil { |
| 354 | return err |
| 355 | } |
| 356 | |
| 357 | timestamp := time.Unix(0, 0) |
| 358 | if lastModified != "" { |
| 359 | if parsed, err := http.ParseTime(lastModified); err == nil { |
| 360 | timestamp = parsed |
| 361 | } |
| 362 | } |
| 363 | return os.Chtimes(dest, timestamp, timestamp) |
| 364 | }) |
| 365 | if err != nil { |
| 366 | return nil, "", "", "", fmt.Errorf("file write failed: %w", err) |
| 367 | } |
| 368 | |
| 369 | snap, err := bkref.Commit(ctx) |
| 370 | if err != nil { |
| 371 | return nil, "", "", "", err |
| 372 | } |
| 373 | bkref = nil |
| 374 | return snap, digest.NewDigest(digest.SHA256, h), lastModified, etagValue(resp.Header.Get("ETag")), nil |
| 375 | } |
| 376 | |
| 377 | func (state *HTTPState) fileResult( |
| 378 | ctx context.Context, |
no test coverage detected