pub mod filesystem;
#[cfg(feature = "storage_examples")]
pub mod examples;
#[cfg(feature = "storage_middleware")]
pub mod middleware;
use std::{
any::{Any, TypeId},
path::Path,
};
use librqbit_core::lengths::ValidPieceIndex;
use crate::torrent_state::{ManagedTorrentShared, TorrentMetadata};
pub trait StorageFactory: Send + Sync + Any {
type Storage: TorrentStorage;
fn create(
&self,
shared: &ManagedTorrentShared,
metadata: &TorrentMetadata,
) -> anyhow::Result<Self::Storage>;
fn create_and_init(
&self,
shared: &ManagedTorrentShared,
metadata: &TorrentMetadata,
) -> anyhow::Result<Self::Storage> {
let mut storage = self.create(shared, metadata)?;
storage.init(shared, metadata)?;
Ok(storage)
}
fn is_type_id(&self, type_id: TypeId) -> bool {
Self::type_id(self) == type_id
}
fn clone_box(&self) -> BoxStorageFactory;
}
pub type BoxStorageFactory = Box<dyn StorageFactory<Storage = Box<dyn TorrentStorage>>>;
pub trait StorageFactoryExt {
fn boxed(self) -> BoxStorageFactory;
}
impl<SF: StorageFactory> StorageFactoryExt for SF {
fn boxed(self) -> BoxStorageFactory {
struct Wrapper<SF> {
sf: SF,
}
impl<SF: StorageFactory> StorageFactory for Wrapper<SF> {
type Storage = Box<dyn TorrentStorage>;
fn create(
&self,
shared: &ManagedTorrentShared,
metadata: &TorrentMetadata,
) -> anyhow::Result<Self::Storage> {
let s = self.sf.create(shared, metadata)?;
Ok(Box::new(s))
}
fn is_type_id(&self, type_id: TypeId) -> bool {
self.sf.type_id() == type_id
}
fn clone_box(&self) -> BoxStorageFactory {
self.sf.clone_box()
}
}
Box::new(Wrapper { sf: self })
}
}
impl<U: StorageFactory + ?Sized> StorageFactory for Box<U> {
type Storage = U::Storage;
fn create(
&self,
shared: &ManagedTorrentShared,
metadata: &TorrentMetadata,
) -> anyhow::Result<U::Storage> {
(**self).create(shared, metadata)
}
fn clone_box(&self) -> BoxStorageFactory {
(**self).clone_box()
}
}
pub trait TorrentStorage: Send + Sync {
fn init(
&mut self,
shared: &ManagedTorrentShared,
metadata: &TorrentMetadata,
) -> anyhow::Result<()>;
fn pread_exact(&self, file_id: usize, offset: u64, buf: &mut [u8]) -> anyhow::Result<()>;
fn pwrite_all(&self, file_id: usize, offset: u64, buf: &[u8]) -> anyhow::Result<()>;
fn remove_file(&self, file_id: usize, filename: &Path) -> anyhow::Result<()>;
fn remove_directory_if_empty(&self, path: &Path) -> anyhow::Result<()>;
fn ensure_file_length(&self, file_id: usize, length: u64) -> anyhow::Result<()>;
fn take(&self) -> anyhow::Result<Box<dyn TorrentStorage>>;
fn on_piece_completed(&self, _piece_index: ValidPieceIndex) -> anyhow::Result<()> {
Ok(())
}
}
impl<U: TorrentStorage + ?Sized> TorrentStorage for Box<U> {
fn pread_exact(&self, file_id: usize, offset: u64, buf: &mut [u8]) -> anyhow::Result<()> {
(**self).pread_exact(file_id, offset, buf)
}
fn pwrite_all(&self, file_id: usize, offset: u64, buf: &[u8]) -> anyhow::Result<()> {
(**self).pwrite_all(file_id, offset, buf)
}
fn remove_file(&self, file_id: usize, filename: &Path) -> anyhow::Result<()> {
(**self).remove_file(file_id, filename)
}
fn ensure_file_length(&self, file_id: usize, length: u64) -> anyhow::Result<()> {
(**self).ensure_file_length(file_id, length)
}
fn take(&self) -> anyhow::Result<Box<dyn TorrentStorage>> {
(**self).take()
}
fn remove_directory_if_empty(&self, path: &Path) -> anyhow::Result<()> {
(**self).remove_directory_if_empty(path)
}
fn init(
&mut self,
shared: &ManagedTorrentShared,
metadata: &TorrentMetadata,
) -> anyhow::Result<()> {
(**self).init(shared, metadata)
}
fn on_piece_completed(&self, piece_id: ValidPieceIndex) -> anyhow::Result<()> {
(**self).on_piece_completed(piece_id)
}
}