Skip to main content

Bspatch

Struct Bspatch 

Source
pub struct Bspatch<'p> { /* private fields */ }
Expand description

Fast and memory saving patcher compatible with bspatch.

Apply patch with a 4k copy buffer and a 1k-4k delta cache buffer:

use std::io;
use qbsdiff::Bspatch;

fn bspatch(source: &[u8], patch: &[u8]) -> io::Result<Vec<u8>> {
    let mut target = Vec::new();
    Bspatch::new(patch)?
        .buffer_size(4096)
        .delta_min(1024)
        .apply(source, io::Cursor::new(&mut target))?;
    Ok(target)
}

Preallocate target file before applying patch:

use std::io;
use std::fs::File;
use std::path::Path;
use qbsdiff::Bspatch;

fn file_allocate(file: &mut File, size: u64) -> io::Result<()> {
    unimplemented!()
}

fn bspatch<P: AsRef<Path>>(source: &[u8], target: P, patch: &[u8]) -> io::Result<u64> {
    let patcher = Bspatch::new(patch)?;
    let mut target_file = File::create(target)?;
    file_allocate(&mut target_file, patcher.hint_target_size())?;
    patcher.apply(source, &mut target_file)
}

Implementations§

Source§

impl<'p> Bspatch<'p>

Source

pub fn new(patch: &'p [u8]) -> Result<Self>

Parse the patch file and create new patcher configuration.

Return error if failed to parse the patch header.

Source

pub fn buffer_size(self, bs: usize) -> Self

Set the main copy buffer size, (bs > 128, default is BUFFER_SIZE).

This is also the write buffer to target stream. A relative big buffer (usually 128k) will speed up writing process if the target stream is unbuffered (e.g. std::fs::File).

Source

pub fn delta_min(self, dm: usize) -> Self

Sets the initial delta cache size, (dm > 128, default is DELTA_MIN).

The delta cache is dynamic and can grow up when needed (but keeps not greater than the size of main copy buffer).

This might be deprecated in later version.

Source

pub fn hint_target_size(&self) -> u64

Hint the final target file size.

Source

pub fn apply<T: Write>(self, source: &[u8], target: T) -> Result<u64>

Apply patch to the source data and output the stream of target.

Parameter source is designed to be a low-level &[u8] binary, rather than a Seek + Read random accessing data. See #8, allowing random accessible source file might be misleading, which could result in unwanted performance loss due to heavy syscall overheads of seek + read (by the way, pread/read_at is not cross-platform).

For those who in search of loading the source file lazily, simply memmap source file to memory is recommended.

The target data size would be returned if no error occurs.

Auto Trait Implementations§

§

impl<'p> Freeze for Bspatch<'p>

§

impl<'p> RefUnwindSafe for Bspatch<'p>

§

impl<'p> Send for Bspatch<'p>

§

impl<'p> Sync for Bspatch<'p>

§

impl<'p> Unpin for Bspatch<'p>

§

impl<'p> UnsafeUnpin for Bspatch<'p>

§

impl<'p> UnwindSafe for Bspatch<'p>

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.