ref: 217d2f751cae9fd59402bfc7c1f42989d6a2551e
parent: 86e3e0791c239bbaaece242cf78fa56794b58a72
author: cinap_lenrek <cinap_lenrek@felloff.net>
date: Sun Apr 28 12:37:29 EDT 2024
ip/torrent: fix webseed Commit 9f755671fb6f8250da9c16a9abebbf975b8bc571 broke webseeding with the last block. The haveiece() call at the end was because the inner is not calling havepiece() on the last block as it does not take the piece length into account. Now, instead, fix the inner loop, making the code more setright foward so we call havepiece() on the last block.
--- a/sys/src/cmd/ip/torrent.c
+++ b/sys/src/cmd/ip/torrent.c
@@ -751,7 +751,7 @@
void
webseed(Dict *w, File *f)
{
- int fd, err, n, m, o, p, x, y;
+ int fd, err, n, m, o, p, x;
uchar buf[MAXIO];
vlong off, len;
Dict *w0;
@@ -791,10 +791,10 @@
break;
x = off / blocksize;
+
p = off - (vlong)x*blocksize;
off += n;
len -= n;
- y = off / blocksize;
o = 0;
while(n > 0){
@@ -803,13 +803,17 @@
m = n;
if((havemap[x>>3] & (0x80>>(x&7))) == 0)
rwpiece(1, x, buf+o, m, p);
- if(x == y)
+
+ p += m;
+ if(p < pieces[x].len)
break;
+
+ p = 0;
o += m;
n -= m;
- p = 0;
if(havepiece(x++, w->str))
continue;
+
if(++err > 10){
close(fd);
werrstr("file corrupted");
@@ -817,8 +821,6 @@
}
}
}
- if(off < f->off + f->len)
- havepiece(off / blocksize, w->str);
havepiece(f->off / blocksize, w->str);
close(fd);
exits(0);