Skip to content

Commit

Permalink
2
Browse files Browse the repository at this point in the history
  • Loading branch information
adithyaov committed Oct 28, 2024
1 parent aa3b0a1 commit 8a21e7b
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions core/src/Streamly/Internal/FileSystem/Posix/File.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ import Data.Bits ((.|.))
import Foreign.C.Error (getErrno, eINTR, errnoToIOError)
import Foreign.C.String (CString)
import Foreign.C.Types (CInt(..))
import Streamly.Internal.FileSystem.Path (Path)
import Streamly.Internal.FileSystem.PosixPath (PosixPath)
import System.IO (IOMode(..), Handle)
import System.Posix.IO (fdToHandle)
import System.Posix.Types (Fd(..), CMode(..), FileMode)

import qualified Streamly.Internal.Data.Array as Array
import qualified Streamly.Internal.FileSystem.Path as Path
import qualified Streamly.Internal.FileSystem.PosixPath as Path

-------------------------------------------------------------------------------
-- Posix
Expand Down Expand Up @@ -71,7 +71,7 @@ defaultExistingFileFlags = defaultFileFlags { noctty = True, nonBlock = True, cr
defaultFileFlags' :: OpenFileFlags
defaultFileFlags' = defaultFileFlags { noctty = True, nonBlock = True }

withFilePath :: Path -> (CString -> IO a) -> IO a
withFilePath :: PosixPath -> (CString -> IO a) -> IO a
withFilePath p = Array.asCStringUnsafe (Path.toChunk p)


Expand Down Expand Up @@ -116,18 +116,18 @@ foreign import capi unsafe "HsUnix.h openat"

-- |Open and optionally create this file. See 'System.Posix.Files'
-- for information on how to use the 'FileMode' type.
openFd :: Path
openFd :: PosixPath
-> OpenMode
-> OpenFileFlags
-> IO Fd
openFd = openFdAt Nothing

throwErrnoPathIfMinus1Retry :: (Eq a, Num a)
=> String -> Path -> IO a -> IO a
=> String -> PosixPath -> IO a -> IO a
throwErrnoPathIfMinus1Retry loc path f = do
throwErrnoPathIfRetry (== -1) loc path f

throwErrnoPathIfRetry :: (a -> Bool) -> String -> Path -> IO a -> IO a
throwErrnoPathIfRetry :: (a -> Bool) -> String -> PosixPath -> IO a -> IO a
throwErrnoPathIfRetry pr loc rpath f =
do
res <- f
Expand All @@ -139,7 +139,7 @@ throwErrnoPathIfRetry pr loc rpath f =
else throwErrnoPath loc rpath
else return res

throwErrnoPath :: String -> Path -> IO a
throwErrnoPath :: String -> PosixPath -> IO a
throwErrnoPath loc path =
do
errno <- getErrno
Expand All @@ -155,15 +155,15 @@ throwErrnoPath loc path =
-- directory tree that would otherwise become inaccessible after dropping
-- privileges.
openFdAt :: Maybe Fd -- ^ Optional directory file descriptor
-> Path -- ^ Pathname to open
-> PosixPath -- ^ Pathname to open
-> OpenMode -- ^ Read-only, read-write or write-only
-> OpenFileFlags -- ^ Append, exclusive, truncate, etc.
-> IO Fd
openFdAt fdMay name how flags =
withFilePath name $ \str ->
throwErrnoPathIfMinus1Retry "openFdAt" name $ openat_ fdMay str how flags

openExistingFile_ :: OpenFileFlags -> Path -> IOMode -> IO Handle
openExistingFile_ :: OpenFileFlags -> PosixPath -> IOMode -> IO Handle
openExistingFile_ df fp iomode = fdToHandle =<< case iomode of
ReadMode -> open ReadOnly df
WriteMode -> open WriteOnly df { trunc = True }
Expand All @@ -173,13 +173,13 @@ openExistingFile_ df fp iomode = fdToHandle =<< case iomode of
open = openFd fp

-- | Open an existing file and return the 'Handle'.
openExistingFile :: Path -> IOMode -> IO Handle
openExistingFile :: PosixPath -> IOMode -> IO Handle
openExistingFile = openExistingFile_ defaultExistingFileFlags

openExistingFileWithCloseOnExec :: Path -> IOMode -> IO Handle
openExistingFileWithCloseOnExec :: PosixPath -> IOMode -> IO Handle
openExistingFileWithCloseOnExec = openExistingFile_ defaultExistingFileFlags { cloexec = True }

openFile_ :: OpenFileFlags -> Path -> IOMode -> IO Handle
openFile_ :: OpenFileFlags -> PosixPath -> IOMode -> IO Handle
openFile_ df fp iomode = fdToHandle =<< case iomode of
ReadMode -> open ReadOnly df
WriteMode -> open WriteOnly df { trunc = True, creat = Just 0o666 }
Expand All @@ -189,8 +189,8 @@ openFile_ df fp iomode = fdToHandle =<< case iomode of
open = openFd fp

-- | Open a file and return the 'Handle'.
openFile :: Path -> IOMode -> IO Handle
openFile :: PosixPath -> IOMode -> IO Handle
openFile = openFile_ defaultFileFlags'

openFileWithCloseOnExec :: Path -> IOMode -> IO Handle
openFileWithCloseOnExec :: PosixPath -> IOMode -> IO Handle
openFileWithCloseOnExec = openFile_ defaultFileFlags' { cloexec = True }

0 comments on commit 8a21e7b

Please sign in to comment.