0 | module System.Linux.File
3 | import Derive.Prelude
5 | import Data.Buffer.Core
6 | import System.Linux.Error
9 | %language ElabReflection
15 | %foreign "C:ep_read,epoll-idris"
16 | prim__read : Bits32 -> (buf : Buffer) -> (offset,max : Bits32) -> PrimIO Int32
18 | %foreign "C:ep_write,epoll-idris"
19 | prim__write : Bits32 -> (buf : Buffer) -> (offset,max : Bits32) -> PrimIO Int32
21 | %foreign "C:close,epoll-idris"
22 | prim__close : Bits32 -> PrimIO ()
24 | %foreign "C:ep_set_nonblocking,epoll-idris"
25 | prim__setNonBlocking : Bits32 -> PrimIO ()
32 | interface FileDesc a where
33 | fileDesc : a -> Bits32
36 | FileDesc Bits32 where fileDesc x = x
40 | data ReadRes : Type where
49 | Bytes : (n : Nat) -> IBuffer n -> ReadRes
52 | Err : EpollErr -> ReadRes
54 | toReadRes : Either EpollErr Nat -> Buffer -> ReadRes
55 | toReadRes (Left EAGAIN) _ = Again
56 | toReadRes (Left x) _ = Err x
57 | toReadRes (Right 0) _ = EOF
58 | toReadRes (Right n) buf = Bytes n (unsafeMakeBuffer buf)
60 | parameters {0 a : Type}
61 | {auto fd : FileDesc a}
65 | close : a -> PrimIO ()
66 | close = prim__close . fileDesc
74 | read : a -> Buffer -> (offset,max : Nat) -> PrimIO (Either EpollErr Nat)
75 | read fi buf offset max w =
76 | let MkIORes res w :=prim__read (fileDesc fi) buf (cast offset) (cast max) w
80 | write : a -> Buffer -> (offset,max : Nat) -> PrimIO (Either EpollErr Nat)
81 | write fi buf offset max w =
82 | let MkIORes res w :=prim__write (fileDesc fi) buf (cast offset) (cast max) w
91 | readBytes : a -> (max : Nat) -> PrimIO ReadRes
92 | readBytes fi max w =
93 | let MkIORes buf w := prim__newBuf (cast max) w
94 | MkIORes res w := read fi buf 0 max w
95 | in MkIORes (toReadRes res buf) w
104 | setNonBlocking : a -> PrimIO ()
105 | setNonBlocking = prim__setNonBlocking . fileDesc
108 | data StdFile : Type where
113 | %runElab derive "StdFile" [Show,Eq,Ord,Finite]
116 | FileDesc StdFile where fileDesc = cast . conIndexStdFile