Skip to content

Commit 9efc157

Browse files
committed
Simplify code by avoiding unnecessary Options in FormatVolumeOptions
1 parent 1aa43f7 commit 9efc157

File tree

2 files changed

+49
-33
lines changed

2 files changed

+49
-33
lines changed

src/boot_sector.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -667,22 +667,21 @@ fn determine_fs_geometry<E: IoError>(
667667
fn format_bpb<E: IoError>(
668668
options: &FormatVolumeOptions,
669669
total_sectors: u32,
670-
bytes_per_sector: u16,
671670
) -> Result<(BiosParameterBlock, FatType), Error<E>> {
672671
let bytes_per_cluster = options.bytes_per_cluster.unwrap_or_else(|| {
673-
let total_bytes = u64::from(total_sectors) * u64::from(bytes_per_sector);
674-
determine_bytes_per_cluster(total_bytes, bytes_per_sector, options.fat_type)
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)
675674
});
676675

677-
let sectors_per_cluster = bytes_per_cluster / u32::from(bytes_per_sector);
676+
let sectors_per_cluster = bytes_per_cluster / u32::from(options.bytes_per_sector);
678677
assert!(sectors_per_cluster <= u32::from(u8::MAX));
679678
let sectors_per_cluster = sectors_per_cluster as u8;
680679

681-
let fats = options.fats.unwrap_or(2_u8);
682-
let root_dir_entries = options.max_root_dir_entries.unwrap_or(512);
680+
let fats = options.fats;
681+
let root_dir_entries = options.max_root_dir_entries;
683682
let (fat_type, reserved_sectors, sectors_per_fat) = determine_fs_geometry(
684683
total_sectors,
685-
bytes_per_sector,
684+
options.bytes_per_sector,
686685
sectors_per_cluster,
687686
root_dir_entries,
688687
fats,
@@ -722,7 +721,7 @@ fn format_bpb<E: IoError>(
722721
sectors_per_fat as u16
723722
};
724723
let bpb = BiosParameterBlock {
725-
bytes_per_sector,
724+
bytes_per_sector: options.bytes_per_sector,
726725
sectors_per_cluster,
727726
reserved_sectors,
728727
fats,
@@ -732,10 +731,10 @@ fn format_bpb<E: IoError>(
732731
} else {
733732
0
734733
},
735-
media: options.media.unwrap_or(0xF8),
734+
media: options.media,
736735
sectors_per_fat_16,
737-
sectors_per_track: options.sectors_per_track.unwrap_or(0x20),
738-
heads: options.heads.unwrap_or(0x40),
736+
sectors_per_track: options.sectors_per_track,
737+
heads: options.heads,
739738
hidden_sectors: 0,
740739
total_sectors_32: if total_sectors >= 0x10000 { total_sectors } else { 0 },
741740
// FAT32 fields start
@@ -750,7 +749,7 @@ fn format_bpb<E: IoError>(
750749
drive_num,
751750
reserved_1: 0,
752751
ext_sig: 0x29,
753-
volume_id: options.volume_id.unwrap_or(0x1234_5678),
752+
volume_id: options.volume_id,
754753
volume_label,
755754
fs_type_label,
756755
};
@@ -767,10 +766,9 @@ fn format_bpb<E: IoError>(
767766
pub(crate) fn format_boot_sector<E: IoError>(
768767
options: &FormatVolumeOptions,
769768
total_sectors: u32,
770-
bytes_per_sector: u16,
771769
) -> Result<(BootSector, FatType), Error<E>> {
772770
let mut boot = BootSector::default();
773-
let (bpb, fat_type) = format_bpb(options, total_sectors, bytes_per_sector)?;
771+
let (bpb, fat_type) = format_bpb(options, total_sectors)?;
774772
boot.bpb = bpb;
775773
boot.oem_name.copy_from_slice(b"MSWIN4.1");
776774
// Boot code copied from FAT32 boot sector initialized by mkfs.fat
@@ -977,7 +975,7 @@ mod tests {
977975
total_sectors_vec.push(u32::MAX);
978976
total_sectors_vec.push(8227);
979977
for total_sectors in total_sectors_vec {
980-
let (boot, _) = format_boot_sector::<()>(&FormatVolumeOptions::new(), total_sectors, bytes_per_sector)
978+
let (boot, _) = format_boot_sector::<()>(&FormatVolumeOptions::new(), total_sectors)
981979
.unwrap_or_else(|_| panic!("format_boot_sector total_sectors: {}", total_sectors));
982980
boot.validate::<()>().expect("validate");
983981
}

src/fs.rs

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -925,22 +925,41 @@ fn write_zeros_until_end_of_sector<IO: ReadWriteSeek>(disk: &mut IO, bytes_per_s
925925
///
926926
/// This struct implements a builder pattern.
927927
/// Options are specified as an argument for `format_volume` function.
928-
#[derive(Default, Debug, Clone)]
928+
#[derive(Debug, Clone)]
929929
pub struct FormatVolumeOptions {
930-
pub(crate) bytes_per_sector: Option<u16>,
930+
pub(crate) bytes_per_sector: u16,
931931
pub(crate) total_sectors: Option<u32>,
932932
pub(crate) bytes_per_cluster: Option<u32>,
933933
pub(crate) fat_type: Option<FatType>,
934-
pub(crate) max_root_dir_entries: Option<u16>,
935-
pub(crate) fats: Option<u8>,
936-
pub(crate) media: Option<u8>,
937-
pub(crate) sectors_per_track: Option<u16>,
938-
pub(crate) heads: Option<u16>,
934+
pub(crate) max_root_dir_entries: u16,
935+
pub(crate) fats: u8,
936+
pub(crate) media: u8,
937+
pub(crate) sectors_per_track: u16,
938+
pub(crate) heads: u16,
939939
pub(crate) drive_num: Option<u8>,
940-
pub(crate) volume_id: Option<u32>,
940+
pub(crate) volume_id: u32,
941941
pub(crate) volume_label: Option<[u8; SFN_SIZE]>,
942942
}
943943

944+
impl Default for FormatVolumeOptions {
945+
fn default() -> Self {
946+
Self {
947+
bytes_per_sector: 512,
948+
total_sectors: None,
949+
bytes_per_cluster: None,
950+
fat_type: None,
951+
max_root_dir_entries: 512,
952+
fats: 2,
953+
media: 0xF8,
954+
sectors_per_track: 0x20,
955+
heads: 0x40,
956+
drive_num: None,
957+
volume_id: 0x1234_5678,
958+
volume_label: None,
959+
}
960+
}
961+
}
962+
944963
impl FormatVolumeOptions {
945964
/// Create options struct for `format_volume` function
946965
///
@@ -996,7 +1015,7 @@ impl FormatVolumeOptions {
9961015
bytes_per_sector.count_ones() == 1 && bytes_per_sector >= 512,
9971016
"Invalid bytes_per_sector"
9981017
);
999-
self.bytes_per_sector = Some(bytes_per_sector);
1018+
self.bytes_per_sector = bytes_per_sector;
10001019
self
10011020
}
10021021

@@ -1017,7 +1036,7 @@ impl FormatVolumeOptions {
10171036
/// Default is `512`.
10181037
#[must_use]
10191038
pub fn max_root_dir_entries(mut self, max_root_dir_entries: u16) -> Self {
1020-
self.max_root_dir_entries = Some(max_root_dir_entries);
1039+
self.max_root_dir_entries = max_root_dir_entries;
10211040
self
10221041
}
10231042

@@ -1032,7 +1051,7 @@ impl FormatVolumeOptions {
10321051
#[must_use]
10331052
pub fn fats(mut self, fats: u8) -> Self {
10341053
assert!((1..=2).contains(&fats), "Invalid number of FATs");
1035-
self.fats = Some(fats);
1054+
self.fats = fats;
10361055
self
10371056
}
10381057

@@ -1041,7 +1060,7 @@ impl FormatVolumeOptions {
10411060
/// Default is `0xF8`.
10421061
#[must_use]
10431062
pub fn media(mut self, media: u8) -> Self {
1044-
self.media = Some(media);
1063+
self.media = media;
10451064
self
10461065
}
10471066

@@ -1050,7 +1069,7 @@ impl FormatVolumeOptions {
10501069
/// Default is `0x20`.
10511070
#[must_use]
10521071
pub fn sectors_per_track(mut self, sectors_per_track: u16) -> Self {
1053-
self.sectors_per_track = Some(sectors_per_track);
1072+
self.sectors_per_track = sectors_per_track;
10541073
self
10551074
}
10561075

@@ -1059,7 +1078,7 @@ impl FormatVolumeOptions {
10591078
/// Default is `0x40`.
10601079
#[must_use]
10611080
pub fn heads(mut self, heads: u16) -> Self {
1062-
self.heads = Some(heads);
1081+
self.heads = heads;
10631082
self
10641083
}
10651084

@@ -1077,7 +1096,7 @@ impl FormatVolumeOptions {
10771096
/// Default is `0x12345678`.
10781097
#[must_use]
10791098
pub fn volume_id(mut self, volume_id: u32) -> Self {
1080-
self.volume_id = Some(volume_id);
1099+
self.volume_id = volume_id;
10811100
self
10821101
}
10831102

@@ -1118,12 +1137,11 @@ pub fn format_volume<S: ReadWriteSeek>(storage: &mut S, options: FormatVolumeOpt
11181137
trace!("format_volume");
11191138
debug_assert!(storage.seek(SeekFrom::Current(0))? == 0);
11201139

1121-
let bytes_per_sector = options.bytes_per_sector.unwrap_or(512);
11221140
let total_sectors = if let Some(total_sectors) = options.total_sectors {
11231141
total_sectors
11241142
} else {
11251143
let total_bytes: u64 = storage.seek(SeekFrom::End(0))?;
1126-
let total_sectors_64 = total_bytes / u64::from(bytes_per_sector);
1144+
let total_sectors_64 = total_bytes / u64::from(options.bytes_per_sector);
11271145
storage.seek(SeekFrom::Start(0))?;
11281146
if total_sectors_64 > u64::from(u32::MAX) {
11291147
error!("Volume has too many sectors: {}", total_sectors_64);
@@ -1133,7 +1151,7 @@ pub fn format_volume<S: ReadWriteSeek>(storage: &mut S, options: FormatVolumeOpt
11331151
};
11341152

11351153
// Create boot sector, validate and write to storage device
1136-
let (boot, fat_type) = format_boot_sector(&options, total_sectors, bytes_per_sector)?;
1154+
let (boot, fat_type) = format_boot_sector(&options, total_sectors)?;
11371155
if boot.validate::<S::Error>().is_err() {
11381156
return Err(Error::InvalidInput);
11391157
}

0 commit comments

Comments
 (0)