| 334 | #pragma mark - |
| 335 | |
| 336 | static |
| 337 | bool doSearch(searchType search, char const* searchValue, char *answer, size_t answer_maxsize) |
| 338 | { |
| 339 | mach_port_t masterPort; |
| 340 | kern_return_t kernResult; |
| 341 | CFMutableDictionaryRef classesToMatch; |
| 342 | io_iterator_t mediaIterator; |
| 343 | |
| 344 | assert(searchValue); |
| 345 | |
| 346 | kernResult = IOMasterPort( MACH_PORT_NULL, &masterPort ); |
| 347 | if ( kernResult != KERN_SUCCESS ) { |
| 348 | fprintf(stderr, "IOMasterPort returned %d\n", kernResult ); |
| 349 | return false; |
| 350 | } |
| 351 | |
| 352 | classesToMatch = IOServiceMatching( kIOMediaClass ); // All Storage Media |
| 353 | if ( classesToMatch == NULL ) { |
| 354 | fprintf( stderr, "IOServiceMatching returned a NULL dictionary.\n" ); |
| 355 | return false; |
| 356 | } |
| 357 | CFDictionarySetValue( classesToMatch, |
| 358 | CFSTR( kIOMediaLeafKey ), kCFBooleanTrue ); // Is a leaf device |
| 359 | |
| 360 | CFStringRef searchCFString = CFStringCreateWithCString(NULL, searchValue, kCFStringEncodingASCII); |
| 361 | switch (search) { |
| 362 | case search_uuid: |
| 363 | if ( ! isUUID(searchValue) ) { |
| 364 | fprintf(stderr, "Error: invalid UUID value '%s'\n", searchValue); |
| 365 | CFRelease(searchCFString); |
| 366 | CFRelease(classesToMatch); // Not use so release it |
| 367 | return false; |
| 368 | } |
| 369 | CFMutableStringRef tmpMutableStr = CFStringCreateMutableCopy(0, 36, searchCFString); |
| 370 | CFStringUppercase(tmpMutableStr, 0); // Convert to Uppercase |
| 371 | CFDictionarySetValue( classesToMatch, CFSTR( kIOMediaUUIDKey ), tmpMutableStr); |
| 372 | CFRelease(tmpMutableStr); |
| 373 | break; |
| 374 | |
| 375 | case search_undefined: |
| 376 | CFRelease(searchCFString); |
| 377 | CFRelease(classesToMatch); // Not use so release it |
| 378 | return false; |
| 379 | break; |
| 380 | } |
| 381 | CFRelease(searchCFString); |
| 382 | |
| 383 | // classesToMatch is consume by IOServiceGetMatchingServices |
| 384 | kernResult = IOServiceGetMatchingServices( masterPort, |
| 385 | classesToMatch, &mediaIterator ); |
| 386 | |
| 387 | if ( kernResult != KERN_SUCCESS ) { |
| 388 | fprintf(stderr, "No Block Storage media found.\n kernResult = %d\n", kernResult ); |
| 389 | return false; |
| 390 | } |
| 391 | |
| 392 | io_service_t service; |
| 393 | CFMutableDictionaryRef serviceProperties = NULL; |