43 lines
1.1 KiB
Go
43 lines
1.1 KiB
Go
package osimage
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
)
|
|
|
|
func ApplyImageStreamed(ctx context.Context, opts ApplyOptions) (*ApplyResult, error) {
|
|
if err := ValidateApplyOptions(opts); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if err := CheckTargetSafe(opts.TargetPath, opts.ExpectedRawSize); err != nil {
|
|
return nil, fmt.Errorf("unsafe target %q: %w", opts.TargetPath, err)
|
|
}
|
|
|
|
src, closeFn, err := OpenDecompressedHTTPStream(ctx, opts.URL, opts.HTTPTimeout)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("open source stream: %w", err)
|
|
}
|
|
defer closeFn()
|
|
|
|
written, err := WriteStreamToTarget(ctx, src, opts.TargetPath, opts.ExpectedRawSize, opts.BufferSize, opts.Progress)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("write target: %w", err)
|
|
}
|
|
|
|
sum, err := VerifyTargetSHA256(ctx, opts.TargetPath, opts.ExpectedRawSize, opts.BufferSize, opts.Progress)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("verify target: %w", err)
|
|
}
|
|
|
|
if err := VerifySHA256(sum, opts.ExpectedRawSHA256); err != nil {
|
|
return nil, fmt.Errorf("final disk checksum mismatch: %w", err)
|
|
}
|
|
|
|
return &ApplyResult{
|
|
BytesWritten: written,
|
|
VerifiedSHA256: sum,
|
|
VerificationOK: true,
|
|
}, nil
|
|
}
|