mod constants;
pub mod entry_ref;
pub mod error;
mod fold;
mod frontier;
mod helpers;
#[cfg(test)]
#[macro_use]
mod joiner_tests;
#[cfg(test)]
#[macro_use]
mod splitter_tests;
mod joiner;
pub mod mode;
mod read_at;
mod splitter;
mod splitter_parallel;
mod tree;
mod windowed;
mod write_at;
use crate::chunk::ChunkAddress;
#[cfg(feature = "encryption")]
use crate::chunk::encryption::EncryptedChunkRef;
use crate::store::{ChunkPut, MaybeSend, MaybeSync};
#[cfg(feature = "encryption")]
pub use joiner::EncryptedJoiner;
#[cfg(feature = "tokio")]
pub use joiner::JoinerReader;
pub use joiner::{GenericJoiner, Joiner};
#[cfg(feature = "tokio")]
pub use windowed::WindowedJoinerReader;
pub use windowed::WindowedReader;
pub use read_at::ReadAt;
#[cfg(feature = "encryption")]
pub use splitter::EncryptedSplitter;
pub use splitter::Splitter;
#[cfg(feature = "encryption")]
pub use splitter_parallel::EncryptedParallelSplitter;
pub use splitter_parallel::ParallelSplitter;
pub use write_at::WriteAt;
pub use entry_ref::EntryRef;
pub use error::FileError;
pub use tree::{ChunkRange, TreeParams};
mod join_ref_sealed {
pub trait Sealed {}
impl Sealed for crate::ChunkAddress {}
#[cfg(feature = "encryption")]
impl Sealed for crate::EncryptedChunkRef {}
}
pub trait JoinRef: join_ref_sealed::Sealed + Clone + Send + Sync + 'static {
type Mode: mode::JoinMode + Send + Sync;
fn into_root_ref(self) -> <Self::Mode as mode::JoinMode>::RootRef;
}
impl JoinRef for ChunkAddress {
type Mode = mode::PlainMode;
fn into_root_ref(self) -> Self {
self
}
}
#[cfg(feature = "encryption")]
impl JoinRef for EncryptedChunkRef {
type Mode = mode::EncryptedMode;
fn into_root_ref(self) -> Self {
self
}
}
pub(crate) fn resolve_seek_position(
pos: std::io::SeekFrom,
current: u64,
span: u64,
) -> std::io::Result<u64> {
use std::io::{Error, ErrorKind::InvalidInput, SeekFrom};
let to_i64 = |v: u64, msg: &str| i64::try_from(v).map_err(|_| Error::new(InvalidInput, msg));
let new_pos = match pos {
SeekFrom::Start(off) => to_i64(off, "seek offset exceeds i64::MAX")?,
SeekFrom::End(off) => to_i64(span, "file span exceeds i64::MAX")? + off,
SeekFrom::Current(off) => to_i64(current, "current position exceeds i64::MAX")? + off,
};
if new_pos < 0 {
return Err(Error::new(InvalidInput, "seek to negative position"));
}
Ok(new_pos as u64)
}
pub async fn join<R, G, const BODY_SIZE: usize>(getter: G, root: R) -> error::Result<Vec<u8>>
where
R: JoinRef,
G: crate::store::ChunkGet<BODY_SIZE>,
{
GenericJoiner::<G, R::Mode, BODY_SIZE>::new(getter, root.into_root_ref())
.await?
.read_all()
.await
}
pub fn split<const BODY_SIZE: usize>(
data: &[u8],
) -> error::Result<(ChunkAddress, crate::store::MemoryStore<BODY_SIZE>)> {
let (root, chunks) = ParallelSplitter::<BODY_SIZE>::split_to_vec(&data)?;
Ok((root, crate::store::MemoryStore::from_chunks(chunks)))
}
#[cfg(feature = "encryption")]
pub fn split_encrypted<const BODY_SIZE: usize>(
data: &[u8],
) -> error::Result<(EncryptedChunkRef, crate::store::MemoryStore<BODY_SIZE>)> {
let (root_ref, chunks) = EncryptedParallelSplitter::<BODY_SIZE>::split_to_vec(&data)?;
Ok((root_ref, crate::store::MemoryStore::from_chunks(chunks)))
}
#[cfg(test)]
pub(crate) const fn levels(length: u64, chunk_size: usize) -> usize {
constants::tree_depth(length, chunk_size, constants::REF_SIZE)
}
pub trait ChunkGetExt<const BODY_SIZE: usize>: crate::store::ChunkGet<BODY_SIZE> {
fn joiner<R: JoinRef>(
self,
root: R,
) -> impl std::future::Future<Output = error::Result<GenericJoiner<Self, R::Mode, BODY_SIZE>>>
+ MaybeSend
where
Self: Sized + Clone + MaybeSend + MaybeSync + 'static,
{
GenericJoiner::new(self, root.into_root_ref())
}
fn read_file<R: JoinRef>(
self,
root: R,
) -> impl std::future::Future<Output = error::Result<Vec<u8>>> + MaybeSend
where
Self: Sized + Clone + MaybeSend + MaybeSync + 'static,
{
join(self, root)
}
}
impl<T, const BODY_SIZE: usize> ChunkGetExt<BODY_SIZE> for T where
T: crate::store::ChunkGet<BODY_SIZE>
{
}
pub trait ChunkPutExt<const BODY_SIZE: usize>: ChunkPut<BODY_SIZE> {
fn write_file<D: ReadAt + Sync>(
&self,
data: D,
) -> impl std::future::Future<Output = error::Result<ChunkAddress>> + MaybeSend
where
Self: Sized + MaybeSync,
{
let split = ParallelSplitter::<BODY_SIZE>::split_to_vec(&data);
async move {
let (root, chunks) = split?;
for chunk in chunks {
self.put(chunk).await.map_err(FileError::store)?;
}
Ok(root)
}
}
#[cfg(feature = "encryption")]
fn write_encrypted_file<D: ReadAt + Sync>(
&self,
data: D,
) -> impl std::future::Future<Output = error::Result<EncryptedChunkRef>> + MaybeSend
where
Self: Sized + MaybeSync,
{
let split = EncryptedParallelSplitter::<BODY_SIZE>::split_to_vec(&data);
async move {
let (root_ref, chunks) = split?;
for chunk in chunks {
self.put(chunk).await.map_err(FileError::store)?;
}
Ok(root_ref)
}
}
}
impl<T, const BODY_SIZE: usize> ChunkPutExt<BODY_SIZE> for T where T: ChunkPut<BODY_SIZE> {}
#[cfg(test)]
mod tests;