Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
hasufell committed Jan 12, 2024
1 parent 4417bc8 commit f35f052
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions posix/System/File/Platform.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
{-# LANGUAGE LambdaCase #-}

module System.File.Platform where

import GHC.IO
import GHC.IO.Exception
import Control.Exception (try)
import System.IO (IOMode(..), Handle)
import System.Posix.IO.PosixString
( defaultFileFlags,
Expand Down Expand Up @@ -30,3 +35,21 @@ openExistingFile fp iomode = fdToHandle =<< case iomode of
where
open = openFd fp
df = defaultFileFlags { noctty = True, nonBlock = True, creat = Nothing }

withFile :: FilePath -> IOMode -> (Handle -> IO r) -> IO r
withFile fp im act =
-- Only annotate when setup or teardown of withFile' raised the exception
catchException
(withFile' fp im undefined True (try . act))
(\e -> ioError (addFilePathToIOError "withFile" fp e))
>>= \case
Left e -> ioError e
Right x -> pure x

addFilePathToIOError :: String -> FilePath -> IOException -> IOException
addFilePathToIOError fun fp ioe
= ioe{ ioe_location = fun, ioe_filename = Just fp }

withFile' :: String -> IOMode -> Bool -> Bool -> (Handle -> IO r) -> IO r
withFile' = undefined

0 comments on commit f35f052

Please sign in to comment.