1 import Base 2 3 # Classifies the filesystem object reported by a metadata lookup. 4 type FileType is Data: 5 Regular{} 6 Directory{} 7 Other{} 8 9 # Stores the kind, size, and modification time returned by stat. 10 type Info is Data: 11 Info{kind: FileType, size: U32, mtime: U32} 12 13 def stat.raw(path: String) -> IO(Result<&1, &1, U32 & String, U32 & U32 & U32>): 14 import "./effs/files_compat.c" 15 import "./effs/files_compat.js" 16 17 # Maps the operating-system kind code into the public file classification. 18 def kind_from_nat(kind: Nat) -> FileType: 19 match kind: 20 case 0n: Regular{} 21 case 1n+rest: 22 match rest: 23 case 0n: Directory{} 24 case 1n+rest: Other{} 25 26 def stat.kind(kind: U32) -> FileType: 27 kind_from_nat(U32.to_nat(kind)) 28 29 def stat.result( 30 result: Result<&1, &1, U32 & String, U32 & U32 & U32> 31 ) -> Result<&1, &1, U32 & String, Info>: 32 match result: 33 case Fail{error}: Fail{error} 34 case Done{(kind, (size, mtime))}: 35 Done{Info{stat.kind(kind), size, mtime}} 36 37 # Reads filesystem metadata for a path. 38 def stat(path: String) -> IO(Result<&1, &1, U32 & String, Info>): 39 do IO>: 40 result : Result<&1, &1, U32 & String, U32 & U32 & U32> <- stat.raw(path) 41 return stat.result(result) 42 43 # Removes a file or an empty directory at a path. 44 def remove(path: String) -> IO(Result<&1, &1, U32 & String, Unit>): 45 import "./effs/files_compat.c" 46 import "./effs/files_compat.js" 47 48 # Renames a path using the filesystem's atomic rename operation. 49 def rename(from: String, to: String) -> IO(Result<&1, &1, U32 & String, Unit>): 50 import "./effs/files_compat.c" 51 import "./effs/files_compat.js" 52 53 def read.words(file: File, max: U32) -> IO(File & Result<&1, &1, U32 & String, U32 & Array>): 54 import "./effs/files_compat.c" 55 import "./effs/files_compat.js" 56 57 def write.words.once( 58 file: File, 59 offset: U32, 60 len: U32, 61 words: Array 62 ) -> IO(File & Result<&1, &1, U32 & String, U32 & Array>): 63 import "./effs/files_compat.c" 64 import "./effs/files_compat.js"