Skip to content

Commit

Permalink
Merge branch 'master' into disks-apfs-filesystem
Browse files Browse the repository at this point in the history
  • Loading branch information
svartalf committed Sep 24, 2019
2 parents b56e57c + 1715265 commit b056f30
Show file tree
Hide file tree
Showing 109 changed files with 1,022 additions and 1,262 deletions.
27 changes: 18 additions & 9 deletions heim-common/src/sys/macos/iokit/ffi.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#![allow(non_camel_case_types, dead_code, unused)]

use libc::c_char;
use core_foundation::base::{CFAllocatorRef, mach_port_t};
use core_foundation::base::{mach_port_t, CFAllocatorRef};
use core_foundation::dictionary::{CFDictionaryRef, CFMutableDictionaryRef};
use mach::{kern_return, boolean};
use libc::c_char;
use mach::{boolean, kern_return};

pub type io_object_t = mach_port_t;
pub type io_registry_entry_t = io_object_t;
Expand All @@ -18,7 +18,10 @@ extern "C" {

// https://developer.apple.com/documentation/iokit/1514652-iomasterport
// Should be deallocated with `mach_port_deallocate(mach_task_self(), masterPort)`
pub fn IOMasterPort(bootstrapPort: mach_port_t, masterPort: *mut mach_port_t) -> kern_return::kern_return_t;
pub fn IOMasterPort(
bootstrapPort: mach_port_t,
masterPort: *mut mach_port_t,
) -> kern_return::kern_return_t;

// https://developer.apple.com/documentation/iokit/1514687-ioservicematching
// The dictionary is commonly passed to IOServiceGetMatchingServices or IOServiceAddNotification
Expand All @@ -28,8 +31,11 @@ extern "C" {
// https://developer.apple.com/documentation/iokit/1514494-ioservicegetmatchingservices?language=objc
// An `existing` iterator handle is returned on success, and should be released by the caller
// when the iteration is finished.
pub fn IOServiceGetMatchingServices(masterPort: mach_port_t, matching: CFDictionaryRef,
existing: *mut io_iterator_t) -> kern_return::kern_return_t;
pub fn IOServiceGetMatchingServices(
masterPort: mach_port_t,
matching: CFDictionaryRef,
existing: *mut io_iterator_t,
) -> kern_return::kern_return_t;

// https://developer.apple.com/documentation/iokit/1514454-ioregistryentrygetparententry?language=objc
// The caller should release `parent` with `IOObjectRelease`
Expand All @@ -47,9 +53,12 @@ extern "C" {

// https://developer.apple.com/documentation/iokit/1514310-ioregistryentrycreatecfpropertie
// The caller should release `properties` with `CFRelease`.
pub fn IORegistryEntryCreateCFProperties(entry: io_registry_entry_t,
properties: *mut CFMutableDictionaryRef, allocator: CFAllocatorRef,
options: IOOptionBits) -> kern_return::kern_return_t;
pub fn IORegistryEntryCreateCFProperties(
entry: io_registry_entry_t,
properties: *mut CFMutableDictionaryRef,
allocator: CFAllocatorRef,
options: IOOptionBits,
) -> kern_return::kern_return_t;

// https://developer.apple.com/documentation/iokit/1514741-ioiteratornext
// The element should be released by the caller when it is finished.
Expand Down
8 changes: 3 additions & 5 deletions heim-common/src/sys/macos/iokit/io_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,15 @@ impl Iterator for IoIterator {

fn next(&mut self) -> Option<Self::Item> {
match unsafe { ffi::IOIteratorNext(self.0) } {
0 => None, // TODO: Should not there be some `NULL` instead of `0`?
io_object => Some(IoObject::from(io_object))
0 => None, // TODO: Should not there be some `NULL` instead of `0`?
io_object => Some(IoObject::from(io_object)),
}
}
}

impl Drop for IoIterator {
fn drop(&mut self) {
let result = unsafe {
ffi::IOObjectRelease(self.0)
};
let result = unsafe { ffi::IOObjectRelease(self.0) };
assert_eq!(result, kern_return::KERN_SUCCESS);
}
}
24 changes: 7 additions & 17 deletions heim-common/src/sys/macos/iokit/io_master_port.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::mem;

use core_foundation::base::{mach_port_t, kCFNull};
use mach::{port, mach_port, kern_return, traps};
use core_foundation::base::{kCFNull, mach_port_t};
use mach::{kern_return, mach_port, port, traps};

use crate::{Result, Error};
use super::{ffi, IoIterator};
use crate::{Error, Result};

/// Safe wrapper around the IOKit master port.
#[derive(Debug)]
Expand All @@ -15,9 +15,7 @@ impl IoMasterPort {
pub fn new() -> Result<IoMasterPort> {
let mut master_port: port::mach_port_t = port::MACH_PORT_NULL;

let result = unsafe {
ffi::IOMasterPort(ffi::kIOMasterPortDefault, &mut master_port)
};
let result = unsafe { ffi::IOMasterPort(ffi::kIOMasterPortDefault, &mut master_port) };

if result != kern_return::KERN_SUCCESS {
Err(Error::last_os_error())
Expand All @@ -40,17 +38,11 @@ impl IoMasterPort {
let mut raw_iterator = mem::MaybeUninit::<ffi::io_iterator_t>::uninit();

let result = unsafe {
ffi::IOServiceGetMatchingServices(
self.0,
service,
raw_iterator.as_mut_ptr(),
)
ffi::IOServiceGetMatchingServices(self.0, service, raw_iterator.as_mut_ptr())
};

if result == kern_return::KERN_SUCCESS {
let raw_iterator = unsafe {
raw_iterator.assume_init()
};
let raw_iterator = unsafe { raw_iterator.assume_init() };
Ok(raw_iterator.into())
} else {
Err(Error::last_os_error())
Expand All @@ -60,9 +52,7 @@ impl IoMasterPort {

impl Drop for IoMasterPort {
fn drop(&mut self) {
let result = unsafe {
mach_port::mach_port_deallocate(traps::mach_task_self(), self.0)
};
let result = unsafe { mach_port::mach_port_deallocate(traps::mach_task_self(), self.0) };
assert_eq!(result, kern_return::KERN_SUCCESS);
}
}
30 changes: 11 additions & 19 deletions heim-common/src/sys/macos/iokit/io_object.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use std::mem;
use std::convert::AsRef;
use std::mem;

use mach::kern_return;
use core_foundation::base::kCFAllocatorDefault;
use core_foundation::base::{CFType, TCFType};
use core_foundation::dictionary::CFMutableDictionaryRef;
use core_foundation::string::CFString;
use core_foundation::base::kCFAllocatorDefault;
use core_foundation::dictionary::{CFDictionary, CFMutableDictionary};
use core_foundation::string::CFString;
use mach::kern_return;

use crate::{Result, Error};
use super::ffi;
use crate::{Error, Result};

/// Safe wrapper around the IOKit `io_object_t` type.
#[derive(Debug)]
Expand All @@ -25,7 +25,7 @@ impl IoObject {
self.0,
props.as_mut_ptr(),
kCFAllocatorDefault,
0
0,
);
if result != kern_return::KERN_SUCCESS {
Err(Error::last_os_error())
Expand All @@ -44,29 +44,23 @@ impl IoObject {
ffi::IORegistryEntryGetParentEntry(
self.0,
plane.as_ref().as_ptr() as *const libc::c_char,
parent.as_mut_ptr()
parent.as_mut_ptr(),
)
};

if result != kern_return::KERN_SUCCESS {
Err(Error::last_os_error())
} else {
let parent = unsafe {
parent.assume_init()
};
let parent = unsafe { parent.assume_init() };
Ok(parent.into())
}
}

/// `class_name` should look like `b"IOBlockStorageDriver\0"` --
/// a binary string with a trailing `0x00` char
pub fn conforms_to(&self, class_name: &[u8]) -> bool {
let result = unsafe {
ffi::IOObjectConformsTo(
self.0,
class_name.as_ptr() as *const libc::c_char
)
};
let result =
unsafe { ffi::IOObjectConformsTo(self.0, class_name.as_ptr() as *const libc::c_char) };

result != 0
}
Expand All @@ -80,9 +74,7 @@ impl From<ffi::io_object_t> for IoObject {

impl Drop for IoObject {
fn drop(&mut self) {
let result = unsafe {
ffi::IOObjectRelease(self.0)
};
let result = unsafe { ffi::IOObjectRelease(self.0) };
assert_eq!(result, kern_return::KERN_SUCCESS);
}
}
10 changes: 4 additions & 6 deletions heim-common/src/sys/macos/iokit/properties.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use core_foundation::base::{CFType, ToVoid};
use core_foundation::dictionary::{CFDictionary, CFDictionaryRef, CFDictionaryGetTypeID};
use core_foundation::string::{CFString, CFStringGetTypeID};
use core_foundation::boolean::{CFBoolean, CFBooleanGetTypeID};
use core_foundation::dictionary::{CFDictionary, CFDictionaryGetTypeID, CFDictionaryRef};
use core_foundation::number::{CFNumber, CFNumberGetTypeID};
use core_foundation::string::{CFString, CFStringGetTypeID};

pub use core_foundation::base::TCFType;

use crate::{Result, Error};
use crate::{Error, Result};

/// Extends `CFDictionary` with a few methods used by `heim` crates.
pub trait DictionaryProps {
Expand Down Expand Up @@ -39,9 +39,7 @@ impl DictionaryProps for CFDictionary<CFString, CFType> {
// and it does not decrements here.
let ptr = value_ref.to_void() as CFDictionaryRef;

unsafe {
Some(CFDictionary::wrap_under_get_rule(ptr))
}
unsafe { Some(CFDictionary::wrap_under_get_rule(ptr)) }
})
.ok_or_else(|| Error::missing_entity(raw_key))
}
Expand Down
13 changes: 6 additions & 7 deletions heim-common/src/sys/macos/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

#![allow(non_camel_case_types)]

use mach::mach_types::{host_t, host_name_port_t};
use mach::vm_types::integer_t;
use mach::kern_return::kern_return_t;
use mach::mach_types::{host_name_port_t, host_t};
use mach::message::mach_msg_type_number_t;
use mach::vm_types::integer_t;

use super::IntoTime;
use crate::units::{Time, time};
use crate::units::{time, Time};

pub mod iokit;

Expand All @@ -28,7 +28,7 @@ extern "C" {
host_priv: host_t,
flavor: host_flavor_t,
host_info_out: host_info_t,
host_info_outCnt: *const mach_msg_type_number_t
host_info_outCnt: *const mach_msg_type_number_t,
) -> kern_return_t;

/// https://developer.apple.com/documentation/kernel/1502863-host_statistics64?language=objc
Expand All @@ -42,8 +42,7 @@ extern "C" {

impl IntoTime for libc::timeval {
fn into_time(self) -> Time {
Time::new::<time::second>(self.tv_sec as f64) +
Time::new::<time::microsecond>(f64::from(self.tv_usec))
Time::new::<time::second>(self.tv_sec as f64)
+ Time::new::<time::microsecond>(f64::from(self.tv_usec))
}

}
10 changes: 5 additions & 5 deletions heim-common/src/sys/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

#![allow(non_camel_case_types, non_upper_case_globals, non_snake_case)]

use std::mem;
use std::iter;
use std::ffi::OsStr;
use std::ffi::CStr;
use std::ffi::OsStr;
use std::iter;
use std::mem;
use std::os::windows::ffi::OsStrExt;

use winapi::um::{winnt, libloaderapi};
use winapi::shared::{ntdef, minwindef};
use winapi::shared::{minwindef, ntdef};
use winapi::um::{libloaderapi, winnt};

use crate::prelude::*;

Expand Down
9 changes: 4 additions & 5 deletions heim-common/src/sys/windows/time.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use winapi::shared::{minwindef, ntdef};

use crate::sys::IntoTime;
use crate::units::{Time, time};
use crate::units::{time, Time};

const HI_T: f64 = 429.496_729_6;
const LO_T: f64 = 1e-7;

impl IntoTime for minwindef::FILETIME {
#[inline]
fn into_time(self) -> Time {
let value = (HI_T * f64::from(self.dwHighDateTime))
+ (LO_T * f64::from(self.dwLowDateTime));
let value =
(HI_T * f64::from(self.dwHighDateTime)) + (LO_T * f64::from(self.dwLowDateTime));

Time::new::<time::second>(value)
}
Expand All @@ -20,8 +20,7 @@ impl IntoTime for ntdef::LARGE_INTEGER {
#[inline]
fn into_time(self) -> Time {
let s = unsafe { self.s() };
let value = (HI_T * f64::from(s.HighPart))
+ (LO_T * f64::from(s.LowPart));
let value = (HI_T * f64::from(s.HighPart)) + (LO_T * f64::from(s.LowPart));

Time::new::<time::second>(value)
}
Expand Down
2 changes: 1 addition & 1 deletion heim-cpu/src/os/linux/freq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ use crate::{sys, CpuFrequency};
/// Order of the stream is constant.
///
/// [CPU frequencies]: ../../struct.CpuFrequency.html
pub fn frequencies() -> impl Stream<Item=Result<CpuFrequency>> {
pub fn frequencies() -> impl Stream<Item = Result<CpuFrequency>> {
sys::frequencies().map_ok(Into::into)
}
4 changes: 2 additions & 2 deletions heim-cpu/src/os/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
//!
//! Available only for `cfg(target_os = "linux")`

mod times;
mod freq;
mod stats;
mod times;

pub use self::times::*;
pub use self::freq::*;
pub use self::stats::*;
pub use self::times::*;
3 changes: 1 addition & 2 deletions heim-cpu/src/sys/linux/count/physical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ struct Collector {
}

fn parse_line(line: &str) -> Result<u64> {
line
.split(':')
line.split(':')
.nth(2)
.map(|value| value.trim())
.ok_or_else(|| Error::incompatible("Unsupported format for /proc/cpuinfo"))
Expand Down

0 comments on commit b056f30

Please sign in to comment.