MCPcopy
hub / github.com/perkeep/perkeep / copyBlob

Method copyBlob

pkg/server/sync.go:626–674  ·  view source on GitHub ↗
(ctx context.Context, sb blob.SizedRef)

Source from the content-addressed store, hash-verified

624}
625
626func (sh *SyncHandler) copyBlob(ctx context.Context, sb blob.SizedRef) (err error) {
627 cs := sh.newCopyStatus(sb)
628 defer func() { cs.setError(err) }()
629 br := sb.Ref
630
631 sh.mu.Lock()
632 sh.copying[br] = cs
633 sh.mu.Unlock()
634
635 if sb.Size > constants.MaxBlobSize {
636 return fmt.Errorf("blob size %d too large; max blob size is %d", sb.Size, constants.MaxBlobSize)
637 }
638
639 cs.setStatus(statusFetching)
640 rc, fromSize, err := sh.from.Fetch(ctx, br)
641 if err != nil {
642 return fmt.Errorf("source fetch: %v", err)
643 }
644 if fromSize != sb.Size {
645 rc.Close()
646 return fmt.Errorf("source fetch size mismatch: get=%d, enumerate=%d", fromSize, sb.Size)
647 }
648
649 buf := make([]byte, fromSize)
650 hash := br.Hash()
651 cs.setStatus(statusReading)
652 n, err := io.ReadFull(io.TeeReader(rc,
653 io.MultiWriter(
654 incrWriter{cs, &cs.nread},
655 hash,
656 )), buf)
657 rc.Close()
658 if err != nil {
659 return fmt.Errorf("Read error after %d/%d bytes: %v", n, fromSize, err)
660 }
661 if !br.HashMatches(hash) {
662 return fmt.Errorf("Read data has unexpected digest %x", hash.Sum(nil))
663 }
664
665 cs.setStatus(statusWriting)
666 newsb, err := sh.to.ReceiveBlob(ctx, br, io.TeeReader(bytes.NewReader(buf), incrWriter{cs, &cs.nwrite}))
667 if err != nil {
668 return fmt.Errorf("dest write: %v", err)
669 }
670 if newsb.Size != sb.Size {
671 return fmt.Errorf("write size mismatch: source_read=%d but dest_write=%d", sb.Size, newsb.Size)
672 }
673 return nil
674}
675
676func (sh *SyncHandler) ReceiveBlob(ctx context.Context, br blob.Ref, r io.Reader) (sb blob.SizedRef, err error) {
677 // TODO: use ctx?

Callers 1

copyWorkerMethod · 0.95

Calls 11

newCopyStatusMethod · 0.95
setErrorMethod · 0.80
LockMethod · 0.80
UnlockMethod · 0.80
setStatusMethod · 0.80
SumMethod · 0.80
FetchMethod · 0.65
CloseMethod · 0.65
HashMethod · 0.65
ReceiveBlobMethod · 0.65
HashMatchesMethod · 0.45

Tested by

no test coverage detected