Plan 9 from Bell Labs’s /usr/web/sources/contrib/mjl/wip/deluge/deluge.c

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


#include "deluge.h"

int mainstacksize = STACKSIZE;
Torrent *torrent;

int list;
int debug = 1;
int verify;
int verbose = 1;


void
usage(void)
{
	fprint(2, "usage: deluge [-d level] [-v] [-l | -V] torrentfile\n");
	threadexitsall("usage");
}

void
threadmain(int argc, char *argv[])
{
	char *errmsg;
	Biobuf *bp;
	char *file;
	Bee torb;
	Torrent t;
	File *f;
	int i;
	vlong fstored;

	ARGBEGIN{
	case 'd':
		debug = atoi(EARGF(usage()));
		break;
	case 'l':
		list = 1;
		break;
	case 'v':
		verbose = 1;
		break;
	case 'V':
		verify = 1;
		break;
	default:
		usage();
	}ARGEND;

	if(argc != 1 || list && verify)
		usage();

	fmtinstall('H', Hfmt);
	fmtinstall('L', Lfmt);
	fmtinstall('M', Mfmt);
	fmtinstall('B', Bfmt);
	fmtinstall('P', Pfmt);
	fmtinstall('C', Cfmt);
	fmtinstall('I', Ifmt);
	fmtinstall('F', Ffmt);
	fmtinstall('R', Rfmt);
	fmtinstall('T', Tfmt);

	file = argv[0];
	bp = Bopen(file, OREAD);
	if(bp == nil)
		sysfatal("open %s: %r", file);
	errmsg = beeunpickle(bp, &torb);
	if(errmsg)
		sysfatal("parsing %s: %s", file, errmsg);
	if(debug)
		beeprint(2, 0, &torb);
	Bterm(bp);

	if(verify){
		torrentinit(&t, &torb, 1, 0);

		print("verifying %L in %d pieces...\n", t.length, t.npieces);
		torrentverify(&t);

		print("total number of bytes: %lld\n", t.length);
		print("total number of verified bytes: %lld\n", t.stored);
		print("verified %L out of %L (%3d%%), %d/%d pieces\n", t.stored, t.length, (int)(100.0*t.stored/t.length), bitnhave(t.haves), t.npieces);
		print("total of %d files:\n", filelen(t.files));
		for(f = t.files; f; f = f->next){
			fstored = 0;
			for(i = f->offset/t.piecelen; i < (f->offset+f->length+t.piecelen-1)/t.piecelen; i++)
				if(bitget(t.haves, i))
					fstored += filebytesinpiece(&t, f, &t.pieces[i]);
			print("\t%3d%%\t%s  (%L/%L)\n", (int)(100.0*fstored/f->length), f->path, fstored, f->length);
		}
		threadexitsall(0);
	}
	if(list){
		torrentinit(&t, &torb, 0, 0);

		print("infohash: %H\n", t.infohash);
		print("total length: %L (%lld bytes)\n", t.length, t.length);
		print("piece length: %L (%lud bytes)\n", (vlong)t.piecelen, t.piecelen);
		print("number of pieces: %d\n", t.npieces);
		print("announce url: %s\n", t.announce);
		print("files\n");
		for(f = t.files; f; f = f->next)
			print("\t%6L\t%s (%lld bytes)\n", f->length, f->path, f->length);
		threadexitsall(0);
	}

	torrentinit(&t, &torb, 1, 1);
	if(!t.filecreated)
		torrentverify(&t);
	torrentprep(&t);
	torrentdo(&t);

	threadexitsall(0);
}

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.