diff --git a/ffiwrapper/sealer_cgo.go b/ffiwrapper/sealer_cgo.go index 4894c14ed..3c01f65df 100644 --- a/ffiwrapper/sealer_cgo.go +++ b/ffiwrapper/sealer_cgo.go @@ -117,20 +117,30 @@ func (sb *Sealer) AddPiece(ctx context.Context, sector abi.SectorID, existingPie var pieceCids []abi.PieceInfo for { - n, err := pr.Read(buf[:]) - if err != nil && err != io.EOF { - return abi.PieceInfo{}, xerrors.Errorf("pr read error: %w", err) + var read int + for rbuf := buf; len(rbuf) > 0; { + n, err := pr.Read(rbuf) + if err != nil && err != io.EOF { + return abi.PieceInfo{}, xerrors.Errorf("pr read error: %w", err) + } + + rbuf = rbuf[n:] + read += n + + if err == io.EOF { + break + } } - if err == io.EOF { + if read == 0 { break } - c, err := sb.pieceCid(buf[:n]) + c, err := sb.pieceCid(buf[:read]) if err != nil { return abi.PieceInfo{}, xerrors.Errorf("pieceCid error: %w", err) } pieceCids = append(pieceCids, abi.PieceInfo{ - Size: abi.UnpaddedPieceSize(len(buf[:n])).Padded(), + Size: abi.UnpaddedPieceSize(len(buf[:read])).Padded(), PieceCID: c, }) } diff --git a/ffiwrapper/sealer_test.go b/ffiwrapper/sealer_test.go index c9b6e3af4..76ef8fea9 100644 --- a/ffiwrapper/sealer_test.go +++ b/ffiwrapper/sealer_test.go @@ -45,7 +45,10 @@ type seal struct { } func data(sn abi.SectorNumber, dlen abi.UnpaddedPieceSize) io.Reader { - return io.LimitReader(rand.New(rand.NewSource(42+int64(sn))), int64(dlen)) + return io.MultiReader( + io.LimitReader(rand.New(rand.NewSource(42+int64(sn))), int64(123)), + io.LimitReader(rand.New(rand.NewSource(42+int64(sn))), int64(dlen-123)), + ) } func (s *seal) precommit(t *testing.T, sb *Sealer, id abi.SectorID, done func()) {