aria2-core 0.3.10

aria2-rust core download engine: multi-protocol segmented downloads, rate limiting, configuration, session persistence, and BitTorrent seeding
Documentation
use async_trait::async_trait;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tracing::{debug, info, warn};

use crate::engine::bittorrent::download::command::BtDownloadCommand;
use crate::engine::command::{Command, CommandStatus};
use crate::error::{Aria2Error, FatalError, Result};
use crate::filesystem::control_file::ControlFile;
#[cfg(feature = "bittorrent")]
use crate::request::request_group::BtConnectionGuard;
use crate::request::request_group::{ActiveConnectionGuard, GroupId};
use crate::util::rwlock_ext::RwLockRecover;

use super::checkpoint::{completed_piece_bytes, legacy_progress_piece_indices};
use super::state::IntegrityPreparation;

impl BtDownloadCommand {
    async fn shutdown_unclaimed_peer_swarm(&mut self) {
        if let Some(mut swarm) = self.initial_peer_swarm.take() {
            swarm.shutdown_all().await;
        }
    }
}

#[async_trait]
impl Command for BtDownloadCommand {
    async fn shutdown(&mut self) {
        self.dht_periodic_lookup.cancel_pending_lookup().await;
        BtDownloadCommand::shutdown(self).await;
    }

    async fn execute(&mut self) -> Result<()> {
        let _connection_guard = ActiveConnectionGuard::new(Arc::clone(&self.group));
        #[cfg(feature = "bittorrent")]
        let _bt_connection_guard = BtConnectionGuard::new(Arc::clone(&self.group));
        if !self.started {
            self.group.recover_mut().start()?;
            self.started = true;
            self.started_at = Some(Instant::now());
        }

        if let Err(message) = self.register_bt_download() {
            self.group.recover().set_last_error(
                crate::request::request_group::DownloadResultCode::DuplicateInfoHash,
                message.clone(),
            );
            return Err(Aria2Error::DownloadFailed(message));
        }
        self.reserve_output_paths()?;

        let (mut meta, piece_length, total_size, num_pieces) = self.prepare_environment().await?;
        let network_info_hash = meta.network_info_hash();
        self.group
            .recover()
            .set_control_file_path(ControlFile::control_path_for(&self.output_path));

        // A zero-length torrent has no pieces or peers to acquire. Complete
        // it after metadata preparation, matching the normal download
        // lifecycle without entering tracker/peer discovery.
        if total_size == 0 {
            self.create_zero_length_payload().await?;
            self.completed_bytes = 0;
            self.progress.set_completed_length(0);
            let payload_exists = self.bt_payload_exists();
            let checkpoint =
                crate::engine::bittorrent::persistence::checkpoint::BtCheckpoint::open(
                    &self.output_path,
                    payload_exists,
                    total_size,
                    piece_length,
                    num_pieces as usize,
                    network_info_hash,
                )
                .await?;
            checkpoint.remove().await?;
            self.group.recover_mut().complete()?;
            info!("BT zero-length download completed without peer discovery");
            self.shutdown_unclaimed_peer_swarm().await;
            return Ok(());
        }

        let IntegrityPreparation {
            payload_exists,
            seed_unverified,
            mut verified_piece_indices,
            integrity_finished_action,
        } = self
            .prepare_integrity_state(
                &meta,
                piece_length,
                total_size,
                num_pieces,
                network_info_hash,
            )
            .await?;

        if self.hash_check_only {
            info!("hash-check-only enabled; stopping after integrity validation");
            if self.check_integrity && verified_piece_indices.len() == num_pieces as usize {
                self.completed_bytes = total_size;
                self.progress.set_completed_length(total_size);
                if let Some(checkpoint) = self.checkpoint.take() {
                    checkpoint.remove().await?;
                }
                self.group.recover_mut().complete()?;
                info!("hash-check-only completed successfully");
                self.shutdown_unclaimed_peer_swarm().await;
                return Ok(());
            }
            return Err(Aria2Error::Fatal(FatalError::Config(
                "hash-check-only: existing data failed torrent piece hash validation".into(),
            )));
        }

        // A successful integrity check with seeding disabled is already a
        // complete download. Finish locally instead of discovering peers or
        // announcing a new torrent session.
        if self.hash_check_completed && !self.bt_hash_check_seed {
            info!(
                "Integrity check completed; bt-hash-check-seed disabled, stopping without seeding"
            );
            let started_at = self.started_at.unwrap_or_else(Instant::now);
            self.finalize_download(started_at, &meta).await?;
            self.shutdown_unclaimed_peer_swarm().await;
            return Ok(());
        }

        // File pre-allocation (mirrors C++ BtFileAllocationEntry queued into
        // FileAllocationMan after integrity checking). Single-file torrents
        // allocate `output_path`; multi-file torrents allocate every file in
        // layout order. Already-completed files are skipped by the worker.
        // The worker runs chunked, cooperative allocation sequentially across
        // downloads, so a huge zero-fill never blocks this task or the engine.
        {
            use crate::filesystem::file_allocation::AllocationStrategy;
            use crate::filesystem::file_allocation_man;
            let strategy = AllocationStrategy::from_str(&self.file_allocation);
            if strategy != AllocationStrategy::None {
                let gid = self.group.recover().gid().value();
                let man = file_allocation_man::shared();
                let allocation_files = if self.hash_check_completed {
                    integrity_finished_action
                        .file_allocation
                        .clone()
                        .unwrap_or_else(|| self.integrity_files(total_size))
                } else {
                    self.integrity_files(total_size)
                };
                if self.multi_file_layout.is_some() {
                    let files: Vec<(std::path::PathBuf, u64)> = allocation_files
                        .into_iter()
                        .map(|file| (file.path, file.length))
                        .collect();
                    file_allocation_man::enqueue_multi(
                        &man,
                        files,
                        strategy,
                        self.secure_falloc,
                        gid,
                    )
                    .await?;
                } else {
                    file_allocation_man::enqueue_path(
                        &man,
                        &self.output_path,
                        total_size,
                        strategy,
                        self.secure_falloc,
                        gid,
                    )
                    .await?;
                }
            }
        }

        // P1 integration: use the C++-compatible progress file only as a
        // fallback when the project extension has no progress. Integrity checks
        // remain authoritative because a progress file records trust, not
        // fresh hash evidence.
        if let Some(ref mgr) = self.progress_manager {
            match mgr.load_progress(&network_info_hash) {
                Ok(saved)
                    if !self.check_integrity
                        && !seed_unverified
                        && payload_exists
                        && self.completed_bytes == 0 =>
                {
                    match legacy_progress_piece_indices(
                        &saved,
                        piece_length,
                        total_size,
                        num_pieces,
                    ) {
                        Some(indices) if !indices.is_empty() => {
                            self.completed_bytes =
                                completed_piece_bytes(&indices, piece_length, total_size);
                            self.progress.set_completed_length(self.completed_bytes);
                            self.group
                                .recover()
                                .set_bt_bitfield(Some(saved.bitfield.clone()));
                            verified_piece_indices = indices;
                            info!(
                                pieces_done = verified_piece_indices.len(),
                                completed_bytes = self.completed_bytes,
                                "Resuming from legacy BT progress"
                            );
                        }
                        Some(_) => debug!("Saved BT progress has no completed pieces"),
                        None => warn!("Ignoring BT progress with incompatible torrent layout"),
                    }
                }
                Ok(_) => debug!(
                    "Ignoring saved BT progress because a newer checkpoint or integrity result is authoritative"
                ),
                Err(e) => {
                    debug!(
                        error = %e,
                        "No saved progress found, starting fresh download"
                    );
                }
            }
        }

        let mut session = self
            .prepare_torrent_session(&meta, piece_length, total_size, network_info_hash)
            .await?;

        let speed_reporter = super::spawn_download_speed_reporter(Arc::clone(&self.progress));
        let transfer_result = async {
            let piece_result = self
                .download_pieces_loop(
                    &mut session,
                    &mut meta,
                    piece_length,
                    total_size,
                    num_pieces,
                    &verified_piece_indices,
                )
                .await;
            self.group.recover().clear_bt_peer_snapshots();
            if let Err(error) = piece_result {
                session.swarm.shutdown_all().await;
                if let Some(actor) = self.tracker_actor.as_ref() {
                    actor.stop().await;
                }
                return Err(error);
            }

            if let Some(actor) = self.tracker_actor.as_ref() {
                actor.announce_completed().await;
            }

            if self.seed_enabled {
                let seeding_connections = if session.initial_peers.is_empty() {
                    Vec::new()
                } else {
                    match self
                        .connect_to_peers(
                            &session.initial_peers,
                            &network_info_hash,
                            meta.info_hash_v2,
                            num_pieces,
                            piece_length,
                            total_size,
                        )
                        .await
                    {
                        Ok(connections) => connections,
                        Err(error) => {
                            session.swarm.shutdown_all().await;
                            return Err(error);
                        }
                    }
                };
                info!(
                    "Starting seeding phase with {} connected peers ({} new connections, {} retained actors)...",
                    seeding_connections.len() + session.swarm.len(),
                    seeding_connections.len(),
                    session.swarm.len()
                );
                self.run_seeding_phase_with_swarm(
                    seeding_connections,
                    session.swarm,
                    session.upload_counter,
                    session.last_pex_send,
                    piece_length,
                    num_pieces,
                    network_info_hash,
                    meta.info_hash_v2,
                    total_size,
                )
                .await?;
            } else {
                info!("Skipping seeding (enabled={})", self.seed_enabled,);
                session.swarm.shutdown_all().await;
                if let Some(actor) = self.tracker_actor.as_ref() {
                    actor.stop().await;
                }
            }

            Ok(())
        }
        .await;
        speed_reporter.abort();
        let _ = speed_reporter.await;
        transfer_result?;

        let started_at = self.started_at.unwrap_or_else(Instant::now);
        self.finalize_download(started_at, &meta).await?;

        Ok(())
    }

    fn status(&self) -> CommandStatus {
        if self.group.recover().status() == crate::request::request_group::DownloadStatus::Complete
        {
            CommandStatus::Completed
        } else if self.completed_bytes > 0 {
            CommandStatus::Running
        } else {
            CommandStatus::Pending
        }
    }

    fn gid(&self) -> GroupId {
        self.group.recover().gid()
    }

    fn request_group(
        &self,
    ) -> Option<std::sync::Arc<std::sync::RwLock<crate::request::request_group::RequestGroup>>>
    {
        Some(std::sync::Arc::clone(&self.group))
    }

    fn timeout(&self) -> Option<Duration> {
        self.group.recover().timeout()
    }
}