file_system/partition/
statistics.rs

1use crate::Mbr;
2
3/// Comprehensive statistics about partitions in an MBR.
4///
5/// This structure provides detailed statistical information about the partitions
6/// present in an MBR, including counts by type, size information, and bootability status.
7/// It's useful for disk analysis, partition management tools, and system diagnostics.
8///
9/// # Fields
10///
11/// ## Partition Counts
12/// * `Total_partitions` - Total number of valid partitions
13/// * `Bootable_partitions` - Number of partitions marked as bootable
14/// * `Fat_partitions` - Number of FAT file system partitions (FAT16, FAT32, etc.)
15/// * `Linux_partitions` - Number of Linux-type partitions
16/// * `Hidden_partitions` - Number of hidden partitions
17/// * `Extended_partitions` - Number of extended partitions
18/// * `Unknown_partitions` - Number of partitions with unknown/unrecognized types
19///
20/// ## Size Information
21/// * `Total_used_sectors` - Total sectors used by all partitions
22/// * `Largest_partition_sectors` - Size of the largest partition in sectors
23/// * `Smallest_partition_sectors` - Size of the smallest partition in sectors
24///
25/// # Examples
26///
27/// ```rust
28/// extern crate alloc;
29/// use file_system::*;
30///
31/// let device = create_device!(Memory_device_type::<512>::new(4 * 1024 * 1024));
32/// // Create an MBR with some partitions
33/// let mut mbr = MBR_type::New_with_signature(0x12345678);
34/// mbr.Add_partition(Partition_type_type::Fat32_lba, 2048, 1024, true).unwrap();
35/// mbr.Add_partition(Partition_type_type::Linux, 4096, 2048, false).unwrap();
36/// mbr.Write_to_device(&device).unwrap();
37///
38/// // Read it back and get statistics
39/// let mbr = MBR_type::Read_from_device(&device).unwrap();
40/// let stats = Partition_statistics_type::From_mbr(&mbr);
41/// println!("Total partitions: {}", stats.Total_partitions);
42/// println!("Bootable partitions: {}", stats.Bootable_partitions);
43/// println!("Total used sectors: {}", stats.Total_used_sectors);
44/// ```
45#[derive(Debug, Clone)]
46pub struct PartitionStatistics {
47    /// Total number of valid partitions in the MBR.
48    pub total_partitions: usize,
49    /// Number of partitions marked as bootable.
50    pub bootable_partitions: usize,
51    /// Number of FAT file system partitions.
52    pub fat_partitions: usize,
53    /// Number of Linux-type partitions.
54    pub linux_partitions: usize,
55    /// Number of hidden partitions.
56    pub hidden_partitions: usize,
57    /// Number of extended partitions.
58    pub extended_partitions: usize,
59    /// Number of partitions with unknown types.
60    pub unknown_partitions: usize,
61    /// Total sectors used by all partitions.
62    pub total_used_sectors: u64,
63    /// Size of the largest partition in sectors.
64    pub largest_partition_sectors: u32,
65    /// Size of the smallest partition in sectors.
66    pub smallest_partition_sectors: u32,
67}
68
69impl PartitionStatistics {
70    /// Generate comprehensive statistics from an MBR.
71    ///
72    /// This method analyzes all partitions in the provided MBR and generates
73    /// detailed statistics about partition types, sizes, and other characteristics.
74    ///
75    /// # Arguments
76    ///
77    /// * `Mbr` - The MBR structure to analyze
78    ///
79    /// # Returns
80    ///
81    /// A new `Partition_statistics_type` containing the computed statistics.
82    ///
83    /// # Examples
84    ///
85    /// ```rust
86    /// extern crate alloc;
87    /// use file_system::*;
88    ///
89    /// let device = create_device!(Memory_device_type::<512>::new(4 * 1024 * 1024));
90    /// // Create an MBR with some partitions
91    /// let mut mbr = MBR_type::New_with_signature(0x12345678);
92    /// mbr.Add_partition(Partition_type_type::Fat32_lba, 2048, 1024, true).unwrap();
93    /// mbr.Add_partition(Partition_type_type::Linux, 4096, 2048, false).unwrap();
94    /// mbr.Write_to_device(&device).unwrap();
95    ///
96    /// // Read it back and analyze
97    /// let mbr = MBR_type::Read_from_device(&device).unwrap();
98    /// let stats = Partition_statistics_type::From_mbr(&mbr);
99    /// if stats.Total_partitions > 0 {
100    ///     println!("Average partition size: {} sectors",
101    ///              stats.Total_used_sectors / stats.Total_partitions as u64);
102    /// }
103    /// ```
104    pub fn from_mbr(mbr: &Mbr) -> Self {
105        mbr.generate_statistics()
106    }
107}