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>
impl<'p> Remote<'p>
Sourcepub fn open(peer: &'p mut dyn Peer, root: &Root) -> Result<Self>
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?
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}pub fn root(&self) -> &Root
pub fn record(&self) -> &Record
Sourcepub fn entry(&mut self, folder: &Target, name: &str) -> Result<Option<Entry>>
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.
Sourcepub fn lookup(&mut self, path: &str) -> Result<Option<Entry>>
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.
Sourcepub fn entries(
&mut self,
folder: &Target,
start: u64,
count: u32,
) -> Result<Entries>
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.
Sourcepub fn read(
&mut self,
file: &Target,
offset: u64,
length: u64,
out: &mut dyn Write,
) -> Result<()>
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?
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}Sourcepub fn have(&mut self, file: &Target) -> Result<HaveMap>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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