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
impl ZsyncAssembly
Sourcepub fn new(
control: ControlFile,
output_path: &Path,
) -> Result<Self, AssemblyError>
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.
Sourcepub fn with_base_url(
control: ControlFile,
output_path: &Path,
base_url: Option<&str>,
) -> Result<Self, AssemblyError>
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 /.
Sourcepub fn with_client(
control: ControlFile,
output_path: &Path,
base_url: Option<&str>,
http: HttpClient,
) -> Result<Self, AssemblyError>
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.
Sourcepub fn from_url(
control_url: &str,
output_path: &Path,
) -> Result<Self, AssemblyError>
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.
Sourcepub fn from_url_with_client(
control_url: &str,
output_path: &Path,
http: HttpClient,
) -> Result<Self, AssemblyError>
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.
Sourcepub fn set_range_gap_threshold(&mut self, threshold: u64)
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.
Sourcepub fn set_abort_flag(&mut self, flag: Arc<AtomicBool>)
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.
Sourcepub fn set_progress_callback<F>(&mut self, callback: F)
pub fn set_progress_callback<F>(&mut self, callback: F)
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.
Sourcepub fn progress(&self) -> (u64, u64)
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.
Sourcepub fn is_complete(&self) -> bool
pub fn is_complete(&self) -> bool
Whether every block of the target is known.
Sourcepub fn blocksize(&self) -> usize
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.
Sourcepub fn block_stats(&self) -> (usize, usize)
pub fn block_stats(&self) -> (usize, usize)
Blocks known so far, and the target’s total block count.
Sourcepub fn submit_source_file(
&mut self,
path: &Path,
) -> Result<usize, AssemblyError>
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.
Sourcepub fn submit_self_referential(&mut self) -> Result<usize, AssemblyError>
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.
Sourcepub fn download_missing_blocks(&mut self) -> Result<usize, AssemblyError>
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.
Sourcepub fn complete(self) -> Result<(), AssemblyError>
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.
Trait Implementations§
Source§impl Drop for ZsyncAssembly
impl Drop for ZsyncAssembly
Source§fn drop(&mut self)
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.