Skip to content

Commit 95a1b05

Browse files
committed
Clear FAT dirty flag before creating FileSystem.
1 parent 0dd360a commit 95a1b05

File tree

1 file changed

+42
-40
lines changed

1 file changed

+42
-40
lines changed

src/fs.rs

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -424,31 +424,33 @@ 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-
};
438-
439-
let mut fat_status_flags = fs.read_fat_status_flags()?;
427+
let mut fat_status_flags = read_fat_flags(&mut fat_slice::<&mut IO, IO::Error, IO>(&mut disk, &bpb), fat_type)?;
440428
if fat_status_flags.dirty() {
441-
if fs.options.ignore_dirty_flag {
429+
if options.ignore_dirty_flag {
442430
warn!("FAT is dirty, clearing dirty flag.");
443431
fat_status_flags.dirty = false;
444-
fs.set_fat_status_flags(fat_status_flags)?;
432+
write_fat_flags(
433+
&mut fat_slice::<&mut IO, IO::Error, IO>(&mut disk, &bpb),
434+
fat_type,
435+
fat_status_flags,
436+
)?;
445437
} else {
446438
return Err(Error::DirtyFileSystem);
447439
}
448440
}
449441

450442
trace!("FileSystem::new end");
451-
Ok(fs)
443+
Ok(Self {
444+
disk: RefCell::new(disk),
445+
options,
446+
fat_type,
447+
bpb,
448+
first_data_sector,
449+
root_dir_sectors,
450+
total_clusters,
451+
fs_info: RefCell::new(fs_info),
452+
current_status_flags: Cell::new(bpb_status_flags),
453+
})
452454
}
453455

454456
/// Returns a type of File Allocation Table (FAT) used by this filesystem.
@@ -553,7 +555,7 @@ impl<IO: Read + Write + Seek, TP, OCC> FileSystem<IO, TP, OCC> {
553555
/// `Error::Io` will be returned if the underlying storage object returned an I/O error.
554556
pub fn read_status_flags(&self) -> Result<FsStatusFlags, Error<IO::Error>> {
555557
let bpb_status = self.current_status_flags.get();
556-
let fat_status = self.read_fat_status_flags()?;
558+
let fat_status = read_fat_flags(&mut self.fat_slice(), self.fat_type)?;
557559
Ok(FsStatusFlags {
558560
dirty: bpb_status.dirty || fat_status.dirty,
559561
io_error: bpb_status.io_error || fat_status.io_error,
@@ -619,31 +621,10 @@ impl<IO: Read + Write + Seek, TP, OCC> FileSystem<IO, TP, OCC> {
619621
Ok(())
620622
}
621623

624+
#[inline]
622625
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-
632626
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)
627+
set_dirty_flag(&mut *disk, self.fat_type(), &self.current_status_flags, dirty)
647628
}
648629

649630
/// Returns a root directory object allowing for futher penetration of a filesystem structure.
@@ -773,6 +754,27 @@ impl<IO: ReadWriteSeek, TP, OCC> Clone for FsIoAdapter<'_, IO, TP, OCC> {
773754
}
774755
}
775756

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

0 commit comments

Comments
 (0)