Skip to main content

ControlFile

Struct ControlFile 

Source
pub struct ControlFile {
    pub version: String,
    pub filename: Option<String>,
    pub mtime: Option<String>,
    pub blocksize: usize,
    pub length: u64,
    pub hash_lengths: HashLengths,
    pub urls: Vec<String>,
    pub sha1: Option<String>,
    pub block_checksums: Vec<BlockChecksum>,
}
Expand description

A parsed control file: what the target is, where to fetch it, and the checksum of every block.

Fields§

§version: String

The zsync header, naming the version of the tool that wrote the file, or empty when the header is absent.

§filename: Option<String>

The suggested name for the target file.

§mtime: Option<String>

The target’s modification time, verbatim from the MTime header.

§blocksize: usize

The block size in bytes, always a power of two.

§length: u64

The target’s length in bytes.

§hash_lengths: HashLengths

How much of each block checksum the file stores.

§urls: Vec<String>

Where the target can be fetched, in the order given. A relative URL resolves against the control file’s own location.

§sha1: Option<String>

The target’s SHA-1 as 40 hex digits, verbatim from the SHA-1 header.

§block_checksums: Vec<BlockChecksum>

One entry per block of the target, in order.

Implementations§

Source§

impl ControlFile

Source

pub fn parse<R: Read>(reader: R) -> Result<Self, ParseError>

Parse a control file.

The header section is read through a byte cap. It arrives from the network, and both a single unterminated line and an endless run of URL: lines would otherwise grow without bound, letting the origin choose how much memory the client spends before any of it is validated. Unknown headers are skipped.

§Errors

Returns a ParseError naming the first problem found in the header, or ParseError::Io when reading fails or the block records end before every block has one.

Source

pub fn num_blocks(&self) -> usize

The number of blocks in the target.

Source

pub fn generate<R: Read + Seek>( reader: &mut R, filename: &str, url: &str, blocksize: Option<usize>, ) -> Result<Self, GenerateError>

Generate a control file by scanning an input file.

Every block is checksummed, with the last one padded with zeros to a full block, and the whole input is hashed with SHA-1. A blocksize of None picks 2048 bytes for inputs under 100,000,000 bytes and 4096 otherwise. A given size must be a power of two, or the result will not parse. The hash lengths are derived from the file and block sizes with the formulas zsyncmake uses, url becomes the only URL, and the version is written as 0.6.2.

§Errors

Returns GenerateError::EmptyFile for an empty input and GenerateError::Io when reading or seeking fails.

Source

pub fn write<W: Write>(&self, writer: &mut W) -> Result<(), WriteError>

Write the control file to a writer.

The block records are written at the lengths Self::hash_lengths gives, so a file written here parses back to the same checksums.

§Errors

Returns WriteError::Io when the writer fails.

Trait Implementations§

Source§

impl Clone for ControlFile

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ControlFile

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.