Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

If torrent contain multiple files, how to know what piece correspond to each file?

Tags:

bittorrent

I'm building a BitTorrent client application in Java and I have 2 small question:

  1. Can torrent contain folders? recursively?
  2. If a torrent contains n files (not directories - for simplicity), do I need to create n files with their corresponding size? When I receive a piece from a peer, how do I know to which file it (the piece) belong?

For example, here is a torrent which contains 2 files:

TorrentInfo{Created By: ruTorrent (PHP Class - Adrien Gibrat)
Main tracker: http://tracker.hebits.net:35777/tracker.php?do=announce&passkey=5d3ab309eda55c1e7183975099958ab2
Comment: null
Info_hash: c504216ca4a113d26f023a10a1249ca3a6217997
Name: Veronica.2017.1080p.BluRay.DTS-HD.MA.5.1.x264-HDH
Piece Length: 16777216
Pieces: 787
Total Size: null
Is Single File Torrent: false
File List:
TorrentFile{fileLength=13202048630, fileDirs=[Veronica.2017.1080p.BluRay.DTS-HD.MA.5.1.x264-HDH.mkv]}
TorrentFile{fileLength=62543, fileDirs=[Veronica.2017.1080p.BluRay.DTS-HD.MA.5.1.x264-HDH.srt]}

The docs doesn't say much: https://wiki.theory.org/index.php/BitTorrentSpecification

like image 367
Stav Alfi Avatar asked Dec 24 '22 08:12

Stav Alfi


1 Answers

what you are doing is similar to mine...

The following bold fonts are important to your questions.

1.yes; no

Info in Multiple File Mode

name: the name of the directory in which to store all the files. This is purely advisory. (string)

path: a list containing one or more string elements that together represent the path and filename. Each element in the list corresponds to either a directory name or (in the case of the final element) the filename. For example, a the file "dir1/dir2/file.ext" would consist of three string elements: "dir1", "dir2", and "file.ext". This is encoded as a bencoded list of strings such as l4:*dir*14:*dir*28:file.exte

Info in Single File Mode

name: the filename. This is purely advisory. (string)

Filename includes floder name.

2.maybe;

Whether you need to create n files with their corresponding size depend on whether you need to download n files.

Peer wire protocol (TCP)

piece:

The piece message is variable length, where X is the length of the block. The payload contains the following information:

index: integer specifying the zero-based piece index

begin: integer specifying the zero-based byte offset within the piece

block: block of data, which is a subset of the piece specified by index.

For the purposes of piece boundaries in the multi-file case, consider the file data as one long continuous stream, composed of the concatenation of each file in the order listed in the files list. The number of pieces and their boundaries are then determined in the same manner as the case of a single file. Pieces may overlap file boundaries.

I am sorry for my english, because I am not native speaker...

like image 62
user9525821 Avatar answered May 16 '23 07:05

user9525821