tCheck piece only when all blocks are received - libeech - bittorrent library
 (HTM) git clone git://z3bra.org/libeech.git
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit 51e138421357f7f58a511bb0eb9656aa9ed1674c
 (DIR) parent 03ccf2b025a14a05633ab312cdad29745d5071db
 (HTM) Author: z3bra <contactatz3bradotorg>
       Date:   Mon,  9 Jul 2018 09:04:02 +0200
       
       Check piece only when all blocks are received
       
       Diffstat:
         M libeech.c                           |      17 ++++++++++++++---
       
       1 file changed, 14 insertions(+), 3 deletions(-)
       ---
 (DIR) diff --git a/libeech.c b/libeech.c
       t@@ -736,18 +736,29 @@ pwprxrq(struct torrent *t, struct peer *p, size_t sz, char *pl)
        static int
        pwprxpc(struct torrent *t, struct peer *p, size_t sz, char *pl)
        {
       +        int i;
                size_t idx, off;
       -        char *blk;
       +        char *blk, bl[BLKNUM / 8 + !!(BLKNUM % 8)];
                struct peer *b;
        
                idx = ntohl(U32(pl));
                off = ntohl(U32(pl+4));
                blk = pl + 8;
        
       +        /* calculate a full piece block bitfield, for comparison */
       +        memset(bl, 0, BLKNUM / 8 + !!(BLKNUM % 8));
       +        for (i = 0; i < (p->piece.sz / BLKSIZ + !!(p->piece.sz % BLKSIZ)); i++)
       +                setbit(bl, i);
       +
                memcpy(p->piece.blks + off, blk, sz - 8);
       +        setbit(p->piece.bl, off / BLKSIZ);
       +
       +        /* Check piece hash and write it on disk when all blocks are received */
       +        if (!memcmp(p->piece.bl, bl, BLKNUM / 8 + !!(BLKNUM % 8))) {
       +                if (!chkpiece(t, &p->piece, idx) && !writepiece(t, &(p->piece)))
       +                        setbit(t->bf, p->piece.n);
        
       -        if (!chkpiece(t, &p->piece, idx) && !writepiece(t, &(p->piece))) {
       -                setbit(t->bf, p->piece.n);
       +                /* Reset piece number, no matter what */
                        p->piece.n = -1;
                }