@@ -5,7 +5,7 @@ use std::{collections::HashMap, io::Read, net::IpAddr, path::PathBuf, sync::Arc}
5
5
use log:: { debug, info, trace, warn} ;
6
6
use tokio:: {
7
7
io:: AsyncReadExt ,
8
- sync:: { mpsc :: UnboundedSender , Mutex } ,
8
+ sync:: { oneshot :: Sender , Mutex } ,
9
9
} ;
10
10
11
11
use crate :: heartbeat;
@@ -29,7 +29,7 @@ pub struct MuxerDevice {
29
29
30
30
// Network types
31
31
pub network_address : Option < IpAddr > ,
32
- pub heartbeat_handle : Option < UnboundedSender < ( ) > > ,
32
+ pub heartbeat_handle : Option < Sender < ( ) > > ,
33
33
pub service_name : Option < String > ,
34
34
35
35
// USB types
@@ -125,16 +125,21 @@ impl SharedDevices {
125
125
return ;
126
126
}
127
127
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
+ } ;
138
143
}
139
144
pub async fn get_pairing_record ( & self , udid : String ) -> Result < Vec < u8 > , std:: io:: Error > {
140
145
let path = PathBuf :: from ( self . plist_storage . clone ( ) ) . join ( format ! ( "{}.plist" , udid) ) ;
@@ -166,13 +171,22 @@ impl SharedDevices {
166
171
}
167
172
// Read the file to a string
168
173
debug ! ( "Reading SystemConfiguration.plist" ) ;
169
- let mut file = std:: fs:: File :: open ( path) . unwrap ( ) ;
174
+ let mut file = std:: fs:: File :: open ( path) ? ;
170
175
let mut contents = Vec :: new ( ) ;
171
- file. read_to_end ( & mut contents) . unwrap ( ) ;
176
+ file. read_to_end ( & mut contents) ? ;
172
177
173
178
// Parse the string into a plist
174
179
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
+ } ;
176
190
match plist. get ( "SystemBUID" ) {
177
191
Some ( plist:: Value :: String ( b) ) => Ok ( b. to_owned ( ) ) ,
178
192
_ => Err ( std:: io:: Error :: new (
@@ -187,14 +201,32 @@ impl SharedDevices {
187
201
trace ! ( "Updating plist cache" ) ;
188
202
let path = PathBuf :: from ( self . plist_storage . clone ( ) ) ;
189
203
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
+ } ;
191
211
let path = entry. path ( ) ;
192
212
trace ! ( "Attempting to read {:?}" , path) ;
193
213
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
+ } ;
195
221
let mut contents = Vec :: new ( ) ;
196
222
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
+ } ,
198
230
Err ( e) => {
199
231
trace ! ( "Could not read plist to memory: {e:?}" ) ;
200
232
continue ;
@@ -223,20 +255,30 @@ impl SharedDevices {
223
255
// This is just used as a last resort, but might not be correct so we'll pass a warning
224
256
warn ! ( "Using the file name as the UDID" ) ;
225
257
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
+ } ,
229
265
None => {
230
266
trace ! ( "File had no name" ) ;
231
267
continue ;
232
268
}
233
269
}
234
270
} ;
235
271
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 ( ) ) ;
240
282
if self . paired_udids . contains ( & udid) {
241
283
trace ! ( "Cache already contained this UDID" ) ;
242
284
continue ;
@@ -277,15 +319,22 @@ impl From<&MuxerDevice> for plist::Dictionary {
277
319
if device. connection_type == "Network" {
278
320
p. insert (
279
321
"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 ( ) ,
281
327
) ;
282
328
}
283
329
p. insert ( "InterfaceIndex" . into ( ) , device. interface_index . into ( ) ) ;
284
330
285
331
// Reassemble the network address back into bytes
286
332
if device. connection_type == "Network" {
287
333
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
+ {
289
338
IpAddr :: V4 ( ip_addr) => {
290
339
data[ 0 ] = 0x02 ;
291
340
data[ 1 ] = 0x00 ;
0 commit comments