Joining a torrent with existing files

When a torrent is created, all the files that are part of the torrent are joined as if they were a single contiguous file.

Then the contiguous file is divided in pieces of fixed size.

In Pleasuredome, the larger torrents have pieces of 16 megabytes.

The last piece of the torrent is the only one that can be smaller than the piece size.

The BitTorrent client requests and sends entire pieces, rather than requesting and sending files.

For example, the client says "send me pieces #2469 and #2470", rather than "send me pacman.zip".

These pieces are hashed, so it's only possible to verify if an entire piece (it's not per file!) has the data it's supposed to have.

When rechecking a torrent, the BitTorrent client scans the file(s) associated to each piece to see if their data matches what the .torrent expects for that piece.

If all of the data matches, then the piece is flagged as "done"; if any of the data is missing, then the piece is flagged as "needs downloading".

So, if the piece doesn't match because of a single byte for example, the entire piece will have to be downloaded.

That's why it's usually necessary to download more than you already have after rechecking a torrent.

If the piece size was smaller, the total amount of data that would have to be downloaded would decrease.

Examples
Let's suppose a torrent consists of 3 pieces of 8 megabytes each, and the layout of its 7 files is according to the image below:




 * Scenario 1: I want the file #3, which is on pieces #1 and #2, which I don't have. Both pieces will have to be entirely downloaded to get the complete file #3.


 * Scenario 2: I have files #6 and #7, so I have the full piece #3. After rechecking, piece #2 will have to be entirely downloaded even though I have a part of it (because of the file #6).

Considering the piece size changed to 2 megabytes and that the files are the same, with the same layout:




 * Scenario 1: I have file #3, so I have the full piece #4. Even though I have part of the pieces #3 and #5, they are not complete and must be entirely downloaded after rechecking.


 * Scenario 2: I have files #6 and #7, so I have part of the piece #8 and the full pieces #9, #10, #11 and #12. The piece #8 will have to be entirely downloaded after rechecking.

The download to achieve the described objectives in both piece sizes is:

From the table it's possible to perceive that smaller piece sizes cause the download to be smaller when joining a torrent with existing files.

FAQ
Here are the Frequently Asked Questions:


 * Question: Could you use a smaller piece size on a torrent? I would have to download less this way, because I already have many of the files.
 * Answer: The torrent uploader decides the piece size. Larger pieces are preferred because they use less resources (RAM memory, processor and bandwidth).


 * Question: Could somebody create an application that generates a dat-file from a .torrent file?
 * Answer: It's impossible, because the .torrent file has hash values of pieces, whereas dat-files have hashes of entire files.


 * Question: What is the "unwanted" folder created by qBittorrent when downloading only some files of a torrent?
 * Answer: When only one file of a torrent is selected to download, qBittorrent has to download all the pieces that have the selected file so that it can verify the pieces' integrity with hash calculation. For example, the piece with the beginning of the selected file usually has the end of an unwanted file, and the piece with the end of the selected file usually has the beginning of an unwanted file. The end of one unwanted file and the beginning of another unwanted file have to be downloaded, and because these files are not entirely downloaded, they are moved to the "unwanted" folder as they are not usable ("corrupted"). If the "unwanted" folder is deleted, some pieces may have to be downloaded again when rechecking the torrent or selecting more files to download.