This commit is contained in:
sxyazi 2025-01-17 18:32:55 +08:00
parent 2ab734a948
commit 52054c916d
No known key found for this signature in database
2 changed files with 14 additions and 4 deletions

View file

@ -11,7 +11,7 @@ pub struct CFDict(CFDictionaryRef);
impl CFDict { impl CFDict {
pub fn take(dict: CFDictionaryRef) -> Result<Self> { pub fn take(dict: CFDictionaryRef) -> Result<Self> {
if dict.is_null() { if dict.is_null() {
bail!("Null pointer"); bail!("Cannot take a null pointer");
} }
Ok(Self(dict)) Ok(Self(dict))
} }
@ -19,7 +19,9 @@ impl CFDict {
pub fn value(&self, key: &str) -> Result<*const c_void> { pub fn value(&self, key: &str) -> Result<*const c_void> {
let key_ = CFString::new(key)?; let key_ = CFString::new(key)?;
let mut value = std::ptr::null(); let mut value = std::ptr::null();
if unsafe { CFDictionaryGetValueIfPresent(self.0, key_.as_void_ptr(), &mut value) } == 0 { if unsafe { CFDictionaryGetValueIfPresent(self.0, key_.as_void_ptr(), &mut value) } == 0
|| value.is_null()
{
bail!("Cannot get the value for the key `{key}`"); bail!("Cannot get the value for the key `{key}`");
} }
Ok(value) Ok(value)

View file

@ -17,6 +17,10 @@ impl Partitions {
{ {
tokio::task::spawn_blocking(move || { tokio::task::spawn_blocking(move || {
let session = unsafe { DASessionCreate(kCFAllocatorDefault) }; let session = unsafe { DASessionCreate(kCFAllocatorDefault) };
if session.is_null() {
return error!("Cannot create a disk arbitration session");
}
defer! { unsafe { CFRelease(session) } };
extern "C" fn on_appeared(_disk: *const c_void, context: *mut c_void) { extern "C" fn on_appeared(_disk: *const c_void, context: *mut c_void) {
let boxed = context as *mut Box<dyn Fn()>; let boxed = context as *mut Box<dyn Fn()>;
@ -85,6 +89,10 @@ impl Partitions {
fn all_partitions(names: Vec<CString>) -> Result<Vec<Partition>> { fn all_partitions(names: Vec<CString>) -> Result<Vec<Partition>> {
let session = unsafe { DASessionCreate(kCFAllocatorDefault) }; let session = unsafe { DASessionCreate(kCFAllocatorDefault) };
if session.is_null() {
bail!("Cannot create a disk arbitration session");
}
defer! { unsafe { CFRelease(session) } };
let mut disks = Vec::with_capacity(names.len()); let mut disks = Vec::with_capacity(names.len());
for name in names { for name in names {
@ -93,7 +101,7 @@ impl Partitions {
continue; continue;
} }
defer! { unsafe { CFRelease(disk as _) } }; defer! { unsafe { CFRelease(disk) } };
let Ok(dict) = CFDict::take(unsafe { DADiskCopyDescription(disk) }) else { let Ok(dict) = CFDict::take(unsafe { DADiskCopyDescription(disk) }) else {
continue; continue;
}; };
@ -120,7 +128,7 @@ impl Partitions {
}; };
if result != 0 { if result != 0 {
bail!("IOServiceGetMatchingServices failed"); bail!("Cannot get the IO matching services");
} }
defer! { unsafe { IOObjectRelease(iterator); } }; defer! { unsafe { IOObjectRelease(iterator); } };