Debian Codegraph @

src/collection/disks/unix/macos/io_kit/io_object.rs

raw symbols SCIP
1 //! Based on [heim's](https://github.com/heim-rs/heim/blob/master/heim-common/src/sys/macos/iokit/io_object.rs)
2 //! implementation.
3
4 use std::mem;
5
6 use anyhow::{anyhow, bail};
7 use core_foundation::{
8 base::{CFType, TCFType, ToVoid, kCFAllocatorDefault},
9 dictionary::{
10 CFDictionary, CFDictionaryGetTypeID, CFDictionaryRef, CFMutableDictionary,
11 CFMutableDictionaryRef,
12 },
13 number::{CFNumber, CFNumberGetTypeID},
14 string::{CFString, CFStringGetTypeID},
15 };
16 use mach2::kern_return;
17
18 use super::bindings::*;
19
20 /// Safe wrapper around the IOKit `io_object_t` type.
21 #[derive(Debug)]
22 pub struct IoObject(io_object_t);
23
24 impl IoObject {
25 /// Returns a typed dictionary with this object's properties.
26 pub fn properties(&self) -> anyhow::Result<CFDictionary<CFString, CFType>> {
27 // SAFETY: The IOKit call should be fine, the arguments are safe. The
28 // `assume_init` should also be fine, as we guard against it with a
29 // check against `result` to ensure it succeeded.
30 unsafe {
31 let mut props = mem::MaybeUninit::<CFMutableDictionaryRef>::uninit();
32
33 let result = IORegistryEntryCreateCFProperties(
34 self.0,
35 props.as_mut_ptr(),
36 kCFAllocatorDefault,
37 0,
38 );
39
40 if result != kern_return::KERN_SUCCESS {
41 bail!("IORegistryEntryCreateCFProperties failed, error code {result}.")
42 } else {
43 let props = props.assume_init();
44 Ok(CFMutableDictionary::wrap_under_create_rule(props).to_immutable())
45 }
46 }
47 }
48
49 /// Gets the [`kIOServicePlane`] parent [`io_object_t`] for this
50 /// [`io_object_t`], if there is one.
51 pub fn service_parent(&self) -> anyhow::Result<IoObject> {
52 let mut parent: io_registry_entry_t = 0;
53
54 // SAFETY: IOKit call, the arguments should be safe.
55 let result = unsafe {
56 IORegistryEntryGetParentEntry(self.0, kIOServicePlane.as_ptr().cast(), &mut parent)
57 };
58
59 if result != kern_return::KERN_SUCCESS {
60 bail!("IORegistryEntryGetParentEntry failed, error code {result}.")
61 } else {
62 Ok(parent.into())
63 }
64 }
65
66 // pub fn conforms_to_block_storage_driver(&self) -> bool {
67 // // SAFETY: IOKit call, the arguments should be safe.
68 // let result =
69 // unsafe { IOObjectConformsTo(self.0,
70 // "IOBlockStorageDriver\0".as_ptr().cast()) };
71
72 // result != 0
73 // }
74 }
75
76 impl From<io_object_t> for IoObject {
77 fn from(obj: io_object_t) -> IoObject {
78 IoObject(obj)
79 }
80 }
81
82 impl Drop for IoObject {
83 fn drop(&mut self) {
84 // SAFETY: IOKit call, the argument here (an `io_object_t`) should be safe and
85 // expected.
86 let result = unsafe { IOObjectRelease(self.0) };
87 assert_eq!(result, kern_return::KERN_SUCCESS);
88 }
89 }
90
91 pub fn get_dict(
92 dict: &CFDictionary<CFString, CFType>, raw_key: &'static str,
93 ) -> anyhow::Result<CFDictionary<CFString, CFType>> {
94 let key = CFString::from_static_string(raw_key);
95
96 dict.find(&key)
97 .map(|value_ref| {
98 // SAFETY: Only used for debug asserts, system API call that should be safe.
99 unsafe {
100 debug_assert!(value_ref.type_of() == CFDictionaryGetTypeID());
101 }
102
103 // "Casting" `CFDictionary<*const void, *const void>` into a needed dict type
104 let ptr = value_ref.to_void() as CFDictionaryRef;
105
106 // SAFETY: System API call, it should be safe?
107 unsafe { CFDictionary::wrap_under_get_rule(ptr) }
108 })
109 .ok_or_else(|| anyhow!("missing key"))
110 }
111
112 pub fn get_i64(
113 dict: &CFDictionary<CFString, CFType>, raw_key: &'static str,
114 ) -> anyhow::Result<i64> {
115 let key = CFString::from_static_string(raw_key);
116
117 dict.find(&key)
118 .and_then(|value_ref| {
119 // SAFETY: Only used for debug asserts, system API call that should be safe.
120 unsafe {
121 debug_assert!(value_ref.type_of() == CFNumberGetTypeID());
122 }
123 value_ref.downcast::<CFNumber>()
124 })
125 .and_then(|number| number.to_i64())
126 .ok_or_else(|| anyhow!("missing key"))
127 }
128
129 pub fn get_string(
130 dict: &CFDictionary<CFString, CFType>, raw_key: &'static str,
131 ) -> anyhow::Result<String> {
132 let key = CFString::from_static_string(raw_key);
133
134 dict.find(&key)
135 .and_then(|value_ref| {
136 // SAFETY: Only used for debug asserts, system API call that should be safe.
137 unsafe {
138 debug_assert!(value_ref.type_of() == CFStringGetTypeID());
139 }
140
141 value_ref.downcast::<CFString>()
142 })
143 .map(|cf_string| cf_string.to_string())
144 .ok_or_else(|| anyhow!("missing key"))
145 }