2024-05-02 18:58:54 +01:00
|
|
|
pub mod examples;
|
2024-04-30 09:09:17 +01:00
|
|
|
pub mod filesystem;
|
2024-05-02 18:58:54 +01:00
|
|
|
pub mod middleware;
|
2024-04-30 09:09:17 +01:00
|
|
|
|
2024-05-02 09:53:53 +01:00
|
|
|
use std::{
|
|
|
|
|
any::{Any, TypeId},
|
|
|
|
|
path::Path,
|
|
|
|
|
};
|
2024-04-30 09:09:17 +01:00
|
|
|
|
|
|
|
|
use crate::torrent_state::ManagedTorrentInfo;
|
|
|
|
|
|
2024-04-30 22:47:23 +01:00
|
|
|
pub trait StorageFactory: Send + Sync + Any {
|
2024-05-01 22:14:34 +01:00
|
|
|
type Storage: TorrentStorage;
|
|
|
|
|
|
|
|
|
|
fn init_storage(&self, info: &ManagedTorrentInfo) -> anyhow::Result<Self::Storage>;
|
2024-05-02 09:53:53 +01:00
|
|
|
fn is_type_id(&self, type_id: TypeId) -> bool {
|
|
|
|
|
Self::type_id(self) == type_id
|
|
|
|
|
}
|
2024-05-01 22:14:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 {
|
2024-05-02 09:46:56 +01:00
|
|
|
struct Wrapper<SF> {
|
2024-05-01 22:14:34 +01:00
|
|
|
sf: SF,
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-02 09:46:56 +01:00
|
|
|
impl<SF: StorageFactory> StorageFactory for Wrapper<SF> {
|
2024-05-01 22:14:34 +01:00
|
|
|
type Storage = Box<dyn TorrentStorage>;
|
|
|
|
|
|
|
|
|
|
fn init_storage(&self, info: &ManagedTorrentInfo) -> anyhow::Result<Self::Storage> {
|
|
|
|
|
let s = self.sf.init_storage(info)?;
|
|
|
|
|
Ok(Box::new(s))
|
|
|
|
|
}
|
2024-05-02 09:53:53 +01:00
|
|
|
|
|
|
|
|
fn is_type_id(&self, type_id: TypeId) -> bool {
|
|
|
|
|
self.sf.type_id() == type_id
|
|
|
|
|
}
|
2024-05-01 22:14:34 +01:00
|
|
|
}
|
|
|
|
|
|
2024-05-02 09:46:56 +01:00
|
|
|
Box::new(Wrapper { sf: self })
|
2024-05-01 22:14:34 +01:00
|
|
|
}
|
2024-04-30 09:09:17 +01:00
|
|
|
}
|
|
|
|
|
|
2024-05-01 22:00:00 +01:00
|
|
|
impl<U: StorageFactory + ?Sized> StorageFactory for Box<U> {
|
2024-05-01 22:14:34 +01:00
|
|
|
type Storage = U::Storage;
|
|
|
|
|
|
|
|
|
|
fn init_storage(&self, info: &ManagedTorrentInfo) -> anyhow::Result<U::Storage> {
|
2024-05-01 22:00:00 +01:00
|
|
|
(**self).init_storage(info)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-30 09:09:17 +01:00
|
|
|
pub trait TorrentStorage: Send + Sync {
|
|
|
|
|
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 ensure_file_length(&self, file_id: usize, length: u64) -> anyhow::Result<()>;
|
|
|
|
|
|
|
|
|
|
fn take(&self) -> anyhow::Result<Box<dyn TorrentStorage>>;
|
|
|
|
|
}
|
2024-05-01 22:00:00 +01:00
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
}
|
|
|
|
|
}
|