1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
use crate::{device::BlockDeviceExt, partition::PartitionType};

/// Specifies whether the partition table on the disk is **MSDOS** or **GPT**.
#[derive(Debug, PartialEq, Clone, Copy, Hash)]
pub enum PartitionTable {
    Msdos,
    Gpt,
}

/// A possible error when validating the partition table.
#[derive(Debug, Error, PartialEq)]
pub enum PartitionTableError {
    #[error(display = "primary partitions exceeded on partition table")]
    PrimaryPartitionsExceeded,
    #[error(display = "partition table not found")]
    NotFound,
}

/// Methods for block devices that may have a partition table.
pub trait PartitionTableExt: BlockDeviceExt {
    /// Fetch the partition table info on this device, if it exists.
    fn get_partition_table(&self) -> Option<PartitionTable>;

    /// Obtain the number of primary and logical partitions, in that order.
    fn get_partition_type_count(&self) -> (usize, usize, bool);

    /// Checks if the additional partition type can be added to the partition table.
    fn supports_additional_partition_type(
        &self,
        new_type: PartitionType,
    ) -> Result<(), PartitionTableError> {
        match self.get_partition_table() {
            Some(PartitionTable::Gpt) => (),
            Some(PartitionTable::Msdos) => {
                let (primary, logical, extended) = self.get_partition_type_count();
                if new_type == PartitionType::Primary {
                    if primary >= 4 || (primary >= 3 && (extended || logical != 0)) {
                        return Err(PartitionTableError::PrimaryPartitionsExceeded);
                    }
                } else if primary >= 4 {
                    return Err(PartitionTableError::PrimaryPartitionsExceeded);
                }
            }
            None => return Err(PartitionTableError::NotFound),
        }

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::path::Path;

    pub struct FictionalBlock {
        partitions: Vec<PartitionType>,
    }

    impl BlockDeviceExt for FictionalBlock {
        fn get_device_name(&self) -> &str { "fictional" }

        fn get_device_path(&self) -> &Path { Path::new("/dev/fictional")  }

        fn get_mount_point(&self) -> Option<&Path> { None }
    }

    impl PartitionTableExt for FictionalBlock {
        fn get_partition_table(&self) -> Option<PartitionTable> { Some(PartitionTable::Msdos) }

        fn get_partition_type_count(&self) -> (usize, usize, bool) {
            self.partitions.iter().fold((0, 0, false), |sum, &part| match part {
                PartitionType::Logical => (sum.0, sum.1 + 1, sum.2),
                PartitionType::Primary => (sum.0 + 1, sum.1, sum.2),
                PartitionType::Extended => (sum.0, sum.1, true),
            })
        }
    }

    #[test]
    fn partition_table_msdos_checks() {
        let maxed_block = FictionalBlock {
            partitions: vec![
                PartitionType::Primary,
                PartitionType::Primary,
                PartitionType::Primary,
                PartitionType::Primary,
            ],
        };

        assert_eq!(
            maxed_block.supports_additional_partition_type(PartitionType::Primary),
            Err(PartitionTableError::PrimaryPartitionsExceeded)
        );

        assert_eq!(
            maxed_block.supports_additional_partition_type(PartitionType::Logical),
            Err(PartitionTableError::PrimaryPartitionsExceeded)
        );

        let max_extended = FictionalBlock {
            partitions: vec![
                PartitionType::Primary,
                PartitionType::Primary,
                PartitionType::Primary,
                PartitionType::Extended,
                PartitionType::Logical,
                PartitionType::Logical,
            ],
        };

        assert_eq!(
            max_extended.supports_additional_partition_type(PartitionType::Primary),
            Err(PartitionTableError::PrimaryPartitionsExceeded)
        );

        assert_eq!(max_extended.supports_additional_partition_type(PartitionType::Logical), Ok(()));

        let free = FictionalBlock {
            partitions: vec![
                PartitionType::Primary,
                PartitionType::Primary,
                PartitionType::Extended,
                PartitionType::Logical,
                PartitionType::Logical,
            ],
        };

        assert_eq!(free.supports_additional_partition_type(PartitionType::Primary), Ok(()));

        assert_eq!(free.supports_additional_partition_type(PartitionType::Logical), Ok(()));
    }
}