1 | module System.Directory
4 | import public System.File
16 | supportC : (fn : String) -> String
17 | supportC fn = "C:\{fn}, libidris2_support, idris_directory.h"
22 | supportNode : (fn : String) -> String
23 | supportNode fn = "node:support:\{fn},support_system_directory"
25 | ok : HasIO io => a -> io (Either FileError a)
26 | ok x = pure (Right x)
28 | %foreign supportC "idris2_currentDirectory"
29 | "node:lambda:()=>process.cwd()"
30 | prim__currentDir : PrimIO (Ptr String)
32 | %foreign supportC "idris2_changeDir"
33 | supportNode "changeDir"
34 | prim__changeDir : String -> PrimIO Int
36 | %foreign supportC "idris2_createDir"
37 | supportNode "createDir"
38 | prim__createDir : String -> PrimIO Int
40 | %foreign supportC "idris2_openDir"
41 | supportNode "openDir"
42 | prim__openDir : String -> PrimIO DirPtr
44 | %foreign supportC "idris2_closeDir"
45 | supportNode "closeDir"
46 | prim__closeDir : DirPtr -> PrimIO ()
48 | %foreign supportC "idris2_removeDir"
49 | supportNode "removeDir"
50 | prim__removeDir : String -> PrimIO ()
52 | %foreign supportC "idris2_nextDirEntry"
53 | supportNode "dirEntry"
54 | prim__dirEntry : DirPtr -> PrimIO (Ptr String)
58 | data Directory : Type where
59 | MkDir : DirPtr -> Directory
63 | createDir : HasIO io => String -> io (Either FileError ())
65 | = do res <- primIO (prim__createDir dir)
73 | changeDir : HasIO io => String -> io Bool
75 | = do ok <- primIO (prim__changeDir dir)
81 | currentDir : HasIO io => io (Maybe String)
83 | = do res <- primIO prim__currentDir
84 | if prim__nullPtr res /= 0
86 | else do let s = prim__getString res
87 | free $
prim__forgetPtr res
92 | openDir : HasIO io => String -> io (Either FileError Directory)
94 | = do res <- primIO (prim__openDir d)
95 | if prim__nullAnyPtr res /= 0
101 | closeDir : HasIO io => Directory -> io ()
102 | closeDir (MkDir d) = primIO (prim__closeDir d)
107 | removeDir : HasIO io => String -> io ()
108 | removeDir dirName = primIO (prim__removeDir dirName)
112 | nextDirEntry : HasIO io => Directory -> io (Either FileError (Maybe String))
113 | nextDirEntry (MkDir d)
114 | = do res <- primIO (prim__dirEntry d)
115 | if prim__nullPtr res /= 0
116 | then if !(getErrno) /= 0
118 | else pure $
Right Nothing
119 | else do let n = prim__getString res
120 | if n == "." || n == ".."
121 | then assert_total $
nextDirEntry (MkDir d)
122 | else pure $
Right (Just n)
126 | collectDir : HasIO io => Directory -> io (Either FileError (List String))
128 | = liftIO $
do let (>>=) : (IO . Either e) a -> (a -> (IO . Either e) b) -> (IO . Either e) b
129 | (>>=) = Prelude.(>>=) @{Monad.Compose {m = IO} {t = Either e}}
130 | Just n <- nextDirEntry d
131 | | Nothing => pure $
Right []
132 | ns <- assert_total $
collectDir d
133 | pure $
Right (n :: ns)
140 | listDir : HasIO io => (name : String) -> io (Either FileError (List String))
141 | listDir name = do Right d <- openDir name
142 | | Left e => pure $
Left e
144 | ignore <- closeDir d