Skip to content

Commit a0fbbda

Browse files
committed
Rename and slightly refactor determine_fs_layout
1 parent 9efc157 commit a0fbbda

File tree

1 file changed

+40
-41
lines changed

1 file changed

+40
-41
lines changed

src/boot_sector.rs

Lines changed: 40 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ fn determine_sectors_per_fat(
577577
sectors_per_fat as u32
578578
}
579579

580-
fn try_fs_geometry(
580+
fn try_fs_layout(
581581
total_sectors: u32,
582582
bytes_per_sector: u16,
583583
sectors_per_cluster: u8,
@@ -638,25 +638,41 @@ fn determine_root_dir_sectors(root_dir_entries: u16, bytes_per_sector: u16, fat_
638638
}
639639
}
640640

641-
fn determine_fs_geometry<E: IoError>(
642-
total_sectors: u32,
643-
bytes_per_sector: u16,
641+
struct FsLayout {
642+
fat_type: FatType,
643+
reserved_sectors: u16,
644+
sectors_per_fat: u32,
644645
sectors_per_cluster: u8,
645-
root_dir_entries: u16,
646-
fats: u8,
647-
) -> Result<(FatType, u16, u32), Error<E>> {
646+
}
647+
648+
fn determine_fs_layout<E: IoError>(options: &FormatVolumeOptions, total_sectors: u32) -> Result<FsLayout, Error<E>> {
649+
let bytes_per_cluster = options.bytes_per_cluster.unwrap_or_else(|| {
650+
let total_bytes = u64::from(total_sectors) * u64::from(options.bytes_per_sector);
651+
determine_bytes_per_cluster(total_bytes, options.bytes_per_sector, options.fat_type)
652+
});
653+
654+
let sectors_per_cluster = bytes_per_cluster / u32::from(options.bytes_per_sector);
655+
assert!(sectors_per_cluster <= u32::from(u8::MAX));
656+
let sectors_per_cluster = sectors_per_cluster as u8;
657+
648658
for &fat_type in &[FatType::Fat32, FatType::Fat16, FatType::Fat12] {
649-
let root_dir_sectors = determine_root_dir_sectors(root_dir_entries, bytes_per_sector, fat_type);
650-
let result = try_fs_geometry(
659+
let root_dir_sectors =
660+
determine_root_dir_sectors(options.max_root_dir_entries, options.bytes_per_sector, fat_type);
661+
let result = try_fs_layout(
651662
total_sectors,
652-
bytes_per_sector,
663+
options.bytes_per_sector,
653664
sectors_per_cluster,
654665
fat_type,
655666
root_dir_sectors,
656-
fats,
667+
options.fats,
657668
);
658669
if let Ok((reserved_sectors, sectors_per_fat)) = result {
659-
return Ok((fat_type, reserved_sectors, sectors_per_fat));
670+
return Ok(FsLayout {
671+
fat_type,
672+
reserved_sectors,
673+
sectors_per_fat,
674+
sectors_per_cluster,
675+
});
660676
}
661677
}
662678

@@ -668,29 +684,12 @@ fn format_bpb<E: IoError>(
668684
options: &FormatVolumeOptions,
669685
total_sectors: u32,
670686
) -> Result<(BiosParameterBlock, FatType), Error<E>> {
671-
let bytes_per_cluster = options.bytes_per_cluster.unwrap_or_else(|| {
672-
let total_bytes = u64::from(total_sectors) * u64::from(options.bytes_per_sector);
673-
determine_bytes_per_cluster(total_bytes, options.bytes_per_sector, options.fat_type)
674-
});
675-
676-
let sectors_per_cluster = bytes_per_cluster / u32::from(options.bytes_per_sector);
677-
assert!(sectors_per_cluster <= u32::from(u8::MAX));
678-
let sectors_per_cluster = sectors_per_cluster as u8;
679-
680-
let fats = options.fats;
681-
let root_dir_entries = options.max_root_dir_entries;
682-
let (fat_type, reserved_sectors, sectors_per_fat) = determine_fs_geometry(
683-
total_sectors,
684-
options.bytes_per_sector,
685-
sectors_per_cluster,
686-
root_dir_entries,
687-
fats,
688-
)?;
687+
let layout = determine_fs_layout(options, total_sectors)?;
689688

690689
// drive_num should be 0 for floppy disks and 0x80 for hard disks - determine it using FAT type
691690
let drive_num = options
692691
.drive_num
693-
.unwrap_or_else(|| if fat_type == FatType::Fat12 { 0 } else { 0x80 });
692+
.unwrap_or_else(|| if layout.fat_type == FatType::Fat12 { 0 } else { 0x80 });
694693

695694
// reserved_0 is always zero
696695
let reserved_0 = [0_u8; 12];
@@ -713,19 +712,19 @@ fn format_bpb<E: IoError>(
713712
fs_type_label.copy_from_slice(fs_type_label_str);
714713

715714
// create Bios Parameter Block struct
716-
let is_fat32 = fat_type == FatType::Fat32;
715+
let is_fat32 = layout.fat_type == FatType::Fat32;
717716
let sectors_per_fat_16 = if is_fat32 {
718717
0
719718
} else {
720-
debug_assert!(sectors_per_fat <= u32::from(u16::MAX));
721-
sectors_per_fat as u16
719+
debug_assert!(layout.sectors_per_fat <= u32::from(u16::MAX));
720+
layout.sectors_per_fat as u16
722721
};
723722
let bpb = BiosParameterBlock {
724723
bytes_per_sector: options.bytes_per_sector,
725-
sectors_per_cluster,
726-
reserved_sectors,
727-
fats,
728-
root_entries: if is_fat32 { 0 } else { root_dir_entries },
724+
sectors_per_cluster: layout.sectors_per_cluster,
725+
reserved_sectors: layout.reserved_sectors,
726+
fats: options.fats,
727+
root_entries: if is_fat32 { 0 } else { options.max_root_dir_entries },
729728
total_sectors_16: if total_sectors < 0x10000 {
730729
total_sectors as u16
731730
} else {
@@ -738,7 +737,7 @@ fn format_bpb<E: IoError>(
738737
hidden_sectors: 0,
739738
total_sectors_32: if total_sectors >= 0x10000 { total_sectors } else { 0 },
740739
// FAT32 fields start
741-
sectors_per_fat_32: if is_fat32 { sectors_per_fat } else { 0 },
740+
sectors_per_fat_32: if is_fat32 { layout.sectors_per_fat } else { 0 },
742741
extended_flags: 0, // mirroring enabled
743742
fs_version: 0,
744743
root_dir_first_cluster: if is_fat32 { 2 } else { 0 },
@@ -755,12 +754,12 @@ fn format_bpb<E: IoError>(
755754
};
756755

757756
// Check if number of clusters is proper for used FAT type
758-
if FatType::from_clusters(bpb.total_clusters()) != fat_type {
757+
if FatType::from_clusters(bpb.total_clusters()) != layout.fat_type {
759758
error!("Total number of clusters and FAT type does not match, please try a different volume size");
760759
return Err(Error::InvalidInput);
761760
}
762761

763-
Ok((bpb, fat_type))
762+
Ok((bpb, layout.fat_type))
764763
}
765764

766765
pub(crate) fn format_boot_sector<E: IoError>(

0 commit comments

Comments
 (0)