/// Checks if a partition can be inserted into the sectors of this device.
///
/// Returns the sectors where this partition will be created, if it can be created.
Checks if a partition can be inserted into the sectors of this device.
Returns the sectors where this partition will be created, if it can be created.
Checks if a partition can be inserted into the sectors of this device . Returns the sectors where this partition will be created if it can be created .
fn can_create_on_device(
        &self,
        parent: DeviceEntity,
        device: &Device,
        start: Sector,
        end: Sector,
    ) -> Result<(u64, u64), Error> {
        let entities = &self.entities.devices;
        fn validate(
            partdev: &Device,
            partition: &Partition,
            offset: u64,
            end: u64,
        ) -> Result<(), Error> {
            // The end of the new partition is before the start of the current.
            let before = || end < partition.offset;
            // The start of the new partition is after the end of the current.
            let after = || offset > partition.offset + partdev.sectors;
            if before() || after() {
                Ok(())
            } else {
                Err(Error::PartitionOverlap)
            }
        }
        // For all children of the device, guarantee that there is no overlap.
        match self.components.devices.children.get(parent) {
            Some(children) => {
                let offset = device.get_sector(start);
                let end = device.get_sector(end);
                // The start sector must be less than end sector.
                if offset >= end {
                    return Err(Error::InputsInverted);
                }
                // The end sector must also exist on the device.
                if device.sectors < end {
                    return Err(Error::ExceedsDevice);
                }
                // Check if there is any overlap, ignoring children marked for removal.
                for &child in children {
                    let child_flags = entities[child];
                    if !child_flags.contains(EntityFlags::REMOVE) {
                        let partdev = &self.components.devices.devices[child];
                        let partition = &self.components.devices.partitions[child];
                        validate(partdev, partition, offset, end)?;
                    }
                }
                // Check if there is an overlap with other partitions queued to be created.
                for (child, &cparent) in self.components.queued_changes.parents.iter() {
                    if cparent == parent {
                        let partdev = &self.components.queued_changes.devices[child];
                        let partition = &self.components.queued_changes.partitions[child];
                        validate(partdev, partition, offset, end)?;
                    }
                }
                Ok((offset, end - offset))
            }
            None => Err(Error::NotPartitionable),
        }
    }
fn can_create_on_device ( & self , parent : DeviceEntity , device : & Device , start : Sector , end : Sector , ) -> Result < ( u64 , u64 ) , Error > { let entities = & self . entities . devices ; fn validate ( partdev : & Device , partition : & Partition , offset : u64 , end : u64 , ) -> Result < ( ) , Error > { // The end of the new partition is before the start of the current. let before = | | end < partition . offset ; // The start of the new partition is after the end of the current. let after = | | offset > partition . offset + partdev . sectors ; if before ( ) || after ( ) { Ok ( ( ) ) } else { Err ( Error :: PartitionOverlap ) } } // For all children of the device, guarantee that there is no overlap. match self . components . devices . children . get ( parent ) { Some ( children ) => { let offset = device . get_sector ( start ) ; let end = device . get_sector ( end ) ; // The start sector must be less than end sector. if offset >= end { return Err ( Error :: InputsInverted ) ; } // The end sector must also exist on the device. if device . sectors < end { return Err ( Error :: ExceedsDevice ) ; } // Check if there is any overlap, ignoring children marked for removal. for & child in children { let child_flags = entities [ child ] ; if ! child_flags . contains ( EntityFlags :: REMOVE ) { let partdev = & self . components . devices . devices [ child ] ; let partition = & self . components . devices . partitions [ child ] ; validate ( partdev , partition , offset , end ) ? ; } } // Check if there is an overlap with other partitions queued to be created. for ( child , & cparent ) in self . components . queued_changes . parents . iter ( ) { if cparent == parent { let partdev = & self . components . queued_changes . devices [ child ] ; let partition = & self . components . queued_changes . partitions [ child ] ; validate ( partdev , partition , offset , end ) ? ; } } Ok ( ( offset , end - offset ) ) } None => Err ( Error :: NotPartitionable ) , } }
Checks if a partition can be inserted into the sectors of this device.
Checks if a partition can be inserted into the sectors of this device .
&self
parent
DeviceEntity
device
&Device
start
Sector
end
Sector