commit adbc5a7ffb4945caa89111eda7d8403a3e7c0c60
parent e51a3f4d1aa382bc5075fba8538008f4d32fc056
Author: Virgil Dupras <hsoft@hardcoded.net>
Date: Tue, 9 Aug 2022 20:02:16 -0400
Create the Filesystem struct
Diffstat:
11 files changed, 54 insertions(+), 61 deletions(-)
diff --git a/buildpc.fs b/buildpc.fs
@@ -1,7 +1,7 @@
?f<< /xcomp/tools.fs
: spit ( a u -- ) >r begin c@+ stderr next ;
: spitfile<<
- word findpath# fopen begin ( fc )
+ word findpath# activefs :open begin ( fc )
begin dup getc keepc? until
dup 0>= while stderr repeat ( fc c ) drop fclose ;
1 to fecho
diff --git a/fs/cc/cc.fs b/fs/cc/cc.fs
@@ -4,7 +4,7 @@ require sys/xhere.fs
1 value _debug
S" /cc/vm" findpath# ( hdl )
syspad :[ ARCH c@+ dup 3 + c, move, ," .fs" syspad :] ( hdl fname )
-fchild dup bool const HASCC ( hdl )
+activefs :child dup bool const HASCC ( hdl )
HASCC not [if] drop ." Unsupported arch for CC" nl> \s [then]
( hdl ) fload
?f<< /cc/gen.fs
diff --git a/fs/fs/fatlo.fs b/fs/fs/fatlo.fs
@@ -37,43 +37,38 @@ fatdrv value bufdrv \ drive from which this sector was read
\ seqential read.
: readsector ( sec cnt -- ) to bufseccnt dup to bufsec fatbuf( bufdrv drv@ ;
+7 struct _ secsz secpercluster reservedseccnt FATcnt rootentcnt seccnt FATsz
+\ secsz in bytes
+\ reservedseccnt number of sectors reserved before FAT starts
+\ FATcnt >1 means backup FATs
+\ rootentcnt count of 32b entries
+\ FATsz in bytes
+instance _ FATHeader 7 CELLSZ * allot
+
\ TODO: support more than one FAT FS at once
-create FATFS
- \ Filesystem prelude
- 0 , 0 , 0 ,
- ' abort ,
- \ FAT-specific fields/method
- 7 CELLSZ * allot
-
-$10 field secsz \ in bytes
-$14 field secpercluster
-$18 field reservedseccnt \ number of sectors reserved before FAT starts
-$1c field FATcnt \ >1 means backup FATs
-$20 field rootentcnt \ count of 32b entries
-$24 field seccnt
-$28 field FATsz \ in bytes
-
-: _sec@ ( sec dst fs -- ) fsdrv drv@ ;
-: readFATvolume ( drv -- )
- FATFS ! 0 fatbuf( FATFS _sec@
- fatbuf( $0b + w@ to FATFS secsz
- fatbuf( $0d + c@ to FATFS secpercluster
- fatbuf( $0e + w@ to FATFS reservedseccnt
- fatbuf( $10 + c@ to FATFS FATcnt
- fatbuf( $11 + w@ to FATFS rootentcnt
- fatbuf( $13 + w@ to FATFS seccnt
- fatbuf( $16 + w@ to FATFS FATsz
- FATFS secsz FATFS fsdrv drvsecsz = not if
+instance Filesystem FATFS
+ fatdrv , 0 , 0 , ' abort , ' FATHeader ,
+
+: readFATvolume ( -- )
+ 0 fatbuf( fatdrv drv@
+ fatbuf( $0b + w@ to FATHeader secsz
+ fatbuf( $0d + c@ to FATHeader secpercluster
+ fatbuf( $0e + w@ to FATHeader reservedseccnt
+ fatbuf( $10 + c@ to FATHeader FATcnt
+ fatbuf( $11 + w@ to FATHeader rootentcnt
+ fatbuf( $13 + w@ to FATHeader seccnt
+ fatbuf( $16 + w@ to FATHeader FATsz
+ FATHeader secsz fatdrv drvsecsz = not if
abort" Drive sector size not matching drive!" then ;
-fatdrv readFATvolume
-
-: BPB_BytsPerSec FATFS secsz ;
-: BPB_SecPerClus FATFS secpercluster ;
-: BPB_RsvdSecCnt FATFS reservedseccnt ;
-: BPB_NumFATs FATFS FATcnt ;
-: BPB_RootEntCnt FATFS rootentcnt ;
-: BPB_TotSec16 FATFS seccnt ;
-: BPB_FATSz16 FATFS FATsz ;
+readFATvolume
+
+: BPB_BytsPerSec FATHeader secsz ;
+: BPB_SecPerClus FATHeader secpercluster ;
+: BPB_RsvdSecCnt FATHeader reservedseccnt ;
+: BPB_NumFATs FATHeader FATcnt ;
+: BPB_RootEntCnt FATHeader rootentcnt ;
+: BPB_TotSec16 FATHeader seccnt ;
+: BPB_FATSz16 FATHeader FATsz ;
: RootDirSectors
BPB_RootEntCnt 32 * BPB_BytsPerSec /mod ( r q ) swap if 1+ then ;
: FirstDataSector BPB_RsvdSecCnt BPB_NumFATs BPB_FATSz16 * + RootDirSectors + ;
@@ -172,7 +167,7 @@ here const )fnbuf
: fatchild ( dirid name -- id-or-0 )
fnbuf! getdirentry readdir findindir dup if getid then ;
-current FATFS 4 + !
+current to FATFS :child
\ File cursor
\ 12b IO handle prelude
@@ -258,4 +253,4 @@ create fcursors( FCursorSize FCURSORCNT * allot0
dup fatbuf( - bufsec BPB_BytsPerSec * + ( dirent doffset ) r@ 32 + !
-1 r@ FCUR_clusteridx! 0 r@ FCUR_pos!
DIR_FileSize r@ FCUR_size! ( ) r> ;
-current FATFS 8 + !
+current to FATFS :open
diff --git a/fs/sys/file.fs b/fs/sys/file.fs
@@ -1,9 +1,9 @@
\ File subsystem, see doc/file
-0 S" lib" fchild S" io.fs" fchild fload
+0 S" lib" activefs :child S" io.fs" activefs :child fload
26 const MAXFSCNT
create filesystems MAXFSCNT CELLSZ * allot0
-activefs filesystems ! \ record our boot FS in the list
+to@ activefs filesystems ! \ record our boot FS in the list
0 value curdir
create _buf $100 allot
@@ -13,7 +13,7 @@ create _buf $100 allot
over c@ '/' = if 1- >r 1+ 0 ( root ) else >r curdir then swap
begin ( dirid a )
c@+ dup '/' = if ( dirid a c )
- drop swap _buf fchild ( a dirid )
+ drop swap _buf activefs :child ( a dirid )
?dup not if drop 0 rdrop r>A exit then swap ( dirid a )
0 _buf c!+ >A
else ( dirid a c )
@@ -21,7 +21,7 @@ create _buf $100 allot
next ( dirid a ) drop _buf r>A ;
: findpath ( path -- id-or-0 )
- findpathdir ?dup if fchild else 0 then ;
+ findpathdir ?dup if activefs :child else 0 then ;
: findpath# ( path -- id ) findpath ?dup not if abort" path not found" then ;
@@ -50,6 +50,6 @@ instance Scratchpad filespad $200 scratchpad$
: require word dup findpath# floaded? not if
stype abort" required" else drop then ;
: with-stdin-file ( w str -- )
- to@ stdin >r findpath# fopen dup >r ( w fcursor )
+ to@ stdin >r findpath# activefs :open dup >r ( w fcursor )
[f<] to stdin execute
r> fclose r> to stdin ;
diff --git a/fs/tests/cc/ast.fs b/fs/tests/cc/ast.fs
@@ -2,7 +2,7 @@
?f<< cc/cc.fs
testbegin
\ Tests for the C compiler AST
-S" tests/cc/test.c" findpath# fopen dup [f<] to stdin parseast fclose
+S" tests/cc/test.c" findpath# activefs :open dup [f<] to stdin parseast fclose
curunit firstchild dup nodeid AST_FUNCTION #eq ( fnode )
dup ast.func.name S" retconst" s= #
diff --git a/fs/xcomp/boothi.fs b/fs/xcomp/boothi.fs
@@ -7,8 +7,8 @@
fecho if dup emit then ;
: fload ( id -- )
dup floaded, curhdl >r
- fopen to curhdl
+ activefs :open to curhdl
to' in< @ >r ['] f< to in<
begin maybeword ?dup if runword 0 else 1 then until
r> to in< curhdl fclose r> to curhdl ;
-0 S" init.fs" fchild fload
+0 S" init.fs" activefs :child fload
diff --git a/fs/xcomp/bootlo.fs b/fs/xcomp/bootlo.fs
@@ -212,12 +212,8 @@ does> ( ??? 'struct -- ??? *to* )
\ File API
\ Anticipating lib/file
-\ TODO: pass FS to methods, like the docs say
-0 value activefs
-: fsdrv ( fs -- drv ) @ ;
-: fchild ( dirid name -- id ) activefs 4 + @ execute ;
-: fopen ( id -- hdl ) activefs 8 + @ execute ;
-: fnewfile ( dirid name fs -- hdl ) activefs 12 + @ execute ;
+5 struct Filesystem :drv :child :open :newfile :data
+alias abort activefs immediate
\ Autoloading
0 value floaded \ address of the current "loaded file" structure
diff --git a/fs/xcomp/pc/glue2.fs b/fs/xcomp/pc/glue2.fs
@@ -1,4 +1,5 @@
\ Glue code that goes between the filesystem part and boothi
-FATFS to activefs
-0 S" drv" fchild S" pc" fchild S" int13h.fs" fchild floaded,
-0 S" fs" fchild S" fatlo.fs" fchild floaded,
+' FATFS to activefs
+0 S" drv" activefs :child S" pc" activefs :child
+ S" int13h.fs" activefs :child floaded,
+0 S" fs" activefs :child S" fatlo.fs" activefs :child floaded,
diff --git a/fs/xcomp/pc/init.fs b/fs/xcomp/pc/init.fs
@@ -1,6 +1,6 @@
\ Initialization for PC
: ARCH S" i386" ;
-0 S" sys" fchild S" file.fs" fchild fload
+0 S" sys" activefs :child S" file.fs" activefs :child fload
\ We now have f<<
f<< /drv/pc/acpi.fs
f<< /drv/pc/com.fs
diff --git a/posix/glue.fs b/posix/glue.fs
@@ -1,7 +1,8 @@
-create POSIXFS
- 0 ,
+instance Filesystem POSIXFS
+ ' abort ,
' _fchild ,
' _fopen ,
' abort ,
+ ' abort ,
-POSIXFS to activefs
+current to activefs
diff --git a/posix/init.fs b/posix/init.fs
@@ -1,4 +1,4 @@
\ Initialization for POSIX Dusk
: ARCH S" none" ;
-0 S" sys" fchild S" file.fs" fchild fload
+0 S" sys" activefs :child S" file.fs" activefs :child fload
\ We now have f<<