@@ -349,6 +349,21 @@ impl GPTHeader {
349
349
350
350
Ok ( ( ) )
351
351
}
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
+ }
352
367
}
353
368
354
369
/// A wrapper type for `String` that represents a partition's name.
@@ -792,6 +807,9 @@ impl GPT {
792
807
/// Write the GPT to a writer. This function will seek automatically in the writer to write the
793
808
/// primary header and the backup header at their proper location.
794
809
///
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
+ ///
795
813
/// # Implementation notes
796
814
///
797
815
/// Calling this function will call `update_from` in order to update the `last_usable_lba`
@@ -820,9 +838,9 @@ impl GPT {
820
838
///
821
839
/// // actually write:
822
840
/// gpt.write_into(&mut cur)
823
- /// .expect("could not write GPT to disk")
841
+ /// .expect("could not write GPT to disk");
824
842
/// ```
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 >
826
844
where
827
845
W : Write + Seek ,
828
846
{
@@ -844,7 +862,7 @@ impl GPT {
844
862
. write_into ( & mut writer, self . sector_size , & self . partitions ) ?;
845
863
backup. write_into ( & mut writer, self . sector_size , & self . partitions ) ?;
846
864
847
- Ok ( ( ) )
865
+ Ok ( backup )
848
866
}
849
867
850
868
/// Finds the partition where the given sector resides.
@@ -1140,6 +1158,21 @@ impl GPT {
1140
1158
1141
1159
Ok ( ( ) )
1142
1160
}
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
+ }
1143
1176
}
1144
1177
1145
1178
impl Index < u32 > for GPT {
@@ -1431,22 +1464,27 @@ mod test {
1431
1464
fn test ( path : & str , ss : u64 ) {
1432
1465
let mut cur = io:: Cursor :: new ( fs:: read ( path) . unwrap ( ) ) ;
1433
1466
let mut gpt = GPT :: read_from ( & mut cur, ss) . unwrap ( ) ;
1467
+ let primary = gpt. clone ( ) ;
1434
1468
gpt. header . crc32_checksum = 1 ;
1435
1469
let backup_lba = gpt. header . backup_lba ;
1436
1470
cur. seek ( SeekFrom :: Start ( ss) ) . unwrap ( ) ;
1437
1471
serialize_into ( & mut cur, & gpt. header ) . unwrap ( ) ;
1438
1472
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( ) ) ;
1440
1475
let partition_entry_lba = gpt. header . partition_entry_lba ;
1441
1476
let first_usable_lba = gpt. header . first_usable_lba ;
1442
1477
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( ) ) ;
1444
1481
let mut gpt = GPT :: read_from ( & mut cur, ss) . unwrap ( ) ;
1445
1482
assert_eq ! ( gpt. header. primary_lba, 1 ) ;
1446
1483
assert_eq ! ( gpt. header. backup_lba, backup_lba) ;
1447
1484
assert_eq ! ( gpt. header. partition_entry_lba, 2 ) ;
1448
1485
assert_eq ! ( gpt. header. first_usable_lba, first_usable_lba) ;
1449
1486
assert_eq ! ( gpt. header. last_usable_lba, last_usable_lba) ;
1487
+ assert_eq ! ( primary, gpt) ;
1450
1488
1451
1489
gpt. header . crc32_checksum = 1 ;
1452
1490
cur. seek ( SeekFrom :: Start ( ss) ) . unwrap ( ) ;
0 commit comments