Crate librsync

source ·
Expand description

librsync bindings for Rust.

This library contains bindings to librsync 1, encapsulating the algorithms of the rsync protocol, which computes differences between files efficiently.

The rsync protocol, when computes differences, does not require the presence of both files. It needs instead the new file and a set of checksums of the first file (the signature). Computed differences can be stored in a delta file. The rsync protocol is then able to reproduce the new file, by having the old one and the delta.

Overview of types and modules

This crate provides the streaming operations to produce signatures, delta and patches in the top-level module with Signature, Delta and Patch structs. Those structs take some input stream (Read or Read + Seek traits) and implement another stream (Read trait) from which the output can be read.

Higher level operations are provided within the whole submodule. If the application does not need fine-grained control over IO operations, signature, delta and patch functions can be used. Those functions apply the results to an output stream (implementing the Write trait) in a single call.

Example: streams

This example shows how to go trough the streaming APIs, starting from an input string and a modified string which act as old and new files. The example simulates a real world scenario, in which the signature of a base file is computed, used as input to compute differencies between the base file and the new one, and finally the new file is reconstructed, by using the base file and the delta.

use std::io::prelude::*;
use std::io::Cursor;
use librsync::{Delta, Patch, Signature};

let base = "base file".as_bytes();
let new = "modified base file".as_bytes();

// create signature starting from base file
let mut sig = Signature::new(base).unwrap();
// create delta from new file and the base signature
let delta = Delta::new(new, &mut sig).unwrap();
// create and store the new file from the base one and the delta
let mut patch = Patch::new(Cursor::new(base), delta).unwrap();
let mut computed_new = Vec::new();
patch.read_to_end(&mut computed_new).unwrap();

// test whether the computed file is exactly the new file, as expected
assert_eq!(computed_new, new);

Note that intermediate results are not stored in temporary containers. This is possible because the operations implement the Read trait. In this way the results does not need to be fully in memory, during computation.

Example: whole file API

This example shows how to go trough the whole file APIs, starting from an input string and a modified string which act as old and new files. Unlike the streaming example, here we call a single function, to get the computation result of signature, delta and patch operations. This is convenient when an output stream (like a network socket or a file) is used as output for an operation.

use std::io::Cursor;
use librsync::whole::*;

let base = "base file".as_bytes();
let new = "modified base file".as_bytes();

// signature
let mut sig = Vec::new();
signature(&mut Cursor::new(base), &mut sig).unwrap();

// delta
let mut dlt = Vec::new();
delta(&mut Cursor::new(new), &mut Cursor::new(sig), &mut dlt).unwrap();

// patch
let mut out = Vec::new();
patch(&mut Cursor::new(base), &mut Cursor::new(dlt), &mut out).unwrap();

assert_eq!(out, new);


  • Process whole files in a single call.


  • A struct to generate a delta between two files.
  • A struct to apply a delta to a basis file, to recreate the new file.
  • A struct to generate a signature.


Type Definitions

  • A Result type alias for this crate’s Error type.