libeech

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

commit ebf503552dc21e5192be8f126dc9f65ae8f2529c
parent af359fd15ba51750850011b0fd8525d0fe08f45c
Author: z3bra <contactatz3bradotorg>
Date:   Mon Oct 23 14:10:41 +0200

Add function to save file list

Diffstat:
libeech.c | 43+++++++++++++++++++++++++++++++++++++++++++
libeech.h | 7+++++++
torrent.c | 4++++
3 files changed, 54 insertions(+), 0 deletions(-)
diff --git a/libeech.c b/libeech.c @@ -14,6 +14,7 @@ /* helpers to retrieve attributes from metadata */ static char * peerid(); static long torrentsize(struct torrent *); +static long torrentfiles(struct torrent *); static int chktorrent(struct be *); static char * @@ -56,6 +57,47 @@ torrentsize(struct torrent *t) return sz; } +static long +torrentfiles(struct torrent *t) +{ + int i; + long n = 0; + char basedir[PATH_MAX], *sp; + struct be file, tmp; + + bekstr(&t->info, "name", 4, &tmp); + memset(basedir, 0, PATH_MAX); + memcpy(basedir, tmp.off, tmp.end - tmp.off); + + /* multi-file torrent */ + if (!bekv(&t->info, "files", 5, &file)) { + t->files = NULL; + belist(&file, (unsigned long *)&n); + t->files = malloc(sizeof(*t->files) * n); + if (!t->files) + return -1; + + for (i = 0; !belistnext(&file) && !belistover(&file); i++) { + /* save file length */ + t->files[i].sz = bekint(&file, "length", 6); + + /* save file path */ + sp = t->files[i].path; + memcpy(t->files[i].path, basedir, PATH_MAX); + bekv(&file, "path", 4, &tmp); + bepath(&tmp, &sp, PATH_MAX); + } + n = i; + /* single-file torrent */ + } else { + n = 1; + t->files = malloc(sizeof(*t->files)); + t->files[0].sz = bekint(&t->info, "length", 6); + memcpy(t->files[0].path, basedir, PATH_MAX); + } + + return n; +} static int chktorrent(struct be *b) { @@ -130,6 +172,7 @@ glch_loadtorrent(struct torrent *t, char *b, size_t s) t->ul = 0; t->dl = 0; t->sz = torrentsize(t); + t->nfile = torrentfiles(t); return 0; } diff --git a/libeech.h b/libeech.h @@ -22,12 +22,19 @@ struct peer { int state; }; +struct file { + size_t sz; + char path[PATH_MAX]; +}; + struct torrent { char id[21]; char ih[20]; char tr[PATH_MAX]; struct be be; struct be info; + struct file *files; + long nfile; long sz; long dl; long ul; diff --git a/torrent.c b/torrent.c @@ -38,6 +38,7 @@ readfile(char *f, char **b) int main(int argc, char *argv[]) { + int i; long s; char *b; char hex[41]; @@ -58,6 +59,9 @@ main(int argc, char *argv[]) printf("Peer ID: %s\n", t.id); printf("Tracker: %s\n", t.tr); printf("Length: %ld bytes\n", t.sz); + printf("Files:\n"); + for (i=0; i<t.nfile; i++) + printf("\t%s\n", t.files[i].path); return 0; }