@@ -60,6 +60,7 @@ type FileSystem struct {
6060 size int64
6161 start int64
6262 file util.File
63+ lazy bool
6364}
6465
6566// Equal compare if two filesystems are equal
@@ -433,6 +434,10 @@ func (fs *FileSystem) writeBootSector() error {
433434}
434435
435436func (fs * FileSystem ) writeFsis () error {
437+ if fs .lazy {
438+ return nil
439+ }
440+
436441 fsInformationSector := fs .bootSector .biosParameterBlock .fsInformationSector
437442 backupBootSector := fs .bootSector .biosParameterBlock .backupBootSector
438443 fsisPrimary := int64 (fsInformationSector * uint16 (SectorSize512 ))
@@ -453,6 +458,10 @@ func (fs *FileSystem) writeFsis() error {
453458}
454459
455460func (fs * FileSystem ) writeFat () error {
461+ if fs .lazy {
462+ return nil
463+ }
464+
456465 reservedSectors := fs .bootSector .biosParameterBlock .dos331BPB .dos20BPB .reservedSectors
457466 fatPrimaryStart := uint64 (reservedSectors ) * uint64 (SectorSize512 )
458467 fatSecondaryStart := fatPrimaryStart + uint64 (fs .table .size )
@@ -688,6 +697,32 @@ func (fs *FileSystem) SetLabel(volumeLabel string) error {
688697 return nil
689698}
690699
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+
691726// read directory entries for a given cluster
692727func (fs * FileSystem ) getClusterList (firstCluster uint32 ) ([]uint32 , error ) {
693728 // first, get the chain of clusters
0 commit comments