@@ -158,58 +158,41 @@ func LabelWithContext(_ context.Context, _ string) (string, error) {
158158}
159159
160160func IOCountersWithContext (_ context.Context , names ... string ) (map [string ]IOCountersStat , error ) {
161- ioKit , err := common .NewLibrary ( common . IOKit )
161+ iokit , err := common .NewIOKitLib ( )
162162 if err != nil {
163163 return nil , err
164164 }
165- defer ioKit .Close ()
165+ defer iokit .Close ()
166166
167- coreFoundation , err := common .NewLibrary ( common . CoreFoundation )
167+ corefoundation , err := common .NewCoreFoundationLib ( )
168168 if err != nil {
169169 return nil , err
170170 }
171- defer coreFoundation .Close ()
171+ defer corefoundation .Close ()
172172
173- ioServiceMatching := common .GetFunc [common.IOServiceMatchingFunc ](ioKit , common .IOServiceMatchingSym )
174- ioServiceGetMatchingServices := common .GetFunc [common.IOServiceGetMatchingServicesFunc ](ioKit , common .IOServiceGetMatchingServicesSym )
175- ioIteratorNext := common .GetFunc [common.IOIteratorNextFunc ](ioKit , common .IOIteratorNextSym )
176- ioObjectRelease := common .GetFunc [common.IOObjectReleaseFunc ](ioKit , common .IOObjectReleaseSym )
173+ match := iokit .IOServiceMatching ("IOMedia" )
177174
178- cfDictionaryAddValue := common .GetFunc [common.CFDictionaryAddValueFunc ](coreFoundation , common .CFDictionaryAddValueSym )
179- cfStringCreateWithCString := common .GetFunc [common.CFStringCreateWithCStringFunc ](coreFoundation , common .CFStringCreateWithCStringSym )
180- cfRelease := common .GetFunc [common.CFReleaseFunc ](coreFoundation , common .CFReleaseSym )
175+ key := corefoundation .CFStringCreateWithCString (common .KCFAllocatorDefault , common .KIOMediaWholeKey , common .KCFStringEncodingUTF8 )
176+ defer corefoundation .CFRelease (uintptr (key ))
181177
182- kCFBooleanTruePtr , _ := coreFoundation .Dlsym ("kCFBooleanTrue" )
183-
184- match := ioServiceMatching ("IOMedia" )
185-
186- key := cfStringCreateWithCString (common .KCFAllocatorDefault , common .KIOMediaWholeKey , common .KCFStringEncodingUTF8 )
187- defer cfRelease (uintptr (key ))
178+ kCFBooleanTruePtr , _ := corefoundation .Dlsym ("kCFBooleanTrue" )
179+ kCFBooleanTrue := * * (* * uintptr )(unsafe .Pointer (& kCFBooleanTruePtr ))
180+ corefoundation .CFDictionaryAddValue (uintptr (match ), uintptr (key ), kCFBooleanTrue )
188181
189182 var drives uint32
190- kCFBooleanTrue := * * (* * uintptr )(unsafe .Pointer (& kCFBooleanTruePtr ))
191- cfDictionaryAddValue (uintptr (match ), uintptr (key ), kCFBooleanTrue )
192- if status := ioServiceGetMatchingServices (common .KIOMainPortDefault , uintptr (match ), & drives ); status != common .KERN_SUCCESS {
183+ if status := iokit .IOServiceGetMatchingServices (common .KIOMainPortDefault , uintptr (match ), & drives ); status != common .KERN_SUCCESS {
193184 return nil , fmt .Errorf ("IOServiceGetMatchingServices error=%d" , status )
194185 }
195- defer ioObjectRelease (drives )
186+ defer iokit . IOObjectRelease (drives )
196187
197188 ic := & ioCounters {
198- ioKit : ioKit ,
199- coreFoundation : coreFoundation ,
200-
201- ioRegistryEntryCreateCFProperties : common .GetFunc [common.IORegistryEntryCreateCFPropertiesFunc ](ioKit , common .IORegistryEntryCreateCFPropertiesSym ),
202- ioObjectRelease : ioObjectRelease ,
203-
204- cfStringCreateWithCString : cfStringCreateWithCString ,
205- cfDictionaryGetValue : common .GetFunc [common.CFDictionaryGetValueFunc ](coreFoundation , common .CFDictionaryGetValueSym ),
206- cfNumberGetValue : common .GetFunc [common.CFNumberGetValueFunc ](coreFoundation , common .CFNumberGetValueSym ),
207- cfRelease : cfRelease ,
189+ iokit : iokit ,
190+ corefoundation : corefoundation ,
208191 }
209192
210193 stats := make ([]IOCountersStat , 0 , 16 )
211194 for {
212- d := ioIteratorNext (drives )
195+ d := iokit . IOIteratorNext (drives )
213196 if d <= 0 {
214197 break
215198 }
@@ -223,7 +206,7 @@ func IOCountersWithContext(_ context.Context, names ...string) (map[string]IOCou
223206 stats = append (stats , * stat )
224207 }
225208
226- ioObjectRelease (d )
209+ iokit . IOObjectRelease (d )
227210 }
228211
229212 ret := make (map [string ]IOCountersStat , 0 )
@@ -243,9 +226,9 @@ func IOCountersWithContext(_ context.Context, names ...string) (map[string]IOCou
243226}
244227
245228const (
246- kIOBSDNameKey = "BSD Name"
247- kIOMediaSizeKey = "Size"
248- kIOMediaPreferredBlockSizeKey = "Preferred Block Size"
229+ kIOBSDNameKey = "BSD Name"
230+ // kIOMediaSizeKey = "Size"
231+ // kIOMediaPreferredBlockSizeKey = "Preferred Block Size"
249232
250233 kIOBlockStorageDriverStatisticsKey = "Statistics"
251234 kIOBlockStorageDriverStatisticsBytesReadKey = "Bytes (Read)"
@@ -257,48 +240,34 @@ const (
257240)
258241
259242type ioCounters struct {
260- ioKit * common.Library
261- coreFoundation * common.Library
262-
263- ioRegistryEntryCreateCFProperties common.IORegistryEntryCreateCFPropertiesFunc
264- ioObjectRelease common.IOObjectReleaseFunc
265-
266- cfStringCreateWithCString common.CFStringCreateWithCStringFunc
267- cfDictionaryGetValue common.CFDictionaryGetValueFunc
268- cfNumberGetValue common.CFNumberGetValueFunc
269- cfRelease common.CFReleaseFunc
243+ iokit * common.IOKitLib
244+ corefoundation * common.CoreFoundationLib
270245}
271246
272247func (i * ioCounters ) getDriveStat (d uint32 ) (* IOCountersStat , error ) {
273- ioRegistryEntryGetParentEntry := common .GetFunc [common.IORegistryEntryGetParentEntryFunc ](i .ioKit , common .IORegistryEntryGetParentEntrySym )
274- ioObjectConformsTo := common .GetFunc [common.IOObjectConformsToFunc ](i .ioKit , common .IOObjectConformsToSym )
275-
276- cfStringGetLength := common .GetFunc [common.CFStringGetLengthFunc ](i .coreFoundation , common .CFStringGetLengthSym )
277- cfStringGetCString := common .GetFunc [common.CFStringGetCStringFunc ](i .coreFoundation , common .CFStringGetCStringSym )
278-
279248 var parent uint32
280- if status := ioRegistryEntryGetParentEntry (d , common .KIOServicePlane , & parent ); status != common .KERN_SUCCESS {
249+ if status := i . iokit . IORegistryEntryGetParentEntry (d , common .KIOServicePlane , & parent ); status != common .KERN_SUCCESS {
281250 return nil , fmt .Errorf ("IORegistryEntryGetParentEntry error=%d" , status )
282251 }
283- defer i .ioObjectRelease (parent )
252+ defer i .iokit . IOObjectRelease (parent )
284253
285- if ! ioObjectConformsTo (parent , "IOBlockStorageDriver" ) {
254+ if ! i . iokit . IOObjectConformsTo (parent , "IOBlockStorageDriver" ) {
286255 // return nil, fmt.Errorf("ERROR: the object is not of the IOBlockStorageDriver class")
287256 return nil , nil
288257 }
289258
290259 var props unsafe.Pointer
291- if status := i .ioRegistryEntryCreateCFProperties (d , unsafe .Pointer (& props ), common .KCFAllocatorDefault , common .KNilOptions ); status != common .KERN_SUCCESS {
260+ if status := i .iokit . IORegistryEntryCreateCFProperties (d , unsafe .Pointer (& props ), common .KCFAllocatorDefault , common .KNilOptions ); status != common .KERN_SUCCESS {
292261 return nil , fmt .Errorf ("IORegistryEntryCreateCFProperties error=%d" , status )
293262 }
294- defer i .cfRelease (uintptr (props ))
263+ defer i .corefoundation . CFRelease (uintptr (props ))
295264
296265 key := i .cfStr (kIOBSDNameKey )
297- defer i .cfRelease (uintptr (key ))
298- name := i .cfDictionaryGetValue (uintptr (props ), uintptr (key ))
266+ defer i .corefoundation . CFRelease (uintptr (key ))
267+ name := i .corefoundation . CFDictionaryGetValue (uintptr (props ), uintptr (key ))
299268
300- buf := common .NewCStr (cfStringGetLength (uintptr (name )))
301- cfStringGetCString (uintptr (name ), buf , buf .Length (), common .KCFStringEncodingUTF8 )
269+ buf := common .NewCStr (i . corefoundation . CFStringGetLength (uintptr (name )))
270+ i . corefoundation . CFStringGetCString (uintptr (name ), buf , buf .Length (), common .KCFStringEncodingUTF8 )
302271
303272 stat , err := i .fillStat (parent )
304273 if err != nil {
@@ -314,19 +283,19 @@ func (i *ioCounters) getDriveStat(d uint32) (*IOCountersStat, error) {
314283
315284func (i * ioCounters ) fillStat (d uint32 ) (* IOCountersStat , error ) {
316285 var props unsafe.Pointer
317- status := i .ioRegistryEntryCreateCFProperties (d , unsafe .Pointer (& props ), common .KCFAllocatorDefault , common .KNilOptions )
286+ status := i .iokit . IORegistryEntryCreateCFProperties (d , unsafe .Pointer (& props ), common .KCFAllocatorDefault , common .KNilOptions )
318287 if status != common .KERN_SUCCESS {
319288 return nil , fmt .Errorf ("IORegistryEntryCreateCFProperties error=%d" , status )
320289 }
321290 if props == nil {
322291 return nil , nil
323292 }
324- defer i .cfRelease (uintptr (props ))
293+ defer i .corefoundation . CFRelease (uintptr (props ))
325294
326295 key := i .cfStr (kIOBlockStorageDriverStatisticsKey )
327- defer i .cfRelease (uintptr (key ))
296+ defer i .corefoundation . CFRelease (uintptr (key ))
328297
329- v := i .cfDictionaryGetValue (uintptr (props ), uintptr (key ))
298+ v := i .corefoundation . CFDictionaryGetValue (uintptr (props ), uintptr (key ))
330299 if v == nil {
331300 return nil , errors .New ("CFDictionaryGetValue failed" )
332301 }
@@ -343,15 +312,15 @@ func (i *ioCounters) fillStat(d uint32) (*IOCountersStat, error) {
343312
344313 for key , off := range statstab {
345314 s := i .cfStr (key )
346- if num := i .cfDictionaryGetValue (uintptr (v ), uintptr (s )); num != nil {
347- i .cfNumberGetValue (uintptr (num ), common .KCFNumberSInt64Type , uintptr (unsafe .Add (unsafe .Pointer (& stat ), off )))
315+ if num := i .corefoundation . CFDictionaryGetValue (uintptr (v ), uintptr (s )); num != nil {
316+ i .corefoundation . CFNumberGetValue (uintptr (num ), common .KCFNumberSInt64Type , uintptr (unsafe .Add (unsafe .Pointer (& stat ), off )))
348317 }
349- i .cfRelease (uintptr (s ))
318+ i .corefoundation . CFRelease (uintptr (s ))
350319 }
351320
352321 return & stat , nil
353322}
354323
355324func (i * ioCounters ) cfStr (str string ) unsafe.Pointer {
356- return i .cfStringCreateWithCString (common .KCFAllocatorDefault , str , common .KCFStringEncodingUTF8 )
325+ return i .corefoundation . CFStringCreateWithCString (common .KCFAllocatorDefault , str , common .KCFStringEncodingUTF8 )
357326}
0 commit comments