safer-file-handles 0.7 → 0.8
raw patch · 4 files changed
+72/−21 lines, 4 filesdep +regional-pointers
Dependencies added: regional-pointers
Files
- System/IO/SaferFileHandles.hs +63/−19
- System/IO/SaferFileHandles/Internal.hs +1/−1
- System/IO/SaferFileHandles/Unsafe.hs +6/−0
- safer-file-handles.cabal +2/−1
System/IO/SaferFileHandles.hs view
@@ -143,7 +143,7 @@ -- ** Text input -- | Note that the following text input operations are polymorphic in the -- IOMode of the given handle. However the IOModes are restricted to- -- 'ReadModes' only which can be either 'R' or 'RW'.+ -- 'ReadModes' only which can be either 'ReadMode' or 'ReadWriteMode'. , hWaitForInput , hReady , hGetChar@@ -154,7 +154,8 @@ -- ** Text ouput -- | Note that the following text output operations are polymorphic in the -- IOMode of the given handle. However the IOModes are restricted to- -- 'WriteModes' only which can be either 'W', 'A' or 'RW'.+ -- 'WriteModes' only which can be either+ -- 'WriteMode', 'AppendMode' or 'ReadWriteMode'. , hPutChar , hPutStr , hPutStrLn@@ -262,10 +263,9 @@ -- from explicit-iomodes import System.IO.ExplicitIOModes ( IO- , hClose- , IOMode(..)- , MkIOMode(mkIOMode) + , Handle+ , ReadMode , WriteMode , AppendMode@@ -274,8 +274,13 @@ , ReadModes , WriteModes + , IOMode(..)+ , MkIOMode(mkIOMode)+ , CheckMode + , hClose+ , BufferMode(..) , HandlePosn , SeekMode(..)@@ -305,9 +310,16 @@ , getPathString, asAbsFile ) +-- from regional-pointers:+import Foreign.Ptr.Region ( RegionalPtr )+import Foreign.Ptr.Region.Unsafe ( unsafePtr )+ -- from ourselves: import System.IO.SaferFileHandles.Internal ( RegionalFileHandle(RegionalFileHandle) )-import System.IO.SaferFileHandles.Unsafe ( wrap, wrap2, wrap3, sanitizeIOError )+import System.IO.SaferFileHandles.Unsafe ( unsafeHandle+ , wrap, wrap2, wrap3+ , sanitizeIOError+ ) -------------------------------------------------------------------------------@@ -948,10 +960,22 @@ -- instead, whose default action is to terminate the program). -- -- Wraps: @System.IO.'SIO.hPutBuf'@.-hPutBuf ∷ (pr `ParentOf` cr, MonadIO cr, WriteModes ioMode)- ⇒ RegionalFileHandle ioMode pr → Ptr α → Int → cr ()-hPutBuf = wrap3 E.hPutBuf+hPutBuf ∷ ( pr1 `ParentOf` cr+ , pr2 `ParentOf` cr+ , MonadIO cr+ , WriteModes ioMode+ )+ ⇒ RegionalFileHandle ioMode pr1+ → RegionalPtr α pr2+ → Int+ → cr ()+hPutBuf = wrapPtr E.hPutBuf +wrapPtr ∷ MonadIO cr+ ⇒ (Handle ioMode → Ptr α → Int → IO β)+ → (RegionalFileHandle ioMode pr1 → RegionalPtr α pr2 → Int → cr β)+wrapPtr f = \h rPtr → liftIO ∘ f (unsafeHandle h) (unsafePtr rPtr)+ -- | 'hGetBuf' @hdl buf count@ reads data from the handle @hdl@ -- into the buffer @buf@ until either EOF is reached or -- @count@ 8-bit bytes have been read.@@ -971,15 +995,29 @@ -- 'RegionalFileHandle', and reads bytes directly. -- -- Wraps: @System.IO.'SIO.hGetBuf'@.-hGetBuf ∷ (pr `ParentOf` cr, MonadIO cr, ReadModes ioMode)- ⇒ RegionalFileHandle ioMode pr → Ptr α → Int → cr Int-hGetBuf = wrap3 E.hGetBuf+hGetBuf ∷ ( pr1 `ParentOf` cr+ , pr2 `ParentOf` cr+ , MonadIO cr+ , ReadModes ioMode+ )+ ⇒ RegionalFileHandle ioMode pr1+ → RegionalPtr α pr2+ → Int+ → cr Int+hGetBuf = wrapPtr E.hGetBuf #if !defined(__NHC__) && !defined(__HUGS__) -- | Wraps: @System.IO.'SIO.hPutBufNonBlocking'@.-hPutBufNonBlocking ∷ (pr `ParentOf` cr, MonadIO cr, WriteModes ioMode)- ⇒ RegionalFileHandle ioMode pr → Ptr α → Int → cr Int-hPutBufNonBlocking = wrap3 E.hPutBufNonBlocking+hPutBufNonBlocking ∷ ( pr1 `ParentOf` cr+ , pr2 `ParentOf` cr+ , MonadIO cr+ , WriteModes ioMode+ )+ ⇒ RegionalFileHandle ioMode pr1+ → RegionalPtr α pr2+ → Int+ → cr Int+hPutBufNonBlocking = wrapPtr E.hPutBufNonBlocking -- | 'hGetBufNonBlocking' @hdl buf count@ reads data from the handle @hdl@ -- into the buffer @buf@ until either EOF is reached, or@@ -1001,9 +1039,16 @@ -- on the 'RegionalFileHandle', and reads bytes directly. -- -- Wraps: @System.IO.'SIO.hGetBufNonBlocking'@.-hGetBufNonBlocking ∷ (pr `ParentOf` cr, MonadIO cr, ReadModes ioMode)- ⇒ RegionalFileHandle ioMode pr → Ptr α → Int → cr Int-hGetBufNonBlocking = wrap3 E.hGetBufNonBlocking+hGetBufNonBlocking ∷ ( pr1 `ParentOf` cr+ , pr2 `ParentOf` cr+ , MonadIO cr+ , ReadModes ioMode+ )+ ⇒ RegionalFileHandle ioMode pr1+ → RegionalPtr α pr2+ → Int+ → cr Int+hGetBufNonBlocking = wrapPtr E.hGetBufNonBlocking #endif @@ -1151,4 +1196,3 @@ -- The End ----------------------------------------------------------------------
System/IO/SaferFileHandles/Internal.hs view
@@ -45,7 +45,7 @@ -- | A regional handle to an opened file parameterized by the 'IOMode' in which -- you opened the file and the region in which it was created. data RegionalFileHandle ioMode (r ∷ * → *) =- RegionalFileHandle (Handle ioMode) (Maybe (CloseHandle r))+ RegionalFileHandle !(Handle ioMode) !(Maybe (CloseHandle r)) instance Dup (RegionalFileHandle ioMode) where dup (RegionalFileHandle h Nothing) = return $ RegionalFileHandle h Nothing
System/IO/SaferFileHandles/Unsafe.hs view
@@ -63,6 +63,12 @@ → (RegionalFileHandle ioMode r → γ → β → m α) wrap3 f = \h z y → liftIO $ sanitizeIOError $ f (unsafeHandle h) z y +-- | Modify thrown @IOErrors@ in the given computation by erasing the+-- 'ioe_handle' field in the @IOError@ which may contain the @Handle@ which+-- caused the @IOError@.+--+-- I use this to ensure that @Handles@ don't /leak/ out the region via+-- exceptions. sanitizeIOError ∷ IO α → IO α sanitizeIOError = modifyIOError $ \e → e { ioe_handle = Nothing }
safer-file-handles.cabal view
@@ -1,5 +1,5 @@ name: safer-file-handles-version: 0.7+version: 0.8 cabal-version: >=1.6 build-type: Custom license: BSD3@@ -58,6 +58,7 @@ , MonadCatchIO-transformers >= 0.2 && < 0.3 , explicit-iomodes >= 0.5 && < 0.6 , pathtype >= 0.0.1 && < 0.6+ , regional-pointers >= 0.4 && < 0.5 exposed-modules: System.IO.SaferFileHandles System.IO.SaferFileHandles.Unsafe other-modules: System.IO.SaferFileHandles.Internal