commit e59dcc42adf7c18382ebf1b58c516a77122563a6
parent 94b90abd07cf165c7ce32bdb395727c6e84e1d9d
Author: Virgil Dupras <hsoft@hardcoded.net>
Date: Wed, 17 Aug 2022 07:57:52 -0400
sys/file: add "curfs" and change the meaning of "activefs"
Diffstat:
4 files changed, 30 insertions(+), 10 deletions(-)
diff --git a/fs/doc/file.txt b/fs/doc/file.txt
@@ -84,6 +84,7 @@ The structure retured by ":info" above goes as follow:
name -- a pointer to a string
size -- size of the file, 0 if dir
dir? -- whether the element is a Directory
+
## Common API
On top of those words, the File subsystem implements those words:
@@ -105,6 +106,22 @@ findpath ( path -- id-or-0 )
findpath# ( path -- id )
Like findpath, but errors out if not found.
+## activefs, curfs, curdir
+
+The File subsystem has 3 important global variables that determines how paths
+are found.
+
+curdir is the ID of the current directory, as changed by "chdir". It is
+initialized to 0, the root of the filesystem.
+
+curfs is a Filesystem bind to the FS associated with "curdir" it only changes
+with "chdir".
+
+activefs is a Filesystem bind to the last "used" Filesystem. On boot, it is
+initialized to the boot FS, but after the File subsystem is loaded, it is
+changed very often. For example, every time "findpath" is called, "activefs"
+changes to the FS that hosts its result.
+
## Paths
Paths uniquely identify a directory or a file on the current set of mounted
diff --git a/fs/sys/file.fs b/fs/sys/file.fs
@@ -11,21 +11,21 @@ struct[ FSInfo
create filesystems MAXFSCNT CELLSZ * allot0
activefs :self filesystems ! \ record our boot FS in the list
+activefs :self structbind Filesystem curfs
0 value curdir
create _buf $100 allot
-0 value _fs
: findpathdir ( path -- dirid? name-or-0 )
- activefs :self to _fs
+ curfs :self ['] activefs rebind
A>r 0 _buf c!+ >A c@+ ( a len )
over c@ '/' = if 1- >r 1+ 0 ( root ) else
over 1+ c@ ':' = if ( a len )
over c@ upcase 'A' - CELLSZ * filesystems + @ ( a len 'fs )
- ?dup not if 2drop r>A 0 exit then to _fs
+ ?dup not if 2drop r>A 0 exit then ['] activefs rebind
2 - >r 2 + 0 ( root ) else >r curdir then then ( a dirid )
swap begin ( dirid a )
c@+ dup '/' = if ( dirid a c )
- drop swap _buf _fs Filesystem :child ( 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 )
diff --git a/fs/tests/fs/fat.fs b/fs/tests/fs/fat.fs
@@ -7,15 +7,15 @@ testbegin
512 TOTSEC RAMDrive :new value mydrv
mydrv 16 1 1 TOTSEC 17 - FAT newFAT12
mydrv FAT :mountvolume value myfat
-activefs :self value oldfs
-myfat ' activefs rebind
-0 S" foo.fs" activefs :newfile #
+curfs :self value oldfs
+myfat ' curfs rebind
+0 S" foo.fs" curfs :newfile #
S" /foo.fs" findpath# ( fsid ) \ found!
-dup activefs :info ( fsid info )
+dup curfs :info ( fsid info )
FSInfo name S" FOO.FS" #s=
-activefs :open ( hdl )
+curfs :open ( hdl )
dup FATFile :cluster0 0 #eq \ no cluster allocated yet
dup S" 42" c@+ rot File :writebuf 2 #eq ( fc ) File :close
f<< /foo.fs 42 #eq
-oldfs ' activefs rebind
+oldfs ' curfs rebind
testend
diff --git a/fs/xcomp/bootlo.fs b/fs/xcomp/bootlo.fs
@@ -238,6 +238,9 @@ struct[ Filesystem
smethod :newfile
: :drv compile drv [compile] Drive ; immediate
]struct
+\ activefs holds a reference to the last "used" fs. It's not the same thing as
+\ "curfs", which goes with "curdir". "activefs" moves a lot more. For example,
+\ it changes after each "findpath" call.
0 structbind Filesystem activefs \ has to be rebinded before first use
extends IO struct[ File
smethod :seek