commit a64a07b066c3b3c8fa8b6b1464be29623067c5c9
parent d064f7275de8516f9b0e588e1d98759138dd43d2
Author: Virgil Dupras <hsoft@hardcoded.net>
Date: Fri, 12 Aug 2022 20:57:10 -0400
fs/fat: untangle some more
Diffstat:
M | fs/fs/fatlo.fs | | | 69 | ++++++++++++++++++++++++++++++++++++--------------------------------- |
1 file changed, 36 insertions(+), 33 deletions(-)
diff --git a/fs/fs/fatlo.fs b/fs/fs/fatlo.fs
@@ -158,26 +158,9 @@ current value :child'
]struct
-$1000 const FATMAXSECSZ
-
-\ TODO: support more than one FAT FS at once
-align4
-create _FATFS
- 0 , FAT :child' , 0 , ' abort , 0 , 0 , 0 ,
- FAT HDRSZ allot
- \ Allocate buffer
- FATMAXSECSZ 1+ allot
- \ 1+ is for the extra byte for FAT12 cross-sector exception
-_FATFS structbind FAT FATFS
-
-: readFATvolume ( drv -- ) dup >r _FATFS !
- 0 FATFS :buf( r@ Drive :sec@
- FATFS :buf( FATFS :hdr( FAT HDRSZ move
- FATFS secsz r> Drive secsz = not if
- abort" Drive sector size not matching drive!" then ;
-
\ File cursor
extends File struct[ FATFile
+ sfield fat
\ all zeroes = free cursor
\ b0 = used
\ b1 = buffer is dirty
@@ -192,26 +175,28 @@ extends File struct[ FATFile
' FATFile structsz const SZ
\ beginning of a buffer with the size :ClusterSize
SZ &+ :buf(
- : :)buf ( self -- a ) :buf( FATFS :ClusterSize + ;
+ : _clustersize ( self -- n ) fat FAT :ClusterSize ;
+ : :)buf ( self -- a ) dup :buf( swap _clustersize + ;
: :free? ( self -- f ) flags not ;
: :dirty? ( self -- f ) flags 2 and ;
- : :bufpos ( self -- a ) dup pos FATFS :ClusterSize mod swap :buf( + ;
- : :dirent ( self -- dirent ) entryoff FATFS :getdirentry ;
+ : :bufpos ( self -- a ) dup pos over _clustersize mod swap :buf( + ;
+ : :dirent ( self -- dirent ) dup entryoff swap fat FAT :getdirentry ;
: :cluster0 ( self -- cl ) :dirent DirEntry cluster ;
+ 0 value self
\ set self to pos. If new pos crosses cluster boundaries compared to current
\ pos, flush current buffer and read a new sector from disk.
- : :fatseek ( pos self -- )
- dup :free? if 2drop exit then ( pos self )
- over 0< if abort" can't seek to negative pos" then
- over FATFS :ClusterSize / over clusteridx = not if
- dup IO :flush >r ( pos )
- dup FATFS :ClusterSize / dup r@ to clusteridx ( pos idx )
- r@ :cluster0 ( pos idx cl )
- swap ?dup if >r begin ( pos cl ) FATFS :FAT@ next then ( pos cl )
- dup r@ :buf( FATFS :readcluster ( pos cl )
- r@ to cluster r> ( pos fc )
- then ( pos self ) to pos ;
+ : :fatseek ( pos self -- ) to self
+ self :free? if drop exit then ( pos )
+ dup 0< if abort" can't seek to negative pos" then
+ dup self _clustersize / self clusteridx = not if
+ self IO :flush ( pos )
+ dup self _clustersize / dup self to clusteridx ( pos idx )
+ self :cluster0 ( pos idx cl )
+ swap ?dup if >r begin ( pos cl ) self fat FAT :FAT@ next then ( pos cl )
+ dup self :buf( self fat FAT :readcluster ( pos cl )
+ self to cluster ( pos )
+ then ( pos ) self to pos ;
current value :fatseek'
: :fatreadbuf ( n self -- a? n )
@@ -243,6 +228,24 @@ struct[ FATCursors
0 over to FATFile flags ( mark as "free" ) ;
]struct
+$1000 const FATMAXSECSZ
+
+\ TODO: support more than one FAT FS at once
+align4
+create _FATFS
+ 0 , FAT :child' , 0 , ' abort , 0 , 0 , 0 ,
+ FAT HDRSZ allot
+ \ Allocate buffer
+ FATMAXSECSZ 1+ allot
+ \ 1+ is for the extra byte for FAT12 cross-sector exception
+_FATFS structbind FAT FATFS
+
+: readFATvolume ( drv -- ) dup >r _FATFS !
+ 0 FATFS :buf( r@ Drive :sec@
+ FATFS :buf( FATFS :hdr( FAT HDRSZ move
+ FATFS secsz r> Drive secsz = not if
+ abort" Drive sector size not matching drive!" then ;
+
create _FATCursors _FATFS , 0 ,
_FATCursors structbind FATCursors cursors
@@ -252,7 +255,7 @@ create _EmptyCursor
\ File handle methods :seek :close
FATFile :fatseek' , FATFile :fatclose' ,
\ FAT fields
- 1 ( flags ) , 0 ( cluster ) , -1 ( clusteridx ) , 0 ( pos ) ,
+ _FATFS , 1 ( flags ) , 0 ( cluster ) , -1 ( clusteridx ) , 0 ( pos ) ,
0 ( size ) , 0 ( entryoff ) ,
\ This is the "low" part. Complete open is finalized in fs/fat