| 1 | //! Disk stats for Unix-like systems that aren't supported through other means. |
| 2 | //! Officially, for now, this means Linux and macOS. |
| 3 | |
| 4 | mod file_systems; |
| 5 | |
| 6 | mod usage; |
| 7 | |
| 8 | cfg_select! { |
| 9 | target_os = "linux" => { |
| 10 | mod linux; |
| 11 | pub use linux::*; |
| 12 | } |
| 13 | target_os = "macos" => { |
| 14 | mod other; |
| 15 | use other::*; |
| 16 | |
| 17 | mod macos; |
| 18 | pub use macos::*; |
| 19 | } |
| 20 | _ => { |
| 21 | mod other; |
| 22 | use other::*; |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | use file_systems::*; |
| 27 | use usage::*; |
| 28 | |
| 29 | use super::{DiskHarvest, keep_disk_entry}; |
| 30 | use crate::collection::DataCollector; |
| 31 | |
| 32 | /// Returns the disk usage of the mounted (and for now, physical) disks. |
| 33 | pub fn get_disk_usage(collector: &DataCollector) -> anyhow::Result<Vec<DiskHarvest>> { |
| 34 | let disk_filter = &collector.filters.disk_filter; |
| 35 | let mount_filter = &collector.filters.mount_filter; |
| 36 | let mut vec_disks: Vec<DiskHarvest> = Vec::new(); |
| 37 | |
| 38 | #[cfg(target_os = "linux")] |
| 39 | let mut mounted_names = std::collections::HashSet::new(); |
| 40 | |
| 41 | for partition in physical_partitions()? { |
| 42 | let name = partition.get_device_name(); |
| 43 | let mount_point = partition.mount_point().to_string_lossy().to_string(); |
| 44 | |
| 45 | #[cfg(target_os = "linux")] |
| 46 | if collector.include_unmounted_disks |
| 47 | && let Some(base) = name.rsplit('/').next() |
| 48 | { |
| 49 | mounted_names.insert(base.to_string()); |
| 50 | } |
| 51 | |
| 52 | // Precedence ordering in the case where name and mount filters disagree, |
| 53 | // "allow" takes precedence over "deny". |
| 54 | // |
| 55 | // For implementation, we do this as follows: |
| 56 | // 1. Is the entry allowed through any filter? That is, does it match an entry |
| 57 | // in a filter where `is_list_ignored` is `false`? If so, we always keep this |
| 58 | // entry. |
| 59 | // 2. Is the entry denied through any filter? That is, does it match an entry in |
| 60 | // a filter where `is_list_ignored` is `true`? If so, we always deny this |
| 61 | // entry. |
| 62 | // 3. Anything else is allowed. |
| 63 | |
| 64 | if keep_disk_entry(&name, &mount_point, disk_filter, mount_filter) { |
| 65 | // The usage line can fail in some cases (for example, if you use Void Linux + |
| 66 | // LUKS, see https://github.com/ClementTsang/bottom/issues/419 for details). |
| 67 | if let Ok(usage) = partition.usage() { |
| 68 | let total = usage.total(); |
| 69 | |
| 70 | vec_disks.push(DiskHarvest { |
| 71 | free_space: Some(usage.free()), |
| 72 | used_space: Some(total - usage.available()), |
| 73 | total_space: Some(total), |
| 74 | mount_point, |
| 75 | name, |
| 76 | }); |
| 77 | } else { |
| 78 | vec_disks.push(DiskHarvest { |
| 79 | free_space: None, |
| 80 | used_space: None, |
| 81 | total_space: None, |
| 82 | mount_point, |
| 83 | name, |
| 84 | }); |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | #[cfg(target_os = "linux")] |
| 90 | { |
| 91 | // If we're including unmounted disks, then we'll add any disks that we previously |
| 92 | // did not mark as mounted. |
| 93 | if collector.include_unmounted_disks { |
| 94 | for disk in unmounted_disks(&mounted_names) { |
| 95 | if keep_disk_entry(&disk.name, &disk.mount_point, disk_filter, &None) { |
| 96 | vec_disks.push(disk); |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | Ok(vec_disks) |
| 103 | } |