Skip to content

Your First Torrent

Outcome: you hold the two forms a torrent takes — a .torrent file and a magnet link — you can say what the info hash is and where it comes from, and you have added a torrent to a client through the API. Evidence: transcript t04, and the magnet's failure in t10.

The map is not the territory

A .torrent file is a map: the file names and sizes, the piece size, and one SHA-1 fingerprint per piece. It is small — the lab's map for a 2 MiB file is 372 bytes. The info hash is the SHA-1 of the map's info section, and it is the torrent's name everywhere: in API calls, in the profile's BT_backup folder, between peers. When two clients agree on an info hash, they are talking about the same data, bit for bit.

Because the lab shares only files it made itself, it made its maps itself. qbittorrent-lab/make_torrent.py is thirty lines of honest plumbing: read a file, hash each 256 KiB piece, write the bencoded dictionary. Its output for the lab's fixture:

file: fixtures/field-notes.txt (2097152 bytes, 8 pieces of 262144)
torrent: fixtures/field-notes.torrent (372 bytes)
info hash: 53b631ea684e0f4ad2189245c7d294a99737919b

The fixture itself — field-notes.txt, 2 MiB of invented field notes, repeated paragraphs and padding — is made by make_fixture.py, and its SHA-256 is recorded in t04 so that chapter 7 can prove a downloaded copy is the same file and not merely the same size.

One deliberate choice in the lab's map: it names no tracker. Nothing in the file says where to meet. That is legal BitTorrent — trackers are a convenience, not a requirement — and it sets up the clean experiment of the next chapter.

(Making a torrent of your own file in daily life is normally one dialog in the desktop program — Tools → Torrent Creator. The lab used a script because a script can be read, checked and shipped in the lab folder; the maps it makes are ordinary torrents either way.)

Adding a torrent

Adding is an upload to the API:

curl -s -b cookies.txt \
  -F 'torrents=@fixtures/field-notes.torrent' \
  http://127.0.0.1:18088/api/v2/torrents/add

and this version answers in kind, naming what it took (t04):

{"added_torrent_ids":["53b631ea684e0f4ad2189245c7d294a99737919b"],
 "failure_count":0,"pending_count":0,"success_count":1}

The id in the reply is the info hash the lab computed before adding — the map and the client agree on the file's identity. A file that is not a torrent at all is refused out of hand (t10): HTTP 415, Error: 'junk.torrent' is not a valid torrent file.

A magnet link is the map reduced to its identity: magnet:?xt=urn:btih:<info hash> plus, usually, hints. It carries no piece hashes — the client must fetch the map itself, from the swarm, before it can fetch a single piece. t10 shows the honest consequence: a magnet for a swarm that does not exist is accepted politely and then sits in state metaDL forever, waiting for a map no one will ever bring. A magnet is a name without an address book; somebody in the swarm has to be home.