4个版本

使用旧的Rust 2015

0.2.3 2018年3月6日
0.2.2 2018年3月5日
0.2.1 2018年3月5日
0.2.0 2018年3月5日

#21 in #rsync

Apache-2.0/MIT

30KB
663 代码行

一个类似rsync协议的实现(与rsync不兼容),纯Rust编写。

extern crate rand;
extern crate rustsync;
use rustsync::*;
use rand::Rng;
fn main() {
  // Create 4 different random strings first.
  let chunk_size = 1000;
  let a = rand::thread_rng()
          .gen_ascii_chars()
          .take(chunk_size)
          .collect::<String>();
  let b = rand::thread_rng()
          .gen_ascii_chars()
          .take(50)
          .collect::<String>();
  let b_ = rand::thread_rng()
          .gen_ascii_chars()
          .take(100)
          .collect::<String>();
  let c = rand::thread_rng()
          .gen_ascii_chars()
          .take(chunk_size)
          .collect::<String>();

  // Now concatenate them in two different ways.

  let mut source = a.clone() + &b + &c;
  let mut modified = a + &b_ + &c;

  // Suppose we want to download `modified`, and we already have
  // `source`, which only differs by a few characters in the
  // middle.

  // We first have to choose a block size, which will be recorded
  // in the signature below. Blocks should normally be much bigger
  // than this in order to be efficient on large files.

  let block = [0; 32];

  // We then create a signature of `source`, to be uploaded to the
  // remote machine. Signatures are typically much smaller than
  // files, with just a few bytes per block.

  let source_sig = signature(source.as_bytes(), block).unwrap();

  // Then, we let the server compare our signature with their
  // version.

  let comp = compare(&source_sig, modified.as_bytes(), block).unwrap();

  // We finally download the result of that comparison, and
  // restore their file from that.

  let mut restored = Vec::new();
  restore_seek(&mut restored, std::io::Cursor::new(source.as_bytes()), vec![0; 1000], &comp).unwrap();
  assert_eq!(&restored[..], modified.as_bytes())
}

依赖项

~1–1.7MB
~36K SLoC