MCPcopy Create free account
hub / github.com/dagger/dagger / writeHTTPStateSnapshot

Function writeHTTPStateSnapshot

core/http.go:322–375  ·  view source on GitHub ↗
(
	ctx context.Context,
	query *Query,
	url string,
	resp *http.Response,
)

Source from the content-addressed store, hash-verified

320}
321
322func 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
377func (state *HTTPState) fileResult(
378 ctx context.Context,

Callers 1

ResolveMethod · 0.85

Calls 11

OpenFileMethod · 0.80
MountRefFunction · 0.70
etagValueFunction · 0.70
NewMethod · 0.65
SnapshotManagerMethod · 0.65
ReleaseMethod · 0.65
GetMethod · 0.65
CloseMethod · 0.65
CommitMethod · 0.65
WithDescriptionMethod · 0.45
CopyMethod · 0.45

Tested by

no test coverage detected