`atomic sandbox materialize` inside a remote sandbox: write the view's tree into `root` and (re)make its cache. Returns the number of entries and the view. Every entry goes through the kernel's path-checked writer (no climbing out, no writing the pointer or the cache). A fresh cache also clears a sandbox that had fallen behind.
(root: &Path, prefix: Option<&str>)
| 377 | /// out, no writing the pointer or the cache). A fresh cache also clears a |
| 378 | /// sandbox that had fallen behind. |
| 379 | pub fn materialize(root: &Path, prefix: Option<&str>) -> CliResult<(u64, String)> { |
| 380 | use tokio_stream::StreamExt; |
| 381 | |
| 382 | let internal = |e: String| CliError::Internal(anyhow::anyhow!(e)); |
| 383 | let pointer = RemoteSandboxPointer::read(root).ok_or_else(|| { |
| 384 | internal(format!( |
| 385 | "{} holds no remote sandbox pointer", |
| 386 | root.display() |
| 387 | )) |
| 388 | })?; |
| 389 | let root_for_thread = root.to_path_buf(); |
| 390 | let prefix = prefix.map(|p| p.to_string()); |
| 391 | let (entries, skeleton) = std::thread::spawn(move || { |
| 392 | let rt = tokio::runtime::Builder::new_current_thread() |
| 393 | .enable_all() |
| 394 | .build() |
| 395 | .map_err(|e| e.to_string())?; |
| 396 | rt.block_on(async move { |
| 397 | let repository = reference(&pointer)?; |
| 398 | let channel = connect(Some(pointer.token.as_bytes())).await?; |
| 399 | let mut stream = sandbox_client(channel) |
| 400 | .materialize(pb::MaterializeRequest { |
| 401 | repository: Some(repository), |
| 402 | target: Some(target(&pointer)), |
| 403 | path_prefix: prefix, |
| 404 | }) |
| 405 | .await |
| 406 | .map_err(refused)? |
| 407 | .into_inner(); |
| 408 | let mut written = 0u64; |
| 409 | while let Some(frame) = stream.next().await { |
| 410 | match frame.map_err(refused)?.frame { |
| 411 | Some(pb::materialize_frame::Frame::Entry(entry)) => { |
| 412 | atomic_repository::write_sandbox_entry( |
| 413 | &root_for_thread, |
| 414 | &entry.path, |
| 415 | wire::tree_entry_kind(&entry)?, |
| 416 | entry.mode.unwrap_or(0o644), |
| 417 | entry.inline.as_deref().unwrap_or_default(), |
| 418 | ) |
| 419 | .map_err(|e| e.to_string())?; |
| 420 | written += 1; |
| 421 | } |
| 422 | Some(pb::materialize_frame::Frame::Done(done)) => { |
| 423 | if done.entries != written { |
| 424 | return Err(format!( |
| 425 | "the server sent {written} entries but said {}", |
| 426 | done.entries |
| 427 | )); |
| 428 | } |
| 429 | let skeleton = wire::skeleton_from_proto( |
| 430 | &done.skeleton.ok_or("no skeleton in the summary")?, |
| 431 | )?; |
| 432 | return Ok((written, skeleton)); |
| 433 | } |
| 434 | None => return Err("an empty materialize frame".to_string()), |
| 435 | } |
| 436 | } |