use crate::storage::{unavailable, Result};
use crate::torrent_data::DataPool;
use crate::{Piece, PieceIndex};
use log::trace;
use std::collections::BTreeMap;
use std::io;
use std::io::SeekFrom;
use std::path::{Path, PathBuf};
use tokio::fs::{File, OpenOptions};
use tokio::io::{AsyncReadExt, AsyncSeekExt, AsyncWriteExt};
use tokio::sync::RwLock;
#[cfg(feature = "tracing")]
use tracing::instrument;
type SlotIndex = usize;
#[derive(Debug)]
pub struct PartsFile {
filename: String,
path: PathBuf,
data_pool: DataPool,
piece_slots: RwLock<BTreeMap<PieceIndex, SlotIndex>>,
slots: RwLock<BTreeMap<SlotIndex, PartSlot>>,
}
impl PartsFile {
pub fn new<S: AsRef<str>, P: AsRef<Path>>(filename: S, path: P, data_pool: DataPool) -> Self {
Self {
filename: filename.as_ref().to_string(),
path: path.as_ref().to_path_buf(),
data_pool,
piece_slots: Default::default(),
slots: Default::default(),
}
}
#[cfg_attr(feature = "tracing", instrument(skip(self, buffer)))]
pub async fn read(
&self,
buffer: &mut [u8],
piece: &PieceIndex,
offset: usize,
) -> Result<usize> {
let filepath = self.absolute_filepath().await;
if !filepath.exists() {
return Err(unavailable());
}
let slot_index = {
let piece_slots = self.piece_slots.read().await;
match piece_slots.get(piece).copied() {
None => return Err(unavailable()),
Some(e) => e,
}
};
let slots = self.slots.read().await;
let slot = slots.get(&slot_index).ok_or(unavailable())?;
let mut file = self.open(false).await?;
let metadata = file.metadata().await?;
let file_offset = slot.offset.saturating_add(offset);
if file_offset > metadata.len() as usize {
return Err(io::Error::new(io::ErrorKind::InvalidInput, "out-of-bounds"));
}
file.seek(SeekFrom::Start(file_offset as u64)).await?;
let bytes_read = file.read(buffer).await?;
Ok(bytes_read)
}
#[cfg_attr(feature = "tracing", instrument(skip(self, data)))]
pub async fn write(&self, data: &[u8], piece: &PieceIndex, offset: usize) -> Result<usize> {
let mut slots = self.slots.write().await;
let mut piece_slots = self.piece_slots.write().await;
let piece = self.data_pool.piece(piece).await.ok_or(unavailable())?;
let slot = match piece_slots
.get(&piece.index)
.and_then(|slot_index| slots.get(slot_index))
{
Some(slot) => slot,
None => {
let slot_index = Self::get_free_slot(&piece, &mut slots);
piece_slots.insert(piece.index, slot_index);
trace!(
"Parts file {:?} created new slot {} for piece {}",
self.absolute_filepath().await,
slot_index,
piece.index
);
slots.get(&slot_index).ok_or(unavailable())?
}
};
if offset + data.len() > slot.len {
return Err(io::Error::new(io::ErrorKind::InvalidInput, "out-of-bounds"));
}
let mut file = self.open(true).await?;
let file_offset = slot.offset.saturating_add(offset);
file.seek(SeekFrom::Start(file_offset as u64)).await?;
file.write_all(data).await?;
file.flush().await?;
Ok(data.len())
}
async fn open(&self, write: bool) -> Result<File> {
let absolute_filepath = self.absolute_filepath().await;
Ok(OpenOptions::new()
.read(true)
.write(write)
.create(write)
.open(absolute_filepath)
.await?)
}
async fn absolute_filepath(&self) -> PathBuf {
self.path.join(&self.filename)
}
fn get_free_slot(piece: &Piece, slots: &mut BTreeMap<SlotIndex, PartSlot>) -> SlotIndex {
let piece_len = piece.len();
let (slot_index, slot) = slots
.iter()
.find(|(_, slot)| !slot.in_use && piece_len == slot.len)
.map(|(index, slot)| {
(
*index,
PartSlot {
offset: slot.offset,
len: slot.len,
in_use: true,
},
)
})
.unwrap_or_else(|| {
if let Some((index, last_slot)) = slots.last_key_value() {
(
index + 1,
PartSlot {
offset: last_slot.offset + last_slot.len + 1,
len: piece.len(),
in_use: true,
},
)
} else {
(
0,
PartSlot {
offset: 0,
len: piece.len(),
in_use: true,
},
)
}
});
slots.insert(slot_index, slot);
slot_index
}
}
#[derive(Debug, Clone)]
struct PartSlot {
offset: usize,
len: usize,
in_use: bool,
}
#[cfg(test)]
mod tests {
use super::*;
use tempfile::tempdir;
#[tokio::test]
async fn test_write_create_new_slot() {
init_logger!();
let temp_dir = tempdir().unwrap();
let temp_path = temp_dir.path().to_str().unwrap();
let filename = ".test.parts";
let piece_index = 2 as PieceIndex;
let piece_len = 16;
let piece_data = "lorem".as_bytes();
let pieces = DataPool::from(vec![Piece {
hash: Default::default(),
index: piece_index,
offset: 0,
length: piece_len,
priority: Default::default(),
blocks: vec![],
availability: 0,
}]);
let part_file = PartsFile::new(filename, temp_path, pieces.clone());
let result = part_file.write(piece_data, &piece_index, 0).await;
match result {
Ok(result) => assert_eq!(
piece_data.len(),
result,
"expected the piece data to have been written"
),
Err(_) => assert!(false, "expected Ok, but got {:?}", result),
}
let mut buffer = vec![0u8; piece_len];
let result = part_file.read(&mut buffer, &piece_index, 0).await;
match result {
Ok(result) => assert_eq!(
piece_data.len(),
result,
"expected the piece data to have been read"
),
Err(_) => assert!(false, "expected Ok, but got {:?}", result),
}
}
}