Ignore free_cluster_count in FSInfo sector if volume has dirty flag

This commit is contained in:
Rafał Harabień 2018-06-07 22:54:35 +02:00
parent 1656540eac
commit ccb205c906

View File

@ -366,10 +366,10 @@ impl <'a> FileSystem<'a> {
/// Note: creating multiple filesystem objects with one underlying device/disk image can /// Note: creating multiple filesystem objects with one underlying device/disk image can
/// cause filesystem corruption. /// cause filesystem corruption.
pub fn new<T: ReadWriteSeek>(disk: &'a mut T, options: FsOptions) -> io::Result<Self> { pub fn new<T: ReadWriteSeek>(disk: &'a mut T, options: FsOptions) -> io::Result<Self> {
// Make sure given image is not seeked // make sure given image is not seeked
debug_assert!(disk.seek(SeekFrom::Current(0))? == 0); debug_assert!(disk.seek(SeekFrom::Current(0))? == 0);
// Read boot sector // read boot sector
let bpb = { let bpb = {
let boot = BootRecord::deserialize(disk)?; let boot = BootRecord::deserialize(disk)?;
if boot.boot_sig != [0x55, 0xAA] { if boot.boot_sig != [0x55, 0xAA] {
@ -392,13 +392,20 @@ impl <'a> FileSystem<'a> {
let total_clusters = data_sectors / bpb.sectors_per_cluster as u32; let total_clusters = data_sectors / bpb.sectors_per_cluster as u32;
let fat_type = FatType::from_clusters(total_clusters); let fat_type = FatType::from_clusters(total_clusters);
let fs_info = if fat_type == FatType::Fat32 { // read FSInfo sector if this is FAT32
let mut fs_info = if fat_type == FatType::Fat32 {
disk.seek(SeekFrom::Start(bpb.fs_info_sector as u64 * 512))?; disk.seek(SeekFrom::Start(bpb.fs_info_sector as u64 * 512))?;
FsInfoSector::deserialize(disk)? FsInfoSector::deserialize(disk)?
} else { } else {
FsInfoSector::default() FsInfoSector::default()
}; };
// if dirty flag is set completly ignore free_cluster_count in FSInfo
if bpb.status_flags().dirty {
fs_info.free_cluster_count = None;
}
// return FileSystem struct
Ok(FileSystem { Ok(FileSystem {
disk: RefCell::new(disk), disk: RefCell::new(disk),
options, options,