Skip to content

Commit 0b7d158

Browse files
committed
Add functions to check if primary header & return backup header
Also test equality.
1 parent 17be6e0 commit 0b7d158

File tree

1 file changed

+43
-5
lines changed

1 file changed

+43
-5
lines changed

src/lib.rs

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,21 @@ impl GPTHeader {
349349

350350
Ok(())
351351
}
352+
353+
/// Returns `true` if the `GPTHeader` is a primary copy (the header is located at the beginning
354+
/// of the disk).
355+
pub fn is_primary(&self) -> bool {
356+
self.primary_lba == 1
357+
}
358+
359+
/// Returns `true` if the `GPTHeader` is a backup copy (the header is located at the end of the
360+
/// disk).
361+
///
362+
/// Note that when the header is a backup copy, the `primary_lba` is the LBA of the backup copy
363+
/// and the `backup_lba` is the LBA of the primary copy.
364+
pub fn is_backup(&self) -> bool {
365+
!self.is_primary()
366+
}
352367
}
353368

354369
/// A wrapper type for `String` that represents a partition's name.
@@ -792,6 +807,9 @@ impl GPT {
792807
/// Write the GPT to a writer. This function will seek automatically in the writer to write the
793808
/// primary header and the backup header at their proper location.
794809
///
810+
/// Returns the backup `GPTHeader` that has been wrote in case of success (or the primary
811+
/// `GPTHeader` if `self` was using a backup header).
812+
///
795813
/// # Implementation notes
796814
///
797815
/// Calling this function will call `update_from` in order to update the `last_usable_lba`
@@ -820,9 +838,9 @@ impl GPT {
820838
///
821839
/// // actually write:
822840
/// gpt.write_into(&mut cur)
823-
/// .expect("could not write GPT to disk")
841+
/// .expect("could not write GPT to disk");
824842
/// ```
825-
pub fn write_into<W: ?Sized>(&mut self, mut writer: &mut W) -> Result<()>
843+
pub fn write_into<W: ?Sized>(&mut self, mut writer: &mut W) -> Result<GPTHeader>
826844
where
827845
W: Write + Seek,
828846
{
@@ -844,7 +862,7 @@ impl GPT {
844862
.write_into(&mut writer, self.sector_size, &self.partitions)?;
845863
backup.write_into(&mut writer, self.sector_size, &self.partitions)?;
846864

847-
Ok(())
865+
Ok(backup)
848866
}
849867

850868
/// Finds the partition where the given sector resides.
@@ -1140,6 +1158,21 @@ impl GPT {
11401158

11411159
Ok(())
11421160
}
1161+
1162+
/// Returns `true` if the `GPTHeader` is a primary copy (the header is located at the beginning
1163+
/// of the disk).
1164+
pub fn is_primary(&self) -> bool {
1165+
self.header.is_primary()
1166+
}
1167+
1168+
/// Returns `true` if the `GPTHeader` is a backup copy (the header is located at the end of the
1169+
/// disk).
1170+
///
1171+
/// Note that when the header is a backup copy, the `primary_lba` is the LBA of the backup copy
1172+
/// and the `backup_lba` is the LBA of the primary copy.
1173+
pub fn is_backup(&self) -> bool {
1174+
self.header.is_backup()
1175+
}
11431176
}
11441177

11451178
impl Index<u32> for GPT {
@@ -1431,22 +1464,27 @@ mod test {
14311464
fn test(path: &str, ss: u64) {
14321465
let mut cur = io::Cursor::new(fs::read(path).unwrap());
14331466
let mut gpt = GPT::read_from(&mut cur, ss).unwrap();
1467+
let primary = gpt.clone();
14341468
gpt.header.crc32_checksum = 1;
14351469
let backup_lba = gpt.header.backup_lba;
14361470
cur.seek(SeekFrom::Start(ss)).unwrap();
14371471
serialize_into(&mut cur, &gpt.header).unwrap();
14381472
let mut gpt = GPT::read_from(&mut cur, ss).unwrap();
1439-
assert_eq!(gpt.header.backup_lba, 1);
1473+
assert!(!gpt.is_primary());
1474+
assert!(gpt.is_backup());
14401475
let partition_entry_lba = gpt.header.partition_entry_lba;
14411476
let first_usable_lba = gpt.header.first_usable_lba;
14421477
let last_usable_lba = gpt.header.last_usable_lba;
1443-
gpt.write_into(&mut cur).unwrap();
1478+
let primary_header = gpt.write_into(&mut cur).unwrap();
1479+
assert!(primary_header.is_primary());
1480+
assert!(!primary_header.is_backup());
14441481
let mut gpt = GPT::read_from(&mut cur, ss).unwrap();
14451482
assert_eq!(gpt.header.primary_lba, 1);
14461483
assert_eq!(gpt.header.backup_lba, backup_lba);
14471484
assert_eq!(gpt.header.partition_entry_lba, 2);
14481485
assert_eq!(gpt.header.first_usable_lba, first_usable_lba);
14491486
assert_eq!(gpt.header.last_usable_lba, last_usable_lba);
1487+
assert_eq!(primary, gpt);
14501488

14511489
gpt.header.crc32_checksum = 1;
14521490
cur.seek(SeekFrom::Start(ss)).unwrap();

0 commit comments

Comments
 (0)