Skip to content

Commit

Permalink
Improve ejectable disk detection on Apple platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
complexspaces committed Oct 29, 2022
1 parent e6f5432 commit 346a7c9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/apple/disk.rs
Expand Up @@ -100,6 +100,7 @@ pub(super) unsafe fn get_disks() -> Vec<Disk> {
let requested_properties = build_requested_properties(&[
ffi::kCFURLVolumeIsEjectableKey,
ffi::kCFURLVolumeIsRemovableKey,
ffi::kCFURLVolumeIsInternalKey,
ffi::kCFURLVolumeTotalCapacityKey,
ffi::kCFURLVolumeAvailableCapacityForImportantUsageKey,
ffi::kCFURLVolumeAvailableCapacityKey,
Expand Down Expand Up @@ -209,7 +210,7 @@ fn get_available_volume_space(disk_props: &RetainedCFDictionary) -> u64 {
)
})
}
.unwrap() as u64
.unwrap_or_default() as u64
}

pub(super) enum DictKey {
Expand Down Expand Up @@ -336,7 +337,21 @@ unsafe fn new_disk(
)
.unwrap_or_default();

ejectable || removable
let is_removable = ejectable || removable;

if is_removable {
is_removable
} else {
// If neither `ejectable` or `removable` return `true`, fallback to checking
// if the disk is attached to the internal system.
let internal = get_bool_value(
disk_props.inner(),
DictKey::Extern(ffi::kCFURLVolumeIsInternalKey),
)
.unwrap_or_default();

!internal
}
};

let total_space = get_int_value(
Expand Down
1 change: 1 addition & 0 deletions src/apple/ffi.rs
Expand Up @@ -23,6 +23,7 @@ extern "C" {
pub static kCFURLVolumeTotalCapacityKey: CFStringRef;
pub static kCFURLVolumeNameKey: CFStringRef;
pub static kCFURLVolumeIsLocalKey: CFStringRef;
pub static kCFURLVolumeIsInternalKey: CFStringRef;
pub static kCFURLVolumeIsBrowsableKey: CFStringRef;
}

Expand Down

0 comments on commit 346a7c9

Please sign in to comment.