use std::fs::File;
use std::io::Read;
use std::path::{Path, PathBuf};
use sapphire_sync::{ContentHash, ContentHasher};
use tokio::io::AsyncWrite;
use tokio::sync::mpsc;
use tokio::task::JoinHandle;
use crate::CHUNK;
use crate::error::{Error, Result};
use crate::frame::{write_blob_chunk, write_blob_end, write_blob_start, write_control};
use crate::message::Message;
use crate::progress::Progress;
pub(crate) enum Out {
Control(Message),
Blob {
hash: ContentHash,
path: PathBuf,
len: u64,
},
}
#[derive(Clone)]
pub(crate) struct Outbox {
urgent: mpsc::UnboundedSender<Message>,
bulk: mpsc::UnboundedSender<Out>,
}
impl Outbox {
pub(crate) fn send(&self, out: Out) -> Result<()> {
let sent = match out {
Out::Control(msg @ (Message::Updates(_) | Message::Done)) => {
self.bulk.send(Out::Control(msg)).is_ok()
}
Out::Control(msg) => self.urgent.send(msg).is_ok(),
blob @ Out::Blob { .. } => self.bulk.send(blob).is_ok(),
};
if sent { Ok(()) } else { Err(Error::Closed) }
}
}
pub(crate) fn spawn_writer<W>(w: W, progress: Progress) -> (Outbox, JoinHandle<()>)
where
W: AsyncWrite + Unpin + Send + 'static,
{
let (urgent_tx, urgent) = mpsc::unbounded_channel();
let (bulk_tx, bulk) = mpsc::unbounded_channel();
let task = tokio::spawn(async move {
let _ = run(w, urgent, bulk, progress).await;
});
(
Outbox {
urgent: urgent_tx,
bulk: bulk_tx,
},
task,
)
}
async fn run<W>(
mut w: W,
mut urgent: mpsc::UnboundedReceiver<Message>,
mut bulk: mpsc::UnboundedReceiver<Out>,
progress: Progress,
) -> Result<()>
where
W: AsyncWrite + Unpin,
{
loop {
tokio::select! {
biased;
Some(msg) = urgent.recv() => write_control(&mut w, &msg).await?,
Some(out) = bulk.recv() => match out {
Out::Control(msg) => write_control(&mut w, &msg).await?,
Out::Blob { hash, path, len } => {
stream_blob(&mut w, &mut urgent, &progress, hash, &path, len).await?
}
},
else => break,
}
progress.touch();
}
Ok(())
}
async fn stream_blob<W>(
w: &mut W,
urgent: &mut mpsc::UnboundedReceiver<Message>,
progress: &Progress,
hash: ContentHash,
path: &Path,
len: u64,
) -> Result<()>
where
W: AsyncWrite + Unpin,
{
let Ok(mut file) = File::open(path) else {
return write_control(w, &Message::Missing(hash)).await;
};
write_blob_start(w, &hash, len).await?;
progress.touch();
let mut buf = vec![0u8; CHUNK];
let mut hasher = ContentHasher::new();
let mut sent = 0u64;
while sent < len {
let want = (len - sent).min(CHUNK as u64) as usize;
let n = fill(&mut file, &mut buf[..want]).unwrap_or(0);
if n == 0 {
break;
}
while let Ok(msg) = urgent.try_recv() {
write_control(w, &msg).await?;
progress.touch();
}
hasher.update(&buf[..n]);
write_blob_chunk(w, &buf[..n]).await?;
sent += n as u64;
progress.touch();
}
if sent == len && hasher.finish() == hash {
write_blob_end(w).await
} else {
write_control(w, &Message::Aborted(hash)).await
}
}
fn fill(file: &mut File, buf: &mut [u8]) -> std::io::Result<usize> {
let mut filled = 0;
while filled < buf.len() {
match file.read(&mut buf[filled..]) {
Ok(0) => break,
Ok(n) => filled += n,
Err(e) if e.kind() == std::io::ErrorKind::Interrupted => {}
Err(e) => return Err(e),
}
}
Ok(filled)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::frame::{Frame, read_frame};
use tokio::io::DuplexStream;
#[derive(Debug, PartialEq)]
enum Seen {
Start(ContentHash, u64),
Chunk(usize),
End,
Control(String),
}
fn seen(frame: Frame) -> Seen {
match frame {
Frame::BlobStart { hash, len } => Seen::Start(hash, len),
Frame::BlobChunk(bytes) => Seen::Chunk(bytes.len()),
Frame::BlobEnd => Seen::End,
Frame::Control(msg) => Seen::Control(format!("{msg:?}")),
}
}
fn control(msg: &Message) -> Seen {
Seen::Control(format!("{msg:?}"))
}
fn file_with(dir: &tempfile::TempDir, name: &str, bytes: &[u8]) -> PathBuf {
let path = dir.path().join(name);
std::fs::write(&path, bytes).unwrap();
path
}
fn patterned(len: usize) -> Vec<u8> {
(0..len).map(|i| (i % 251) as u8).collect()
}
async fn read_all(mut r: DuplexStream) -> (Vec<Seen>, Vec<u8>) {
let mut frames = Vec::new();
let mut content = Vec::new();
while let Some(frame) = read_frame(&mut r).await.unwrap() {
if let Frame::BlobChunk(bytes) = &frame {
content.extend_from_slice(bytes);
}
frames.push(seen(frame));
}
(frames, content)
}
async fn send_all(items: Vec<Out>) -> (Vec<Seen>, Vec<u8>) {
let (w, r) = tokio::io::duplex(64 * 1024);
let (outbox, task) = spawn_writer(w, Progress::new());
let reading = tokio::spawn(read_all(r));
for item in items {
outbox.send(item).unwrap();
}
drop(outbox);
task.await.unwrap();
reading.await.unwrap()
}
fn blob(hash: ContentHash, path: PathBuf, len: usize) -> Out {
Out::Blob {
hash,
path,
len: len as u64,
}
}
#[tokio::test]
async fn a_file_is_sent_as_start_chunks_end() {
let dir = tempfile::tempdir().unwrap();
let bytes = patterned(2 * CHUNK + 1);
let hash = ContentHash::of_bytes(&bytes);
let path = file_with(&dir, "f", &bytes);
let (frames, content) = send_all(vec![blob(hash, path, bytes.len())]).await;
assert_eq!(
frames,
[
Seen::Start(hash, bytes.len() as u64),
Seen::Chunk(CHUNK),
Seen::Chunk(CHUNK),
Seen::Chunk(1),
Seen::End,
]
);
assert!(content == bytes);
}
#[tokio::test]
async fn a_file_of_exactly_one_chunk_sends_one_chunk() {
let dir = tempfile::tempdir().unwrap();
let bytes = patterned(CHUNK);
let hash = ContentHash::of_bytes(&bytes);
let path = file_with(&dir, "f", &bytes);
let (frames, _) = send_all(vec![blob(hash, path, CHUNK)]).await;
assert_eq!(
frames,
[
Seen::Start(hash, CHUNK as u64),
Seen::Chunk(CHUNK),
Seen::End
]
);
}
#[tokio::test]
async fn an_empty_file_sends_start_and_end() {
let dir = tempfile::tempdir().unwrap();
let hash = ContentHash::of_bytes(b"");
let path = file_with(&dir, "f", b"");
let (frames, _) = send_all(vec![blob(hash, path, 0)]).await;
assert_eq!(frames, [Seen::Start(hash, 0), Seen::End]);
}
#[tokio::test]
async fn a_changed_file_is_aborted() {
let dir = tempfile::tempdir().unwrap();
let bytes = patterned(CHUNK + 10);
let path = file_with(&dir, "f", &bytes);
let hash = ContentHash::of_bytes(&vec![0u8; bytes.len()]);
let (frames, _) = send_all(vec![blob(hash, path, bytes.len())]).await;
assert_eq!(
frames,
[
Seen::Start(hash, bytes.len() as u64),
Seen::Chunk(CHUNK),
Seen::Chunk(10),
control(&Message::Aborted(hash)),
]
);
}
#[tokio::test]
async fn a_vanished_file_is_answered_missing() {
let dir = tempfile::tempdir().unwrap();
let hash = ContentHash::of_bytes(b"gone");
let path = dir.path().join("never-written");
let (frames, _) = send_all(vec![blob(hash, path, 4)]).await;
assert_eq!(frames, [control(&Message::Missing(hash))]);
}
#[tokio::test]
async fn urgent_control_overtakes_a_blob() {
let dir = tempfile::tempdir().unwrap();
let bytes = patterned(8 * CHUNK);
let hash = ContentHash::of_bytes(&bytes);
let path = file_with(&dir, "f", &bytes);
let want = Message::Want(ContentHash::of_bytes(b"other"));
let (w, mut r) = tokio::io::duplex(CHUNK);
let (outbox, task) = spawn_writer(w, Progress::new());
outbox.send(blob(hash, path, bytes.len())).unwrap();
let first = read_frame(&mut r).await.unwrap().unwrap();
assert!(matches!(first, Frame::BlobStart { .. }), "got {first:?}");
outbox.send(Out::Control(want.clone())).unwrap();
drop(outbox);
let (frames, _) = read_all(r).await;
task.await.unwrap();
let at = |s: &Seen| frames.iter().position(|f| f == s).unwrap();
assert!(at(&control(&want)) < at(&Seen::End), "{frames:?}");
}
#[tokio::test]
async fn bulk_keeps_its_order() {
let dir = tempfile::tempdir().unwrap();
let hash = ContentHash::of_bytes(b"small");
let path = file_with(&dir, "f", b"small");
let (frames, _) = send_all(vec![
blob(hash, path, 5),
Out::Control(Message::Updates(vec![])),
Out::Control(Message::Done),
])
.await;
assert_eq!(
frames,
[
Seen::Start(hash, 5),
Seen::Chunk(5),
Seen::End,
control(&Message::Updates(vec![])),
control(&Message::Done),
]
);
}
#[tokio::test]
async fn the_writer_drains_both_queues_before_exiting() {
let dir = tempfile::tempdir().unwrap();
let hash = ContentHash::of_bytes(b"small");
let path = file_with(&dir, "f", b"small");
let want = Message::Want(hash);
let (frames, _) = send_all(vec![
Out::Control(Message::Updates(vec![])),
Out::Control(want.clone()),
blob(hash, path, 5),
Out::Control(Message::Settled),
Out::Control(Message::Done),
])
.await;
for expected in [
control(&Message::Updates(vec![])),
control(&want),
Seen::End,
control(&Message::Settled),
control(&Message::Done),
] {
assert!(frames.contains(&expected), "{expected:?} in {frames:?}");
}
assert_eq!(frames.len(), 7, "{frames:?}");
}
}