Skip to content

Commit

Permalink
Make get pair record async to avoid locks
Browse files Browse the repository at this point in the history
  • Loading branch information
jkcoxson committed Jan 29, 2025
1 parent 6e224b9 commit 8ecbde6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
13 changes: 8 additions & 5 deletions src/devices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
use std::{collections::HashMap, io::Read, net::IpAddr, path::PathBuf, sync::Arc};

use log::{debug, info, trace, warn};
use tokio::sync::{mpsc::UnboundedSender, Mutex};
use tokio::{
io::AsyncReadExt,
sync::{mpsc::UnboundedSender, Mutex},
};

use crate::heartbeat;

Expand Down Expand Up @@ -84,7 +87,7 @@ impl SharedDevices {
}
self.last_index += 1;
self.last_interface_index += 1;
let pairing_file = self.get_pairing_record(udid.clone())?;
let pairing_file = self.get_pairing_record(udid.clone()).await?;
let pairing_file = idevice::pairing_file::PairingFile::from_bytes(&pairing_file)?;

let handle = if self.use_heartbeat {
Expand Down Expand Up @@ -153,7 +156,7 @@ impl SharedDevices {
.unwrap();
self.devices.remove(udid);
}
pub fn get_pairing_record(&self, udid: String) -> Result<Vec<u8>, std::io::Error> {
pub async fn get_pairing_record(&self, udid: String) -> Result<Vec<u8>, std::io::Error> {
let path = PathBuf::from(self.plist_storage.clone()).join(format!("{}.plist", udid));
info!("Attempting to read pairing file: {path:?}");
if !path.exists() {
Expand All @@ -165,9 +168,9 @@ impl SharedDevices {
}
// Read the file
info!("Reading pairing record for device: {:?}", udid);
let mut file = std::fs::File::open(path).unwrap();
let mut file = tokio::fs::File::open(path).await?;
let mut contents = Vec::new();
file.read_to_end(&mut contents).unwrap();
file.read_to_end(&mut contents).await?;
Ok(contents)
}
pub fn get_buid(&self) -> Result<String, std::io::Error> {
Expand Down
10 changes: 6 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,21 +382,23 @@ async fn handle_stream(
}
"ReadPairRecord" => {
let lock = data.lock().await;
let pair_file = match lock.get_pairing_record(
match parsed.plist.get("PairRecordID") {
let pair_file = match lock
.get_pairing_record(match parsed.plist.get("PairRecordID") {
Some(plist::Value::String(p)) => p.to_owned(),
_ => {
warn!("Request did not contain PairRecordID");
return;
}
},
) {
})
.await
{
Ok(pair_file) => pair_file,
Err(_) => {
// Unimplemented
return;
}
};
std::mem::drop(lock);

let mut p = plist::Dictionary::new();
p.insert("PairRecordData".into(), plist::Value::Data(pair_file));
Expand Down

0 comments on commit 8ecbde6

Please sign in to comment.