Skip to content

Commit 4971e1c

Browse files
committed
Clear FAT dirty flag before creating FileSystem.
1 parent 34c59d1 commit 4971e1c

File tree

1 file changed

+45
-45
lines changed

1 file changed

+45
-45
lines changed

src/fs.rs

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -424,31 +424,30 @@ impl<IO: Read + Write + Seek, TP, OCC> FileSystem<IO, TP, OCC> {
424424
// Validate the numbers stored in the free_cluster_count and next_free_cluster are within bounds for volume
425425
fs_info.validate_and_fix(total_clusters);
426426

427-
let mut fs = Self {
428-
disk: RefCell::new(disk),
429-
options,
430-
fat_type,
431-
bpb,
432-
first_data_sector,
433-
root_dir_sectors,
434-
total_clusters,
435-
fs_info: RefCell::new(fs_info),
436-
current_status_flags: Cell::new(bpb_status_flags),
437-
};
438427

439-
let mut fat_status_flags = fs.read_fat_status_flags()?;
428+
let mut fat_status_flags = read_fat_flags(&mut fat_slice::<&mut IO, IO::Error, IO>(&mut disk, &bpb), fat_type)?;
440429
if fat_status_flags.dirty() {
441-
if fs.options.ignore_dirty_flag {
430+
if options.ignore_dirty_flag {
442431
warn!("FAT is dirty, clearing dirty flag.");
443432
fat_status_flags.dirty = false;
444-
fs.set_fat_status_flags(fat_status_flags)?;
433+
write_fat_flags(&mut fat_slice::<&mut IO, IO::Error, IO>(&mut disk, &bpb), fat_type, fat_status_flags)?;
445434
} else {
446435
return Err(Error::DirtyFileSystem);
447436
}
448437
}
449438

450439
trace!("FileSystem::new end");
451-
Ok(fs)
440+
Ok(Self {
441+
disk: RefCell::new(disk),
442+
options,
443+
fat_type,
444+
bpb,
445+
first_data_sector,
446+
root_dir_sectors,
447+
total_clusters,
448+
fs_info: RefCell::new(fs_info),
449+
current_status_flags: Cell::new(bpb_status_flags),
450+
})
452451
}
453452

454453
/// Returns a type of File Allocation Table (FAT) used by this filesystem.
@@ -499,15 +498,16 @@ impl<IO: Read + Write + Seek, TP, OCC> FileSystem<IO, TP, OCC> {
499498
self.bpb.clusters_from_bytes(bytes)
500499
}
501500

502-
fn fat_slice(&self) -> FatSlice<'_, IO, TP, OCC, IO::Error> {
503-
let io = FsIoAdapter { fs: self };
501+
fn fat_slice(&self) -> FatSlice<'_, IO, IO::Error> {
502+
let io = FsIoAdapter {
503+
disk: &self.disk,
504+
fat_type: self.fat_type(),
505+
current_status_flags: &self.current_status_flags,
506+
};
504507
fat_slice(io, &self.bpb)
505508
}
506509

507-
pub(crate) fn cluster_iter(
508-
&self,
509-
cluster: u32,
510-
) -> ClusterIterator<FatSlice<'_, IO, TP, OCC, IO::Error>, IO::Error> {
510+
pub(crate) fn cluster_iter(&self, cluster: u32) -> ClusterIterator<FatSlice<'_, IO, IO::Error>, IO::Error> {
511511
let disk_slice = self.fat_slice();
512512
ClusterIterator::new(disk_slice, self.fat_type, cluster)
513513
}
@@ -553,7 +553,7 @@ impl<IO: Read + Write + Seek, TP, OCC> FileSystem<IO, TP, OCC> {
553553
/// `Error::Io` will be returned if the underlying storage object returned an I/O error.
554554
pub fn read_status_flags(&self) -> Result<FsStatusFlags, Error<IO::Error>> {
555555
let bpb_status = self.current_status_flags.get();
556-
let fat_status = self.read_fat_status_flags()?;
556+
let fat_status = read_fat_flags(&mut self.fat_slice(), self.fat_type)?;
557557
Ok(FsStatusFlags {
558558
dirty: bpb_status.dirty || fat_status.dirty,
559559
io_error: bpb_status.io_error || fat_status.io_error,
@@ -619,31 +619,10 @@ impl<IO: Read + Write + Seek, TP, OCC> FileSystem<IO, TP, OCC> {
619619
Ok(())
620620
}
621621

622+
#[inline]
622623
pub(crate) fn set_dirty_flag(&self, dirty: bool) -> Result<(), Error<IO::Error>> {
623-
let mut status_flags = self.current_status_flags.get();
624-
625-
if status_flags.dirty == dirty {
626-
// Dirty flag did not change.
627-
return Ok(());
628-
}
629-
630-
status_flags.dirty = dirty;
631-
632624
let mut disk = self.disk.borrow_mut();
633-
write_bpb_status_flags(&mut *disk, self.fat_type(), status_flags)?;
634-
self.current_status_flags.set(status_flags);
635-
636-
Ok(())
637-
}
638-
639-
#[inline]
640-
fn read_fat_status_flags(&self) -> Result<FsStatusFlags, Error<IO::Error>> {
641-
read_fat_flags(&mut self.fat_slice(), self.fat_type)
642-
}
643-
644-
#[inline]
645-
fn set_fat_status_flags(&self, status_flags: FsStatusFlags) -> Result<(), Error<IO::Error>> {
646-
write_fat_flags(&mut self.fat_slice(), self.fat_type, status_flags)
625+
set_dirty_flag(&mut *disk, self.fat_type(), &self.current_status_flags, dirty)
647626
}
648627

649628
/// Returns a root directory object allowing for futher penetration of a filesystem structure.
@@ -773,6 +752,27 @@ impl<IO: ReadWriteSeek, TP, OCC> Clone for FsIoAdapter<'_, IO, TP, OCC> {
773752
}
774753
}
775754

755+
fn set_dirty_flag<IO: Seek + Write>(
756+
disk: &mut IO,
757+
fat_type: FatType,
758+
current_status_flags: &Cell<FsStatusFlags>,
759+
dirty: bool,
760+
) -> Result<(), Error<IO::Error>> {
761+
let mut status_flags = current_status_flags.get();
762+
763+
if status_flags.dirty == dirty {
764+
// Dirty flag did not change.
765+
return Ok(());
766+
}
767+
768+
status_flags.dirty = dirty;
769+
770+
write_bpb_status_flags(disk, fat_type, status_flags)?;
771+
current_status_flags.set(status_flags);
772+
773+
Ok(())
774+
}
775+
776776
fn write_bpb_status_flags<IO: Seek + Write>(
777777
disk: &mut IO,
778778
fat_type: FatType,

0 commit comments

Comments
 (0)