Skip to content

Commit

Permalink
Validate downloaded pieces
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanjermakov committed Oct 24, 2023
1 parent b7e41d4 commit dc9ffaf
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#[macro_use]
extern crate log;

use anyhow::{Context, Result};
use anyhow::{Context, Result, Error};
use futures::future::join_all;
use rand::{distributions::Alphanumeric, thread_rng, Rng};
use std::{collections::BTreeMap, fs, path::PathBuf, sync::Arc};
Expand Down Expand Up @@ -32,7 +32,7 @@ async fn main() -> Result<()> {
env_logger::Env::default().filter_or(env_logger::DEFAULT_FILTER_ENV, "info"),
);

let path = PathBuf::from("data/knoppix.torrent");
let path = PathBuf::from("data/academic_test.torrent");
let bencoded = fs::read(path).context("no metadata file")?;
let metainfo_dict = match parse_bencoded(bencoded) {
(Some(metadata), left) if left.is_empty() => metadata,
Expand Down Expand Up @@ -76,13 +76,26 @@ async fn main() -> Result<()> {
let handles = resp
.peers
.into_iter()
.take(30)
.map(|p| {
let state = state.clone();
handle_peer(p, state)
})
.collect::<Vec<_>>();
join_all(handles).await;
}

let state = state.lock().await;
if state.pieces.len() != state.metainfo.info.pieces.len() {
return Err(Error::msg("pieces length mismatch"))
}

if state.pieces.values().any(|p| !p.completed) {
return Err(Error::msg("incomplete pieces"))
}

// TODO: split pieces into files and save to disk

Ok(())
}

Expand Down

0 comments on commit dc9ffaf

Please sign in to comment.