@@ -21,8 +21,13 @@ import IO
21data File : Type where
21data File : Type where
22 FHandle : (p : Ptr) -> File
22 FHandle : (p : Ptr) -> File
24+||| A directory handle
25+data Directory : Type where
26+ DHandle : (p : Ptr) -> Directory
24-- Usage hints for erasure analysis
28-- Usage hints for erasure analysis
25%used FHandle p
29%used FHandle p
27||| An error from a file operation
32||| An error from a file operation
28-- This is built in idris_mkFileError() in rts/idris_stdfgn.c. Make sure
33-- This is built in idris_mkFileError() in rts/idris_stdfgn.c. Make sure
@@ -280,3 +285,46 @@ writeFile fn contents = do
280 Right () <- fPutStr h contents | Left err => pure (Left err)
285 Right () <- fPutStr h contents | Left err => pure (Left err)
281 closeFile h
286 closeFile h
282 pure (Right ())
287 pure (Right ())
291+dirOpen : (d : String) -> IO (Either FileError Directory)
293+ = do dptr <- foreign FFI_C "idris_dirOpen" (String -> IO Ptr) d
295+ then do err <- getFileError
297+ else pure (Right (DHandle dptr))
300+dirClose : Directory -> IO ()
301+dirClose (DHandle d) = foreign FFI_C "idris_dirClose" (Ptr -> IO ()) d
304+dirError : Directory -> IO Bool
306+ = do err <- foreign FFI_C "idris_dirError" (Ptr -> IO Int) d
307+ pure (not (err == 0))
310+dirEntry : Directory -> IO (Either FileError String)
312+ = do fn <- foreign FFI_C "idris_nextDirEntry" (Ptr -> IO String) d
313+ if !(dirError (DHandle d))
314+ then pure (Left FileReadError)
315+ else pure (Right fn)
318+createDir : String -> IO (Either FileError ())
320+ = do ok <- foreign FFI_C "idris_mkdir" (String -> IO Int) d
322+ then pure (Right ())
323+ else do err <- getFileError
327+changeDir : String -> IO Bool
329+ = do ok <- foreign FFI_C "chdir" (String -> IO Int) dir