Skip to main content

ZsyncAssembly

Struct ZsyncAssembly 

Source
pub struct ZsyncAssembly { /* private fields */ }
Expand description

One transfer of a target file, from a control file to the finished output.

Implementations§

Source§

impl ZsyncAssembly

Source

pub fn new( control: ControlFile, output_path: &Path, ) -> Result<Self, AssemblyError>

Assemble the target control describes into output_path.

There is no base URL, so the control file’s URLs must be absolute. Nothing is written until a block is found or downloaded.

Source

pub fn with_base_url( control: ControlFile, output_path: &Path, base_url: Option<&str>, ) -> Result<Self, AssemblyError>

Assemble with relative control file URLs resolved against base_url.

The base is joined as given, so for a directory it should end in /.

Source

pub fn with_client( control: ControlFile, output_path: &Path, base_url: Option<&str>, http: HttpClient, ) -> Result<Self, AssemblyError>

Assemble using a caller-supplied HTTP client.

The client carries the transport policy for every range request the assembly makes, so an embedder that needs its own TLS roots or that must refuse a redirect to plain HTTP can set it once here.

Source

pub fn from_url( control_url: &str, output_path: &Path, ) -> Result<Self, AssemblyError>

Fetch the control file at control_url and assemble into output_path.

Relative URLs in the control file resolve against control_url’s directory.

§Errors

Returns AssemblyError::Http when the control file cannot be fetched or does not parse.

Source

pub fn from_url_with_client( control_url: &str, output_path: &Path, http: HttpClient, ) -> Result<Self, AssemblyError>

Fetch the control file and assemble, both using http.

The same client serves the control-file fetch and every subsequent range request, so one policy covers the whole transfer.

Source

pub fn set_range_gap_threshold(&mut self, threshold: u64)

Sets how many known bytes may separate two needed ranges before they are fetched with separate requests.

Ranges closer than this are fetched as one, downloading the gap again to save a round trip. The default is DEFAULT_RANGE_GAP_THRESHOLD.

Source

pub fn set_abort_flag(&mut self, flag: Arc<AtomicBool>)

Sets a flag polled during the scan and download loops.

Once the caller raises it, the running operation stops at the next chunk boundary and returns AssemblyError::Aborted, so a long transfer can be cancelled without waiting for it to finish.

Source

pub fn set_progress_callback<F>(&mut self, callback: F)
where F: Fn(u64, u64) + Send + Sync + 'static,

Sets a callback to run after each block is downloaded and verified.

It receives the values Self::progress returns at that point, and runs on the thread calling Self::download_missing_blocks, so a slow callback slows the download. Blocks found in local files do not trigger it.

Source

pub fn progress(&self) -> (u64, u64)

Bytes of the target known so far, and the target’s length.

The count is known blocks times the block size, capped at the length.

Source

pub fn is_complete(&self) -> bool

Whether every block of the target is known.

Source

pub fn blocksize(&self) -> usize

The block size the control file declares.

Progress consumers need it to convert between the block counts that Self::block_stats reports and the byte counts in Self::progress.

Source

pub fn block_stats(&self) -> (usize, usize)

Blocks known so far, and the target’s total block count.

Source

pub fn submit_source_file( &mut self, path: &Path, ) -> Result<usize, AssemblyError>

Scans the file at path for blocks of the target, at any offset, and copies each one found into the temporary file.

Returns the number of blocks found. A file shorter than one block, or two when HashLengths::seq_matches is 2, cannot contain a match and returns 0.

§Errors

Returns AssemblyError::Io when reading the source or writing the temporary file fails, and AssemblyError::Aborted when the abort flag is raised.

Source

pub fn submit_self_referential(&mut self) -> Result<usize, AssemblyError>

Scans the temporary file itself for further blocks of the target.

A target that repeats a block, such as a run of zeros, needs it only once to fill every copy. Run this after the source files, when the temporary file holds what they supplied. Returns the number of blocks found, which is 0 when nothing has been written yet.

§Errors

Returns AssemblyError::Io when reading or writing the temporary file fails, and AssemblyError::Aborted when the abort flag is raised.

Source

pub fn download_missing_blocks(&mut self) -> Result<usize, AssemblyError>

Downloads the blocks still missing and writes those that verify.

Blocks are fetched from the control file’s first URL, with nearby ranges merged as Self::set_range_gap_threshold allows. Each block is checked against its checksum before it is written. One that fails stays missing, so a server sending wrong data cannot corrupt the output.

Returns the number of blocks written. Call it until it returns 0, then check Self::is_complete: 0 means nothing was missing or nothing that arrived verified, and looping on Self::is_complete alone never ends against a server that sends the wrong bytes.

§Errors

Returns AssemblyError::NoUrls when the control file lists no URL, AssemblyError::Http or AssemblyError::Io when a request or a write fails, and AssemblyError::Aborted when the abort flag is raised.

Source

pub fn complete(self) -> Result<(), AssemblyError>

Verifies the assembled file and moves it to the output path.

The temporary file is cut to the target’s length, its SHA-1 is compared with the control file’s when the control file has one, and it is renamed to the output path. On any error the assembly is dropped, which removes the temporary file.

§Errors

Returns AssemblyError::Control when blocks are still missing, AssemblyError::ChecksumMismatch when the digests differ, and AssemblyError::Io when reading, truncating or renaming fails.

Source

pub fn abort(self)

Discards the assembly and its partial output.

Equivalent to dropping it; both remove the temp file.

Trait Implementations§

Source§

impl Drop for ZsyncAssembly

Source§

fn drop(&mut self)

Removes the temp file unless ZsyncAssembly::complete already renamed it into place.

Every failure path would otherwise leave a partial .zsync-tmp beside the output. Nothing resumes from it: a later run builds a fresh matcher that cannot tell which of its blocks are already written.

Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.