|
| 1 | +torrentcheck - catalog a .torrent file and optionally verify content hashes |
| 2 | +Usage: torrentcheck -t torrent-file [-p content-path] [-n] [-h] [-c] [-d] |
| 3 | +Options: -n suppresses progress count, -h shows all hash values, |
| 4 | + -c or -d uses comma or dot formatted byte counts. |
| 5 | +Returns 0 if successful, nonzero return code if errors found. |
| 6 | + |
| 7 | +Option: -sha1 [optional hash] acts as a simple SHA1 filter. |
| 8 | +If -sha1 is followed by a hex hash, the return code will be zero |
| 9 | +on match and nonzero otherwise. |
| 10 | + |
| 11 | +Summary |
| 12 | +------- |
| 13 | +This program is a command-line utility to catalog and verify torrent files. |
| 14 | +Run with only the -t option, it displays the metadata, name, and size of |
| 15 | +each file in the torrent. Run with the -t and -p options, it computes the |
| 16 | +hashes of all files in the torrent, compares them against the hashes stored |
| 17 | +in the metadata, and warns of any errors. |
| 18 | + |
| 19 | +If torrentcheck returns "torrent is good" at the end of its output, every |
| 20 | +byte of every file in the torrent is present and correct, to a high degree of |
| 21 | +certainty as explained below. |
| 22 | + |
| 23 | +For example, if you run torrents on a fast external server and then download |
| 24 | +the files, this utility will verify that the files you received are complete |
| 25 | +and uncorrupted. It can also be used to verify backups or to automatically |
| 26 | +check a series of torrents using scripting. |
| 27 | + |
| 28 | +The -t parameter should be the path to the .torrent metadata file. The -p path |
| 29 | +should point to the file or files. It can include or leave out the torrent name. |
| 30 | +The -n option suppresses the running count, which is useful if you are writing |
| 31 | +the output to a file. The -h option shows all piece hash values. The -c or -d |
| 32 | +options produce comma or dot formatted byte counts for readability. |
| 33 | + |
| 34 | +The -sha1 option disables torrent checking, and instead acts as a SHA1 filter. |
| 35 | +Most Windows machines do not have a SHA1 utility, so I included this mode as a |
| 36 | +convenience feature. It reads in binary data from standard input until end of |
| 37 | +file, and prints the SHA1 hash. If a SHA1 hash is provided on the command line, |
| 38 | +it will return 0 if the hashes match or nonzero if they do not. This mode |
| 39 | +should agree with the output of "openssl dgst -sha1" or "digest -a sha1" |
| 40 | + |
| 41 | +Examples |
| 42 | +-------- |
| 43 | +torrentcheck -t \torrents\ubuntu-10.10-desktop-i386.iso.torrent |
| 44 | +torrentcheck -t \torrents\ubuntu-10.10-desktop-i386.iso.torrent -p \download |
| 45 | +torrentcheck -t \torrents\ubuntu-10.10-desktop-i386.iso.torrent -p \download && echo good |
| 46 | +torrentcheck -t \torrents\ubuntu-10.10-desktop-i386.iso.torrent -p \download || echo bad |
| 47 | +torrentcheck -t \torrents\ubuntu-10.10-desktop-i386.iso.torrent -p \download\ubuntu-10.10-desktop-i386.iso |
| 48 | +torrentcheck -sha1 < \download\ubuntu-10.10-desktop-i386.iso |
| 49 | +torrentcheck -sha1 b28bbd742aff85d21b9ad96bb45b67c2d133be99 < \download\ubuntu-10.10-desktop-i386.iso && echo good |
| 50 | +(These are for Windows; use forward slashes in Unix/Linux) |
| 51 | + |
| 52 | +Automation and scripting |
| 53 | +------------------------ |
| 54 | +Torrentcheck returns 0 in the Unix $? return code or Windows errorlevel |
| 55 | +if it successfully verifies a torrent, or nonzero return codes if it fails. |
| 56 | + |
| 57 | +If you have your torrents in \torrents and the downloaded files in \share, |
| 58 | +make a "bad" directory under \torrents, cd to \torrents, and run: |
| 59 | + |
| 60 | +(Windows) |
| 61 | +for %i in (*.torrent) do torrentcheck -t "%i" -p \share || move "%i" bad |
| 62 | +(Linux) |
| 63 | +for i in *.torrent; do torrentcheck -t "$i" -p /share || mv "$i" bad ; done |
| 64 | + |
| 65 | +This will check all the torrents, and move any that are not fully |
| 66 | +downloaded and correct into \torrents\bad. |
| 67 | + |
| 68 | +Run this command to generate a master list file with the contents of all your |
| 69 | +torrents. This file can be searched to find a particular file and which torrent |
| 70 | +it comes from. |
| 71 | + |
| 72 | +(Windows) |
| 73 | +for %i in (*.torrent) do torrentcheck -t "%i" >> masterlist.txt & echo. >> masterlist.txt |
| 74 | +(Linux) |
| 75 | +for i in *.torrent; do torrentcheck -t "$i" >> masterlist.txt ; echo >> masterlist.txt ; done |
| 76 | + |
| 77 | +Detailed description |
| 78 | +-------------------- |
| 79 | +BitTorrent is a file sharing system which uses a metadata file, usually with |
| 80 | +the .torrent extension, to identify a data file or group of files. Given the |
| 81 | +metadata file, a BitTorrent client can download and share the data files. |
| 82 | +It can also verify the integrity of the files. |
| 83 | + |
| 84 | +The metadata file uses an encoding scheme called "bencode" which can store |
| 85 | +integers, strings, lists, and key-value pairs. It can represent binary values |
| 86 | +without any escaping, so a bencoded string can be loaded into memory and parsed |
| 87 | +in place, without any decoding. Torrent metadata contains the names and sizes |
| 88 | +of all the files in the torrent, and also contains a series of SHA1 hashes on |
| 89 | +each piece of the data file or files. The piece size is specified in the |
| 90 | +metadata, ranging from 32KiB (32768) to 4MiB (4194304) in a sample of torrents. |
| 91 | + |
| 92 | +SHA1 is a complex error-checking code designed by the National Security Agency |
| 93 | +for the military Defense Messaging System. It inputs an arbitrarily long byte |
| 94 | +string and outputs a 20-byte check code. If any bit in the input changes, the |
| 95 | +check code will change. SHA1 is complex enough so that even by deliberate |
| 96 | +effort it is very difficult to find two strings with the same check code. The |
| 97 | +chance of this happening by accident is small enough to ignore. |
| 98 | + |
| 99 | +To check a single-file torrent, allocate a buffer equal to the "piece size" |
| 100 | +string in the metadata, open the input file identified by the "name" string |
| 101 | +or specified on the command line, and read in pieces one at a time. The last |
| 102 | +piece will likely be short; keep track of the number of bytes actually read. |
| 103 | +Hash each piece, and compare the hash code against the corresponding hash code |
| 104 | +in the metadata. Any mismatch is an error. |
| 105 | + |
| 106 | +To check a multiple-file torrent, allocate a buffer as above. Read files in |
| 107 | +order from the "files" list in the metadata and reconstruct the paths, where |
| 108 | +the "name" string may be the base directory. Read from each file in sequence |
| 109 | +into the buffer until the buffer is full or the last file has been read, then |
| 110 | +hash it and check against the list in the metadata. Any mismatch is an error. |
| 111 | + |
| 112 | +Hash pieces span multiple files, so a missing or corrupt file can cause the |
| 113 | +previous or next file to fail as well. In particular, a missing file usually |
| 114 | +causes the previous and next files to fail verification. This is an artifact of |
| 115 | +the torrent format, and there is no way to avoid it. Torrents often contain a |
| 116 | +large media file and a small descriptive text file. If the text file is |
| 117 | +missing, the media file usually cannot be verified. |
| 118 | + |
| 119 | +Torrentcheck also verifies the length of each file, and flags an error if the |
| 120 | +length is wrong even if the hash codes match. It is designed to handle files |
| 121 | +over 4GB on a 32-bit machine. |
| 122 | + |
| 123 | +The SHA1 implementation used by torrentcheck was written by David Ireland, |
| 124 | +AM Kuchling, and Peter Gutmann. The source code does not contain a copyright |
| 125 | +notice, and this file is widely used on the Internet. |
| 126 | + |
| 127 | +Compiling |
| 128 | +--------- |
| 129 | +There is no makefile. The required gcc lines are at the top of the |
| 130 | +torrentcheck.c source file. The major catch in compiling is making 64-bit file |
| 131 | +I/O work. It is tested on Windows, Linux, and Solaris, but you may have to |
| 132 | +experiment with compiler options to get 64-bit ftell and fseek working. |
0 commit comments