diff --git a/src/boot_sector.rs b/src/boot_sector.rs index c85e61c..5e57887 100644 --- a/src/boot_sector.rs +++ b/src/boot_sector.rs @@ -7,7 +7,7 @@ use byteorder::LittleEndian; use byteorder_ext::{ReadBytesExt, WriteBytesExt}; use dir_entry::DIR_ENTRY_SIZE; -use fs::{FatType, FsStatusFlags, FormatVolumeOptions}; +use fs::{FatType, FormatVolumeOptions, FsStatusFlags}; use table::RESERVED_FAT_ENTRIES; const KB: u64 = 1024; @@ -227,10 +227,7 @@ impl BiosParameterBlock { } if self.total_sectors() <= self.first_data_sector() { - return Err(Error::new( - ErrorKind::Other, - "Invalid BPB (total_sectors field value is too small)", - )); + return Err(Error::new(ErrorKind::Other, "Invalid BPB (total_sectors field value is too small)")); } let total_clusters = self.total_clusters(); @@ -444,9 +441,14 @@ fn determine_bytes_per_cluster(total_bytes: u64, fat_type: FatType, bytes_per_se cmp::min(cmp::max(bytes_per_cluster, bytes_per_sector as u32), MAX_CLUSTER_SIZE) } -fn determine_sectors_per_fat(total_sectors: u32, reserved_sectors: u16, fats: u8, root_dir_sectors: u32, - sectors_per_cluster: u8, fat_type: FatType) -> u32 { - +fn determine_sectors_per_fat( + total_sectors: u32, + reserved_sectors: u16, + fats: u8, + root_dir_sectors: u32, + sectors_per_cluster: u8, + fat_type: FatType, +) -> u32 { // TODO: check if this calculation is always correct (especially for FAT12) let tmp_val1 = total_sectors - (reserved_sectors as u32 + root_dir_sectors as u32); let mut tmp_val2 = (256 * sectors_per_cluster as u32) + fats as u32; @@ -464,7 +466,8 @@ fn format_bpb(options: &FormatVolumeOptions) -> io::Result<(BiosParameterBlock, let total_sectors = options.total_sectors; let total_bytes = total_sectors as u64 * bytes_per_sector as u64; let fat_type = options.fat_type.unwrap_or_else(|| determine_fat_type(total_bytes)); - let bytes_per_cluster = options.bytes_per_cluster + let bytes_per_cluster = options + .bytes_per_cluster .unwrap_or_else(|| determine_bytes_per_cluster(total_bytes, fat_type, bytes_per_sector)); let sectors_per_cluster = (bytes_per_cluster / bytes_per_sector as u32) as u8; @@ -481,15 +484,23 @@ fn format_bpb(options: &FormatVolumeOptions) -> io::Result<(BiosParameterBlock, // Check if volume has enough space to accomodate reserved sectors, FAT, root directory and some data space // Having less than 8 sectors for FAT and data would make a little sense if total_sectors <= reserved_sectors as u32 + root_dir_sectors as u32 + 8 { - return Err(Error::new(ErrorKind::Other, "Volume is too small",)); + return Err(Error::new(ErrorKind::Other, "Volume is too small")); } // calculate File Allocation Table size - let sectors_per_fat = determine_sectors_per_fat(total_sectors, reserved_sectors, fats, root_dir_sectors, - sectors_per_cluster, fat_type); + let sectors_per_fat = determine_sectors_per_fat( + total_sectors, + reserved_sectors, + fats, + root_dir_sectors, + sectors_per_cluster, + fat_type, + ); // drive_num should be 0 for floppy disks and 0x80 for hard disks - determine it using FAT type - let drive_num = options.drive_num.unwrap_or_else(|| if fat_type == FatType::Fat12 { 0 } else { 0x80 }); + let drive_num = options + .drive_num + .unwrap_or_else(|| if fat_type == FatType::Fat12 { 0 } else { 0x80 }); // reserved_0 is always zero let reserved_0 = [0u8; 12]; @@ -544,7 +555,10 @@ fn format_bpb(options: &FormatVolumeOptions) -> io::Result<(BiosParameterBlock, // Check if number of clusters is proper for used FAT type if FatType::from_clusters(bpb.total_clusters()) != fat_type { - return Err(Error::new(ErrorKind::Other, "Total number of clusters and FAT type does not match. Try other volume size")); + return Err(Error::new( + ErrorKind::Other, + "Total number of clusters and FAT type does not match. Try other volume size", + )); } Ok((bpb, fat_type)) diff --git a/src/dir.rs b/src/dir.rs index 5e2cc73..1c1a027 100644 --- a/src/dir.rs +++ b/src/dir.rs @@ -11,7 +11,7 @@ use dir_entry::{DirEntry, DirEntryData, DirFileEntryData, DirLfnEntryData, FileA #[cfg(feature = "alloc")] use dir_entry::{LFN_ENTRY_LAST_FLAG, LFN_PART_LEN}; use file::File; -use fs::{DiskSlice, FsIoAdapter, FileSystem, ReadWriteSeek}; +use fs::{DiskSlice, FileSystem, FsIoAdapter, ReadWriteSeek}; #[cfg(feature = "alloc")] type LfnUtf16 = Vec; @@ -1002,12 +1002,8 @@ mod tests { assert_eq!(&ShortNameGenerator::new("Foo.b").generate().unwrap(), b"FOO B "); assert_eq!(&ShortNameGenerator::new("Foo.baR").generate().unwrap(), b"FOO BAR"); assert_eq!(&ShortNameGenerator::new("Foo+1.baR").generate().unwrap(), b"FOO_1~1 BAR"); - assert_eq!( - &ShortNameGenerator::new("ver +1.2.text").generate().unwrap(), b"VER_12~1TEX" - ); - assert_eq!( - &ShortNameGenerator::new(".bashrc.swp").generate().unwrap(), b"BASHRC~1SWP" - ); + assert_eq!(&ShortNameGenerator::new("ver +1.2.text").generate().unwrap(), b"VER_12~1TEX"); + assert_eq!(&ShortNameGenerator::new(".bashrc.swp").generate().unwrap(), b"BASHRC~1SWP"); } #[test] diff --git a/src/fs.rs b/src/fs.rs index 9639a85..5b1006d 100644 --- a/src/fs.rs +++ b/src/fs.rs @@ -12,10 +12,10 @@ use io::{Error, ErrorKind, SeekFrom}; use byteorder::LittleEndian; use byteorder_ext::{ReadBytesExt, WriteBytesExt}; -use boot_sector::{BootSector, BiosParameterBlock, format_boot_sector}; +use boot_sector::{format_boot_sector, BiosParameterBlock, BootSector}; use dir::{Dir, DirRawStream}; use file::File; -use table::{alloc_cluster, count_free_clusters, read_fat_flags, format_fat, ClusterIterator, RESERVED_FAT_ENTRIES}; +use table::{alloc_cluster, count_free_clusters, format_fat, read_fat_flags, ClusterIterator, RESERVED_FAT_ENTRIES}; use time::{TimeProvider, DEFAULT_TIME_PROVIDER}; // FAT implementation based on: @@ -453,9 +453,7 @@ impl FileSystem { } fn fat_slice<'b>(&'b self) -> DiskSlice> { - let io = FsIoAdapter { - fs: self, - }; + let io = FsIoAdapter { fs: self }; fat_slice(io, &self.bpb) } @@ -621,9 +619,7 @@ impl<'a, T: ReadWriteSeek> Seek for FsIoAdapter<'a, T> { // Note: derive cannot be used because of invalid bounds. See: https://github.com/rust-lang/rust/issues/26925 impl<'a, T: ReadWriteSeek> Clone for FsIoAdapter<'a, T> { fn clone(&self) -> Self { - FsIoAdapter { - fs: self.fs, - } + FsIoAdapter { fs: self.fs } } } @@ -948,8 +944,7 @@ pub fn format_volume(mut disk: T, options: FormatVolumeOptions }; assert!(root_dir_first_cluster == boot.bpb.root_dir_first_cluster); let first_data_sector = reserved_sectors + sectors_per_all_fats + root_dir_sectors; - let root_dir_first_sector = first_data_sector - + boot.bpb.sectors_from_clusters(root_dir_first_cluster - RESERVED_FAT_ENTRIES); + let root_dir_first_sector = first_data_sector + boot.bpb.sectors_from_clusters(root_dir_first_cluster - RESERVED_FAT_ENTRIES); let root_dir_pos = boot.bpb.bytes_from_sectors(root_dir_first_sector); disk.seek(SeekFrom::Start(root_dir_pos))?; write_zeros(&mut disk, boot.bpb.cluster_size() as u64)?; diff --git a/src/table.rs b/src/table.rs index bf4ac71..70a9bb7 100644 --- a/src/table.rs +++ b/src/table.rs @@ -120,7 +120,13 @@ pub(crate) fn count_free_clusters(fat: &mut T, fat_type: FatType, t } } -pub(crate) fn format_fat(fat: &mut T, fat_type: FatType, media: u8, bytes_per_fat: u64, total_clusters: u32) -> io::Result<()> { +pub(crate) fn format_fat( + fat: &mut T, + fat_type: FatType, + media: u8, + bytes_per_fat: u64, + total_clusters: u32, +) -> io::Result<()> { // init first two reserved entries to FAT ID match fat_type { FatType::Fat12 => { diff --git a/tests/format.rs b/tests/format.rs index b3f2090..296cd47 100644 --- a/tests/format.rs +++ b/tests/format.rs @@ -39,7 +39,9 @@ fn basic_fs_test(fs: &FileSystem) { let filenames = subdir2.iter().map(|r| r.unwrap().file_name()).collect::>(); assert_eq!(filenames, [".", "..", "test file name.txt"]); - subdir1.rename("subdir2 with long name/test file name.txt", &root_dir, "new-name.txt").expect("rename"); + subdir1 + .rename("subdir2 with long name/test file name.txt", &root_dir, "new-name.txt") + .expect("rename"); let filenames = subdir2.iter().map(|r| r.unwrap().file_name()).collect::>(); assert_eq!(filenames, [".", ".."]); @@ -85,7 +87,6 @@ fn test_format_50mb() { assert_eq!(fs.fat_type(), fatfs::FatType::Fat16); } - #[test] fn test_format_512mb_512sec() { let total_bytes = 2 * 1024 * MB;