This commit is contained in:
Nicholas Wehr
2025-08-21 22:43:03 -07:00
parent 2a653857aa
commit 3bdc491aba

View File

@ -91,7 +91,12 @@ impl Client {
pub fn new(root: impl Into<PathBuf>, peers: Vec<PeerRecord>) -> Self { pub fn new(root: impl Into<PathBuf>, peers: Vec<PeerRecord>) -> Self {
let root: PathBuf = root.into(); let root: PathBuf = root.into();
let n = find_max_number_file(&root).unwrap(); let n = find_max_number_file(&root).unwrap();
Self { root, peers: Arc::new(Mutex::new(peers)), timeout: Duration::from_secs(3), max_block: n } Self {
root,
peers: Arc::new(Mutex::new(peers)),
timeout: Duration::from_secs(3),
max_block: n,
}
} }
pub fn update_peers(&self, peers: Vec<PeerRecord>) { pub fn update_peers(&self, peers: Vec<PeerRecord>) {
*self.peers.lock() = peers; *self.peers.lock() = peers;
@ -187,7 +192,7 @@ fn find_max_number_file(root: &Path) -> Result<u64> {
Ok(()) Ok(())
} }
let mut best = None; let mut best = Some(0);
walk(root, &mut best)?; walk(root, &mut best)?;
Ok(best.expect("cannot find block files")) Ok(best.expect("cannot find block files"))
} }
@ -253,11 +258,19 @@ async fn handle_conn(
) -> Result<(), HlfsError> { ) -> Result<(), HlfsError> {
let mut op = [0u8; 1]; let mut op = [0u8; 1];
sock.read_exact(&mut op).await?; sock.read_exact(&mut op).await?;
if op[0] != OP_REQ_BLOCK { if op[0] != OP_REQ_BLOCK && op[0] != OP_REQ_MAX_BLOCK {
warn!(%addr, "hlfs: bad op"); warn!(%addr, "hlfs: bad op");
return Err(HlfsError::Proto); return Err(HlfsError::Proto);
} }
if op[0] == OP_REQ_MAX_BLOCK {
let mut b = BytesMut::with_capacity(1 + 8);
b.put_u8(OP_RES_MAX_BLOCK);
put_u64(&mut b, max_block);
let _ = sock.write_all(&b).await;
return Ok(());
}
let mut num = [0u8; 8]; let mut num = [0u8; 8];
sock.read_exact(&mut num).await?; sock.read_exact(&mut num).await?;
let number = u64::from_le_bytes(num); let number = u64::from_le_bytes(num);