From 9347e522f9c91fea08db07c7c693d5436396e329 Mon Sep 17 00:00:00 2001 From: Richard Stephens Date: Sat, 13 Jan 2024 16:38:36 +0100 Subject: [PATCH] refactor: pr feedback #132 - refactor to separate fn to avoid API break --- src/cli/commands.rs | 2 +- src/lib.rs | 31 +++++++++++++++++++++++++------ 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/cli/commands.rs b/src/cli/commands.rs index e52f9e7..b6ab831 100644 --- a/src/cli/commands.rs +++ b/src/cli/commands.rs @@ -529,7 +529,7 @@ fn write(gpt: &mut GPT, opt: &Opt) -> Result<()> { gpt.write_into(&mut f)?; if opt.init { - GPT::write_protective_mbr_into(&mut f, gpt.sector_size, false)?; + GPT::write_protective_mbr_into(&mut f, gpt.sector_size)?; println!("protective MBR has been written"); } diff --git a/src/lib.rs b/src/lib.rs index 522f99a..23702f0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1232,11 +1232,30 @@ impl GPT { /// This function writes a protective MBR in the first sector of the disk /// starting at byte 446 and ending at byte 511. Any existing data will be overwritten. /// - /// * `bootable` - Indicates whether the partition in the MBR partition table should be marked - /// as bootable. Some legacy BIOS systems do not consider a disk to be bootable if there isn't - /// an MBR partition marked as bootable, so this should be set to `true` if you intend the disk - /// to be bootable on those systems. - pub fn write_protective_mbr_into( + /// See also: [`Self::write_bootable_protective_mbr_into`]. + pub fn write_protective_mbr_into(mut writer: &mut W, sector_size: u64) -> Result<()> + where + W: Write + Seek, + { + Self::write_protective_mbr_into_impl(&mut writer, sector_size, false) + } + + /// This function writes a protective MBR in the first sector of the disk + /// starting at byte 446 and ending at byte 511. Any existing data will be overwritten. + /// This function differs from [`Self::write_protective_mbr_into`] in that the partition in the + /// MBR partition table is marked as bootable. Some legacy BIOS systems do not consider a disk + /// to be bootable if there isn't an MBR partition marked as bootable in the MBR partition + /// table. + pub fn write_bootable_protective_mbr_into( + mut writer: &mut W, + sector_size: u64, + ) -> Result<()> + where + W: Write + Seek, + { + Self::write_protective_mbr_into_impl(&mut writer, sector_size, true) + } + fn write_protective_mbr_into_impl( mut writer: &mut W, sector_size: u64, bootable: bool, @@ -1825,7 +1844,7 @@ mod test { fn test(ss: u64) { let data = vec![2; ss as usize * 100]; let mut cur = io::Cursor::new(data); - GPT::write_protective_mbr_into(&mut cur, ss, false).unwrap(); + GPT::write_protective_mbr_into(&mut cur, ss).unwrap(); let data = cur.get_ref(); assert_eq!(data[510], 0x55);