1//! Live Share: a notebook one Snowbound holds, opened on others through a short code. The
2//! host serves its notebook's storage verbs (`session::Storage`) and batches guests' ops into
3//! guarded publications. A guest runs the same replica and durable queue as on an SMB share;
4//! protected sections use transactions. A guest first meets the host in the code's room,
5//! where approval grants a device's own access credential. Presence has a separate room
6//! whose key changes when a device is removed. Large bodies travel a chunk at a time, each answered before
7//! the next, so a relay never holds much for a slow peer.
8
9use super::{
10 Event, Hello, Line, Live, Peer, Presence, Reach, Relayed, Room, Sender,
11 wire::{self, Delta, Failure, Reply, Request, Touched, Welcome, WireEntry, Written, kind},
12};
13use crate::{Error, Result, background::Reports, discover, session::Storage};
14use onestore::{CommitError, CommitState, RevisionIndex, Stamp, Store, Transaction};
15use sha2::{Digest, Sha256};
16use std::{
17 collections::{BTreeMap, HashMap},
18 io,
19 path::PathBuf,
20 sync::{
21 Arc, Condvar, Mutex, Weak,
22 atomic::{AtomicBool, AtomicU64, Ordering},
23 mpsc,
24 },
25 thread,
26 time::Duration,
27};
28use web_time::Instant;
29
30#[path = "share/batch.rs"]
31mod batch;
32#[path = "share/membership.rs"]
33mod membership;
34#[cfg(target_arch = "wasm32")]
35#[path = "share/web.rs"]
36mod web;
37pub use membership::Host;
38
39/// The most bytes one message of a read or an upload carries.
40const CHUNK: usize = 128 << 10;
41/// The most bytes of chunks a guest has asked for and not yet been given.
42const WINDOW: usize = 512 << 10;
43/// How long a request waits for its reply.
44const TIMEOUT: Duration = Duration::from_secs(60);
45use crate::MAX_FILE_BYTES as LIMIT;
46/// Snapshots of files being read, per guest, and how long one is kept unread.
47const SNAPSHOTS: usize = 8;
48const SNAPSHOT_AGE: Duration = Duration::from_secs(120);
49/// Sections whose image a host keeps to check guests' commits on and tell them what changed,
50/// and a guest keeps to take those changes on without reading.
51const IMAGES: usize = 4;
52/// Earlier images of those a host keeps besides, to tell a guest that missed a change what
53/// it was, and the most bytes they hold.
54const VERSIONS: usize = 16;
55const VERSIONS_BYTES: usize = 64 << 20;
56/// How long a report of a change settles in a guest's background: its host reports each
57/// commit once, at once.
58pub const SETTLE: Duration = Duration::from_millis(20);
59/// The longest a relay may ask a guest joining to wait before it is told so.
60const PATIENT: Duration = Duration::from_secs(10);
61/// What a host says leaving as it stops sharing.
62const STOPPED: &str = "stopped";
63/// What a host says hanging up on a guest that asks too much too fast.
64const FLOODED: &str = "flooded";
65/// A guest's requests a host holds at once, waiting and in hand, past which it hangs up.
66const QUEUED: usize = 32;
67/// The threads working through each guest's requests.
68const WORKERS: usize = 2;
69/// Requests a guest may start each second, and at once: every request but a read's later
70/// chunks and an upload's, which come from memory or go to it.
71const STARTS: f64 = 100.0;
72const BURST: f64 = 200.0;
73
74/// A code as typed, `7kq 4mz 9xr`, as it is shown, `7KQ-4MZ-9XR`; none where it is not a
75/// code (`super::code`).
76pub fn code(typed: &str) -> Option<String> {
77 let (number, secret) = super::code::parse(typed)?;
78 super::code::format(number, &secret)
79}
80
81/// What a host keeps of a share to take it up again after a relaunch.
82#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
83pub struct Sharing {
84 pub share: [u8; 16],
85 /// The presence key, rotated when a device is removed.
86 pub secret: [u8; 16],
87 /// The code, or its secret alone until it has a number (`super::code`).
88 pub code: String,
89 pub password: String,
90 #[serde(default)]
91 pub approve: bool,
92 #[serde(default)]
93 pub members: Vec<Device>,
94}
95
96#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
97pub struct Device {
98 pub secret: [u8; 16],
99 pub name: String,
100 pub device: Option<String>,
101 /// The device's last commit to each section, by path, the only one it may send again.
102 #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
103 pub landed: BTreeMap<String, Landed>,
104}
105
106/// A commit a device landed, as it would send it again: a batch's revisions, which OneNote's
107/// Optimize may drop from the section once later ones supersede them, or a whole transaction's
108/// stamp after it, as `digest` hashes it.
109#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
110pub enum Landed {
111 Batch(BTreeMap<onestore::ExGuid, onestore::ExGuid>),
112 Commit(u64),
113}
114
115impl Sharing {
116 /// A new share: its own id, room secret and code.
117 pub fn new(password: &str) -> io::Result<Self> {
118 let mut random = [0; 32];
119 getrandom::fill(&mut random)
120 .map_err(|_| io::Error::other("System random source failed"))?;
121 Ok(Self {
122 share: random[..16].try_into().expect("16 bytes"),
123 secret: random[16..].try_into().expect("16 bytes"),
124 code: super::code::secret()?,
125 password: password.to_owned(),
126 approve: false,
127 members: Vec::new(),
128 })
129 }
130}
131
132/// Where a guest keeps a share's notebook: `live://` and the share's id.
133pub fn location(share: &[u8; 16]) -> String {
134 format!("live://{}", super::hex(share))
135}
136
137/// Why joining failed.
138#[derive(Clone, Debug, PartialEq, Eq)]
139pub enum Refusal {
140 /// Not a code, or one mistyped, which spends none of the relay's tries.
141 Malformed,
142 /// The person sharing runs a Snowbound of another Live Share version: the newer one's
143 /// `true` where it is theirs, so this one should update.
144 Version {
145 theirs_newer: bool,
146 },
147 /// The code's secret or password is wrong.
148 Wrong,
149 /// No one shares with the code's number now.
150 NoOne,
151 /// The code had too many wrong tries.
152 Expired,
153 /// Too many wrong codes from this network; try again after the wait, where known.
154 TooMany(Option<Duration>),
155 /// The relay is full.
156 Busy,
157 /// The relay couldn't be reached, and why, and no one answered on this network.
158 Unreachable(super::Trouble),
159 /// The relay let this end in, but no one answered.
160 TimedOut,
161 Declined,
162 Cancelled,
163 NotAdmitted,
164}
165
166/// Meets the host sharing `code` (and `password`) as `me`, on the networks `reach` names and
167/// through `relay`: the share it welcomes this end to.
168pub fn join(
169 me: Hello,
170 code: &str,
171 password: &str,
172 reach: Option<Reach>,
173 relay: Option<&str>,
174) -> std::result::Result<Welcome, Refusal> {
175 join_while(me, code, password, reach, relay, |_| true)
176}
177
178/// Joins while `waiting` returns true, reporting whether the host is deciding approval.
179pub fn join_while(
180 me: Hello,
181 code: &str,
182 password: &str,
183 reach: Option<Reach>,
184 relay: Option<&str>,
185 continue_joining: impl Fn(bool) -> bool,
186) -> std::result::Result<Welcome, Refusal> {
187 crate::task::ready(join_while_async(
188 me,
189 code,
190 password,
191 reach,
192 relay,
193 continue_joining,
194 ))
195 .map_err(|_| Refusal::Busy)?
196}
197
198pub async fn join_while_async(
199 me: Hello,
200 code: &str,
201 password: &str,
202 reach: Option<Reach>,
203 relay: Option<&str>,
204 continue_joining: impl Fn(bool) -> bool,
205) -> std::result::Result<Welcome, Refusal> {
206 let code = self::code(code).ok_or(Refusal::Malformed)?;
207 let (welcomed, welcome) = mpsc::channel();
208 let approving = Arc::new(AtomicBool::new(false));
209 let approval = Arc::clone(&approving);
210 let (changed, waiting) = crate::task::channel();
211 let live = Live::start(
212 me,
213 &Room::join(&code, password),
214 reach,
215 relay,
216 move |event| match event {
217 Event::Frame {
218 kind: kind::WELCOME,
219 body,
220 ..
221 } => {
222 if let Ok(body) = minicbor::decode::<Welcome>(body) {
223 let _ = welcomed.send(Ok(body));
224 }
225 }
226 Event::Frame {
227 kind: kind::APPROVAL,
228 body,
229 ..
230 } => {
231 if let Ok(body) = minicbor::decode::<wire::Approval>(body) {
232 match body {
233 wire::Approval::Pending => approval.store(true, Ordering::Release),
234 wire::Approval::Declined => {
235 let _ = welcomed.send(Err(Refusal::Declined));
236 }
237 wire::Approval::Failed => {
238 let _ = welcomed.send(Err(Refusal::NotAdmitted));
239 }
240 }
241 }
242 }
243 _ => {
244 let _ = changed.try_send(());
245 }
246 },
247 )
248 .map_err(|_| Refusal::Unreachable(super::Trouble::Other))?;
249 let start = Instant::now();
250 loop {
251 if !continue_joining(approving.load(Ordering::Acquire)) {
252 return Err(Refusal::Cancelled);
253 }
254 if let Ok(welcome) = welcome.try_recv() {
255 return welcome;
256 }
257 if let Some(version) = live.other_version() {
258 return Err(Refusal::Version {
259 theirs_newer: version > wire::VERSION,
260 });
261 }
262 if live.failed() > 0 {
263 return Err(Refusal::Wrong);
264 }
265 // A peer on this network may yet answer what the relay refused.
266 let waited = start.elapsed();
267 let settled = waited > Duration::from_secs(3) || reach.is_none();
268 match live.relayed() {
269 Relayed::Refused(404, _) if settled => return Err(Refusal::NoOne),
270 Relayed::Refused(410, _) => return Err(Refusal::Expired),
271 // A short wait, as a crowd joining at once meets, passes as the relay is asked
272 // again.
273 Relayed::Refused(429, wait) if wait.is_none_or(|wait| wait > PATIENT) => {
274 return Err(Refusal::TooMany(wait));
275 }
276 Relayed::Refused(503, _) if settled => return Err(Refusal::Busy),
277 Relayed::Unreachable(trouble) if waited > Duration::from_secs(10) => {
278 return Err(Refusal::Unreachable(trouble));
279 }
280 Relayed::Unknown if relay.is_none() && waited > Duration::from_secs(10) => {
281 return Err(Refusal::NoOne);
282 }
283 _ if waited
284 > Duration::from_secs(if approving.load(Ordering::Acquire) {
285 300
286 } else {
287 20
288 }) =>
289 {
290 return Err(Refusal::TimedOut);
291 }
292 _ => {}
293 }
294 crate::task::wait(&waiting, Some(Duration::from_millis(100))).await;
295 }
296}
297
298/// A host's side of its guests' storage requests.
299struct Served {
300 storage: Box<dyn Storage>,
301 /// Sections' images by path, with their stamps, to check commits on.
302 images: Mutex<Vec<Image>>,
303 /// Files being read a chunk at a time, by guest and the read's first request.
304 snapshots: Mutex<ByGuest<(Arc<Vec<u8>>, Instant)>>,
305 /// Bytes a later request carries, by guest and upload.
306 puts: Mutex<ByGuest<Vec<u8>>>,
307 guests: Mutex<BTreeMap<[u8; 16], Admitted>>,
308 writers: Mutex<HashMap<String, mpsc::SyncSender<batch::Waiting>>>,
309 /// The share, which keeps what each device last landed.
310 members: Weak<membership::Members>,
311 /// Hears the paths guests changed, as the host's own notebook should.
312 host: Mutex<Option<crate::session::Listener>>,
313 /// The share's room, to tell guests what changed.
314 room: Mutex<Option<Sender>>,
315}
316
317/// A guest as its host serves it: the line to it, its requests waiting for its workers, how
318/// many more it may start now, and the sections it read lately, newest first, as it keeps
319/// their images.
320struct Admitted {
321 hello: Arc<Hello>,
322 /// The device's access secret.
323 device: [u8; 16],
324 line: Line,
325 queue: mpsc::SyncSender<(u16, Vec<u8>)>,
326 starts: f64,
327 counted: Instant,
328 held: Vec<String>,
329}
330
331/// A section's path, stamp and image.
332type Image = (String, Stamp, Arc<Vec<u8>>);
333/// What a guest's requests left, by guest and request.
334type ByGuest<T> = HashMap<([u8; 16], u64), T>;
335
336/// Whether a guest may name `path`: a catalog path inside the notebook, and not presence's
337/// own secret, which guests meet in the share's room instead.
338fn allowed(path: &str) -> bool {
339 path.split('/').all(|part| {
340 !part.is_empty() && part != "." && part != ".." && !part.contains(['\\', '\0', ':'])
341 }) && !path.to_ascii_lowercase().starts_with(".snowbound/live")
342}
343
344/// The folder holding catalog path `path`.
345fn folder(path: &str) -> String {
346 path.rsplit_once('/')
347 .map_or(String::new(), |(folder, _)| folder.to_owned())
348}
349
350fn refused(kind: io::ErrorKind, message: &str) -> Error {
351 io::Error::new(kind, message.to_owned()).into()
352}
353
354impl Served {
355 /// Serves `peer`, of device `device`, on `line`, through workers of its own that end as
356 /// it leaves.
357 fn admit(
358 self: &Arc<Self>,
359 source: &Arc<super::Shared>,
360 hello: &Arc<Hello>,
361 device: [u8; 16],
362 line: &Line,
363 ) -> bool {
364 let peer = hello.peer;
365 let (queue, waiting) = mpsc::sync_channel::<(u16, Vec<u8>)>(QUEUED - WORKERS);
366 let waiting = Arc::new(Mutex::new(waiting));
367 let guest = Admitted {
368 hello: Arc::clone(hello),
369 device,
370 line: line.clone(),
371 queue,
372 starts: BURST,
373 counted: Instant::now(),
374 held: Vec::new(),
375 };
376 let mut guests = self.guests.lock().unwrap();
377 if !source.connected(hello) {
378 drop(guests);
379 drop(guest);
380 return false;
381 }
382 let retired = guests.insert(peer, guest);
383 drop(guests);
384 drop(retired);
385 for _ in 0..WORKERS {
386 let (served, waiting, line) =
387 (Arc::downgrade(self), Arc::clone(&waiting), line.clone());
388 thread::spawn(move || {
389 loop {
390 let next = waiting.lock().unwrap().recv();
391 let (Ok((kind, body)), Some(served)) = (next, served.upgrade()) else {
392 return;
393 };
394 let _ = line.send(kind::REPLY, &served.handle(&peer, kind, &body));
395 }
396 });
397 }
398 true
399 }
400
401 /// Hands a request from `peer` to its workers, or hangs up on a guest that has too many
402 /// waiting or starts them too fast.
403 fn queue(&self, source: &Arc<super::Shared>, hello: &Arc<Hello>, kind: u16, body: &[u8]) {
404 let peer = &hello.peer;
405 let mut guests = self.guests.lock().unwrap();
406 let Some(guest) = guests.get_mut(peer) else {
407 return;
408 };
409 if !Arc::ptr_eq(&guest.hello, hello) || !source.connected(hello) {
410 return;
411 }
412 let continued = kind == kind::PUT
413 || matches!(kind, kind::READ | kind::READ_FILE)
414 && minicbor::decode::<Request>(body).is_ok_and(|request| request.handle.is_some());
415 let now = Instant::now();
416 guest.starts =
417 (guest.starts + now.duration_since(guest.counted).as_secs_f64() * STARTS).min(BURST);
418 guest.counted = now;
419 if !continued {
420 guest.starts -= 1.0;
421 }
422 if guest.starts < 0.0 || guest.queue.try_send((kind, body.to_vec())).is_err() {
423 let guest = guests.remove(peer);
424 drop(guests);
425 if let Some(guest) = guest {
426 guest.line.hang_up(FLOODED);
427 }
428 }
429 }
430
431 fn forget(&self, peer: &[u8; 16], expected: Option<(&Arc<super::Shared>, &Arc<Hello>)>) {
432 let retired = {
433 let mut snapshots = self.snapshots.lock().unwrap();
434 let mut puts = self.puts.lock().unwrap();
435 let mut guests = self.guests.lock().unwrap();
436 if let Some((source, hello)) = expected
437 && (!source.gone(hello)
438 || !guests
439 .get(peer)
440 .is_some_and(|guest| Arc::ptr_eq(&guest.hello, hello)))
441 {
442 return;
443 }
444 let retired = guests.remove(peer);
445 snapshots.retain(|(guest, _), _| guest != peer);
446 puts.retain(|(guest, _), _| guest != peer);
447 retired
448 };
449 drop(retired);
450 }
451
452 /// What the device of guest `peer` last landed on the section at `path`.
453 fn landed(&self, peer: &[u8; 16], path: &str) -> Option<Landed> {
454 let device = self.guests.lock().unwrap().get(peer)?.device;
455 self.members.upgrade()?.landed(&device, path)
456 }
457
458 /// Notes `landed` as what the device of guest `peer` last landed on the section at `path`.
459 fn remember(&self, peer: &[u8; 16], path: &str, landed: Landed) {
460 let device = self
461 .guests
462 .lock()
463 .unwrap()
464 .get(peer)
465 .map(|guest| guest.device);
466 if let (Some(device), Some(members)) = (device, self.members.upgrade()) {
467 members.remember(&device, path, landed);
468 }
469 }
470
471 /// Tells every guest, and the host, that a guest changed the files at `paths`.
472 fn changed(&self, paths: &[String]) {
473 self.tell(paths);
474 if let Some(listener) = &*self.host.lock().unwrap() {
475 listener(paths);
476 }
477 }
478
479 /// Tells every guest the files at `paths` changed, and what they are now.
480 fn tell(&self, paths: &[String]) {
481 let touched = Touched {
482 paths: paths.to_vec(),
483 stamps: (paths.iter())
484 .map(|path| self.storage.stamp(path).ok().map(|stamp| digest(&stamp)))
485 .collect(),
486 };
487 if let Some(room) = self.room.lock().unwrap().as_ref() {
488 room.send(kind::TOUCHED, &touched, None);
489 }
490 }
491
492 /// Notes that `guest` holds the section at `path`, as it now keeps its image.
493 fn holds(guest: &mut Admitted, path: &str) {
494 guest.held.retain(|held| held != path);
495 guest.held.insert(0, path.to_owned());
496 guest.held.truncate(IMAGES);
497 }
498
499 fn handle(self: &Arc<Self>, peer: &[u8; 16], kind: u16, body: &[u8]) -> Reply {
500 let request = match minicbor::decode::<Request>(body) {
501 Ok(request) => request,
502 Err(_) => {
503 return failed(
504 0,
505 refused(io::ErrorKind::InvalidData, "A malformed request"),
506 );
507 }
508 };
509 let id = request.id;
510 if !self.guests.lock().unwrap().contains_key(peer) {
511 return failed(
512 id,
513 refused(
514 io::ErrorKind::PermissionDenied,
515 "The device is no longer connected",
516 ),
517 );
518 }
519 match self.answer(peer, kind, request) {
520 Ok(reply) => Reply { id, ..reply },
521 Err(error) => failed(id, error),
522 }
523 }
524
525 fn answer(self: &Arc<Self>, peer: &[u8; 16], kind: u16, request: Request) -> Result<Reply> {
526 let path = request.path.as_str();
527 if !(path.is_empty() && matches!(kind, kind::LIST | kind::PUT) || allowed(path))
528 || request.to.as_deref().is_some_and(|to| !allowed(to))
529 {
530 return Err(refused(
531 io::ErrorKind::PermissionDenied,
532 "Outside the notebook",
533 ));
534 }
535 let to = || {
536 request
537 .to
538 .as_deref()
539 .ok_or_else(|| refused(io::ErrorKind::InvalidInput, "No target"))
540 };
541 let stamp = || -> Result<Stamp> {
542 Ok(request
543 .stamp
544 .as_ref()
545 .ok_or_else(|| refused(io::ErrorKind::InvalidInput, "No stamp"))?
546 .try_into()?)
547 };
548 let done = Reply::default();
549 Ok(match kind {
550 kind::LIST => Reply {
551 entries: Some(
552 self.storage
553 .entries(path)?
554 .into_iter()
555 .filter(|entry| allowed(&within(path, &entry.name)))
556 .map(|entry| wire_entry(&entry))
557 .collect(),
558 ),
559 ..done
560 },
561 kind::STAMP => Reply {
562 stamp: Some((&self.storage.stamp(path)?).into()),
563 ..done
564 },
565 kind::EXISTS => Reply {
566 exists: Some(self.storage.exists(path)),
567 ..done
568 },
569 kind::READ | kind::READ_FILE => self.read(peer, kind, &request)?,
570 kind::PUT => {
571 let handle = request.handle.unwrap_or_default();
572 let bytes = request.bytes.unwrap_or_default();
573 let mut puts = self.puts.lock().unwrap();
574 let key = (*peer, handle);
575 let length = puts.get(&key).map_or(0, Vec::len);
576 if request.offset != Some(length as u64) || bytes.is_empty() {
577 puts.remove(&key);
578 return Err(refused(
579 io::ErrorKind::InvalidInput,
580 "An upload out of order",
581 ));
582 }
583 let held: usize = puts
584 .iter()
585 .filter(|((guest, _), _)| guest == peer)
586 .map(|(_, bytes)| bytes.len())
587 .sum();
588 if bytes.len() > LIMIT.saturating_sub(held) {
589 puts.remove(&key);
590 return Err(refused(
591 io::ErrorKind::FileTooLarge,
592 "An upload is too large",
593 ));
594 }
595 puts.entry(key).or_default().extend_from_slice(&bytes);
596 done
597 }
598 kind::COMMIT => {
599 let transaction = Transaction::from_bytes(&self.carried(peer, &request)?)?;
600 self.commit(peer, path, &transaction)?;
601 self.changed(&[path.to_owned()]);
602 done
603 }
604 kind::EDITS => self.batch(peer, request)?,
605 kind::CONFIRM => {
606 self.storage.confirm(path, &stamp()?)?;
607 done
608 }
609 kind::CREATE => {
610 self.storage.create(path, &self.carried(peer, &request)?)?;
611 self.changed(&[folder(path)]);
612 done
613 }
614 kind::CREATE_DIRECTORY => {
615 self.storage.create_directory(path)?;
616 self.changed(&[folder(path)]);
617 done
618 }
619 kind::HIDE => {
620 self.storage.hide(path)?;
621 done
622 }
623 kind::RENAME | kind::REPLACE => {
624 let to = to()?;
625 if kind == kind::RENAME {
626 self.storage.rename(path, to)?;
627 } else {
628 self.storage.replace(path, to)?;
629 }
630 self.changed(&[folder(path), folder(to)]);
631 done
632 }
633 kind::DELETE => {
634 self.storage.delete(path)?;
635 self.changed(&[folder(path)]);
636 done
637 }
638 kind::PLACE => {
639 let ancestor = request
640 .ancestor
641 .ok_or_else(|| refused(io::ErrorKind::InvalidInput, "No ancestor"))?;
642 let name = request.name.as_deref().unwrap_or_default();
643 self.storage.place(path, ancestor, name)?;
644 self.changed(&[path.to_owned()]);
645 done
646 }
647 kind::SUPERSEDE => {
648 self.storage.supersede(path, &stamp()?, to()?)?;
649 self.changed(&[folder(path)]);
650 done
651 }
652 _ => return Err(refused(io::ErrorKind::Unsupported, "An unknown request")),
653 })
654 }
655
656 /// The bytes a request carries, itself or in its uploads.
657 fn carried(&self, peer: &[u8; 16], request: &Request) -> Result<Vec<u8>> {
658 match (&request.bytes, request.handle) {
659 (Some(bytes), _) => Ok(bytes.clone()),
660 (None, Some(handle)) => self
661 .puts
662 .lock()
663 .unwrap()
664 .remove(&(*peer, handle))
665 .ok_or_else(|| refused(io::ErrorKind::InvalidInput, "No such upload")),
666 (None, None) => Ok(Vec::new()),
667 }
668 }
669
670 /// A chunk of a file as it stood when its read began.
671 fn read(&self, peer: &[u8; 16], kind: u16, request: &Request) -> Result<Reply> {
672 let offset = request.offset.unwrap_or_default() as usize;
673 let mut snapshots = self.snapshots.lock().unwrap();
674 snapshots.retain(|_, (_, read)| read.elapsed() < SNAPSHOT_AGE);
675 if let (kind::READ, None, Some(base)) = (kind, request.handle, &request.stamp)
676 && let Ok(base) = Stamp::try_from(base)
677 && let Some(changes) = self.changes(&request.path, &base)?
678 {
679 if let Some(guest) = self.guests.lock().unwrap().get_mut(peer) {
680 Self::holds(guest, &request.path);
681 }
682 return Ok(changes);
683 }
684 let (image, handle) = match request.handle {
685 Some(handle) => {
686 let (image, read) = snapshots
687 .get_mut(&(*peer, handle))
688 .ok_or_else(|| refused(io::ErrorKind::TimedOut, "The read went stale"))?;
689 *read = Instant::now();
690 (Arc::clone(image), handle)
691 }
692 None => {
693 drop(snapshots);
694 if kind == kind::READ
695 && let Some(guest) = self.guests.lock().unwrap().get_mut(peer)
696 {
697 Self::holds(guest, &request.path);
698 }
699 let limit = (request.limit.unwrap_or(LIMIT as u64) as usize).min(LIMIT);
700 let image = match kind {
701 kind::READ => self.image(&request.path)?,
702 _ => Arc::new(self.storage.read_file(&request.path, limit)?),
703 };
704 if image.len() > limit {
705 return Err(io::Error::from(io::ErrorKind::FileTooLarge).into());
706 }
707 snapshots = self.snapshots.lock().unwrap();
708 if snapshots.keys().filter(|(guest, _)| guest == peer).count() >= SNAPSHOTS {
709 snapshots.retain(|(guest, _), _| guest != peer);
710 }
711 (image, request.id)
712 }
713 };
714 let end = image.len().min(offset.saturating_add(CHUNK));
715 let bytes = image.get(offset..end).unwrap_or_default().to_vec();
716 if end < image.len() {
717 snapshots.insert((*peer, handle), (Arc::clone(&image), Instant::now()));
718 } else {
719 snapshots.remove(&(*peer, handle));
720 }
721 Ok(Reply {
722 bytes: Some(bytes),
723 length: Some(image.len() as u64),
724 handle: Some(handle),
725 stamp: (kind == kind::READ)
726 .then(|| Stamp::of(&image).ok())
727 .flatten()
728 .map(|stamp| (&stamp).into()),
729 ..Reply::default()
730 })
731 }
732
733 /// What changed in the section at `path` since the image with stamp `base`, where that
734 /// image is kept and the changes are much smaller than the section.
735 fn changes(&self, path: &str, base: &Stamp) -> Result<Option<Reply>> {
736 let before = self
737 .images
738 .lock()
739 .unwrap()
740 .iter()
741 .find_map(|(held, at, image)| (held == path && at == base).then(|| Arc::clone(image)));
742 let Some(before) = before else {
743 return Ok(None);
744 };
745 let after = self.image(path)?;
746 let writes = match delta(&before, &after) {
747 Some(writes) => writes,
748 None if before == after => Vec::new(),
749 None => return Ok(None),
750 };
751 Ok(Some(Reply {
752 writes: Some(writes),
753 length: Some(after.len() as u64),
754 stamp: Some((&Stamp::of(&after)?).into()),
755 ..Reply::default()
756 }))
757 }
758
759 /// The section or TOC at `path` as it stands: the image kept for it while its stamp holds.
760 fn image(&self, path: &str) -> Result<Arc<Vec<u8>>> {
761 let stamp = self.storage.stamp(path)?;
762 let kept = self
763 .images
764 .lock()
765 .unwrap()
766 .iter()
767 .find_map(|(held, at, image)| {
768 (held == path && *at == stamp).then(|| Arc::clone(image))
769 });
770 if let Some(image) = kept {
771 return Ok(image);
772 }
773 let image = Arc::new(self.storage.read(path)?);
774 self.keep(path, Arc::clone(&image));
775 Ok(image)
776 }
777
778 /// Keeps `image` as the section at `path` now, with the newest of each of `IMAGES`
779 /// sections and earlier images up to `VERSIONS` and `VERSIONS_BYTES`.
780 fn keep(&self, path: &str, image: Arc<Vec<u8>>) {
781 let Ok(stamp) = Stamp::of(&image) else {
782 return;
783 };
784 let mut images = self.images.lock().unwrap();
785 images.retain(|(held, at, _)| held != path || *at != stamp);
786 images.insert(0, (path.to_owned(), stamp, image));
787 let (mut newest, mut versions, mut bytes) = (Vec::new(), 0, 0);
788 images.retain(|(held, _, image)| {
789 if !newest.contains(held) {
790 newest.push(held.clone());
791 return newest.len() <= IMAGES;
792 }
793 versions += 1;
794 bytes += image.len();
795 newest.iter().take(IMAGES).any(|kept| kept == held)
796 && versions <= VERSIONS
797 && bytes <= VERSIONS_BYTES
798 });
799 }
800
801 /// Commits `guest`'s transaction once the section it makes parses.
802 fn commit(&self, guest: &[u8; 16], path: &str, transaction: &Transaction) -> Result<()> {
803 let not_committed = |error: io::Error| {
804 Error::Remote(CommitError {
805 state: CommitState::NotCommitted,
806 error,
807 })
808 };
809 let image = self.image(path).map_err(|error| match error {
810 Error::Io(error) => not_committed(error),
811 error => error,
812 })?;
813 let landed = Landed::Commit(digest(&transaction.stamp()));
814 if Stamp::of(&image).ok().as_ref() != Some(transaction.base()) {
815 if self.landed(guest, path).as_ref() == Some(&landed) {
816 return Ok(());
817 }
818 return Err(not_committed(io::Error::new(
819 io::ErrorKind::StaleNetworkFileHandle,
820 "The section changed since",
821 )));
822 }
823 let mut next = (*image).clone();
824 let checked = transaction.apply(&mut next).and_then(|()| {
825 let store = Store::parse(&next)?;
826 RevisionIndex::parse(&store).map(drop)
827 });
828 if let Err(error) = checked {
829 return Err(not_committed(io::Error::new(
830 io::ErrorKind::InvalidData,
831 error.to_string(),
832 )));
833 }
834 self.storage.commit(path, transaction)?;
835 self.remember(guest, path, landed);
836 self.tell_delta(path, &image, &next, Some(guest));
837 self.keep(path, Arc::new(next));
838 Ok(())
839 }
840
841 /// Tells the guests holding the section at `path` what a commit changed in it, from
842 /// `before` to `after`, where that is small enough to send: all but the guest whose
843 /// commit it was, which has it already.
844 fn tell_delta(&self, path: &str, before: &[u8], after: &[u8], committed: Option<&[u8; 16]>) {
845 let (Ok(base), Some(writes)) = (Stamp::of(before), delta(before, after)) else {
846 return;
847 };
848 let delta = Delta {
849 path: path.to_owned(),
850 base: (&base).into(),
851 length: after.len() as u64,
852 writes,
853 };
854 let mut guests = self.guests.lock().unwrap();
855 let holding: Vec<[u8; 16]> = guests
856 .iter_mut()
857 .filter(|(id, guest)| Some(*id) != committed && guest.held.iter().any(|h| h == path))
858 .map(|(id, guest)| {
859 Self::holds(guest, path);
860 *id
861 })
862 .collect();
863 drop(guests);
864 if let (Some(room), false) = (self.room.lock().unwrap().as_ref(), holding.is_empty()) {
865 room.send(kind::DELTA, &delta, Some(&holding));
866 }
867 }
868
869 /// The host's own change to the section at `path`, which guests hear as a delta where
870 /// the host kept the image before it.
871 fn changed_here(&self, path: &str) {
872 let kept = self
873 .images
874 .lock()
875 .unwrap()
876 .iter()
877 .find_map(|(held, at, image)| (held == path).then(|| (at.clone(), Arc::clone(image))));
878 let Some((at, before)) = kept else {
879 return;
880 };
881 if self.storage.stamp(path).is_ok_and(|now| now == at) {
882 return;
883 }
884 if let Ok(after) = self.storage.read(path) {
885 self.tell_delta(path, &before, &after, None);
886 self.keep(path, Arc::new(after));
887 }
888 }
889}
890
891/// A stamp as `Touched` names it.
892fn digest(stamp: &Stamp) -> u64 {
893 let hash = Sha256::new()
894 .chain_update(stamp.header)
895 .chain_update(stamp.length.to_le_bytes())
896 .finalize();
897 u64::from_le_bytes(hash[..8].try_into().expect("eight bytes"))
898}
899
900/// The writes that make `after` of `before`, a commit's appended bytes, patches and header,
901/// where their encoding fits a chunk and they are much less than `after` itself.
902fn delta(before: &[u8], after: &[u8]) -> Option<Vec<Written>> {
903 const BLOCK: usize = 4096;
904 if before.len() < 1024 || after.len() < before.len() {
905 return None;
906 }
907 let mut writes = vec![Written {
908 offset: 0,
909 bytes: after[..1024].to_vec(),
910 }];
911 let mut at = 1024;
912 while at < before.len() {
913 let end = (at + BLOCK).min(before.len());
914 if before[at..end] != after[at..end] {
915 let first = (at..end).find(|&i| before[i] != after[i]).unwrap_or(at);
916 let last = (at..end).rfind(|&i| before[i] != after[i]).unwrap_or(first) + 1;
917 match writes.last_mut() {
918 Some(write) if write.offset as usize + write.bytes.len() + 64 >= first => {
919 let from = write.offset as usize;
920 write.bytes = after[from..last].to_vec();
921 }
922 _ => writes.push(Written {
923 offset: first as u64,
924 bytes: after[first..last].to_vec(),
925 }),
926 }
927 }
928 at = end;
929 }
930 if after.len() > before.len() {
931 writes.push(Written {
932 offset: before.len() as u64,
933 bytes: after[before.len()..].to_vec(),
934 });
935 }
936 let sent: usize = writes.iter().map(|write| write.bytes.len()).sum();
937 (sent <= after.len() / 2 && sent <= CHUNK && minicbor::to_vec(&writes).ok()?.len() <= CHUNK)
938 .then_some(writes)
939}
940
941/// `image` with `writes`, `length` long: none where a write falls outside it.
942fn written(image: &[u8], length: u64, writes: &[Written]) -> Option<Vec<u8>> {
943 let length = usize::try_from(length)
944 .ok()
945 .filter(|length| *length >= image.len())?;
946 let mut next = image.to_vec();
947 next.resize(length, 0);
948 for write in writes {
949 let offset = usize::try_from(write.offset).ok()?;
950 next.get_mut(offset..offset.checked_add(write.bytes.len())?)?
951 .copy_from_slice(&write.bytes);
952 }
953 Some(next)
954}
955
956fn within(folder: &str, name: &str) -> String {
957 if folder.is_empty() {
958 name.to_owned()
959 } else {
960 format!("{folder}/{name}")
961 }
962}
963
964fn wire_entry(entry: &discover::Entry) -> WireEntry {
965 WireEntry {
966 name: entry.name.clone(),
967 kind: match entry.kind {
968 discover::EntryKind::File => 0,
969 discover::EntryKind::Directory => 1,
970 discover::EntryKind::Other => 2,
971 discover::EntryKind::Evicted => 3,
972 },
973 size: entry.listed.size,
974 modified: entry.listed.modified,
975 }
976}
977
978fn entry(entry: &WireEntry) -> discover::Entry {
979 discover::Entry {
980 name: entry.name.clone(),
981 kind: match entry.kind {
982 0 => discover::EntryKind::File,
983 1 => discover::EntryKind::Directory,
984 3 => discover::EntryKind::Evicted,
985 _ => discover::EntryKind::Other,
986 },
987 listed: discover::Listed {
988 size: entry.size,
989 modified: entry.modified,
990 },
991 }
992}
993
994fn state_number(state: CommitState) -> u8 {
995 match state {
996 CommitState::NotCommitted => 0,
997 CommitState::Unknown => 1,
998 CommitState::Committed => 2,
999 }
1000}
1001
1002fn failed(id: u64, error: Error) -> Reply {
1003 let (kind, state) = match &error {
1004 Error::Io(error) | Error::RemoteIo(error) => (error.kind(), None),
1005 Error::Remote(error) => (error.error.kind(), Some(state_number(error.state))),
1006 Error::Document(_) => (io::ErrorKind::InvalidData, None),
1007 _ => (io::ErrorKind::Other, None),
1008 };
1009 let message = match &error {
1010 Error::Remote(error) => error.error.to_string(),
1011 error => error.to_string(),
1012 };
1013 Reply {
1014 id,
1015 failure: Some(Failure {
1016 kind: wire::error_number(kind),
1017 message,
1018 state,
1019 }),
1020 ..Reply::default()
1021 }
1022}
1023
1024/// How a request failed.
1025enum Failed {
1026 /// It never left this end.
1027 Unsent(io::Error),
1028 /// Its answer was lost: whatever it asked may have happened.
1029 Lost(io::Error),
1030 /// The host refused it.
1031 Refused(Failure),
1032}
1033
1034impl Failed {
1035 fn io(self) -> io::Error {
1036 match self {
1037 Failed::Unsent(error) | Failed::Lost(error) => error,
1038 Failed::Refused(failure) => {
1039 io::Error::new(wire::error_kind(failure.kind), failure.message)
1040 }
1041 }
1042 }
1043
1044 /// As a commit's failure: one never sent was not committed, one whose answer was lost may
1045 /// have been.
1046 fn commit(self) -> CommitError {
1047 let state = match &self {
1048 Failed::Unsent(_) => CommitState::NotCommitted,
1049 Failed::Lost(_) => CommitState::Unknown,
1050 Failed::Refused(failure) => match failure.state {
1051 Some(1) => CommitState::Unknown,
1052 Some(2) => CommitState::Committed,
1053 _ => CommitState::NotCommitted,
1054 },
1055 };
1056 CommitError {
1057 state,
1058 error: self.io(),
1059 }
1060 }
1061}
1062
1063/// A guest's way to a share: the share's room, and the requests waiting on its host.
1064pub struct Guest {
1065 live: Live,
1066 inner: Arc<Inner>,
1067}
1068
1069struct Inner {
1070 share: [u8; 16],
1071 me: Hello,
1072 reach: Option<Reach>,
1073 relay: Option<String>,
1074 events: Arc<dyn Fn() + Send + Sync>,
1075 presence: Mutex<Option<([u8; 16], Live)>>,
1076 here: Mutex<Presence>,
1077 /// The host and the line to it, while connected.
1078 host: Mutex<Option<(Arc<Hello>, Line)>>,
1079 pending: Mutex<HashMap<u64, crate::task::Answer<Reply>>>,
1080 next: AtomicU64,
1081 /// Where the host's reports of changed files go, while a background watches.
1082 watch: Mutex<Option<Arc<Reports>>>,
1083 /// Why this device can no longer reach the share.
1084 ended: Mutex<Option<Ended>>,
1085 /// Bytes of chunks asked for and not yet given.
1086 asked: (Mutex<Chunks>, Condvar),
1087 /// The sections read lately, kept as the host's deltas change them, newest first.
1088 images: Mutex<Vec<(String, Arc<Vec<u8>>)>>,
1089 /// The stamps of sections whose image held is the host's now, as its last report of
1090 /// them said.
1091 current: Mutex<HashMap<String, Stamp>>,
1092 #[cfg(target_arch = "wasm32")]
1093 catalog: Mutex<Option<PathBuf>>,
1094}
1095
1096#[derive(Clone, Copy, Debug, PartialEq, Eq)]
1097pub enum Ended {
1098 Stopped,
1099 Removed,
1100}
1101
1102#[derive(Default)]
1103struct Chunks {
1104 bytes: usize,
1105 #[cfg(target_arch = "wasm32")]
1106 waiting: Vec<std::task::Waker>,
1107}
1108
1109struct Requested<'a> {
1110 inner: &'a Inner,
1111 id: u64,
1112 chunked: bool,
1113}
1114
1115impl Drop for Requested<'_> {
1116 fn drop(&mut self) {
1117 self.inner.pending.lock().unwrap().remove(&self.id);
1118 if self.chunked {
1119 let (asked, room) = &self.inner.asked;
1120 let mut asked = asked.lock().unwrap();
1121 asked.bytes -= CHUNK;
1122 #[cfg(target_arch = "wasm32")]
1123 let waiting = std::mem::take(&mut asked.waiting);
1124 drop(asked);
1125 room.notify_all();
1126 #[cfg(target_arch = "wasm32")]
1127 for waker in waiting {
1128 waker.wake();
1129 }
1130 }
1131 }
1132}
1133
1134impl Guest {
1135 /// Joins share `share` through its `secret` as `me`, where `reach` and `relay` say.
1136 /// `events` runs on a network thread whenever the host comes or goes, or the peers change.
1137 pub fn start(
1138 me: Hello,
1139 share: [u8; 16],
1140 secret: [u8; 16],
1141 reach: Option<Reach>,
1142 relay: Option<&str>,
1143 events: impl Fn() + Send + Sync + 'static,
1144 ) -> io::Result<Arc<Self>> {
1145 let inner = Arc::new(Inner {
1146 share,
1147 me: me.clone(),
1148 reach,
1149 relay: relay.map(str::to_owned),
1150 events: Arc::new(events),
1151 presence: Mutex::default(),
1152 here: Mutex::default(),
1153 host: Mutex::default(),
1154 pending: Mutex::default(),
1155 next: AtomicU64::new(1),
1156 watch: Mutex::default(),
1157 ended: Mutex::default(),
1158 asked: Default::default(),
1159 images: Mutex::default(),
1160 current: Mutex::default(),
1161 #[cfg(target_arch = "wasm32")]
1162 catalog: Mutex::default(),
1163 });
1164 let heard = Arc::clone(&inner);
1165 let live = Live::start_with(
1166 me,
1167 &Room::Notebook(secret),
1168 reach,
1169 relay,
1170 move |source, event| {
1171 heard.heard(source, event);
1172 (heard.events)();
1173 },
1174 )?;
1175 Ok(Arc::new(Self { live, inner }))
1176 }
1177
1178 /// Where the notebook is kept: `location(share)`.
1179 pub fn location(&self) -> String {
1180 location(&self.inner.share)
1181 }
1182
1183 /// The host, while connected.
1184 pub fn host(&self) -> Option<Arc<Hello>> {
1185 let host = self.inner.host.lock().unwrap();
1186 host.as_ref().map(|(hello, _)| Arc::clone(hello))
1187 }
1188
1189 /// Why the host ended this device's access.
1190 pub fn ended(&self) -> Option<Ended> {
1191 *self.inner.ended.lock().unwrap()
1192 }
1193
1194 /// Everyone in the share's room: the host and the other guests.
1195 pub fn peers(&self) -> Vec<Peer> {
1196 self.inner
1197 .presence
1198 .lock()
1199 .unwrap()
1200 .as_ref()
1201 .map(|(_, live)| live.peers())
1202 .unwrap_or_default()
1203 }
1204
1205 pub fn set_presence(&self, presence: Presence) {
1206 *self.inner.here.lock().unwrap() = presence.clone();
1207 if let Some((_, live)) = &*self.inner.presence.lock().unwrap() {
1208 live.set_presence(presence);
1209 }
1210 }
1211
1212 /// Sends the host's reports of changed files to `reports` while it stays connected.
1213 pub(crate) fn watch(&self, reports: Reports) -> io::Result<()> {
1214 if self.host().is_none() {
1215 return Err(self.offline());
1216 }
1217 *self.inner.watch.lock().unwrap() = Some(Arc::new(reports));
1218 Ok(())
1219 }
1220
1221 fn offline(&self) -> io::Error {
1222 if let Some(version) = self.live.other_version() {
1223 return io::Error::new(io::ErrorKind::NotConnected, wire::Version(version));
1224 }
1225 let message = match &*self.inner.ended.lock().unwrap() {
1226 Some(Ended::Stopped) => "The host stopped sharing this notebook",
1227 Some(Ended::Removed) => {
1228 "This device was removed. Ask the person sharing for a new link or code."
1229 }
1230 None => "The computer sharing this notebook can’t be reached",
1231 };
1232 io::Error::new(io::ErrorKind::NotConnected, message)
1233 }
1234
1235 /// Asks the host `kind` of `request`, waiting for its reply.
1236 #[cfg(not(target_arch = "wasm32"))]
1237 fn request(&self, kind: u16, request: Request) -> std::result::Result<Reply, Failed> {
1238 crate::task::ready(self.request_async(kind, request)).map_err(Failed::Unsent)?
1239 }
1240
1241 async fn request_async(
1242 &self,
1243 kind: u16,
1244 mut request: Request,
1245 ) -> std::result::Result<Reply, Failed> {
1246 let Some((_, line)) = self.inner.host.lock().unwrap().clone() else {
1247 return Err(Failed::Unsent(self.offline()));
1248 };
1249 let chunked = matches!(kind, kind::READ | kind::READ_FILE | kind::PUT);
1250 if chunked {
1251 #[cfg(not(target_arch = "wasm32"))]
1252 {
1253 let (asked, _room) = &self.inner.asked;
1254 let mut asked = asked.lock().unwrap();
1255 while asked.bytes + CHUNK > WINDOW {
1256 asked = _room.wait(asked).unwrap();
1257 }
1258 asked.bytes += CHUNK;
1259 }
1260 #[cfg(target_arch = "wasm32")]
1261 std::future::poll_fn(|context| {
1262 let mut asked = self.inner.asked.0.lock().unwrap();
1263 if asked.bytes + CHUNK > WINDOW {
1264 if !asked
1265 .waiting
1266 .iter()
1267 .any(|waker| waker.will_wake(context.waker()))
1268 {
1269 asked.waiting.push(context.waker().clone());
1270 }
1271 std::task::Poll::Pending
1272 } else {
1273 asked.bytes += CHUNK;
1274 std::task::Poll::Ready(())
1275 }
1276 })
1277 .await;
1278 }
1279 let id = self.inner.next.fetch_add(1, Ordering::Relaxed);
1280 request.id = id;
1281 let (answer, answered) = crate::task::response();
1282 self.inner.pending.lock().unwrap().insert(id, answer);
1283 let _requested = Requested {
1284 inner: &self.inner,
1285 id,
1286 chunked,
1287 };
1288 let sent = line.send(kind, &request);
1289 let reply = match sent {
1290 Err(error) => Err(Failed::Unsent(error)),
1291 Ok(()) => match crate::task::answered(&answered, TIMEOUT).await {
1292 Ok(reply) => Ok(reply),
1293 Err(mpsc::RecvTimeoutError::Timeout) => Err(Failed::Lost(io::Error::new(
1294 io::ErrorKind::TimedOut,
1295 "The computer sharing this notebook didn’t answer",
1296 ))),
1297 Err(mpsc::RecvTimeoutError::Disconnected) => Err(Failed::Lost(self.offline())),
1298 },
1299 };
1300 let reply = reply?;
1301 match reply.failure {
1302 Some(failure) => Err(Failed::Refused(failure)),
1303 None => Ok(reply),
1304 }
1305 }
1306
1307 #[cfg(not(target_arch = "wasm32"))]
1308 fn ask(&self, kind: u16, request: Request) -> io::Result<Reply> {
1309 self.request(kind, request).map_err(Failed::io)
1310 }
1311
1312 async fn ask_async(&self, kind: u16, request: Request) -> io::Result<Reply> {
1313 self.request_async(kind, request).await.map_err(Failed::io)
1314 }
1315
1316 /// Puts `bytes` in `request`, or uploads them first where they are large.
1317 #[cfg(not(target_arch = "wasm32"))]
1318 fn carry(&self, request: &mut Request, bytes: Vec<u8>) -> std::result::Result<(), Failed> {
1319 crate::task::ready(self.carry_async(request, bytes)).map_err(Failed::Unsent)?
1320 }
1321
1322 async fn carry_async(
1323 &self,
1324 request: &mut Request,
1325 bytes: Vec<u8>,
1326 ) -> std::result::Result<(), Failed> {
1327 if bytes.len() > LIMIT {
1328 return Err(Failed::Unsent(io::ErrorKind::FileTooLarge.into()));
1329 }
1330 if bytes.len() <= CHUNK {
1331 request.bytes = Some(bytes);
1332 return Ok(());
1333 }
1334 let handle = self.inner.next.fetch_add(1, Ordering::Relaxed);
1335 for (index, chunk) in bytes.chunks(CHUNK).enumerate() {
1336 let put = Request {
1337 handle: Some(handle),
1338 offset: Some((index * CHUNK) as u64),
1339 bytes: Some(chunk.to_vec()),
1340 ..Request::default()
1341 };
1342 // Nothing the upload carries happens before the request that uses it.
1343 self.request_async(kind::PUT, put)
1344 .await
1345 .map_err(|failed| match failed {
1346 Failed::Lost(error) => Failed::Unsent(error),
1347 failed => failed,
1348 })?;
1349 }
1350 request.handle = Some(handle);
1351 Ok(())
1352 }
1353
1354 /// Reads the file at `path` a chunk at a time, as `kind` reads it; a section held, as
1355 /// the changes to it.
1356 #[cfg(not(target_arch = "wasm32"))]
1357 fn read(&self, kind: u16, path: &str, limit: usize) -> io::Result<Vec<u8>> {
1358 crate::task::ready(self.read_async(kind, path, limit))?
1359 }
1360
1361 async fn read_async(&self, kind: u16, path: &str, limit: usize) -> io::Result<Vec<u8>> {
1362 let held = (kind == kind::READ)
1363 .then(|| self.inner.image(path))
1364 .flatten();
1365 let first = self
1366 .ask_async(
1367 kind,
1368 Request {
1369 path: path.to_owned(),
1370 offset: Some(0),
1371 limit: Some(limit as u64),
1372 stamp: (held.as_deref())
1373 .and_then(|image| Stamp::of(image).ok())
1374 .map(|stamp| (&stamp).into()),
1375 ..Request::default()
1376 },
1377 )
1378 .await?;
1379 if let (Some(writes), Some(held)) = (&first.writes, held) {
1380 let stamp: Option<Stamp> = first.stamp.as_ref().and_then(|s| s.try_into().ok());
1381 let image = written(&held, first.length.unwrap_or_default(), writes)
1382 .filter(|image| stamp.is_some() && Stamp::of(image).ok() == stamp)
1383 .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "Changes that miss"))?;
1384 self.inner.hold(path, Arc::new(image.clone()));
1385 return Ok(image);
1386 }
1387 let length = first.length.unwrap_or_default() as usize;
1388 if length > limit {
1389 return Err(io::ErrorKind::FileTooLarge.into());
1390 }
1391 let mut image = first.bytes.unwrap_or_default();
1392 while image.len() < length {
1393 let chunk = self
1394 .ask_async(
1395 kind,
1396 Request {
1397 path: path.to_owned(),
1398 offset: Some(image.len() as u64),
1399 handle: first.handle,
1400 ..Request::default()
1401 },
1402 )
1403 .await?
1404 .bytes
1405 .unwrap_or_default();
1406 if chunk.is_empty() {
1407 return Err(io::ErrorKind::UnexpectedEof.into());
1408 }
1409 image.extend_from_slice(&chunk);
1410 }
1411 image.truncate(length);
1412 if kind == kind::READ {
1413 self.inner.hold(path, Arc::new(image.clone()));
1414 }
1415 Ok(image)
1416 }
1417
1418 /// The image of the section at `path` with stamp `stamp`, where this guest holds it.
1419 fn held(&self, path: &str, stamp: &Stamp) -> Option<Vec<u8>> {
1420 let image = self.inner.image(path)?;
1421 (Stamp::of(&image).ok().as_ref() == Some(stamp)).then(|| image.to_vec())
1422 }
1423
1424 #[cfg(not(target_arch = "wasm32"))]
1425 pub(crate) fn entries(&self, folder: &str) -> io::Result<Vec<discover::Entry>> {
1426 crate::task::ready(self.entries_async(folder))?
1427 }
1428
1429 pub(crate) async fn entries_async(&self, folder: &str) -> io::Result<Vec<discover::Entry>> {
1430 let reply = self
1431 .ask_async(
1432 kind::LIST,
1433 Request {
1434 path: folder.to_owned(),
1435 ..Request::default()
1436 },
1437 )
1438 .await?;
1439 let entries: Vec<_> = reply
1440 .entries
1441 .unwrap_or_default()
1442 .iter()
1443 .map(entry)
1444 .collect();
1445 #[cfg(target_arch = "wasm32")]
1446 self.cache_entries(folder, &entries).await?;
1447 Ok(entries)
1448 }
1449
1450 /// The stamp of the file at `path`: the host's last report of it where this guest holds
1451 /// that image, else the host's answer.
1452 #[cfg(not(target_arch = "wasm32"))]
1453 fn stamp(&self, path: &str) -> io::Result<Stamp> {
1454 crate::task::ready(self.stamp_async(path))?
1455 }
1456
1457 async fn stamp_async(&self, path: &str) -> io::Result<Stamp> {
1458 if let Some(stamp) = self.inner.current.lock().unwrap().get(path) {
1459 return Ok(stamp.clone());
1460 }
1461 let reply = self
1462 .ask_async(
1463 kind::STAMP,
1464 Request {
1465 path: path.to_owned(),
1466 ..Request::default()
1467 },
1468 )
1469 .await?;
1470 reply
1471 .stamp
1472 .as_ref()
1473 .ok_or_else(|| io::Error::from(io::ErrorKind::InvalidData))?
1474 .try_into()
1475 }
1476
1477 #[cfg(not(target_arch = "wasm32"))]
1478 fn commit(
1479 &self,
1480 path: &str,
1481 transaction: &Transaction,
1482 ) -> std::result::Result<(), CommitError> {
1483 crate::task::ready(self.commit_async(path, transaction)).map_err(|error| CommitError {
1484 state: CommitState::NotCommitted,
1485 error,
1486 })?
1487 }
1488
1489 async fn commit_async(
1490 &self,
1491 path: &str,
1492 transaction: &Transaction,
1493 ) -> std::result::Result<(), CommitError> {
1494 let mut request = Request {
1495 path: path.to_owned(),
1496 ..Request::default()
1497 };
1498 self.carry_async(&mut request, transaction.to_bytes())
1499 .await
1500 .map_err(Failed::commit)?;
1501 self.request_async(kind::COMMIT, request)
1502 .await
1503 .map(drop)
1504 .map_err(Failed::commit)
1505 }
1506
1507 #[cfg(not(target_arch = "wasm32"))]
1508 fn confirm(&self, path: &str, base: &Stamp) -> std::result::Result<(), CommitError> {
1509 crate::task::ready(self.confirm_async(path, base)).map_err(|error| CommitError {
1510 state: CommitState::NotCommitted,
1511 error,
1512 })?
1513 }
1514
1515 async fn confirm_async(
1516 &self,
1517 path: &str,
1518 base: &Stamp,
1519 ) -> std::result::Result<(), CommitError> {
1520 let request = Request {
1521 path: path.to_owned(),
1522 stamp: Some(base.into()),
1523 ..Request::default()
1524 };
1525 self.request_async(kind::CONFIRM, request)
1526 .await
1527 .map(drop)
1528 .map_err(Failed::commit)
1529 }
1530
1531 async fn edits_async(
1532 &self,
1533 path: &str,
1534 transaction: &Transaction,
1535 edits: &[crate::PendingEdit],
1536 revisions: &BTreeMap<onestore::ExGuid, onestore::ExGuid>,
1537 ) -> std::result::Result<(), CommitError> {
1538 let bytes = batch::encode(
1539 &batch::Edits {
1540 edits: edits
1541 .iter()
1542 .map(|edit| (edit.author.clone(), edit.edit.clone()))
1543 .collect(),
1544 revisions: revisions.clone(),
1545 },
1546 LIMIT,
1547 )
1548 .map_err(|error| CommitError {
1549 state: CommitState::NotCommitted,
1550 error,
1551 })?;
1552 let mut request = Request {
1553 path: path.to_owned(),
1554 stamp: Some(transaction.base().into()),
1555 ..Request::default()
1556 };
1557 self.carry_async(&mut request, bytes)
1558 .await
1559 .map_err(Failed::commit)?;
1560 match self.request_async(kind::EDITS, request).await {
1561 Ok(reply) => {
1562 if let Some(stamp) = reply.stamp {
1563 let stamp = Stamp::try_from(&stamp).map_err(|error| CommitError {
1564 state: CommitState::Unknown,
1565 error,
1566 })?;
1567 self.inner
1568 .current
1569 .lock()
1570 .unwrap()
1571 .insert(path.to_owned(), stamp);
1572 }
1573 Ok(())
1574 }
1575 Err(Failed::Refused(failure))
1576 if wire::error_kind(failure.kind) == io::ErrorKind::Unsupported =>
1577 {
1578 self.commit_async(path, transaction).await?;
1579 self.inner.published(path, transaction);
1580 Ok(())
1581 }
1582 Err(error) => Err(error.commit()),
1583 }
1584 }
1585
1586 /// A request on `path` that answers nothing but whether it happened.
1587 #[cfg(not(target_arch = "wasm32"))]
1588 fn verb(&self, kind: u16, path: &str, request: Request) -> Result<()> {
1589 self.ask(
1590 kind,
1591 Request {
1592 path: path.to_owned(),
1593 ..request
1594 },
1595 )?;
1596 Ok(())
1597 }
1598}
1599
1600impl Inner {
1601 fn image(&self, path: &str) -> Option<Arc<Vec<u8>>> {
1602 let images = self.images.lock().unwrap();
1603 images
1604 .iter()
1605 .find_map(|(held, image)| (held == path).then(|| Arc::clone(image)))
1606 }
1607
1608 fn hold(&self, path: &str, image: Arc<Vec<u8>>) {
1609 let mut images = self.images.lock().unwrap();
1610 images.retain(|(held, _)| held != path);
1611 images.insert(0, (path.to_owned(), image));
1612 images.truncate(IMAGES);
1613 }
1614
1615 /// Takes on a delta to an image held.
1616 fn apply(&self, delta: &Delta) {
1617 let held = self.image(&delta.path);
1618 let base: Option<Stamp> = (&delta.base).try_into().ok();
1619 if let Some(image) = held
1620 && Stamp::of(&image).ok() == base
1621 && let Some(next) = written(&image, delta.length, &delta.writes)
1622 {
1623 self.hold(&delta.path, Arc::new(next));
1624 }
1625 }
1626
1627 /// Takes on this guest's own commit to an image held; the host's report, not this, says
1628 /// whether the image is the host's, as another's commit may have followed.
1629 fn published(&self, path: &str, transaction: &Transaction) {
1630 if let Some(image) = self.image(path) {
1631 let mut next = (*image).clone();
1632 if transaction.apply(&mut next).is_ok() {
1633 self.hold(path, Arc::new(next));
1634 }
1635 }
1636 }
1637
1638 /// Takes the host's report of what the files at `paths` are now.
1639 fn touched(&self, touched: &Touched) {
1640 let mut current = self.current.lock().unwrap();
1641 for (at, path) in touched.paths.iter().enumerate() {
1642 let stamp = self.image(path).and_then(|image| Stamp::of(&image).ok());
1643 match stamp.filter(|stamp| touched.stamps.get(at) == Some(&Some(digest(stamp)))) {
1644 Some(stamp) => current.insert(path.clone(), stamp),
1645 None => current.remove(path),
1646 };
1647 }
1648 }
1649
1650 fn heard(self: &Arc<Self>, source: &Arc<super::Shared>, event: Event) {
1651 match event {
1652 Event::Met(hello, line) if hello.serves == Some(self.share) => {
1653 let mut host = self.host.lock().unwrap();
1654 let retired = if source.connected(hello) {
1655 host.replace((Arc::clone(hello), line.clone()))
1656 } else {
1657 None
1658 };
1659 drop(host);
1660 drop(retired);
1661 }
1662 Event::Left(hello) => {
1663 let (retired, presence, pending, reports) = {
1664 let mut host = self.host.lock().unwrap();
1665 if !host
1666 .as_ref()
1667 .is_some_and(|(admitted, _)| Arc::ptr_eq(admitted, hello))
1668 || !source.gone(hello)
1669 {
1670 return;
1671 }
1672 let retired = host.take();
1673 let presence = self.presence.lock().unwrap().take();
1674 self.current.lock().unwrap().clear();
1675 let pending = std::mem::take(&mut *self.pending.lock().unwrap());
1676 let reports = self.watch.lock().unwrap().take();
1677 (retired, presence, pending, reports)
1678 };
1679 drop(retired);
1680 drop(presence);
1681 drop(pending);
1682 if let Some(reports) = reports {
1683 reports.lost();
1684 }
1685 }
1686 // A reply only reaches its waiter, so one read off a retired route still counts.
1687 Event::Frame {
1688 from,
1689 kind: kind::REPLY,
1690 body,
1691 } => {
1692 let serves = (self.host.lock().unwrap().as_ref())
1693 .is_some_and(|(host, _)| host.peer == from.peer);
1694 if serves && let Ok(reply) = minicbor::decode::<Reply>(body) {
1695 let waiting = self.pending.lock().unwrap().remove(&reply.id);
1696 if let Some(waiting) = waiting {
1697 let _ = waiting.send(reply);
1698 }
1699 }
1700 }
1701 Event::Frame { from, kind, body } => {
1702 let host = self.host.lock().unwrap();
1703 if !host
1704 .as_ref()
1705 .is_some_and(|(admitted, _)| Arc::ptr_eq(admitted, from))
1706 || !source.connected(from)
1707 {
1708 return;
1709 }
1710 match kind {
1711 kind::WELCOME => {
1712 if let Ok(welcome) = minicbor::decode::<Welcome>(body)
1713 && !(self.presence.lock().unwrap().as_ref())
1714 .is_some_and(|(held, _)| *held == welcome.room)
1715 {
1716 drop(host);
1717 self.join_presence(source, from, welcome.room);
1718 }
1719 }
1720 kind::DELTA => {
1721 if let Ok(delta) = minicbor::decode::<Delta>(body) {
1722 self.apply(&delta);
1723 }
1724 }
1725 kind::TOUCHED => {
1726 if let Ok(touched) = minicbor::decode::<Touched>(body) {
1727 self.touched(&touched);
1728 let reports = self.watch.lock().unwrap().clone();
1729 drop(host);
1730 if let Some(reports) = reports {
1731 reports.touched(&touched.paths);
1732 }
1733 }
1734 }
1735 kind::BYE => {
1736 if let Ok(bye) = minicbor::decode::<wire::Bye>(body) {
1737 let ended = match bye.reason.as_str() {
1738 STOPPED => Some(Ended::Stopped),
1739 "removed" => Some(Ended::Removed),
1740 _ => None,
1741 };
1742 if ended.is_some() {
1743 *self.ended.lock().unwrap() = ended;
1744 }
1745 }
1746 }
1747 _ => {}
1748 }
1749 }
1750 _ => {}
1751 }
1752 }
1753
1754 fn join_presence(
1755 self: &Arc<Self>,
1756 source: &Arc<super::Shared>,
1757 from: &Arc<Hello>,
1758 secret: [u8; 16],
1759 ) {
1760 let inner = Arc::downgrade(self);
1761 let connected = Mutex::new(None);
1762 let live = Live::start_with(
1763 self.me.clone(),
1764 &Room::Notebook(secret),
1765 self.reach,
1766 self.relay.as_deref(),
1767 move |source, event| {
1768 let Some(inner) = inner.upgrade() else {
1769 return;
1770 };
1771 let reports = {
1772 let host = inner.host.lock().unwrap();
1773 let presence = inner.presence.lock().unwrap();
1774 let Some((_, live)) = presence.as_ref() else {
1775 return;
1776 };
1777 if !Arc::ptr_eq(&live.shared, source) {
1778 return;
1779 }
1780 match event {
1781 Event::Changed => {
1782 let mut connected = connected.lock().unwrap();
1783 let host = host.as_ref().map(|(host, _)| host.peer);
1784 let peers = source.peers();
1785 let present = host
1786 .filter(|host| peers.iter().any(|peer| peer.hello.peer == *host));
1787 if *connected == present {
1788 None
1789 } else {
1790 *connected = present;
1791 inner.current.lock().unwrap().clear();
1792 if present.is_some() {
1793 inner
1794 .watch
1795 .lock()
1796 .unwrap()
1797 .clone()
1798 .map(|reports| (reports, vec![String::new()]))
1799 } else {
1800 None
1801 }
1802 }
1803 }
1804 Event::Frame { from, kind, body }
1805 if host
1806 .as_ref()
1807 .is_some_and(|(host, _)| host.peer == from.peer)
1808 && source.connected(from) =>
1809 {
1810 match kind {
1811 kind::DELTA => {
1812 if let Ok(delta) = minicbor::decode::<Delta>(body) {
1813 inner.apply(&delta);
1814 }
1815 None
1816 }
1817 kind::TOUCHED => {
1818 if let Ok(touched) = minicbor::decode::<Touched>(body) {
1819 inner.touched(&touched);
1820 inner
1821 .watch
1822 .lock()
1823 .unwrap()
1824 .clone()
1825 .map(|reports| (reports, touched.paths))
1826 } else {
1827 None
1828 }
1829 }
1830 _ => None,
1831 }
1832 }
1833 _ => None,
1834 }
1835 };
1836 if let Some((reports, paths)) = reports {
1837 reports.touched(&paths);
1838 }
1839 (inner.events)();
1840 },
1841 );
1842 let live = match live {
1843 Ok(live) => live,
1844 Err(error) => return eprintln!("Live Share: could not join presence: {error}"),
1845 };
1846 let here = self.here.lock().unwrap();
1847 live.set_presence(here.clone());
1848 let refresh = Arc::clone(&live.shared);
1849 let mut candidate = Some(live);
1850 let (retired, paths, reports) = {
1851 let host = self.host.lock().unwrap();
1852 if host
1853 .as_ref()
1854 .is_some_and(|(admitted, _)| Arc::ptr_eq(admitted, from))
1855 && source.connected(from)
1856 {
1857 let mut presence = self.presence.lock().unwrap();
1858 if presence.as_ref().is_some_and(|(held, _)| *held == secret) {
1859 (None, Vec::new(), None)
1860 } else {
1861 let retired = presence.replace((secret, candidate.take().unwrap()));
1862 let paths = self
1863 .current
1864 .lock()
1865 .unwrap()
1866 .drain()
1867 .map(|(path, _)| path)
1868 .collect::<Vec<_>>();
1869 let reports = self.watch.lock().unwrap().clone();
1870 (retired, paths, reports)
1871 }
1872 } else {
1873 (None, Vec::new(), None)
1874 }
1875 };
1876 drop(here);
1877 drop(retired);
1878 if candidate.is_none() {
1879 (refresh.events)(&refresh, Event::Changed);
1880 }
1881 drop(candidate);
1882 if let Some(reports) = reports {
1883 reports.touched(&paths);
1884 }
1885 }
1886}
1887
1888/// A section a Live Share host serves, as a guest's replica publishes to it.
1889pub struct HostedRemote {
1890 guest: Arc<Guest>,
1891 path: String,
1892 /// The stamp last asked for, which an image the guest holds may already have.
1893 seen: Option<Stamp>,
1894 /// The host refused the last batch, so the next step reads its image before sending
1895 /// edits. A stamp clears it, as an uncertain batch goes again regardless.
1896 rejected: bool,
1897 #[cfg(target_arch = "wasm32")]
1898 pending: Option<web::Pending>,
1899}
1900
1901impl HostedRemote {
1902 pub fn new(guest: &Arc<Guest>, path: &str) -> Self {
1903 Self {
1904 guest: Arc::clone(guest),
1905 path: path.to_owned(),
1906 seen: None,
1907 rejected: false,
1908 #[cfg(target_arch = "wasm32")]
1909 pending: None,
1910 }
1911 }
1912}
1913
1914#[cfg(not(target_arch = "wasm32"))]
1915impl crate::Remote for HostedRemote {
1916 fn accepts_edits(&self) -> bool {
1917 !self.rejected
1918 && self
1919 .guest
1920 .host()
1921 .is_some_and(|host| host.ops == Some(1) && host.kinds.contains(&kind::EDITS))
1922 }
1923
1924 fn publish_edits(
1925 &mut self,
1926 transaction: &Transaction,
1927 edits: &[crate::PendingEdit],
1928 revisions: &BTreeMap<onestore::ExGuid, onestore::ExGuid>,
1929 ) -> std::result::Result<(), CommitError> {
1930 let result =
1931 crate::task::ready(
1932 self.guest
1933 .edits_async(&self.path, transaction, edits, revisions),
1934 )
1935 .map_err(|error| CommitError {
1936 state: CommitState::NotCommitted,
1937 error,
1938 })?;
1939 if result.is_ok() {
1940 self.seen = None;
1941 }
1942 if result
1943 .as_ref()
1944 .is_err_and(|error| error.state == CommitState::NotCommitted)
1945 {
1946 self.rejected = true;
1947 self.guest.inner.current.lock().unwrap().remove(&self.path);
1948 }
1949 result
1950 }
1951
1952 fn read(&mut self) -> io::Result<Vec<u8>> {
1953 self.rejected = false;
1954 if let Some(image) = (self.seen.as_ref()).and_then(|seen| self.guest.held(&self.path, seen))
1955 {
1956 return Ok(image);
1957 }
1958 self.guest.read(kind::READ, &self.path, LIMIT)
1959 }
1960
1961 fn stamp(&mut self) -> io::Result<Stamp> {
1962 self.rejected = false;
1963 let stamp = self.guest.stamp(&self.path)?;
1964 self.seen = Some(stamp.clone());
1965 Ok(stamp)
1966 }
1967
1968 fn publish(&mut self, transaction: &Transaction) -> std::result::Result<(), CommitError> {
1969 if let Err(error) = self.guest.commit(&self.path, transaction) {
1970 // The host's image moved on, and its report may not have come yet.
1971 self.guest.inner.current.lock().unwrap().remove(&self.path);
1972 return Err(error);
1973 }
1974 self.guest.inner.published(&self.path, transaction);
1975 Ok(())
1976 }
1977
1978 fn confirm(&mut self, base: &Stamp) -> std::result::Result<(), CommitError> {
1979 self.guest.confirm(&self.path, base)
1980 }
1981}
1982
1983/// A notebook a Live Share host serves, as a guest's `session::Notebook` reaches it. Its
1984/// folders as last listed are kept at `listed`, so the notebook opens while the host can't
1985/// be reached.
1986pub struct Hosted {
1987 guest: Arc<Guest>,
1988 listed: PathBuf,
1989}
1990
1991/// Each folder's entries as last listed: name, `WireEntry::kind`, size and modified time.
1992type Listings = BTreeMap<String, Vec<(String, u8, u64, u64)>>;
1993
1994impl Hosted {
1995 pub(crate) fn new(guest: Arc<Guest>, listed: PathBuf) -> Self {
1996 #[cfg(target_arch = "wasm32")]
1997 {
1998 *guest.inner.catalog.lock().unwrap() = Some(listed.clone());
1999 }
2000 Self { guest, listed }
2001 }
2002}
2003
2004/// The host's folders, or as they were last listed while it can't be reached.
2005#[cfg(not(target_arch = "wasm32"))]
2006struct Source<'a> {
2007 guest: &'a Guest,
2008 kept: Listings,
2009 listed: Listings,
2010}
2011
2012#[cfg(not(target_arch = "wasm32"))]
2013impl discover::Source for Source<'_> {
2014 fn entries(&mut self, path: &str, limit: usize) -> io::Result<Vec<discover::Entry>> {
2015 let entries = match self.guest.entries(path) {
2016 Ok(entries) => entries,
2017 Err(error) if error.kind() == io::ErrorKind::NotConnected => self
2018 .kept
2019 .get(path)
2020 .ok_or(error)?
2021 .iter()
2022 .map(|(name, kind, size, modified)| {
2023 entry(&WireEntry {
2024 name: name.clone(),
2025 kind: *kind,
2026 size: *size,
2027 modified: *modified,
2028 })
2029 })
2030 .collect(),
2031 Err(error) => return Err(error),
2032 };
2033 if entries.len() > limit {
2034 return Err(io::ErrorKind::FileTooLarge.into());
2035 }
2036 self.listed.insert(
2037 path.to_owned(),
2038 entries
2039 .iter()
2040 .map(|listed| {
2041 let wire = wire_entry(listed);
2042 (wire.name, wire.kind, wire.size, wire.modified)
2043 })
2044 .collect(),
2045 );
2046 Ok(entries)
2047 }
2048
2049 fn read(&mut self, path: &str, limit: usize) -> io::Result<Vec<u8>> {
2050 self.guest.read(kind::READ, path, limit)
2051 }
2052
2053 fn read_asset(&mut self, path: &str, limit: usize) -> io::Result<Vec<u8>> {
2054 self.guest.read(kind::READ_FILE, path, limit)
2055 }
2056}
2057
2058#[cfg(not(target_arch = "wasm32"))]
2059impl Storage for Hosted {
2060 fn discover(
2061 &self,
2062 cache: &mut discover::Cache,
2063 limits: discover::Limits,
2064 ) -> Result<discover::Folder> {
2065 let kept = crate::fs::read(&self.listed)
2066 .ok()
2067 .and_then(|bytes| serde_json::from_slice(&bytes).ok())
2068 .unwrap_or_default();
2069 let mut source = Source {
2070 guest: &self.guest,
2071 kept,
2072 listed: Listings::new(),
2073 };
2074 let folder = cache.discover(&mut source, limits)?;
2075 if let Ok(json) = serde_json::to_vec(&source.listed) {
2076 let _ = crate::fs::write(&self.listed, json);
2077 }
2078 Ok(folder)
2079 }
2080
2081 fn location(&self) -> String {
2082 self.guest.location()
2083 }
2084
2085 fn entries(&self, folder: &str) -> io::Result<Vec<discover::Entry>> {
2086 self.guest.entries(folder)
2087 }
2088
2089 fn exists(&self, path: &str) -> bool {
2090 let request = Request {
2091 path: path.to_owned(),
2092 ..Request::default()
2093 };
2094 self.guest
2095 .ask(kind::EXISTS, request)
2096 .is_ok_and(|reply| reply.exists == Some(true))
2097 }
2098
2099 fn stamp(&self, path: &str) -> io::Result<Stamp> {
2100 self.guest.stamp(path)
2101 }
2102
2103 fn read(&self, path: &str) -> Result<Vec<u8>> {
2104 Ok(self.guest.read(kind::READ, path, LIMIT)?)
2105 }
2106
2107 fn read_file(&self, path: &str, limit: usize) -> Result<Vec<u8>> {
2108 Ok(self.guest.read(kind::READ_FILE, path, limit)?)
2109 }
2110
2111 fn create(&self, path: &str, bytes: &[u8]) -> Result<()> {
2112 if bytes.len() > LIMIT {
2113 return Err(io::Error::from(io::ErrorKind::FileTooLarge).into());
2114 }
2115 let mut request = Request {
2116 path: path.to_owned(),
2117 ..Request::default()
2118 };
2119 self.guest
2120 .carry(&mut request, bytes.to_vec())
2121 .map_err(Failed::io)?;
2122 self.guest.verb(kind::CREATE, path, request)
2123 }
2124
2125 fn create_directory(&self, path: &str) -> Result<()> {
2126 self.guest
2127 .verb(kind::CREATE_DIRECTORY, path, Request::default())
2128 }
2129
2130 fn hide(&self, path: &str) -> Result<()> {
2131 self.guest.verb(kind::HIDE, path, Request::default())
2132 }
2133
2134 fn rename(&self, from: &str, to: &str) -> Result<()> {
2135 let request = Request {
2136 to: Some(to.to_owned()),
2137 ..Request::default()
2138 };
2139 self.guest.verb(kind::RENAME, from, request)
2140 }
2141
2142 fn rename_root(&self, _: &str, _: &[String]) -> Result<String> {
2143 Err(refused(
2144 io::ErrorKind::Unsupported,
2145 "Only the computer sharing this notebook can rename its folder",
2146 ))
2147 }
2148
2149 fn replace(&self, from: &str, to: &str) -> Result<()> {
2150 let request = Request {
2151 to: Some(to.to_owned()),
2152 ..Request::default()
2153 };
2154 self.guest.verb(kind::REPLACE, from, request)
2155 }
2156
2157 fn delete(&self, path: &str) -> Result<()> {
2158 self.guest.verb(kind::DELETE, path, Request::default())
2159 }
2160
2161 fn place(&self, path: &str, ancestor: [u8; 16], name: &str) -> Result<()> {
2162 let request = Request {
2163 ancestor: Some(ancestor),
2164 name: Some(name.to_owned()),
2165 ..Request::default()
2166 };
2167 self.guest.verb(kind::PLACE, path, request)
2168 }
2169
2170 fn commit(&self, path: &str, transaction: &Transaction) -> Result<()> {
2171 Ok(self.guest.commit(path, transaction)?)
2172 }
2173
2174 fn confirm(&self, path: &str, base: &Stamp) -> std::result::Result<(), CommitError> {
2175 self.guest.confirm(path, base)
2176 }
2177
2178 fn supersede(&self, path: &str, base: &Stamp, with: &str) -> Result<()> {
2179 let request = Request {
2180 to: Some(with.to_owned()),
2181 stamp: Some(wire::WireStamp::from(base)),
2182 ..Request::default()
2183 };
2184 self.guest.verb(kind::SUPERSEDE, path, request)
2185 }
2186}
2187
2188#[cfg(test)]
2189mod tests {
2190 use super::*;
2191
2192 #[test]
2193 fn refused_uploads_release_their_bytes_and_share_one_guest_budget() {
2194 let directory = tempfile::tempdir().unwrap();
2195 std::fs::create_dir(directory.path().join("notebook")).unwrap();
2196 let served = Arc::new(Served {
2197 storage: crate::session::Notebook::open(
2198 directory.path().join("notebook"),
2199 directory.path().join("cache"),
2200 )
2201 .unwrap()
2202 .into_storage(),
2203 images: Mutex::default(),
2204 snapshots: Mutex::default(),
2205 puts: Mutex::default(),
2206 guests: Mutex::default(),
2207 writers: Mutex::default(),
2208 members: Weak::new(),
2209 host: Mutex::default(),
2210 room: Mutex::default(),
2211 });
2212 let peer = [1; 16];
2213 let put = |handle, offset, bytes| {
2214 served.answer(
2215 &peer,
2216 kind::PUT,
2217 Request {
2218 handle: Some(handle),
2219 offset: Some(offset),
2220 bytes: Some(bytes),
2221 ..Request::default()
2222 },
2223 )
2224 };
2225 put(1, 0, vec![1; 3]).unwrap();
2226 put(2, 0, vec![2; 2]).unwrap();
2227 assert!(put(1, 2, vec![3]).is_err());
2228 assert_eq!(
2229 served.puts.lock().unwrap().get(&(peer, 2)).unwrap(),
2230 &[2; 2]
2231 );
2232 assert!(!served.puts.lock().unwrap().contains_key(&(peer, 1)));
2233 put(1, 0, vec![4]).unwrap();
2234 assert!(put(1, 1, Vec::new()).is_err());
2235 served
2236 .puts
2237 .lock()
2238 .unwrap()
2239 .insert((peer, 3), vec![0; LIMIT - 2]);
2240 assert!(put(2, 2, vec![5]).is_err());
2241 assert!(!served.puts.lock().unwrap().contains_key(&(peer, 2)));
2242 assert!(put(3, (LIMIT - 2) as u64, vec![6; 3]).is_err());
2243 assert!(served.puts.lock().unwrap().is_empty());
2244 put(4, 0, vec![7]).unwrap();
2245 }
2246
2247 /// A commit's writes, found by comparing images, rebuild the image after it from the one
2248 /// before, and a change touching most of the file is left to be read whole.
2249 #[test]
2250 fn deltas_rebuild_the_image_after_a_commit() {
2251 let before: Vec<u8> = (0..20_000u32).map(|at| (at % 251) as u8).collect();
2252 let mut after = before.clone();
2253 after[3] ^= 1;
2254 after[5000..5010].fill(9);
2255 after[5050] ^= 1;
2256 after[17_000] ^= 1;
2257 after.extend_from_slice(&[7; 3000]);
2258 let writes = delta(&before, &after).unwrap();
2259 assert_eq!(
2260 writes.len(),
2261 4,
2262 "the header, two runs merged as one, one more, the tail"
2263 );
2264 assert_eq!(
2265 written(&before, after.len() as u64, &writes).unwrap(),
2266 after
2267 );
2268 let rewritten: Vec<u8> = before.iter().map(|byte| byte ^ 1).collect();
2269 assert!(delta(&before, &rewritten).is_none());
2270 assert!(delta(&before, &before[..10_000]).is_none());
2271 let outside = [Written {
2272 offset: after.len() as u64,
2273 bytes: vec![1],
2274 }];
2275 assert!(written(&before, after.len() as u64, &outside).is_none());
2276 }
2277
2278 #[test]
2279 fn differential_reads_include_metadata_in_the_chunk_budget() {
2280 let before = vec![0; CHUNK * 4];
2281 let mut after = before.clone();
2282 after.resize(before.len() + CHUNK - 1024, 1);
2283 assert!(delta(&before, &after).is_none());
2284 after.truncate(before.len() + CHUNK / 2);
2285 let writes = delta(&before, &after).unwrap();
2286 assert!(minicbor::to_vec(&writes).unwrap().len() <= CHUNK);
2287 assert_eq!(
2288 written(&before, after.len() as u64, &writes).unwrap(),
2289 after
2290 );
2291 }
2292}