URI:
       tWrite pieces received to a metafile - libeech - bittorrent library
  HTML git clone git://z3bra.org/libeech.git
   DIR Log
   DIR Files
   DIR Refs
   DIR README
   DIR LICENSE
       ---
   DIR commit 00a21c7271abed9eb6faa3d028e706f04c5f5128
   DIR parent 7534ab0fb8db1833a70ac699db471235974a5e23
  HTML Author: z3bra <willy at mailoo dot org>
       Date:   Tue,  3 Jul 2018 15:37:20 +0200
       
       Write pieces received to a metafile
       
       Diffstat:
         M libeech.c                           |      46 +++++++++++++++++++++++++++----
       
       1 file changed, 40 insertions(+), 6 deletions(-)
       ---
   DIR diff --git a/libeech.c b/libeech.c
       t@@ -9,7 +9,11 @@
        #include <stdlib.h>
        #include <string.h>
        #include <time.h>
       +#include <unistd.h>
        
       +#include <sys/types.h>
       +#include <sys/stat.h>
       +#include <sys/mman.h>
        #include <sys/socket.h>
        #include <netinet/in.h>
        
       t@@ -52,6 +56,7 @@ static int chktorrent(struct be *);
        
        /* helpers to deal with pieces/blocks */
        static long readpiece(struct torrent *, struct piece *, long);
       +static long writepiece(struct torrent *, struct piece *);
        static int chkpiece(struct torrent *, struct piece *, long);
        
        /* manage a list of peers */
       t@@ -277,6 +282,39 @@ readpiece(struct torrent *t, struct piece *p, long n)
                return p->sz;
        }
        
       +static long
       +writepiece(struct torrent *t, struct piece *p)
       +{
       +        int fd;
       +        char *addr, hex[41];
       +        size_t off;
       +        struct stat sb;
       +
       +        off = p->n * t->psz;
       +
       +        tohex(t->ih, hex, 20);
       +        if ((fd = open(hex, O_RDWR|O_CREAT, 0644)) < 0) {
       +                perror(hex);
       +                return -1;
       +        }
       +
       +        if (!stat(hex, &sb) && (size_t)sb.st_size < off + p->sz)
       +                ftruncate(fd, off + p->sz);
       +
       +        addr = mmap(0, off + p->sz, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
       +        if (addr == MAP_FAILED) {
       +                perror("mmap");
       +                close(fd);
       +                return -1;
       +        }
       +
       +        memcpy(addr + off, p->blks, p->sz);
       +        munmap(addr, off + p->sz);
       +        close(fd);
       +
       +        return 0;
       +}
       +
        static int
        chkpiece(struct torrent *t, struct piece *p, long n)
        {
       t@@ -594,12 +632,8 @@ pwprxpc(struct torrent *t, struct peer *p, size_t sz, char *pl)
        
                memcpy(p->piece.blks + off, blk, sz - 8);
        
       -        if (!chkpiece(t, &p->piece, idx)) {
       -                printf("PIECE: hash OK, piece full\n");
       -                /* writepiece(t, p->piece, idx); */
       -        } else {
       -                printf("PIECE: has doesn't match (yet)\n");
       -        }
       +        if (!chkpiece(t, &p->piece, idx) && !writepiece(t, &(p->piece)))
       +                setbit(t->bf, p->piece.n);
        
                return 0;
        }