git: 9front

Download patch

ref: 68b1622f202e3b435d0012ed95e19af3093bf2ab
parent: f2547cdfc54a85104bff5fdb6b692dd5fa0668f6
author: cinap_lenrek <cinap_lenrek@felloff.net>
date: Sun Apr 28 12:37:29 EDT 2024

ip/torrent: fix webseed

Commit 5d1ba1a79297c47c9852e91c54d3ea8db5cff685 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);
--