Skip to content

Commit

Permalink
refactor: pr feedback #132 - refactor to separate fn to avoid API break
Browse files Browse the repository at this point in the history
  • Loading branch information
richardstephens committed Jan 13, 2024
1 parent f75abbe commit 9347e52
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/cli/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Expand Down
31 changes: 25 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<W: ?Sized>(
/// See also: [`Self::write_bootable_protective_mbr_into`].
pub fn write_protective_mbr_into<W: ?Sized>(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<W: ?Sized>(
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<W: ?Sized>(
mut writer: &mut W,
sector_size: u64,
bootable: bool,
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 9347e52

Please sign in to comment.