libeech

BitTorrent library
git clone git://z3bra.org/libeech.git
Log | Files | Refs | README | LICENSE

commit e399823985f0dc7169562860c68296139e259d87
parent 87b942d302df90a66247babfad7dbaf08602d9ac
Author: z3bra <contactatz3bradotorg>
Date:   Fri Jun 29 22:59:57 +0200

Remove struct data for struct piece

`struct data` was meant to hold any kind of data, while holding its size.
It was used in the torrent struct, to hold and keep in memory all the pieces.

On startup, the pieces are all read and checked in the same struct,
to avoid keeping the whole torrent in memory.
This also removes the need for malloc() calls, which is always a plus!

Diffstat:
libeech.c | 26+++++++++++---------------
libeech.h | 9+++++----
2 files changed, 16 insertions(+), 19 deletions(-)
diff --git a/libeech.c b/libeech.c @@ -51,8 +51,8 @@ static long torrentfiles(struct torrent *); static int chktorrent(struct be *); /* helpers to deal with pieces/blocks */ -static long readpiece(struct torrent *, struct data *, long); -static int chkpiece(struct torrent *, struct data *, long); +static long readpiece(struct torrent *, struct piece *, long); +static int chkpiece(struct torrent *, struct piece *, long); /* manage a list of peers */ static struct peer * addpeer(struct peer *, char *, int); @@ -225,7 +225,7 @@ chktorrent(struct be *b) } static long -readpiece(struct torrent *t, struct data *p, long n) +readpiece(struct torrent *t, struct piece *p, long n) { int i; size_t off, fp, sz; @@ -234,10 +234,6 @@ readpiece(struct torrent *t, struct data *p, long n) /* last piece might be truncated, so recalculate it*/ p->sz = (n == t->npiece - 1) ? t->sz % t->psz : t->psz; - p->data = malloc(p->sz); - if (!p->data) - return -1; - /* Calculate file offset for this piece */ off = t->psz * n; @@ -257,15 +253,13 @@ readpiece(struct torrent *t, struct data *p, long n) } fh = fopen(t->files[i].path, "r"); - if (!fh) { - free(p->data); + if (!fh) return -1; - } if (off + sz - fp) fseek(fh, off + sz - fp, SEEK_SET); - sz += fread(p->data, 1, p->sz - sz, fh); + sz += fread(p->blks, 1, p->sz - sz, fh); fclose(fh); } @@ -273,10 +267,10 @@ readpiece(struct torrent *t, struct data *p, long n) } static int -chkpiece(struct torrent *t, struct data *p, long n) +chkpiece(struct torrent *t, struct piece *p, long n) { char hash[20]; - sha1(p->data, p->sz, hash); + sha1(p->blks, p->sz, hash); return memcmp(t->ph + n*20, hash, 20); } @@ -569,6 +563,7 @@ glch_loadtorrent(struct torrent *t, char *b, size_t s) { long i; struct be v; + struct piece piece; if (beinit(&t->be, b, s) < 0) return -1; @@ -600,13 +595,14 @@ glch_loadtorrent(struct torrent *t, char *b, size_t s) t->ph = malloc(t->npiece * 20); bekstr(&t->info, "pieces", 6, &v); memcpy(t->ph, v.off, v.end - v.off); - t->pieces = malloc(t->npiece * sizeof(*t->pieces)); t->bf = malloc(t->npiece / 8 + 1); memset(t->bf, 0, t->npiece / 8 + 1); for (i = 0; i < t->npiece; i++) { - if (readpiece(t, &t->pieces[i], i) > 0 && !chkpiece(t, &t->pieces[i], i)) + if (readpiece(t, &piece, i) > 0 && !chkpiece(t, &piece, i)) { + printf("PIECE %ld/%ld\n", i + 1, t->npiece); setbit(t->bf, i); + } } return 1; diff --git a/libeech.h b/libeech.h @@ -20,10 +20,11 @@ enum { ISINTERESTED = 1 << 8, }; -struct data { - long n; +struct piece { + int n; size_t sz; - char *data; + char blks[PCESIZ]; + char bf[PCESIZ / BLKSIZ]; }; struct peer { @@ -34,6 +35,7 @@ struct peer { char rxbuf[MSGSIZ]; long rxbufsz; char bf[PCENUM / 8]; + struct piece *piece; struct peer *next; }; @@ -52,7 +54,6 @@ struct torrent { struct be info; struct peer *peers; struct file *files; - struct data *pieces; long npiece; long nfile; long psz;