diff --git a/System/Posix/IO.hsc b/System/Posix/IO.hsc index ec510f76423cc068062c9c4b896790583fa2f646..438c44c94c197c8e70da2b25fb97514065adaf15 100644 --- a/System/Posix/IO.hsc +++ b/System/Posix/IO.hsc @@ -76,13 +76,12 @@ import System.Posix.Internals ( withFilePath ) -- for information on how to use the 'FileMode' type. openFd :: FilePath -> OpenMode - -> Maybe FileMode -- ^Just x => creates the file with the given modes, Nothing => the file must exist. -> OpenFileFlags -> IO Fd -openFd name how maybe_mode flags = do - withFilePath name $ \str -> do +openFd name how flags = + withFilePath name $ \str -> throwErrnoPathIfMinus1Retry "openFd" name $ - open_ str how maybe_mode flags + open_ str how flags -- |Create and open this file in WriteOnly mode. A special case of -- 'openFd'. See 'System.Posix.Files' for information on how to use @@ -90,4 +89,4 @@ openFd name how maybe_mode flags = do createFile :: FilePath -> FileMode -> IO Fd createFile name mode - = openFd name WriteOnly (Just mode) defaultFileFlags{ trunc=True } + = openFd name WriteOnly defaultFileFlags{ trunc=True, creat=(Just mode) } diff --git a/System/Posix/IO/ByteString.hsc b/System/Posix/IO/ByteString.hsc index 2ce5ed61963f38cb43f157c4a06d4a69beb5f85e..6bbfed1bfce9876ae54784dadd0ba5676d2465a1 100644 --- a/System/Posix/IO/ByteString.hsc +++ b/System/Posix/IO/ByteString.hsc @@ -76,13 +76,12 @@ import System.Posix.ByteString.FilePath -- for information on how to use the 'FileMode' type. openFd :: RawFilePath -> OpenMode - -> Maybe FileMode -- ^Just x => creates the file with the given modes, Nothing => the file must exist. -> OpenFileFlags -> IO Fd -openFd name how maybe_mode flags = do - withFilePath name $ \str -> do +openFd name how flags = + withFilePath name $ \str -> throwErrnoPathIfMinus1Retry "openFd" name $ - open_ str how maybe_mode flags + open_ str how flags -- |Create and open this file in WriteOnly mode. A special case of -- 'openFd'. See 'System.Posix.Files' for information on how to use @@ -90,4 +89,4 @@ openFd name how maybe_mode flags = do createFile :: RawFilePath -> FileMode -> IO Fd createFile name mode - = openFd name WriteOnly (Just mode) defaultFileFlags{ trunc=True } + = openFd name WriteOnly defaultFileFlags{ trunc=True, creat=(Just mode) } diff --git a/System/Posix/IO/Common.hsc b/System/Posix/IO/Common.hsc index a838352758706af874fa348a62a06f2f834c9d57..225ff17bcc1d2682cc7132b8afa3e8a5cdf0a3cf 100644 --- a/System/Posix/IO/Common.hsc +++ b/System/Posix/IO/Common.hsc @@ -133,11 +133,16 @@ data OpenMode = ReadOnly | WriteOnly | ReadWrite -- |Correspond to some of the int flags from C's fcntl.h. data OpenFileFlags = OpenFileFlags { - append :: Bool, -- ^ O_APPEND - exclusive :: Bool, -- ^ O_EXCL - noctty :: Bool, -- ^ O_NOCTTY - nonBlock :: Bool, -- ^ O_NONBLOCK - trunc :: Bool -- ^ O_TRUNC + append :: Bool, -- ^ O_APPEND + exclusive :: Bool, -- ^ O_EXCL, result is undefined if O_CREAT is Nothing + noctty :: Bool, -- ^ O_NOCTTY + nonBlock :: Bool, -- ^ O_NONBLOCK + trunc :: Bool, -- ^ O_TRUNC + nofollow :: Bool, -- ^ O_NOFOLLOW + creat :: Maybe FileMode, -- ^ O_CREAT + cloexec :: Bool, -- ^ O_CLOEXEC + directory :: Bool, -- ^ O_DIRECTORY + sync :: Bool -- ^ O_SYNC } @@ -150,7 +155,12 @@ defaultFileFlags = exclusive = False, noctty = False, nonBlock = False, - trunc = False + trunc = False, + nofollow = False, + creat = Nothing, + cloexec = False, + directory = False, + sync = False } @@ -158,24 +168,29 @@ defaultFileFlags = -- for information on how to use the 'FileMode' type. open_ :: CString -> OpenMode - -> Maybe FileMode -- ^Just x => creates the file with the given modes, Nothing => the file must exist. -> OpenFileFlags -> IO Fd -open_ str how maybe_mode (OpenFileFlags appendFlag exclusiveFlag nocttyFlag - nonBlockFlag truncateFlag) = do +open_ str how (OpenFileFlags appendFlag exclusiveFlag nocttyFlag + nonBlockFlag truncateFlag nofollowFlag + creatFlag cloexecFlag directoryFlag + syncFlag) = do fd <- c_open str all_flags mode_w return (Fd fd) where all_flags = creat .|. flags .|. open_mode flags = - (if appendFlag then (#const O_APPEND) else 0) .|. - (if exclusiveFlag then (#const O_EXCL) else 0) .|. - (if nocttyFlag then (#const O_NOCTTY) else 0) .|. - (if nonBlockFlag then (#const O_NONBLOCK) else 0) .|. - (if truncateFlag then (#const O_TRUNC) else 0) - - (creat, mode_w) = case maybe_mode of + (if appendFlag then (#const O_APPEND) else 0) .|. + (if exclusiveFlag then (#const O_EXCL) else 0) .|. + (if nocttyFlag then (#const O_NOCTTY) else 0) .|. + (if nonBlockFlag then (#const O_NONBLOCK) else 0) .|. + (if truncateFlag then (#const O_TRUNC) else 0) .|. + (if nofollowFlag then (#const O_NOFOLLOW) else 0) .|. + (if cloexecFlag then (#const O_CLOEXEC) else 0) .|. + (if directoryFlag then (#const O_DIRECTORY) else 0) .|. + (if syncFlag then (#const O_SYNC) else 0) + + (creat, mode_w) = case creatFlag of Nothing -> (0,0) Just x -> ((#const O_CREAT), x) diff --git a/changelog.md b/changelog.md index 0d1a9255785f49bd0c886446aa9808c621562114..617607f66cf5bfd9562c6cf3ed3ccbc0ab673119 100644 --- a/changelog.md +++ b/changelog.md @@ -8,6 +8,13 @@ CRDLY(CR0,CR1,CR2,CR2), TABDLY(TAB0,TAB1,TAB2,TAB3) BSDLY(BS0,BS1), VTDLY(VT0,VT1), FFDLY(FF0,FF1) + * Add support for `O_NOFOLLOW`, `O_CLOEXEC`, `O_DIRECTORY` and `O_SYNC` + (#6, #57) + + * Refactor API of `openFd` removing `Maybe FileMode` argument, + which now must be passed as part of `OpenFileFlags` + (e.g. `defaultFileFlags { creat = Just mode }`) (#58) + ## 2.7.2.2 *May 2017* * Bundled with GHC 8.2.1