pub fn read_from<R: ?Sized>(mut reader: &mut R, sector_size: u64) -> Result<GPT>
    where
        R: Read + Seek,
    {
        use self::Error::*;

        reader.seek(SeekFrom::Start(sector_size))?;
        let header = GPTHeader::read_from(&mut reader).or_else(|primary_err| {
            let len = reader.seek(SeekFrom::End(0))?;
            reader.seek(SeekFrom::Start((len / sector_size - 1) * sector_size))?;

            GPTHeader::read_from(&mut reader).map_err(|backup_err| {
                match (primary_err, backup_err) {
                    (InvalidSignature, InvalidSignature) => InvalidSignature,
                    (x, y) => Error::ReadError(Box::new(x), Box::new(y)),
                }
            })
        })?;

        let mut partitions = Vec::with_capacity(header.number_of_partition_entries as usize);
        for i in 0..header.number_of_partition_entries {
            reader.seek(SeekFrom::Start(
                header.partition_entry_lba * sector_size
                    + u64::from(i) * u64::from(header.size_of_partition_entry),
            ))?;
            partitions.push(GPTPartitionEntry::read_from(&mut reader)?);
        }

        let sum = header.generate_partition_entry_array_crc32(&partitions);
        if header.partition_entry_array_crc32 != sum {
            return Err(Error::InvalidPartitionEntryArrayChecksum(
                header.partition_entry_array_crc32,
                sum,
            ));
        }

        let align = GPT::find_alignment(&header, &partitions);

        Ok(GPT {
            sector_size,
            header,
            partitions,
            align,
        })
    }
/// Read the GPT on a reader. This function will try to read the backup header if the primary
/// header could not be read.
///
/// # Implementation notes
///
/// The field `last_usable_lba` on the header is not updated to reflect the actual size of the
/// disk. You must do this yourself by calling `update_from`.
///
/// # Examples
///
/// Basic usage:
/// ```
/// let mut f = std::fs::File::open("tests/fixtures/disk1.img")
///     .expect("could not open disk");
/// let gpt = gptman::GPT::read_from(&mut f, 512)
///     .expect("could not read the partition table");
/// ```
Read the GPT on a reader. This function will try to read the backup header if the primary
header could not be read.
Read the GPT on a reader . This function will try to read the backup header if the primary header could not be read .
pub fn read_from<R: ?Sized>(mut reader: &mut R, sector_size: u64) -> Result<GPT>
    where
        R: Read + Seek,
    {
        use self::Error::*;
        reader.seek(SeekFrom::Start(sector_size))?;
        let header = GPTHeader::read_from(&mut reader).or_else(|primary_err| {
            let len = reader.seek(SeekFrom::End(0))?;
            reader.seek(SeekFrom::Start((len / sector_size - 1) * sector_size))?;
            GPTHeader::read_from(&mut reader).map_err(|backup_err| {
                match (primary_err, backup_err) {
                    (InvalidSignature, InvalidSignature) => InvalidSignature,
                    (x, y) => Error::ReadError(Box::new(x), Box::new(y)),
                }
            })
        })?;
        let mut partitions = Vec::with_capacity(header.number_of_partition_entries as usize);
        for i in 0..header.number_of_partition_entries {
            reader.seek(SeekFrom::Start(
                header.partition_entry_lba * sector_size
                    + u64::from(i) * u64::from(header.size_of_partition_entry),
            ))?;
            partitions.push(GPTPartitionEntry::read_from(&mut reader)?);
        }
        let sum = header.generate_partition_entry_array_crc32(&partitions);
        if header.partition_entry_array_crc32 != sum {
            return Err(Error::InvalidPartitionEntryArrayChecksum(
                header.partition_entry_array_crc32,
                sum,
            ));
        }
        let align = GPT::find_alignment(&header, &partitions);
        Ok(GPT {
            sector_size,
            header,
            partitions,
            align,
        })
    }
pub fn read_from < R : ? Sized > ( mut reader : & mut R , sector_size : u64 ) -> Result < GPT > where R : Read + Seek , { use self :: Error :: * ; reader . seek ( SeekFrom :: Start ( sector_size ) ) ? ; let header = GPTHeader :: read_from ( & mut reader ) . or_else ( | primary_err | { let len = reader . seek ( SeekFrom :: End ( 0 ) ) ? ; reader . seek ( SeekFrom :: Start ( ( len / sector_size - 1 ) * sector_size ) ) ? ; GPTHeader :: read_from ( & mut reader ) . map_err ( | backup_err | { match ( primary_err , backup_err ) { ( InvalidSignature , InvalidSignature ) => InvalidSignature , ( x , y ) => Error :: ReadError ( Box :: new ( x ) , Box :: new ( y ) ) , } } ) } ) ? ; let mut partitions = Vec :: with_capacity ( header . number_of_partition_entries as usize ) ; for i in 0 .. header . number_of_partition_entries { reader . seek ( SeekFrom :: Start ( header . partition_entry_lba * sector_size + u64 :: from ( i ) * u64 :: from ( header . size_of_partition_entry ) , ) ) ? ; partitions . push ( GPTPartitionEntry :: read_from ( & mut reader ) ? ) ; } let sum = header . generate_partition_entry_array_crc32 ( & partitions ) ; if header . partition_entry_array_crc32 != sum { return Err ( Error :: InvalidPartitionEntryArrayChecksum ( header . partition_entry_array_crc32 , sum , ) ) ; } let align = GPT :: find_alignment ( & header , & partitions ) ; Ok ( GPT { sector_size , header , partitions , align , } ) }
Read the GPT on a reader.
Read the GPT on a reader .
reader
&mut R
sector_size
u64
reader
&mut R
sector_size
u64
implementation notes
The field `last_usable_lba` on the header is not updated to reflect the actual size of the
disk. You must do this yourself by calling `update_from`.
The field ` last_usable_lba ` on the header is not updated to reflect the actual size of the disk . You must do this yourself by calling ` update_from ` .
examples
Basic usage:
```
let mut f = std::fs::File::open("tests/fixtures/disk1.img")
.expect("could not open disk");
let gpt = gptman::GPT::read_from(&mut f, 512)
.expect("could not read the partition table");
```
Basic usage : ` ` ` let mut f = std :: fs :: File :: open ( " tests / fixtures / disk1 . img " ) . expect ( " could not open disk " ) ; let gpt = gptman :: GPT :: read_from ( &mut f 512 ) . expect ( " could not read the partition table " ) ; ` ` `