@@ -60,6 +60,7 @@ type FileSystem struct {
60
60
size int64
61
61
start int64
62
62
file util.File
63
+ lazy bool
63
64
}
64
65
65
66
// Equal compare if two filesystems are equal
@@ -433,6 +434,10 @@ func (fs *FileSystem) writeBootSector() error {
433
434
}
434
435
435
436
func (fs * FileSystem ) writeFsis () error {
437
+ if fs .lazy {
438
+ return nil
439
+ }
440
+
436
441
fsInformationSector := fs .bootSector .biosParameterBlock .fsInformationSector
437
442
backupBootSector := fs .bootSector .biosParameterBlock .backupBootSector
438
443
fsisPrimary := int64 (fsInformationSector * uint16 (SectorSize512 ))
@@ -453,6 +458,10 @@ func (fs *FileSystem) writeFsis() error {
453
458
}
454
459
455
460
func (fs * FileSystem ) writeFat () error {
461
+ if fs .lazy {
462
+ return nil
463
+ }
464
+
456
465
reservedSectors := fs .bootSector .biosParameterBlock .dos331BPB .dos20BPB .reservedSectors
457
466
fatPrimaryStart := uint64 (reservedSectors ) * uint64 (SectorSize512 )
458
467
fatSecondaryStart := fatPrimaryStart + uint64 (fs .table .size )
@@ -688,6 +697,32 @@ func (fs *FileSystem) SetLabel(volumeLabel string) error {
688
697
return nil
689
698
}
690
699
700
+ // SetLazy sets the lazy flag for the filesystem. If lazy is true, then the filesystem will not write FAT tables and
701
+ // other metadata to the disk when creating/writing files or directories. After all changes to file system are done
702
+ // Commit() must be called to write the changes to the disk.
703
+ func (fs * FileSystem ) SetLazy (lazy bool ) {
704
+ fs .lazy = lazy
705
+ }
706
+
707
+ // Commit writes the FAT tables and other metadata to the disk. This is only necessary if lazy is set to true.
708
+ func (fs * FileSystem ) Commit () error {
709
+ curr := fs .lazy
710
+ fs .lazy = false
711
+ defer func () {
712
+ fs .lazy = curr
713
+ }()
714
+
715
+ if err := fs .writeFsis (); err != nil {
716
+ return fmt .Errorf ("failed to write the file system information sector: %w" , err )
717
+ }
718
+
719
+ if err := fs .writeFat (); err != nil {
720
+ return fmt .Errorf ("failed to write the file allocation table: %w" , err )
721
+ }
722
+
723
+ return nil
724
+ }
725
+
691
726
// read directory entries for a given cluster
692
727
func (fs * FileSystem ) getClusterList (firstCluster uint32 ) ([]uint32 , error ) {
693
728
// first, get the chain of clusters
0 commit comments