Tweak everything for APIs to support everything while not changing too much

This commit is contained in:
Igor Katson 2024-04-30 22:47:23 +01:00
parent ebd4731c09
commit fd30ad9cbf
6 changed files with 70 additions and 110 deletions

View file

@ -37,8 +37,6 @@ struct InMemoryExampleStorage {
lengths: Lengths,
file_infos: FileInfos,
map: RwLock<HashMap<ValidPieceIndex, InMemoryPiece>>,
// TODO: chunk tracker - rename to PieceTracker and extract chunks out of it (only keep pieces)
// this sucker here would track chunks, and the storage above too.
}
impl InMemoryExampleStorage {

View file

@ -14,16 +14,15 @@ use self::opened_file::OpenedFile;
use super::{StorageFactory, TorrentStorage};
pub struct FilesystemStorageFactory {
pub output_folder: PathBuf,
pub allow_overwrite: bool,
}
#[derive(Default)]
pub struct FilesystemStorageFactory {}
impl StorageFactory for FilesystemStorageFactory {
fn init_storage(&self, meta: &ManagedTorrentInfo) -> anyhow::Result<Box<dyn TorrentStorage>> {
let mut files = Vec::<OpenedFile>::new();
let output_folder = &meta.options.output_folder;
for file_details in meta.info.iter_file_details(&meta.lengths)? {
let mut full_path = self.output_folder.clone();
let mut full_path = output_folder.clone();
let relative_path = file_details
.filename
.to_pathbuf()
@ -31,7 +30,7 @@ impl StorageFactory for FilesystemStorageFactory {
full_path.push(relative_path);
std::fs::create_dir_all(full_path.parent().context("bug: no parent")?)?;
let file = if self.allow_overwrite {
let file = if meta.options.allow_overwrite {
OpenOptions::new()
.create(true)
.truncate(false)
@ -45,20 +44,21 @@ impl StorageFactory for FilesystemStorageFactory {
.create_new(true)
.write(true)
.open(&full_path)
.with_context(|| format!("error creating {:?}", &full_path))?;
.with_context(|| {
format!(
"error creating a new file (because allow_overwrite = false) {:?}",
&full_path
)
})?;
OpenOptions::new().read(true).write(true).open(&full_path)?
};
files.push(OpenedFile::new(file));
}
Ok(Box::new(FilesystemStorage {
output_folder: self.output_folder.clone(),
output_folder: output_folder.clone(),
opened_files: files,
}))
}
fn output_folder(&self) -> Option<&Path> {
Some(&self.output_folder)
}
}
pub struct FilesystemStorage {

View file

@ -1,16 +1,12 @@
pub mod example;
pub mod filesystem;
use std::path::Path;
use std::{any::Any, path::Path};
use crate::torrent_state::ManagedTorrentInfo;
pub trait StorageFactory: Send + Sync {
pub trait StorageFactory: Send + Sync + Any {
fn init_storage(&self, info: &ManagedTorrentInfo) -> anyhow::Result<Box<dyn TorrentStorage>>;
fn output_folder(&self) -> Option<&Path> {
None
}
}
pub trait TorrentStorage: Send + Sync {
@ -23,8 +19,4 @@ pub trait TorrentStorage: Send + Sync {
fn ensure_file_length(&self, file_id: usize, length: u64) -> anyhow::Result<()>;
fn take(&self) -> anyhow::Result<Box<dyn TorrentStorage>>;
fn output_folder(&self) -> Option<&Path> {
None
}
}