[][src]Struct gptman::GPT

pub struct GPT {
    pub sector_size: u64,
    pub header: GPTHeader,
    pub align: u64,
    // some fields omitted
}

A type representing a GUID partition table including its partitions, the sector size of the disk and the alignment of the partitions to the sectors.

Examples

Read an existing GPT on a reader and list its partitions:

let mut f = std::fs::File::open("tests/fixtures/disk1.img")
    .expect("could not open disk");
let gpt = gptman::GPT::find_from(&mut f)
    .expect("could not find GPT");

println!("Disk GUID: {:?}", gpt.header.disk_guid);

for (i, p) in gpt.iter() {
    if p.is_used() {
        println!("Partition #{}: type = {:?}, size = {} bytes, starting lba = {}",
            i,
            p.partition_type_guid,
            p.size().unwrap() * gpt.sector_size,
            p.starting_lba);
    }
}

Fields

sector_size: u64

Sector size of the disk.

You should not change this, otherwise the starting locations of your partitions will be different in bytes.

header: GPTHeader

GPT partition header (disk GUID, first/last usable LBA, etc...)

align: u64

Partitions alignment (in sectors)

This field change the behavior of the methods get_maximum_partition_size(), find_free_sectors(), find_first_place(), find_last_place() and find_optimal_place() so they return only values aligned to the alignment.

Panics

The value must be greater than 0, otherwise you will encounter divisions by zero.

Implementations

impl GPT[src]

pub fn new_from<R>(
    reader: &mut R,
    sector_size: u64,
    disk_guid: [u8; 16]
) -> Result<GPT> where
    R: Read + Seek
[src]

Make a new GPT based on a reader. (This operation does not write anything to disk!)

Examples

Basic usage:

let ss = 512;
let data = vec![0; 100 * ss as usize];
let mut cur = std::io::Cursor::new(data);
let gpt = gptman::GPT::new_from(&mut cur, ss as u64, [0xff; 16])
    .expect("could not make a partition table");

pub fn read_from<R: ?Sized>(reader: &mut R, sector_size: u64) -> Result<GPT> where
    R: Read + Seek
[src]

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");

pub fn find_from<R: ?Sized>(reader: &mut R) -> Result<GPT> where
    R: Read + Seek
[src]

Find the GPT on a reader. This function will try to read the GPT on a disk using a sector size of 512 but if it fails it will automatically try to read the GPT using a sector size of 4096.

Examples

Basic usage:

let mut f_512 = std::fs::File::open("tests/fixtures/disk1.img")
    .expect("could not open disk");
let gpt_512 = gptman::GPT::find_from(&mut f_512)
    .expect("could not read the partition table");

let mut f_4096 = std::fs::File::open("tests/fixtures/disk2.img")
    .expect("could not open disk");
let gpt_4096 = gptman::GPT::find_from(&mut f_4096)
    .expect("could not read the partition table");

pub fn write_into<W: ?Sized>(&mut self, writer: &mut W) -> Result<GPTHeader> where
    W: Write + Seek
[src]

Write the GPT to a writer. This function will seek automatically in the writer to write the primary header and the backup header at their proper location.

Returns the backup GPTHeader that has been wrote in case of success (or the primary GPTHeader if self was using a backup header).

Note that the checksums are re-calculated, thus updating the header.

Errors

The partitions will be checked for consistency before being wrote to disk:

  • the partition GUIDs must be unique,
  • the partitions must have positive size,
  • the partitions must not overlap,
  • the partitions must fit within the disk.

Examples

Basic usage:

let ss = 512;
let data = vec![0; 100 * ss as usize];
let mut cur = std::io::Cursor::new(data);
let mut gpt = gptman::GPT::new_from(&mut cur, ss as u64, [0xff; 16])
    .expect("could not make a partition table");

// actually write:
gpt.write_into(&mut cur)
    .expect("could not write GPT to disk");

pub fn find_at_sector(&self, sector: u64) -> Option<u32>[src]

Finds the partition where the given sector resides.

pub fn find_free_sectors(&self) -> Vec<(u64, u64)>[src]

Find free spots in the partition table. This function will return a vector of tuple with on the left: the starting LBA of the free spot; and on the right: the size (in sectors) of the free spot. This function will automatically align with the alignment defined in the GPT.

Examples

Basic usage:

let ss = 512;
let data = vec![0; 100 * ss as usize];
let mut cur = std::io::Cursor::new(data);
let mut gpt = gptman::GPT::new_from(&mut cur, ss as u64, [0xff; 16])
    .expect("could not create partition table");

gpt[1] = gptman::GPTPartitionEntry {
    partition_type_guid: [0xff; 16],
    unique_partition_guid: [0xff; 16],
    starting_lba: gpt.header.first_usable_lba + 5,
    ending_lba: gpt.header.last_usable_lba - 5,
    attribute_bits: 0,
    partition_name: "A Robot Named Fight!".into(),
};

// NOTE: align to the sectors, so we can use every last one of them
// NOTE: this is only for the demonstration purpose, this is not recommended
gpt.align = 1;

assert_eq!(
    gpt.find_free_sectors(),
    vec![(gpt.header.first_usable_lba, 5), (gpt.header.last_usable_lba - 4, 5)]
);

pub fn find_first_place(&self, size: u64) -> Option<u64>[src]

Find the first place (most on the left) where you could start a new partition of the size given in parameter. This function will automatically align with the alignment defined in the GPT.

Examples

Basic usage:

let ss = 512;
let data = vec![0; 100 * ss as usize];
let mut cur = std::io::Cursor::new(data);
let mut gpt = gptman::GPT::new_from(&mut cur, ss as u64, [0xff; 16])
    .expect("could not create partition table");

gpt[1] = gptman::GPTPartitionEntry {
    partition_type_guid: [0xff; 16],
    unique_partition_guid: [0xff; 16],
    starting_lba: gpt.header.first_usable_lba + 5,
    ending_lba: gpt.header.last_usable_lba - 5,
    attribute_bits: 0,
    partition_name: "A Robot Named Fight!".into(),
};

// NOTE: align to the sectors, so we can use every last one of them
// NOTE: this is only for the demonstration purpose, this is not recommended
gpt.align = 1;

assert_eq!(gpt.find_first_place(5), Some(gpt.header.first_usable_lba));

pub fn find_last_place(&self, size: u64) -> Option<u64>[src]

Find the last place (most on the right) where you could start a new partition of the size given in parameter. This function will automatically align with the alignment defined in the GPT.

Examples

Basic usage:

let ss = 512;
let data = vec![0; 100 * ss as usize];
let mut cur = std::io::Cursor::new(data);
let mut gpt = gptman::GPT::new_from(&mut cur, ss as u64, [0xff; 16])
    .expect("could not create partition table");

gpt[1] = gptman::GPTPartitionEntry {
    partition_type_guid: [0xff; 16],
    unique_partition_guid: [0xff; 16],
    starting_lba: gpt.header.first_usable_lba + 5,
    ending_lba: gpt.header.last_usable_lba - 5,
    attribute_bits: 0,
    partition_name: "A Robot Named Fight!".into(),
};

// NOTE: align to the sectors, so we can use every last one of them
// NOTE: this is only for the demonstration purpose, this is not recommended
gpt.align = 1;

assert_eq!(gpt.find_last_place(5), Some(gpt.header.last_usable_lba - 4));

pub fn find_optimal_place(&self, size: u64) -> Option<u64>[src]

Find the most optimal place (in the smallest free space) where you could start a new partition of the size given in parameter. This function will automatically align with the alignment defined in the GPT.

Examples

Basic usage:

let ss = 512;
let data = vec![0; 100 * ss as usize];
let mut cur = std::io::Cursor::new(data);
let mut gpt = gptman::GPT::new_from(&mut cur, ss as u64, [0xff; 16])
    .expect("could not create partition table");

gpt[1] = gptman::GPTPartitionEntry {
    partition_type_guid: [0xff; 16],
    unique_partition_guid: [0xff; 16],
    starting_lba: gpt.header.first_usable_lba + 10,
    ending_lba: gpt.header.last_usable_lba - 5,
    attribute_bits: 0,
    partition_name: "A Robot Named Fight!".into(),
};

// NOTE: align to the sectors, so we can use every last one of them
// NOTE: this is only for the demonstration purpose, this is not recommended
gpt.align = 1;

// NOTE: the space as the end is more optimal because it will allow you to still be able to
//       insert a bigger partition later
assert_eq!(gpt.find_optimal_place(5), Some(gpt.header.last_usable_lba - 4));

pub fn get_maximum_partition_size(&self) -> Result<u64>[src]

Get the maximum size (in sectors) of a partition you could create in the GPT. This function will automatically align with the alignment defined in the GPT.

Examples

Basic usage:

let ss = 512;
let data = vec![0; 100 * ss as usize];
let mut cur = std::io::Cursor::new(data);
let mut gpt = gptman::GPT::new_from(&mut cur, ss as u64, [0xff; 16])
    .expect("could not create partition table");

// NOTE: align to the sectors, so we can use every last one of them
// NOTE: this is only for the demonstration purpose, this is not recommended
gpt.align = 1;

assert_eq!(
    gpt.get_maximum_partition_size().unwrap_or(0),
    gpt.header.last_usable_lba + 1 - gpt.header.first_usable_lba
);

pub fn sort(&mut self)[src]

Sort the partition entries in the array by the starting LBA.

pub fn remove(&mut self, i: u32) -> Result<()>[src]

Remove a partition entry in the array.

This is the equivalent of: gpt[i] = gptman::GPTPartitionEntry::empty();

Errors

This function will return an error if i is lesser or equal to 0 or greater than the number of partition entries (which can be obtained in the header).

pub fn remove_at_sector(&mut self, sector: u64) -> Result<()>[src]

Remove a partiton entry in the array that resides at a given sector.

Errors

It is an error to provide a sector which does not belong to a partition.

pub fn iter(&self) -> impl Iterator<Item = (u32, &GPTPartitionEntry)>[src]

Get an iterator over the partition entries and their index. The index always starts at 1.

pub fn iter_mut(
    &mut self
) -> impl Iterator<Item = (u32, &mut GPTPartitionEntry)>
[src]

Get a mutable iterator over the partition entries and their index. The index always starts at 1.

pub fn write_protective_mbr_into<W: ?Sized>(
    writer: &mut W,
    sector_size: u64
) -> Result<()> where
    W: Write + Seek
[src]

This function writes a protective MBR in the first sector of the disk starting at byte 446 and ending at byte 511. Any existing data will be overwritten.

pub fn is_primary(&self) -> bool[src]

Returns true if the GPTHeader is a primary copy (the header is located at the beginning of the disk).

pub fn is_backup(&self) -> bool[src]

Returns true if the GPTHeader is a backup copy (the header is located at the end of the disk).

Note that when the header is a backup copy, the primary_lba is the LBA of the backup copy and the backup_lba is the LBA of the primary copy.

Trait Implementations

impl Clone for GPT[src]

impl Debug for GPT[src]

impl Eq for GPT[src]

impl Index<u32> for GPT[src]

type Output = GPTPartitionEntry

The returned type after indexing.

impl IndexMut<u32> for GPT[src]

impl PartialEq<GPT> for GPT[src]

impl StructuralEq for GPT[src]

impl StructuralPartialEq for GPT[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,