Skip to content

Commit 50e1e5d

Browse files
authored
Don't update header's LBA when writing to disk (#50)
1 parent 4c5c0c7 commit 50e1e5d

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

src/lib.rs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ use std::io::{Read, Seek, SeekFrom, Write};
9090
use std::ops::{Index, IndexMut};
9191

9292
/// Linux specific helpers
93+
#[cfg(target_os = "linux")]
9394
pub mod linux;
9495

9596
const DEFAULT_ALIGN: u64 = 2048;
@@ -259,7 +260,7 @@ impl GPTHeader {
259260
}
260261

261262
/// 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.
263264
pub fn write_into<W: ?Sized>(
264265
&mut self,
265266
mut writer: &mut W,
@@ -825,10 +826,7 @@ impl GPT {
825826
/// Returns the backup `GPTHeader` that has been wrote in case of success (or the primary
826827
/// `GPTHeader` if `self` was using a backup header).
827828
///
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.
832830
///
833831
/// # Errors
834832
///
@@ -839,8 +837,6 @@ impl GPT {
839837
/// * the partitions must not overlap,
840838
/// * the partitions must fit within the disk.
841839
///
842-
/// Note that the `self` object will be updated even if one of those error occurs.
843-
///
844840
/// # Examples
845841
///
846842
/// Basic usage:
@@ -859,8 +855,6 @@ impl GPT {
859855
where
860856
W: Write + Seek,
861857
{
862-
self.header.update_from(&mut writer, self.sector_size)?;
863-
864858
self.check_partition_guids()?;
865859
self.check_partition_boundaries()?;
866860

@@ -1752,4 +1746,21 @@ mod test {
17521746
test(512);
17531747
test(4096);
17541748
}
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+
}
17551766
}

src/linux.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#![cfg(target_os = "linux")]
2-
31
use std::fs;
42
use std::io;
53
use std::os::linux::fs::MetadataExt;

0 commit comments

Comments
 (0)