From c7d88b3612fdf74a7964a670d6e79128f97f46b0 Mon Sep 17 00:00:00 2001 From: Julian Ospald <hasufell@posteo.de> Date: Sun, 1 May 2016 17:55:41 +0200 Subject: [PATCH] Add support for more OpenFileFlags and refactor 'openFd' (PR #59) * 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) Closes #59 --- System/Posix/IO.hsc | 9 +++---- System/Posix/IO/ByteString.hsc | 9 +++---- System/Posix/IO/Common.hsc | 47 ++++++++++++++++++++++------------ changelog.md | 7 +++++ 4 files changed, 46 insertions(+), 26 deletions(-) diff --git a/System/Posix/IO.hsc b/System/Posix/IO.hsc index ec510f7..438c44c 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 2ce5ed6..6bbfed1 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 a838352..225ff17 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 0d1a925..617607f 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 -- GitLab