summaryrefslogtreecommitdiff
path: root/btcheck.c
diff options
context:
space:
mode:
Diffstat (limited to 'btcheck.c')
-rw-r--r--btcheck.c127
1 files changed, 127 insertions, 0 deletions
diff --git a/btcheck.c b/btcheck.c
new file mode 100644
index 0000000..de62d72
--- /dev/null
+++ b/btcheck.c
@@ -0,0 +1,127 @@
+/* $Id$ */
+/*
+ * Copyright (c) 2005 Dimitri Sokolyuk <demon@vhost.dyndns.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <sys/queue.h>
+
+#include <err.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "meta.h"
+#include "files.h"
+#include "tools.h"
+#include "version.h"
+
+#ifndef lint
+static const char rcsid[] = "@(#)$Id$";
+#endif /* not lint */
+
+const char usagearg[] = "[-v] torrent [torrent ...]";
+
+void progress(struct btmeta *, int);
+void done(struct btmeta *);
+void printbf(char *, int);
+
+void
+printbf(char *bf, int sz)
+{
+ int i;
+
+ for (i = 0; i < sz * NBBY; i++) {
+ printf("%c", "01"[!!btisset(bf, i)]);
+ if ((i + 1) % 8 == 0)
+ printf("%c", "\n "[!!((i + 1) % 0x40)]);
+ }
+ printf("\n");
+}
+
+int
+main(int argc, char **argv)
+{
+ extern char *__progname;
+ struct btmeta *mp;
+ struct progresscb cb;
+ int ch, vflag;
+
+ vflag = 0;
+ while ((ch = getopt(argc, argv, "v")) != -1) {
+ switch (ch) {
+ case 'v':
+ vflag = 1;
+ break;
+ case '?':
+ default:
+ usage(usagearg);
+ /* NOTREACHED */
+ }
+ }
+
+ argc -= optind;
+ argv += optind;
+
+ if (argc < 1)
+ usage(usagearg);
+
+ printf("%s %d.%d - check BitTorrent files\n",
+ __progname, VERSION_MAJOR, VERSION_MINOR);
+
+ for (; *argv != NULL; ++argv) {
+ if ((mp = btreadmeta(*argv)) == NULL) {
+ warnx("cannot read %s\n", *argv);
+ continue;
+ }
+
+ if (btmkhier(mp) == -1)
+ return -1;
+ printf("\nfile: %s\n", mp->fname);
+ printf("size: %s\n", metric((double) mp->size));
+#if 0
+ btchkfiles(mp, progress);
+ done(mp);
+#else
+ cb.progress = progress;
+ cb.done = done;
+ btchkfiles(mp, &cb);
+#endif
+ if (vflag)
+ printbf(mp->bitfield, mp->bitlen);
+
+ btfreemeta(mp);
+ }
+ return 0;
+}
+
+void
+progress(struct btmeta *mp, int n)
+{
+ if (n % 10 == 0) {
+ printf("\rchecking %d of %d (%d ok)",
+ n, mp->npieces, mp->good);
+ fflush(stdout);
+ }
+}
+
+void
+done(struct btmeta *mp)
+{
+ printf("\rtotal good pieces %d/%d (%d%%)\n",
+ mp->good, mp->npieces, mp->good * 100 / mp->npieces);
+
+ printf("left: %s\n", metric((double) mp->left));
+}