Skip to content

Commit 17be6e0

Browse files
committed
Move partition_entry_lba update to update_from & improve doc
1 parent cca1ac1 commit 17be6e0

File tree

1 file changed

+32
-8
lines changed

1 file changed

+32
-8
lines changed

src/lib.rs

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@ pub struct GPTHeader {
182182
/// 16 bytes representing the UUID of the GPT.
183183
pub disk_guid: [u8; 16],
184184
/// Location (in sectors) of the partition entries array.
185+
///
186+
/// This is always `2` if the header is a primary header and not the backup header.
185187
pub partition_entry_lba: u64,
186188
/// Number of partition entries in the array.
187189
pub number_of_partition_entries: u32,
@@ -317,9 +319,9 @@ impl GPTHeader {
317319
self.partition_entry_array_crc32 = self.generate_partition_entry_array_crc32(partitions);
318320
}
319321

320-
/// Updates the header to match the specifications of the reader given in argument.
321-
/// `first_usable_lba`, `last_usable_lba`, `primary_lba`, `backup_lba` will be updated after
322-
/// this operation.
322+
/// Updates the header to match the specifications of the seeker given in argument.
323+
/// `first_usable_lba`, `last_usable_lba`, `primary_lba`, `backup_lba`, `partition_entry_lba`
324+
/// will be updated after this operation.
323325
pub fn update_from<S: ?Sized>(&mut self, seeker: &mut S, sector_size: u64) -> Result<()>
324326
where
325327
S: Seek,
@@ -337,6 +339,13 @@ impl GPTHeader {
337339
}
338340
self.last_usable_lba = len - partition_array_size - 1 - 1;
339341
self.first_usable_lba = 2 + partition_array_size;
342+
// NOTE: the partition_entry_lba is either 2 either something near the end of the disk.
343+
// If it is something near the end of the disk, it means the self object is a backup
344+
// GPT header (which is located at the end of the disk) and its partition_entry_lba
345+
// must be updated accordingly
346+
if self.partition_entry_lba != 2 {
347+
self.partition_entry_lba = self.last_usable_lba + 1;
348+
}
340349

341350
Ok(())
342351
}
@@ -783,7 +792,24 @@ impl GPT {
783792
/// Write the GPT to a writer. This function will seek automatically in the writer to write the
784793
/// primary header and the backup header at their proper location.
785794
///
786-
/// # Examples:
795+
/// # Implementation notes
796+
///
797+
/// Calling this function will call `update_from` in order to update the `last_usable_lba`
798+
/// among other fields of the GPT header, thus modifying the `self` object.
799+
///
800+
/// # Errors
801+
///
802+
/// The partitions will be checked for consistency before being wrote to disk:
803+
///
804+
/// * the partition GUIDs must be unique,
805+
/// * the partitions must have positive size,
806+
/// * the partitions must not overlap,
807+
/// * the partitions must fit within the disk.
808+
///
809+
/// Note that the `self` object will be updated even if one of those error occurs.
810+
///
811+
/// # Examples
812+
///
787813
/// Basic usage:
788814
/// ```
789815
/// let ss = 512;
@@ -800,11 +826,9 @@ impl GPT {
800826
where
801827
W: Write + Seek,
802828
{
803-
self.check_partition_guids()?;
804829
self.header.update_from(&mut writer, self.sector_size)?;
805-
if self.header.partition_entry_lba != 2 {
806-
self.header.partition_entry_lba = self.header.last_usable_lba + 1;
807-
}
830+
831+
self.check_partition_guids()?;
808832
self.check_partition_boundaries()?;
809833

810834
let mut backup = self.header.clone();

0 commit comments

Comments
 (0)