Skip to content

Commit

Permalink
Merge #267: New binary to parse torrent files (only for debugging pur…
Browse files Browse the repository at this point in the history
…poses)

d9cdd65 feat: new binary to par torrent files (Jose Celano)

Pull request description:

  I'm trying to fix bug #266

  When I try to upload this torrent:

  https://academictorrents.com/details/3cd18ff2d3eec881207dcc5ca5a2c3a2a3afe462

  I get this error trying to parse it:

  ```
  InvalidType("Invalid Type: sequence (expected: `field identifier`)")
  ```

  I've tried to reproduce the error with a clean project:

  https://github.com/torrust/torrust-parse-torrent

  But It works.

  I've added a binary to reproduce the error in this project. You can run it with:

  ```
  cargo run --bin parse_torrent ./tests/fixtures/torrents/MC_GRID.zip-3cd18ff2d3eec881207dcc5ca5a2c3a2a3afe462.torrent
  ```

  Output:

  ```s
  Reading the torrent file ...
  Decoding torrent with standard serde implementation ...
  InvalidType("Invalid Type: sequence (expected: `field identifier`)")
  Error: Custom { kind: Other, error: "Error: invalid torrent!. Invalid Type: sequence (expected: `field identifier`)" }
  ```

  I need more ideas about how to solve this bug. I've checked the dependencies, and It does not work even if I use the same "serde" dependencies in this project.

  The torrent only has two different things:

  - The "pieces" field is big.
  - It has an extra field from BEP 19.

  I only have two more things to test:

  - ~~Create another torrent with a big "pieces" field. Only to check if the size is the problem~~. DONE.
  - Clone the "serve" crate and try to debug the code. I think the error is thrown [here](https://github.com/serde-rs/serde/blob/dad15b9fd0bef97b7a7c90a8a165b6ffbc682cae/serde/src/de/mod.rs#L1646). It seems the deserializer is expecting a file identifier (I guess for a Dictionary) and is getting a sequence.

Top commit has no ACKs.

Tree-SHA512: c9f02021801d492e34b7550662cfb6747bbf81f3724c3afdd3d92e22061066c400b721f35e80100d3253ff79fbda10d0bf0f7ea29df964f57ee97a1607b50fd3
  • Loading branch information
josecelano committed Sep 11, 2023
2 parents 9ba65cc + d9cdd65 commit 08f0b86
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
35 changes: 35 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug executable 'parse_torrent'",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/target/debug/parse_torrent",
"args": ["./tests/fixtures/torrents/MC_GRID.zip-3cd18ff2d3eec881207dcc5ca5a2c3a2a3afe462.torrent"],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "cargo build",
"miDebuggerPath": "/usr/bin/gdb",
"linux": {
"miDebuggerPath": "/usr/bin/gdb"
},
"windows": {
"miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe"
},
"osx": {
"miDebuggerPath": "/usr/local/bin/gdb"
}
}
]
}
41 changes: 41 additions & 0 deletions src/bin/parse_torrent.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//! Command line tool to parse a torrent file and print the decoded torrent.
//!
//! It's only used for debugging purposes.
use std::env;
use std::fs::File;
use std::io::{self, Read};

use serde_bencode::de::from_bytes;
use serde_bencode::value::Value as BValue;
use torrust_index_backend::utils::parse_torrent;

fn main() -> io::Result<()> {
let args: Vec<String> = env::args().collect();
if args.len() != 2 {
eprintln!("Usage: cargo run --bin parse_torrent <PATH_TO_TORRENT_FILE>");
eprintln!("Example: cargo run --bin parse_torrent ./tests/fixtures/torrents/MC_GRID.zip-3cd18ff2d3eec881207dcc5ca5a2c3a2a3afe462.torrent");
std::process::exit(1);
}

println!("Reading the torrent file ...");

let mut file = File::open(&args[1])?;
let mut bytes = Vec::new();
file.read_to_end(&mut bytes)?;

println!("Decoding torrent with standard serde implementation ...");

match from_bytes::<BValue>(&bytes) {
Ok(_value) => match parse_torrent::decode_torrent(&bytes) {
Ok(torrent) => {
println!("Parsed torrent: \n{torrent:#?}");
Ok(())
}
Err(e) => Err(io::Error::new(io::ErrorKind::Other, format!("Error: invalid torrent!. {e}"))),
},
Err(e) => Err(io::Error::new(
io::ErrorKind::Other,
format!("Error: invalid bencode data!. {e}"),
)),
}
}
Binary file not shown.

0 comments on commit 08f0b86

Please sign in to comment.