Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: migrate disk collection code off of heim, remove heim #1064

Merged
merged 39 commits into from
Apr 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
acd79e5
refactor: fix some refresh code
ClementTsang Mar 9, 2023
e58746d
remove async from the freebsd code
ClementTsang Mar 11, 2023
7754d02
some file/implementation organization
ClementTsang Mar 14, 2023
577bf72
more restructuring
ClementTsang Mar 16, 2023
5e1592c
Some other fixes
ClementTsang Mar 19, 2023
b2594ce
remove futures
ClementTsang Mar 19, 2023
48158e7
ready for some big changes?
ClementTsang Mar 23, 2023
4d5f250
big changes
ClementTsang Mar 26, 2023
69675dc
linux io + reads
ClementTsang Apr 1, 2023
ecf0ac2
use lossy conversion for mount point
ClementTsang Apr 1, 2023
8724780
add windows refresh
ClementTsang Apr 1, 2023
794eda3
so long heim, and thanks for all the fish
ClementTsang Apr 1, 2023
c53a32e
fix filter behaviour, remove string allocation when reading lines
ClementTsang Apr 3, 2023
042c530
rename unix -> system for more accurate file struct representation
ClementTsang Apr 4, 2023
7e0a2be
fix freebsd
ClementTsang Apr 4, 2023
37f4fd7
port generic unix partition code
ClementTsang Apr 5, 2023
b10d124
add bindings and fix errors
ClementTsang Apr 5, 2023
ad1d8f9
finish macOS bindings for I/O
ClementTsang Apr 5, 2023
9c8df52
disable conform check, this seems to... make disk I/O work on macOS?????
ClementTsang Apr 5, 2023
c027da9
fix linux
ClementTsang Apr 5, 2023
32e38ae
add safety comments
ClementTsang Apr 5, 2023
2628254
more comments
ClementTsang Apr 5, 2023
72485bc
update changelog
ClementTsang Apr 5, 2023
6d6bec0
changelog
ClementTsang Apr 5, 2023
096190d
We're going full 0.9.0 for this
ClementTsang Apr 5, 2023
e92cba7
update lock
ClementTsang Apr 5, 2023
c396659
fix some typing
ClementTsang Apr 6, 2023
eff2f16
bleh
ClementTsang Apr 6, 2023
461cde4
some file management
ClementTsang Apr 6, 2023
7f0c669
hoist out get_disk_usage
ClementTsang Apr 6, 2023
12559df
fix some stuff for Windows
ClementTsang Apr 6, 2023
30ea987
typing and remove dead code allow lint
ClementTsang Apr 6, 2023
0f4287b
unify typing
ClementTsang Apr 6, 2023
661c578
fix
ClementTsang Apr 6, 2023
111133c
fix 2
ClementTsang Apr 6, 2023
a30aac1
macOS fix
ClementTsang Apr 7, 2023
4d22589
Add bindings file for windows
ClementTsang Apr 9, 2023
0765343
add windows implementation
ClementTsang Apr 10, 2023
e5d241a
fix macos
ClementTsang Apr 10, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add safety comments
  • Loading branch information
ClementTsang committed Apr 9, 2023
commit 32e38ae5d847c389ae8e33f860281b5f6158dfa6
3 changes: 3 additions & 0 deletions src/app/data_harvester/disks/system/linux/partition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,12 @@ impl Partition {
.map_err(|e| anyhow::anyhow!("invalid path: {e:?}"))?;

let mut vfs = mem::MaybeUninit::<libc::statvfs>::uninit();

// SAFETY: libc call, `path` is a valid C string and buf is a valid pointer to write to.
let result = unsafe { libc::statvfs(path.as_ptr(), vfs.as_mut_ptr()) };

if result == 0 {
// SAFETY: If result is 0, it succeeded, and vfs should be non-null.
let vfs = unsafe { vfs.assume_init() };
Ok(Usage::new(vfs))
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use super::{bindings::*, IoIterator};
pub fn get_disks() -> anyhow::Result<IoIterator> {
let mut media_iter: io_iterator_t = 0;

// SAFETY: This is a safe syscall,
// SAFETY: This is a safe syscall via IOKit, all the arguments should be safe.
let result = unsafe {
IOServiceGetMatchingServices(
kIOMasterPortDefault,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ impl Iterator for IoIterator {

fn next(&mut self) -> Option<Self::Item> {
// Basically, we just stop when we hit 0.

// SAFETY: IOKit call, the passed argument (an `io_iterator_t`) is what is expected.
match unsafe { IOIteratorNext(self.0) } {
0 => None,
io_object => Some(IoObject::from(io_object)),
Expand All @@ -45,6 +47,7 @@ impl Iterator for IoIterator {

impl Drop for IoIterator {
fn drop(&mut self) {
// SAFETY: IOKit call, the passed argument (an `io_iterator_t`) is what is expected.
let result = unsafe { IOObjectRelease(self.0) };
assert_eq!(result, kern_return::KERN_SUCCESS);
}
Expand Down
13 changes: 9 additions & 4 deletions src/app/data_harvester/disks/system/macos/io_kit/io_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ pub struct IoObject(io_object_t);
impl IoObject {
/// Returns a typed dictionary with this object's properties.
pub fn properties(&self) -> anyhow::Result<CFDictionary<CFString, CFType>> {
// SAFETY: The IOKit call should be fine, the arguments are safe. The `assume_init` should also be fine, as
// we guard against it with a check against `result` to ensure it succeeded.
unsafe {
let mut props = mem::MaybeUninit::<CFMutableDictionaryRef>::uninit();

Expand All @@ -46,6 +48,7 @@ impl IoObject {
pub fn service_parent(&self) -> anyhow::Result<IoObject> {
let mut parent: io_registry_entry_t = 0;

// SAFETY: IOKit call, the arguments should be safe.
let result = unsafe {
IORegistryEntryGetParentEntry(self.0, kIOServicePlane.as_ptr().cast(), &mut parent)
};
Expand All @@ -58,6 +61,7 @@ impl IoObject {
}

// pub fn conforms_to_block_storage_driver(&self) -> bool {
// // SAFETY: IOKit call, the arguments should be safe.
// let result =
// unsafe { IOObjectConformsTo(self.0, "IOBlockStorageDriver\0".as_ptr().cast()) };

Expand All @@ -73,6 +77,7 @@ impl From<io_object_t> for IoObject {

impl Drop for IoObject {
fn drop(&mut self) {
// SAFETY: IOKit call, the argument here (an `io_object_t`) should be safe and expected.
let result = unsafe { IOObjectRelease(self.0) };
assert_eq!(result, kern_return::KERN_SUCCESS);
}
Expand All @@ -85,16 +90,15 @@ pub fn get_dict(

dict.find(&key)
.map(|value_ref| {
// SAFETY: Only used for debug asserts, system API call that should be safe.
unsafe {
debug_assert!(value_ref.type_of() == CFDictionaryGetTypeID());
}

// "Casting" `CFDictionary<*const void, *const void>` into a needed dict type

// TODO: I think that reference to an original dict is still stored somewhere
// and it does not decrements here.
let ptr = value_ref.to_void() as CFDictionaryRef;

// SAFETY: System API call, it should be safe?
unsafe { CFDictionary::wrap_under_get_rule(ptr) }
})
.ok_or_else(|| anyhow!("missing key"))
Expand All @@ -107,10 +111,10 @@ pub fn get_i64(

dict.find(&key)
.and_then(|value_ref| {
// SAFETY: Only used for debug asserts, system API call that should be safe.
unsafe {
debug_assert!(value_ref.type_of() == CFNumberGetTypeID());
}

value_ref.downcast::<CFNumber>()
})
.and_then(|number| number.to_i64())
Expand All @@ -124,6 +128,7 @@ pub fn get_string(

dict.find(&key)
.and_then(|value_ref| {
// SAFETY: Only used for debug asserts, system API call that should be safe.
unsafe {
debug_assert!(value_ref.type_of() == CFStringGetTypeID());
}
Expand Down
9 changes: 9 additions & 0 deletions src/app/data_harvester/disks/system/other/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ extern "C" {

/// Returns all the mounts on the system at the moment.
pub(crate) fn mounts() -> anyhow::Result<Vec<libc::statfs>> {
// SAFETY: System API FFI call, arguments should be correct.
let expected_len = unsafe { getfsstat64(std::ptr::null_mut(), 0, MNT_NOWAIT) };

let mut mounts: Vec<libc::statfs> = Vec::with_capacity(expected_len as usize);

// SAFETY: System API FFI call, arguments should be correct.
let result = unsafe {
getfsstat64(
mounts.as_mut_ptr(),
Expand All @@ -29,6 +33,11 @@ pub(crate) fn mounts() -> anyhow::Result<Vec<libc::statfs>> {
expected_len, result,
"Expected {expected_len} statfs entries, but instead got {result} entries",
);

// SAFETY: We have a debug assert check, and if `result` is not correct (-1), we check against it.
// Otherwise, getfsstat64 should return the number of statfs structures if it succeeded.
//
// Source: https://man.freebsd.org/cgi/man.cgi?query=getfsstat&sektion=2&format=html
unsafe {
mounts.set_len(result as usize);
}
Expand Down
2 changes: 2 additions & 0 deletions src/app/data_harvester/disks/system/other/partition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,11 @@ impl Partition {
let path = CString::new(self.mount_point().as_os_str().as_bytes())?;
let mut vfs = std::mem::MaybeUninit::<libc::statvfs>::uninit();

// SAFETY: System API call. Arguments should be correct.
let result = unsafe { libc::statvfs(path.as_ptr(), vfs.as_mut_ptr()) };

if result == 0 {
// SAFETY: We check that it succeeded (result is 0), which means vfs should be populated.
Ok(Usage::new(unsafe { vfs.assume_init() }))
} else {
bail!("statvfs failed to get the disk usage for disk {path:?}")
Expand Down