tRead piece from metafile (./<HASH>) instead of files - libeech - bittorrent library
(HTM) git clone git://z3bra.org/libeech.git
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
(DIR) LICENSE
---
(DIR) commit 4d2331780189dff51f9159dbaab0b972efd9b237
(DIR) parent a9cf1902aadd303a909d2f09d434b4d936b8cc90
(HTM) Author: z3bra <contactatz3bradotorg>
Date: Wed, 4 Jul 2018 08:52:10 +0200
Read piece from metafile (./<HASH>) instead of files
Diffstat:
M libeech.c | 37 ++++++++-----------------------
1 file changed, 9 insertions(+), 28 deletions(-)
---
(DIR) diff --git a/libeech.c b/libeech.c
t@@ -245,10 +245,9 @@ chktorrent(struct be *b)
static long
readpiece(struct torrent *t, struct piece *p, long n)
{
- int i;
- size_t off, fp;
- ssize_t sz;
- FILE *fh = NULL;
+ char hex[41];
+ size_t off;
+ FILE *f = NULL;
/* last piece might be truncated, so recalculate it*/
p->sz = (n == t->npiece - 1) ? t->sz % t->psz : t->psz;
t@@ -256,31 +255,13 @@ readpiece(struct torrent *t, struct piece *p, long n)
/* Calculate file offset for this piece */
off = t->psz * n;
- /* find file whose piece's offset belongs to */
- fp = i = 0;
- while (fp + t->files[i].sz < off && i < t->nfile)
- fp += t->files[i++].sz;
-
- sz = 0;
- while (sz < p->sz) {
- /* Keep up reading data from next files if needed*/
- if (off + sz >= t->files[i].sz) {
- i++;
- if (i >= t->nfile)
- return -1;
- fp += t->files[i].sz;
- }
-
- fh = fopen(t->files[i].path, "r");
- if (!fh)
- return -1;
-
- if (off + sz - fp)
- fseek(fh, off + sz - fp, SEEK_SET);
+ f = fopen(tohex(t->ih, hex, 20), "r");
+ if (!f)
+ return -1;
- sz += fread(p->blks, 1, p->sz - sz, fh);
- fclose(fh);
- }
+ fseek(f, off, SEEK_SET);
+ fread(p->blks, 1, p->sz, f);
+ fclose(f);
return p->sz;
}