@@ -5,7 +5,7 @@ use std::{collections::HashMap, io::Read, net::IpAddr, path::PathBuf, sync::Arc}
55use log:: { debug, info, trace, warn} ;
66use tokio:: {
77 io:: AsyncReadExt ,
8- sync:: { mpsc :: UnboundedSender , Mutex } ,
8+ sync:: { oneshot :: Sender , Mutex } ,
99} ;
1010
1111use crate :: heartbeat;
@@ -29,7 +29,7 @@ pub struct MuxerDevice {
2929
3030 // Network types
3131 pub network_address : Option < IpAddr > ,
32- pub heartbeat_handle : Option < UnboundedSender < ( ) > > ,
32+ pub heartbeat_handle : Option < Sender < ( ) > > ,
3333 pub service_name : Option < String > ,
3434
3535 // USB types
@@ -125,16 +125,21 @@ impl SharedDevices {
125125 return ;
126126 }
127127 info ! ( "Removing device: {:?}" , udid) ;
128- let _ = & self
129- . devices
130- . get ( udid)
131- . unwrap ( )
132- . heartbeat_handle
133- . as_ref ( )
134- . unwrap ( )
135- . send ( ( ) )
136- . unwrap ( ) ;
137- self . devices . remove ( udid) ;
128+ match self . devices . remove ( udid) {
129+ Some ( d) => match d. heartbeat_handle {
130+ Some ( h) => {
131+ if let Err ( e) = h. send ( ( ) ) {
132+ warn ! ( "Couldn't send kill signal to heartbeat thread: {e:?}" ) ;
133+ }
134+ }
135+ None => {
136+ warn ! ( "Heartbeat handle option has none, can't kill" ) ;
137+ }
138+ } ,
139+ None => {
140+ warn ! ( "No heartbeat handle found for device" ) ;
141+ }
142+ } ;
138143 }
139144 pub async fn get_pairing_record ( & self , udid : String ) -> Result < Vec < u8 > , std:: io:: Error > {
140145 let path = PathBuf :: from ( self . plist_storage . clone ( ) ) . join ( format ! ( "{}.plist" , udid) ) ;
@@ -166,13 +171,22 @@ impl SharedDevices {
166171 }
167172 // Read the file to a string
168173 debug ! ( "Reading SystemConfiguration.plist" ) ;
169- let mut file = std:: fs:: File :: open ( path) . unwrap ( ) ;
174+ let mut file = std:: fs:: File :: open ( path) ? ;
170175 let mut contents = Vec :: new ( ) ;
171- file. read_to_end ( & mut contents) . unwrap ( ) ;
176+ file. read_to_end ( & mut contents) ? ;
172177
173178 // Parse the string into a plist
174179 debug ! ( "Parsing SystemConfiguration.plist" ) ;
175- let plist = plist:: from_bytes :: < plist:: Dictionary > ( & contents) . unwrap ( ) ;
180+ let plist = match plist:: from_bytes :: < plist:: Dictionary > ( & contents) {
181+ Ok ( p) => p,
182+ Err ( e) => {
183+ log:: error!( "Failed to parse plist: {e:?}" ) ;
184+ return Err ( std:: io:: Error :: new (
185+ std:: io:: ErrorKind :: InvalidInput ,
186+ "unable to parse plist" ,
187+ ) ) ;
188+ }
189+ } ;
176190 match plist. get ( "SystemBUID" ) {
177191 Some ( plist:: Value :: String ( b) ) => Ok ( b. to_owned ( ) ) ,
178192 _ => Err ( std:: io:: Error :: new (
@@ -187,14 +201,32 @@ impl SharedDevices {
187201 trace ! ( "Updating plist cache" ) ;
188202 let path = PathBuf :: from ( self . plist_storage . clone ( ) ) ;
189203 for entry in std:: fs:: read_dir ( path) . expect ( "Plist storage is unreadable!!" ) {
190- let entry = entry. unwrap ( ) ;
204+ let entry = match entry {
205+ Ok ( e) => e,
206+ Err ( e) => {
207+ warn ! ( "Unable to read entry in plist storage: {e:?}" ) ;
208+ continue ;
209+ }
210+ } ;
191211 let path = entry. path ( ) ;
192212 trace ! ( "Attempting to read {:?}" , path) ;
193213 if path. is_file ( ) {
194- let mut file = tokio:: fs:: File :: open ( & path) . await . unwrap ( ) ;
214+ let mut file = match tokio:: fs:: File :: open ( & path) . await {
215+ Ok ( f) => f,
216+ Err ( e) => {
217+ warn ! ( "Unable to read plist storage entry to memory: {e:?}" ) ;
218+ continue ;
219+ }
220+ } ;
195221 let mut contents = Vec :: new ( ) ;
196222 let plist: plist:: Dictionary = match file. read_to_end ( & mut contents) . await {
197- Ok ( _) => plist:: from_bytes ( & contents) . unwrap ( ) ,
223+ Ok ( _) => match plist:: from_bytes ( & contents) {
224+ Ok ( p) => p,
225+ Err ( e) => {
226+ warn ! ( "Unable to parse entry file to plist: {e:?}" ) ;
227+ continue ;
228+ }
229+ } ,
198230 Err ( e) => {
199231 trace ! ( "Could not read plist to memory: {e:?}" ) ;
200232 continue ;
@@ -223,20 +255,30 @@ impl SharedDevices {
223255 // This is just used as a last resort, but might not be correct so we'll pass a warning
224256 warn ! ( "Using the file name as the UDID" ) ;
225257 match path. file_name ( ) {
226- Some ( f) => {
227- f. to_str ( ) . unwrap ( ) . split ( '.' ) . collect :: < Vec < & str > > ( ) [ 0 ] . to_string ( )
228- }
258+ Some ( f) => match f. to_str ( ) {
259+ Some ( f) => f. split ( '.' ) . collect :: < Vec < & str > > ( ) [ 0 ] . to_string ( ) ,
260+ None => {
261+ warn ! ( "Failed to get entry file name string" ) ;
262+ continue ;
263+ }
264+ } ,
229265 None => {
230266 trace ! ( "File had no name" ) ;
231267 continue ;
232268 }
233269 }
234270 } ;
235271
236- self . known_mac_addresses . insert (
237- mac_addr. to_owned ( ) ,
238- path. file_stem ( ) . unwrap ( ) . to_string_lossy ( ) . to_string ( ) ,
239- ) ;
272+ let stem = match path. file_stem ( ) {
273+ Some ( s) => s,
274+ None => {
275+ warn ! ( "Failed to get file stem for entry" ) ;
276+ continue ;
277+ }
278+ } ;
279+
280+ self . known_mac_addresses
281+ . insert ( mac_addr. to_owned ( ) , stem. to_string_lossy ( ) . to_string ( ) ) ;
240282 if self . paired_udids . contains ( & udid) {
241283 trace ! ( "Cache already contained this UDID" ) ;
242284 continue ;
@@ -277,15 +319,22 @@ impl From<&MuxerDevice> for plist::Dictionary {
277319 if device. connection_type == "Network" {
278320 p. insert (
279321 "EscapedFullServiceName" . into ( ) ,
280- device. service_name . clone ( ) . unwrap ( ) . into ( ) ,
322+ device
323+ . service_name
324+ . clone ( )
325+ . expect ( "Network device, but no service name" )
326+ . into ( ) ,
281327 ) ;
282328 }
283329 p. insert ( "InterfaceIndex" . into ( ) , device. interface_index . into ( ) ) ;
284330
285331 // Reassemble the network address back into bytes
286332 if device. connection_type == "Network" {
287333 let mut data = [ 0u8 ; 152 ] ;
288- match device. network_address . unwrap ( ) {
334+ match device
335+ . network_address
336+ . expect ( "Network device, but no address" )
337+ {
289338 IpAddr :: V4 ( ip_addr) => {
290339 data[ 0 ] = 0x02 ;
291340 data[ 1 ] = 0x00 ;
0 commit comments