Skip to main content

Remote

Struct Remote 

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

Reads parts of one tree straight from a peer, with nothing kept.

It starts from a root that the caller trusts. Each thing it hands back was shown by the peer and checked against that root: an entry by its place in its folder, and bytes by their place in their file.

Implementations§

Source§

impl<'p> Remote<'p>

Source

pub fn open(peer: &'p mut dyn Peer, root: &Root) -> Result<Self>

Asks the peer for the record of root, and checks it.

Examples found in repository?
examples/roundtrip.rs (line 261)
220fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
221    let mut arguments = std::env::args_os().skip(1);
222    let (Some(folder), Some(work)) = (arguments.next(), arguments.next()) else {
223        eprintln!("usage: roundtrip <folder> <work-dir>");
224        std::process::exit(2);
225    };
226    let work = PathBuf::from(work);
227    fs::create_dir(&work)?;
228
229    let source = DirStore::new(work.join("source-store"));
230    let started = Instant::now();
231    let root = build(&source, &folder, &BuildOptions::new())?;
232    let record = record_of(&source, &root)?;
233    let (size, totals) = (record.top().size, record.totals());
234    report("built", started, size);
235    println!("root         {root}");
236    println!(
237        "the tree     {size} bytes; kept once: {} chunks of {} bytes, and {} bytes of lists",
238        totals.chunks, totals.chunk_bytes, totals.list_bytes
239    );
240
241    let started = Instant::now();
242    let summary = verify(&source, &root)?;
243    report("checked", started, totals.chunk_bytes);
244    println!(
245        "             {} different files in {} different folders",
246        summary.files, summary.folders
247    );
248
249    let fetched = DirStore::new(work.join("fetched-store"));
250    let started = Instant::now();
251    fetch(&fetched, &root, &mut Responder::new(&source))?;
252    report("fetched", started, totals.chunk_bytes);
253
254    if let Some(file) = largest_file(&source, record.top())? {
255        // A stretch of 4 KiB is read from 64 places spread over the file, each
256        // asked for on its own, and the time one takes on average is printed:
257        // a single read is shorter than some systems' clocks can tell apart.
258        let length = file.size.min(4096);
259        let places: u64 = 64;
260        let mut holder = Responder::new(&source);
261        let mut remote = Remote::open(&mut holder, &root)?;
262        let mut stretch = Vec::new();
263        let started = Instant::now();
264        for place in 0..places {
265            stretch.clear();
266            let offset = (file.size - length) * place / (places - 1);
267            remote.read(&file, offset, length, &mut stretch)?;
268        }
269        println!(
270            "a stretch    {:>8.4} s  {} bytes at a time, from {places} places in a file of {} bytes",
271            started.elapsed().as_secs_f64() / places as f64,
272            stretch.len(),
273            file.size
274        );
275    }
276
277    let written = work.join("written");
278    let started = Instant::now();
279    write_to_disk(&fetched, &root, &written)?;
280    report("written out", started, size);
281
282    let again = DirStore::new(work.join("again-store"));
283    let started = Instant::now();
284    let rebuilt = build(&again, &written, &BuildOptions::new())?;
285    report("built again", started, size);
286    if rebuilt != root {
287        return Err(format!("the copy has the root {rebuilt}, and not {root}").into());
288    }
289    println!("the copy has the same root");
290    Ok(())
291}
Source

pub fn root(&self) -> &Root

Source

pub fn record(&self) -> &Record

Source

pub fn entry(&mut self, folder: &Target, name: &str) -> Result<Option<Entry>>

What is under name in folder, or nothing when the folder has no such name. Either answer is proven by the one entry the peer shows.

Source

pub fn lookup(&mut self, path: &str) -> Result<Option<Entry>>

What is at path in the tree: names with / between them. One question is asked for each name on the way.

Source

pub fn entries( &mut self, folder: &Target, start: u64, count: u32, ) -> Result<Entries>

Up to count entries of a folder, from place start. Fewer come back when the folder ends first, or when the peer gives fewer at a time. An empty folder gives none.

Source

pub fn read( &mut self, file: &Target, offset: u64, length: u64, out: &mut dyn Write, ) -> Result<()>

Writes length bytes of a file, from byte offset, to out.

The peer shows the branch of the file’s list that holds the stretch, and that proves each chunk’s number and the byte it starts at. Then the chunks are asked for by their numbers, and each is checked against the list before any of its bytes is written. Nothing else of the file is taken.

Examples found in repository?
examples/roundtrip.rs (line 267)
220fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
221    let mut arguments = std::env::args_os().skip(1);
222    let (Some(folder), Some(work)) = (arguments.next(), arguments.next()) else {
223        eprintln!("usage: roundtrip <folder> <work-dir>");
224        std::process::exit(2);
225    };
226    let work = PathBuf::from(work);
227    fs::create_dir(&work)?;
228
229    let source = DirStore::new(work.join("source-store"));
230    let started = Instant::now();
231    let root = build(&source, &folder, &BuildOptions::new())?;
232    let record = record_of(&source, &root)?;
233    let (size, totals) = (record.top().size, record.totals());
234    report("built", started, size);
235    println!("root         {root}");
236    println!(
237        "the tree     {size} bytes; kept once: {} chunks of {} bytes, and {} bytes of lists",
238        totals.chunks, totals.chunk_bytes, totals.list_bytes
239    );
240
241    let started = Instant::now();
242    let summary = verify(&source, &root)?;
243    report("checked", started, totals.chunk_bytes);
244    println!(
245        "             {} different files in {} different folders",
246        summary.files, summary.folders
247    );
248
249    let fetched = DirStore::new(work.join("fetched-store"));
250    let started = Instant::now();
251    fetch(&fetched, &root, &mut Responder::new(&source))?;
252    report("fetched", started, totals.chunk_bytes);
253
254    if let Some(file) = largest_file(&source, record.top())? {
255        // A stretch of 4 KiB is read from 64 places spread over the file, each
256        // asked for on its own, and the time one takes on average is printed:
257        // a single read is shorter than some systems' clocks can tell apart.
258        let length = file.size.min(4096);
259        let places: u64 = 64;
260        let mut holder = Responder::new(&source);
261        let mut remote = Remote::open(&mut holder, &root)?;
262        let mut stretch = Vec::new();
263        let started = Instant::now();
264        for place in 0..places {
265            stretch.clear();
266            let offset = (file.size - length) * place / (places - 1);
267            remote.read(&file, offset, length, &mut stretch)?;
268        }
269        println!(
270            "a stretch    {:>8.4} s  {} bytes at a time, from {places} places in a file of {} bytes",
271            started.elapsed().as_secs_f64() / places as f64,
272            stretch.len(),
273            file.size
274        );
275    }
276
277    let written = work.join("written");
278    let started = Instant::now();
279    write_to_disk(&fetched, &root, &written)?;
280    report("written out", started, size);
281
282    let again = DirStore::new(work.join("again-store"));
283    let started = Instant::now();
284    let rebuilt = build(&again, &written, &BuildOptions::new())?;
285    report("built again", started, size);
286    if rebuilt != root {
287        return Err(format!("the copy has the root {rebuilt}, and not {root}").into());
288    }
289    println!("the copy has the same root");
290    Ok(())
291}
Source

pub fn have(&mut self, file: &Target) -> Result<HaveMap>

Which chunks of a file the peer says it holds, one bit a chunk. This is what the peer says about itself: nothing proves it, and a chunk it claims is still checked when it comes. Only the number of chunks is held to what the file’s size allows.

Auto Trait Implementations§

§

impl<'p> !RefUnwindSafe for Remote<'p>

§

impl<'p> !Send for Remote<'p>

§

impl<'p> !Sync for Remote<'p>

§

impl<'p> !UnwindSafe for Remote<'p>

§

impl<'p> Forget for Remote<'p>

§

impl<'p> Freeze for Remote<'p>

§

impl<'p> Move for Remote<'p>

§

impl<'p> Unpin for Remote<'p>

§

impl<'p> UnsafeUnpin for Remote<'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> Same for T

Source§

type Output = T

Should always be Self
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.