@@ -90,6 +90,7 @@ use std::io::{Read, Seek, SeekFrom, Write};
90
90
use std:: ops:: { Index , IndexMut } ;
91
91
92
92
/// Linux specific helpers
93
+ #[ cfg( target_os = "linux" ) ]
93
94
pub mod linux;
94
95
95
96
const DEFAULT_ALIGN : u64 = 2048 ;
@@ -259,7 +260,7 @@ impl GPTHeader {
259
260
}
260
261
261
262
/// Write the GPT header into a writer. This operation will update the CRC32 checksums of the
262
- /// current struct and seek at the correct location before trying to write to disk.
263
+ /// current struct and seek at the location `primary_lba` before trying to write to disk.
263
264
pub fn write_into < W : ?Sized > (
264
265
& mut self ,
265
266
mut writer : & mut W ,
@@ -825,10 +826,7 @@ impl GPT {
825
826
/// Returns the backup `GPTHeader` that has been wrote in case of success (or the primary
826
827
/// `GPTHeader` if `self` was using a backup header).
827
828
///
828
- /// # Implementation notes
829
- ///
830
- /// Calling this function will call `update_from` in order to update the `last_usable_lba`
831
- /// among other fields of the GPT header, thus modifying the `self` object.
829
+ /// Note that the checksums are re-calculated, thus updating the header.
832
830
///
833
831
/// # Errors
834
832
///
@@ -839,8 +837,6 @@ impl GPT {
839
837
/// * the partitions must not overlap,
840
838
/// * the partitions must fit within the disk.
841
839
///
842
- /// Note that the `self` object will be updated even if one of those error occurs.
843
- ///
844
840
/// # Examples
845
841
///
846
842
/// Basic usage:
@@ -859,8 +855,6 @@ impl GPT {
859
855
where
860
856
W : Write + Seek ,
861
857
{
862
- self . header . update_from ( & mut writer, self . sector_size ) ?;
863
-
864
858
self . check_partition_guids ( ) ?;
865
859
self . check_partition_boundaries ( ) ?;
866
860
@@ -1752,4 +1746,21 @@ mod test {
1752
1746
test ( 512 ) ;
1753
1747
test ( 4096 ) ;
1754
1748
}
1749
+
1750
+ #[ test]
1751
+ fn read_from_smaller_disk_and_write_to_bigger_disk ( ) {
1752
+ fn test ( path : & str , ss : u64 ) {
1753
+ let mut f = fs:: File :: open ( path) . unwrap ( ) ;
1754
+ let len = f. seek ( SeekFrom :: End ( 0 ) ) . unwrap ( ) ;
1755
+ let gpt1 = GPT :: read_from ( & mut f, ss) . unwrap ( ) ;
1756
+ let data = vec ! [ 0 ; len as usize * 2 ] ;
1757
+ let mut cur = io:: Cursor :: new ( data) ;
1758
+ gpt1. clone ( ) . write_into ( & mut cur) . unwrap ( ) ;
1759
+ let gpt2 = GPT :: read_from ( & mut cur, ss) . unwrap ( ) ;
1760
+ assert_eq ! ( gpt1, gpt2) ;
1761
+ }
1762
+
1763
+ test ( DISK1 , 512 ) ;
1764
+ test ( DISK2 , 4096 ) ;
1765
+ }
1755
1766
}
0 commit comments