refactor: apply most of rustfmt changes

This commit is contained in:
Rafał Harabień 2018-12-09 00:32:41 +01:00
parent a3ab0098da
commit a88d751c47
5 changed files with 46 additions and 34 deletions

View File

@ -7,7 +7,7 @@ use byteorder::LittleEndian;
use byteorder_ext::{ReadBytesExt, WriteBytesExt}; use byteorder_ext::{ReadBytesExt, WriteBytesExt};
use dir_entry::DIR_ENTRY_SIZE; use dir_entry::DIR_ENTRY_SIZE;
use fs::{FatType, FsStatusFlags, FormatVolumeOptions}; use fs::{FatType, FormatVolumeOptions, FsStatusFlags};
use table::RESERVED_FAT_ENTRIES; use table::RESERVED_FAT_ENTRIES;
const KB: u64 = 1024; const KB: u64 = 1024;
@ -227,10 +227,7 @@ impl BiosParameterBlock {
} }
if self.total_sectors() <= self.first_data_sector() { if self.total_sectors() <= self.first_data_sector() {
return Err(Error::new( return Err(Error::new(ErrorKind::Other, "Invalid BPB (total_sectors field value is too small)"));
ErrorKind::Other,
"Invalid BPB (total_sectors field value is too small)",
));
} }
let total_clusters = self.total_clusters(); 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) 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, fn determine_sectors_per_fat(
sectors_per_cluster: u8, fat_type: FatType) -> u32 { 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) // 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 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; 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_sectors = options.total_sectors;
let total_bytes = total_sectors as u64 * bytes_per_sector as u64; 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 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)); .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; 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 // 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 // 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 { 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 // calculate File Allocation Table size
let sectors_per_fat = determine_sectors_per_fat(total_sectors, reserved_sectors, fats, root_dir_sectors, let sectors_per_fat = determine_sectors_per_fat(
sectors_per_cluster, fat_type); 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 // 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 // reserved_0 is always zero
let reserved_0 = [0u8; 12]; 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 // Check if number of clusters is proper for used FAT type
if FatType::from_clusters(bpb.total_clusters()) != 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)) Ok((bpb, fat_type))

View File

@ -11,7 +11,7 @@ use dir_entry::{DirEntry, DirEntryData, DirFileEntryData, DirLfnEntryData, FileA
#[cfg(feature = "alloc")] #[cfg(feature = "alloc")]
use dir_entry::{LFN_ENTRY_LAST_FLAG, LFN_PART_LEN}; use dir_entry::{LFN_ENTRY_LAST_FLAG, LFN_PART_LEN};
use file::File; use file::File;
use fs::{DiskSlice, FsIoAdapter, FileSystem, ReadWriteSeek}; use fs::{DiskSlice, FileSystem, FsIoAdapter, ReadWriteSeek};
#[cfg(feature = "alloc")] #[cfg(feature = "alloc")]
type LfnUtf16 = Vec<u16>; type LfnUtf16 = Vec<u16>;
@ -1002,12 +1002,8 @@ mod tests {
assert_eq!(&ShortNameGenerator::new("Foo.b").generate().unwrap(), b"FOO B "); 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.baR").generate().unwrap(), b"FOO BAR");
assert_eq!(&ShortNameGenerator::new("Foo+1.baR").generate().unwrap(), b"FOO_1~1 BAR"); assert_eq!(&ShortNameGenerator::new("Foo+1.baR").generate().unwrap(), b"FOO_1~1 BAR");
assert_eq!( assert_eq!(&ShortNameGenerator::new("ver +1.2.text").generate().unwrap(), b"VER_12~1TEX");
&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(".bashrc.swp").generate().unwrap(), b"BASHRC~1SWP"
);
} }
#[test] #[test]

View File

@ -12,10 +12,10 @@ use io::{Error, ErrorKind, SeekFrom};
use byteorder::LittleEndian; use byteorder::LittleEndian;
use byteorder_ext::{ReadBytesExt, WriteBytesExt}; 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 dir::{Dir, DirRawStream};
use file::File; 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}; use time::{TimeProvider, DEFAULT_TIME_PROVIDER};
// FAT implementation based on: // FAT implementation based on:
@ -453,9 +453,7 @@ impl<T: ReadWriteSeek> FileSystem<T> {
} }
fn fat_slice<'b>(&'b self) -> DiskSlice<FsIoAdapter<'b, T>> { fn fat_slice<'b>(&'b self) -> DiskSlice<FsIoAdapter<'b, T>> {
let io = FsIoAdapter { let io = FsIoAdapter { fs: self };
fs: self,
};
fat_slice(io, &self.bpb) 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 // 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> { impl<'a, T: ReadWriteSeek> Clone for FsIoAdapter<'a, T> {
fn clone(&self) -> Self { fn clone(&self) -> Self {
FsIoAdapter { FsIoAdapter { fs: self.fs }
fs: self.fs,
}
} }
} }
@ -948,8 +944,7 @@ pub fn format_volume<T: ReadWriteSeek>(mut disk: T, options: FormatVolumeOptions
}; };
assert!(root_dir_first_cluster == boot.bpb.root_dir_first_cluster); 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 first_data_sector = reserved_sectors + sectors_per_all_fats + root_dir_sectors;
let root_dir_first_sector = first_data_sector let root_dir_first_sector = first_data_sector + boot.bpb.sectors_from_clusters(root_dir_first_cluster - RESERVED_FAT_ENTRIES);
+ 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); let root_dir_pos = boot.bpb.bytes_from_sectors(root_dir_first_sector);
disk.seek(SeekFrom::Start(root_dir_pos))?; disk.seek(SeekFrom::Start(root_dir_pos))?;
write_zeros(&mut disk, boot.bpb.cluster_size() as u64)?; write_zeros(&mut disk, boot.bpb.cluster_size() as u64)?;

View File

@ -120,7 +120,13 @@ pub(crate) fn count_free_clusters<T: ReadSeek>(fat: &mut T, fat_type: FatType, t
} }
} }
pub(crate) fn format_fat<T: ReadWriteSeek>(fat: &mut T, fat_type: FatType, media: u8, bytes_per_fat: u64, total_clusters: u32) -> io::Result<()> { pub(crate) fn format_fat<T: ReadWriteSeek>(
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 // init first two reserved entries to FAT ID
match fat_type { match fat_type {
FatType::Fat12 => { FatType::Fat12 => {

View File

@ -39,7 +39,9 @@ fn basic_fs_test(fs: &FileSystem) {
let filenames = subdir2.iter().map(|r| r.unwrap().file_name()).collect::<Vec<String>>(); let filenames = subdir2.iter().map(|r| r.unwrap().file_name()).collect::<Vec<String>>();
assert_eq!(filenames, [".", "..", "test file name.txt"]); 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::<Vec<String>>(); let filenames = subdir2.iter().map(|r| r.unwrap().file_name()).collect::<Vec<String>>();
assert_eq!(filenames, [".", ".."]); assert_eq!(filenames, [".", ".."]);
@ -85,7 +87,6 @@ fn test_format_50mb() {
assert_eq!(fs.fat_type(), fatfs::FatType::Fat16); assert_eq!(fs.fat_type(), fatfs::FatType::Fat16);
} }
#[test] #[test]
fn test_format_512mb_512sec() { fn test_format_512mb_512sec() {
let total_bytes = 2 * 1024 * MB; let total_bytes = 2 * 1024 * MB;