use crate::error::{Aria2Error, Result};
use std::path::{Path, PathBuf};
mod native;
mod persistence;
const IN_FLIGHT_BLOCK_LENGTH: u64 = 16 * 1024;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ControlFileInFlightPiece {
pub index: u32,
pub length: u32,
pub bitfield: Vec<u8>,
}
#[derive(Debug, Clone)]
pub struct ControlFile {
path: PathBuf,
total_length: u64,
completed_length: u64,
upload_length: u64,
bitfield: Vec<u8>,
in_flight_pieces: Vec<ControlFileInFlightPiece>,
num_pieces: usize,
checksum_algo: u8,
checksum_value: Vec<u8>,
torrent_checkpoint: bool,
torrent_info_hash: Option<[u8; 20]>,
torrent_piece_length: Option<u32>,
native_piece_length: Option<u32>,
native_layout: bool,
}
impl ControlFile {
pub fn path(&self) -> &Path {
&self.path
}
pub fn total_length(&self) -> u64 {
self.total_length
}
pub fn piece_length(&self) -> Option<u32> {
self.native_piece_length.or(self.torrent_piece_length)
}
pub fn completed_length(&self) -> u64 {
self.completed_length
}
pub fn bitfield(&self) -> &[u8] {
&self.bitfield
}
pub fn in_flight_pieces(&self) -> &[ControlFileInFlightPiece] {
&self.in_flight_pieces
}
pub fn set_in_flight_pieces(&mut self, pieces: Vec<ControlFileInFlightPiece>) {
self.in_flight_pieces = pieces;
}
pub fn set_checksum(&mut self, algo: u8, value: Vec<u8>) {
self.checksum_algo = algo;
self.checksum_value = value;
}
pub fn checksum_algo(&self) -> u8 {
self.checksum_algo
}
pub fn checksum_value(&self) -> &[u8] {
&self.checksum_value
}
pub fn upload_length(&self) -> u64 {
self.upload_length
}
pub fn uses_native_layout(&self) -> bool {
self.native_layout
}
pub fn mark_torrent_checkpoint(&mut self) {
self.torrent_checkpoint = true;
}
pub fn is_torrent_checkpoint(&self) -> bool {
self.torrent_checkpoint
}
pub fn set_torrent_info_hash(&mut self, info_hash: [u8; 20]) {
self.torrent_info_hash = Some(info_hash);
}
pub fn torrent_info_hash(&self) -> Option<[u8; 20]> {
self.torrent_info_hash
}
pub fn set_torrent_piece_length(&mut self, piece_length: u32) {
self.torrent_piece_length = Some(piece_length);
}
pub fn torrent_piece_length(&self) -> Option<u32> {
self.torrent_piece_length
}
pub fn set_bitfield(&mut self, bitfield: Vec<u8>) {
self.bitfield = bitfield;
}
pub(crate) fn normalize_legacy_piece_layout(&mut self, num_pieces: usize) -> Result<()> {
if !self.native_layout {
self.normalize_bitfield(num_pieces)?;
}
Ok(())
}
pub async fn open_or_create(
ctrl_path: &Path,
total_length: u64,
num_pieces: usize,
) -> Result<Self> {
if ctrl_path.exists() {
let mut control_file = Self::load(ctrl_path).await?.ok_or_else(|| {
Aria2Error::FileIo(format!(
"Failed to load control file: {}",
ctrl_path.display()
))
})?;
if !control_file.native_layout {
control_file.normalize_bitfield(num_pieces)?;
}
Ok(control_file)
} else {
let piece_length = piece_length_for(total_length, num_pieces)?;
Self::new_with_piece_length(ctrl_path, total_length, piece_length)
}
}
pub async fn open_or_create_with_piece_length(
ctrl_path: &Path,
total_length: u64,
piece_length: u32,
) -> Result<Self> {
if piece_length == 0 {
return Err(Aria2Error::InvalidArgument(
"Control file piece length must not be 0".to_string(),
));
}
if ctrl_path.exists() {
let control_file = Self::load(ctrl_path).await?.ok_or_else(|| {
Aria2Error::FileIo(format!(
"Failed to load control file: {}",
ctrl_path.display()
))
})?;
let expected_pieces = piece_count(total_length, piece_length)?;
if control_file.total_length() == total_length
&& control_file.piece_length() == Some(piece_length)
&& control_file.bitfield().len() == expected_pieces.div_ceil(8)
{
return Ok(control_file);
}
return Err(Aria2Error::InvalidArgument(format!(
"Control file layout does not match piece length {}: {}",
piece_length,
ctrl_path.display()
)));
}
Self::new_with_piece_length(ctrl_path, total_length, piece_length)
}
fn new_with_piece_length(
ctrl_path: &Path,
total_length: u64,
piece_length: u32,
) -> Result<Self> {
let actual_num_pieces = piece_count(total_length, piece_length)?;
let bitfield_len = actual_num_pieces.div_ceil(8);
Ok(Self {
path: ctrl_path.to_path_buf(),
total_length,
completed_length: 0,
upload_length: 0,
bitfield: vec![0u8; bitfield_len],
in_flight_pieces: Vec::new(),
num_pieces: actual_num_pieces,
checksum_algo: 0,
checksum_value: Vec::new(),
torrent_checkpoint: false,
torrent_info_hash: None,
torrent_piece_length: None,
native_piece_length: Some(piece_length),
native_layout: false,
})
}
pub fn mark_piece_done(&mut self, index: usize) {
let byte_index = index / 8;
let bit_index = index % 8;
if index < self.num_pieces && byte_index < self.bitfield.len() {
self.bitfield[byte_index] |= 1 << (7 - bit_index);
self.in_flight_pieces
.retain(|piece| piece.index as usize != index);
self.completed_length = self.calculate_completed();
}
}
fn normalize_bitfield(&mut self, num_pieces: usize) -> Result<()> {
let piece_length = piece_length_for(self.total_length, num_pieces)?;
self.native_piece_length = Some(piece_length);
self.num_pieces = piece_count(self.total_length, piece_length)?;
self.bitfield.resize(self.num_pieces.div_ceil(8), 0);
if let Some(last_byte) = self.bitfield.last_mut() {
let valid_bits = self.num_pieces % 8;
if valid_bits != 0 {
*last_byte &= u8::MAX << (8 - valid_bits);
}
}
self.completed_length = self.calculate_completed();
Ok(())
}
pub fn is_piece_done(&self, index: usize) -> bool {
let byte_index = index / 8;
let bit_index = index % 8;
if byte_index < self.bitfield.len() {
(self.bitfield[byte_index] & (1 << (7 - bit_index))) != 0
} else {
false
}
}
pub fn completed_pieces(&self) -> usize {
self.bitfield.iter().map(|b| b.count_ones() as usize).sum()
}
fn calculate_completed(&self) -> u64 {
if self.total_length == 0 || self.num_pieces == 0 {
return 0;
}
let piece_size = self
.torrent_piece_length
.or(self.native_piece_length)
.map(u64::from)
.unwrap_or_else(|| self.total_length.div_ceil(self.num_pieces as u64));
(0..self.num_pieces)
.filter(|&index| self.is_piece_done(index))
.map(|index| {
let offset = index as u64 * piece_size;
self.total_length.saturating_sub(offset).min(piece_size)
})
.sum()
}
pub fn update_completed_length(&mut self, length: u64) {
self.completed_length = length.min(self.total_length);
}
fn piece_span(&self, index: usize) -> Option<(u64, u64)> {
let piece_length = u64::from(self.effective_piece_length().ok()?);
let offset = (index as u64).checked_mul(piece_length)?;
(index < self.num_pieces && offset < self.total_length)
.then(|| (offset, piece_length.min(self.total_length - offset)))
}
fn in_flight_prefix(&self, index: usize) -> u64 {
let Some(piece) = self
.in_flight_pieces
.iter()
.find(|piece| piece.index as usize == index)
else {
return 0;
};
let blocks = (0..piece.bitfield.len() * 8)
.take_while(|&block| piece.bitfield[block / 8] & (1 << (7 - block % 8)) != 0)
.count() as u64;
blocks
.saturating_mul(IN_FLIGHT_BLOCK_LENGTH)
.min(u64::from(piece.length))
}
fn set_in_flight_prefix(&mut self, index: usize, written: u64) {
let Some((_, length)) = self.piece_span(index) else {
return;
};
let block_count = length.div_ceil(IN_FLIGHT_BLOCK_LENGTH);
let blocks = if written >= length {
block_count
} else {
written / IN_FLIGHT_BLOCK_LENGTH
};
if self.is_piece_done(index)
|| blocks == 0
|| blocks.saturating_mul(IN_FLIGHT_BLOCK_LENGTH).min(length)
<= self.in_flight_prefix(index)
{
return;
}
let (Ok(piece_index), Ok(piece_length)) = (u32::try_from(index), u32::try_from(length))
else {
return;
};
let mut bitfield = vec![0u8; block_count.div_ceil(8) as usize];
for block in 0..blocks as usize {
bitfield[block / 8] |= 1 << (7 - block % 8);
}
let record = ControlFileInFlightPiece {
index: piece_index,
length: piece_length,
bitfield,
};
match self
.in_flight_pieces
.iter_mut()
.find(|piece| piece.index as usize == index)
{
Some(existing) => *existing = record,
None => self.in_flight_pieces.push(record),
}
}
pub fn record_completed_range(&mut self, start: u64, length: u64) {
let end = start.saturating_add(length).min(self.total_length);
if start >= end {
return;
}
if self.num_pieces <= 1 {
if start <= self.completed_length {
self.completed_length = self.completed_length.max(end);
}
return;
}
let Ok(piece_length) = self.effective_piece_length().map(u64::from) else {
return;
};
for index in (start / piece_length) as usize..end.div_ceil(piece_length) as usize {
let Some((offset, span)) = self.piece_span(index) else {
break;
};
if self.is_piece_done(index)
|| (offset < start && self.in_flight_prefix(index) < start - offset)
{
continue;
}
if end >= offset + span {
self.mark_piece_done(index);
} else {
self.set_in_flight_prefix(index, end - offset);
}
}
self.recount_completed();
}
pub fn record_completed_prefix(&mut self, length: u64) {
if self.num_pieces <= 1 {
self.update_completed_length(length);
} else {
self.record_completed_range(0, length);
}
}
fn recount_completed(&mut self) {
let in_flight: u64 = self
.in_flight_pieces
.iter()
.filter(|piece| !self.is_piece_done(piece.index as usize))
.map(|piece| self.in_flight_prefix(piece.index as usize))
.sum();
self.completed_length = (self.calculate_completed() + in_flight).min(self.total_length);
}
fn effective_piece_length(&self) -> Result<u32> {
self.torrent_piece_length
.or(self.native_piece_length)
.or_else(|| piece_length_for(self.total_length, self.num_pieces).ok())
.filter(|piece_length| *piece_length != 0)
.ok_or_else(|| {
Aria2Error::InvalidArgument(
"Control file piece length cannot be represented".to_string(),
)
})
}
pub fn control_path_for(output_path: &Path) -> PathBuf {
if output_path == Path::new(".") || output_path == Path::new("..") {
return output_path.join(".aria2");
}
let Some(file_name) = output_path.file_name() else {
return output_path.join(".aria2");
};
let mut control_name = file_name.to_os_string();
control_name.push(".aria2");
output_path.with_file_name(control_name)
}
}
pub(super) fn checksum_length(algo: u8) -> Option<usize> {
match algo {
1 => Some(16), 2 => Some(20), 3 => Some(32), 4 => Some(8), _ => None,
}
}
pub(super) fn piece_length_for(total_length: u64, num_pieces: usize) -> Result<u32> {
let divisor = num_pieces.max(1) as u64;
let piece_length = if total_length == 0 {
1
} else {
total_length.div_ceil(divisor)
};
u32::try_from(piece_length).map_err(|_| {
Aria2Error::InvalidArgument(format!(
"Control file piece length exceeds native aria2 limit: {}",
piece_length
))
})
}
pub(super) fn piece_count(total_length: u64, piece_length: u32) -> Result<usize> {
if piece_length == 0 {
return Err(Aria2Error::InvalidArgument(
"Control file piece length must not be 0".to_string(),
));
}
usize::try_from(total_length.div_ceil(piece_length as u64))
.map_err(|_| Aria2Error::InvalidArgument("Control file has too many pieces".to_string()))
}
pub(super) fn validate_in_flight_pieces(
pieces: &[ControlFileInFlightPiece],
total_length: u64,
piece_length: u32,
num_pieces: usize,
_completed_bitfield: &[u8],
) -> Result<()> {
let mut seen = std::collections::HashSet::with_capacity(pieces.len());
for piece in pieces {
let index = piece.index as usize;
if index >= num_pieces {
return Err(Aria2Error::FileIo(format!(
"In-flight torrent piece index is out of range: {}",
piece.index
)));
}
if !seen.insert(piece.index) {
return Err(Aria2Error::FileIo(format!(
"Duplicate in-flight torrent piece index: {}",
piece.index
)));
}
if piece.length == 0 || piece.length > piece_length {
return Err(Aria2Error::FileIo(format!(
"In-flight torrent piece length is out of range: {}",
piece.length
)));
}
let block_count = (piece.length as usize).div_ceil(16 * 1024);
let expected_bitfield_length = block_count.div_ceil(8);
if piece.bitfield.len() != expected_bitfield_length {
return Err(Aria2Error::FileIo(format!(
"In-flight block bitfield length mismatch: expected {}, actual {}",
expected_bitfield_length,
piece.bitfield.len()
)));
}
let unused_bits = (8 - block_count % 8) % 8;
if unused_bits != 0
&& piece
.bitfield
.last()
.is_some_and(|byte| byte & ((1u8 << unused_bits) - 1) != 0)
{
return Err(Aria2Error::FileIo(
"In-flight block bitfield has set trailing bits".to_string(),
));
}
let piece_offset = u64::from(piece.index) * u64::from(piece_length);
if piece_offset >= total_length {
return Err(Aria2Error::FileIo(format!(
"In-flight torrent piece offset is out of range: {}",
piece.index
)));
}
}
Ok(())
}
#[cfg(test)]
mod tests;