commit dfc32f0965c98d745b710f4df2a9d21c1a8e34d7
parent 14179ee95572b9b6c2122985bbfc38dec09701a8
Author: Virgil Dupras <hsoft@hardcoded.net>
Date: Sat, 13 Aug 2022 08:45:11 -0400
fs/fat: remove FATCursors and its structbind "cursors"
This logic moved into FATFile as "static" words. This clears up all global
singletons from this unit. Theoretically, this unit can support multiple volumes
at once!
Diffstat:
M | fs/fs/fatlo.fs | | | 63 | ++++++++++++++++++++++++++++++--------------------------------- |
1 file changed, 30 insertions(+), 33 deletions(-)
diff --git a/fs/fs/fatlo.fs b/fs/fs/fatlo.fs
@@ -44,6 +44,7 @@ extends Filesystem struct[ FAT
sfield bufsec \ sector number of current buf
sfield bufseccnt \ number of sectors ahead for sequential read
sfield bufcluster \ cluster number of current buf
+sfield lastcursor
' FAT structsz const SZ
SZ &+ :hdr(
@@ -197,7 +198,6 @@ extends File struct[ FATFile
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 )
dup :free? if 2drop 0 exit then ( n self )
@@ -208,44 +208,43 @@ extends File struct[ FATFile
r@ :bufpos r@ :)buf over - ( n a nmax )
rot min ( a n )
dup r> to+ pos ( a n ) ;
- current value :fatreadbuf'
: :fatclose ( self -- ) dup IO :flush 0 swap to flags ;
- current value :fatclose'
-]struct
-
-struct[ FATCursors
- sfield fat
- sfield last
- : :cursorsize fat FAT :ClusterSize FATFile SZ + ;
- : :findfreecursor ( self -- hdl ) >r
- r@ last begin ( ll )
- ?dup while dup CELLSZ + FATFile :free? if rdrop CELLSZ + exit then
- llnext repeat
- \ no existing free cursor, create a new one
- align4 to' r@ last llinsert r> :cursorsize allot ( newll ) CELLSZ + ( hdl )
- 0 over to FATFile flags ( mark as "free" ) ;
+ \ these words below are "static" words not called with "self" as an argument,
+ \ but "fat".
+ : :cursorsize ( fat -- sz ) FAT :ClusterSize SZ + ;
+
+ create _EmptyCursor
+ \ IO handle methods: :readbuf, :writebuf, :flush
+ ' :fatreadbuf , ' abort , ' drop ,
+ \ File handle methods :seek :close
+ ' :fatseek , ' :fatclose ,
+ \ FAT fields
+ 0 ( fat ) , 0 ( flags ) , 0 ( cluster ) , -1 ( clusteridx ) , 0 ( pos ) ,
+ 0 ( size ) , 0 ( entryoff ) ,
+
+ : :createcursor ( fat -- hdl )
+ align4 dup to' FAT lastcursor llinsert ( fat newll )
+ swap :cursorsize allot ( newll ) CELLSZ + ( hdl )
+ 0 over to flags ( mark as "free" ) ;
+
+ : :findfreecursor ( fat -- hdl ) >r
+ r@ FAT lastcursor begin ( ll )
+ ?dup while dup CELLSZ + :free? not while llnext repeat
+ CELLSZ + else r@ :createcursor then
+ _EmptyCursor over SZ move
+ r> over to fat ;
]struct
\ TODO: support more than one FAT FS at once
-create _FATCursors 0 ( fat ) , 0 ,
-_FATCursors structbind FATCursors cursors
-
-create _EmptyCursor
- \ IO handle methods: :readbuf, :writebuf, :flush
- FATFile :fatreadbuf' , ' abort , ' drop ,
- \ File handle methods :seek :close
- FATFile :fatseek' , FATFile :fatclose' ,
- \ FAT fields
- 0 ( fat ) , 1 ( flags ) , 0 ( cluster ) , -1 ( clusteridx ) , 0 ( pos ) ,
- 0 ( size ) , 0 ( entryoff ) ,
0 value _self
\ This is the "low" part. Complete open is finalized in fs/fat
: FATFS:open ( id self -- hdl ) to _self
- _self FAT :getdirentry _EmptyCursor cursors :findfreecursor dup >r
- ( dirent 'empty hdl ) FATFile SZ move ( dirent )
+ _self FAT :getdirentry
+ _self FATFile :findfreecursor >r ( dirent )
+ 1 to r@ FATFile flags \ mark as "used"
\ write the rest
dup _self FAT :buf( - _self FAT bufsec _self FAT secsz * + ( dirent doffset )
r@ to FATFile entryoff DirEntry filesize r@ to FATFile size ( ) r> ;
@@ -253,7 +252,7 @@ create _EmptyCursor
: mountFATvolume ( drv -- fs )
align4 here >r dup , ( drv R:fs )
FAT :child' , ['] FATFS:open , ['] abort , ( drv )
- 0 , 0 , 0 , ( drv )
+ 0 , 0 , 0 , 0 , ( drv )
\ At this point, "here" points to the FAT-header-to-be. Read the first sector
\ directly in "here": we'll have the header right here!
dup 0 here rot Drive :sec@ ( drv )
@@ -262,7 +261,5 @@ create _EmptyCursor
r@ FAT secsz swap Drive secsz over = not if
abort" Drive sector size not matching drive!" then ( secsz )
\ Allocate buffer. 1+ is for the extra byte for FAT12 cross-sector exception
- 1+ allot
- r@ to _FATCursors FATCursors fat
- r@ to _EmptyCursor FATFile fat r> ( fs ) ;
+ 1+ allot r> ( fs ) ;