Skip to content
Snippets Groups Projects
Commit c7d88b36 authored by Julian Ospald's avatar Julian Ospald :tea: Committed by Herbert Valerio Riedel
Browse files

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
parent 41c57761
No related branches found
No related tags found
No related merge requests found
...@@ -76,13 +76,12 @@ import System.Posix.Internals ( withFilePath ) ...@@ -76,13 +76,12 @@ import System.Posix.Internals ( withFilePath )
-- for information on how to use the 'FileMode' type. -- for information on how to use the 'FileMode' type.
openFd :: FilePath openFd :: FilePath
-> OpenMode -> OpenMode
-> Maybe FileMode -- ^Just x => creates the file with the given modes, Nothing => the file must exist.
-> OpenFileFlags -> OpenFileFlags
-> IO Fd -> IO Fd
openFd name how maybe_mode flags = do openFd name how flags =
withFilePath name $ \str -> do withFilePath name $ \str ->
throwErrnoPathIfMinus1Retry "openFd" name $ 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 -- |Create and open this file in WriteOnly mode. A special case of
-- 'openFd'. See 'System.Posix.Files' for information on how to use -- 'openFd'. See 'System.Posix.Files' for information on how to use
...@@ -90,4 +89,4 @@ openFd name how maybe_mode flags = do ...@@ -90,4 +89,4 @@ openFd name how maybe_mode flags = do
createFile :: FilePath -> FileMode -> IO Fd createFile :: FilePath -> FileMode -> IO Fd
createFile name mode createFile name mode
= openFd name WriteOnly (Just mode) defaultFileFlags{ trunc=True } = openFd name WriteOnly defaultFileFlags{ trunc=True, creat=(Just mode) }
...@@ -76,13 +76,12 @@ import System.Posix.ByteString.FilePath ...@@ -76,13 +76,12 @@ import System.Posix.ByteString.FilePath
-- for information on how to use the 'FileMode' type. -- for information on how to use the 'FileMode' type.
openFd :: RawFilePath openFd :: RawFilePath
-> OpenMode -> OpenMode
-> Maybe FileMode -- ^Just x => creates the file with the given modes, Nothing => the file must exist.
-> OpenFileFlags -> OpenFileFlags
-> IO Fd -> IO Fd
openFd name how maybe_mode flags = do openFd name how flags =
withFilePath name $ \str -> do withFilePath name $ \str ->
throwErrnoPathIfMinus1Retry "openFd" name $ 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 -- |Create and open this file in WriteOnly mode. A special case of
-- 'openFd'. See 'System.Posix.Files' for information on how to use -- 'openFd'. See 'System.Posix.Files' for information on how to use
...@@ -90,4 +89,4 @@ openFd name how maybe_mode flags = do ...@@ -90,4 +89,4 @@ openFd name how maybe_mode flags = do
createFile :: RawFilePath -> FileMode -> IO Fd createFile :: RawFilePath -> FileMode -> IO Fd
createFile name mode createFile name mode
= openFd name WriteOnly (Just mode) defaultFileFlags{ trunc=True } = openFd name WriteOnly defaultFileFlags{ trunc=True, creat=(Just mode) }
...@@ -133,11 +133,16 @@ data OpenMode = ReadOnly | WriteOnly | ReadWrite ...@@ -133,11 +133,16 @@ data OpenMode = ReadOnly | WriteOnly | ReadWrite
-- |Correspond to some of the int flags from C's fcntl.h. -- |Correspond to some of the int flags from C's fcntl.h.
data OpenFileFlags = data OpenFileFlags =
OpenFileFlags { OpenFileFlags {
append :: Bool, -- ^ O_APPEND append :: Bool, -- ^ O_APPEND
exclusive :: Bool, -- ^ O_EXCL exclusive :: Bool, -- ^ O_EXCL, result is undefined if O_CREAT is Nothing
noctty :: Bool, -- ^ O_NOCTTY noctty :: Bool, -- ^ O_NOCTTY
nonBlock :: Bool, -- ^ O_NONBLOCK nonBlock :: Bool, -- ^ O_NONBLOCK
trunc :: Bool -- ^ O_TRUNC 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 = ...@@ -150,7 +155,12 @@ defaultFileFlags =
exclusive = False, exclusive = False,
noctty = False, noctty = False,
nonBlock = False, nonBlock = False,
trunc = False trunc = False,
nofollow = False,
creat = Nothing,
cloexec = False,
directory = False,
sync = False
} }
...@@ -158,24 +168,29 @@ defaultFileFlags = ...@@ -158,24 +168,29 @@ defaultFileFlags =
-- for information on how to use the 'FileMode' type. -- for information on how to use the 'FileMode' type.
open_ :: CString open_ :: CString
-> OpenMode -> OpenMode
-> Maybe FileMode -- ^Just x => creates the file with the given modes, Nothing => the file must exist.
-> OpenFileFlags -> OpenFileFlags
-> IO Fd -> IO Fd
open_ str how maybe_mode (OpenFileFlags appendFlag exclusiveFlag nocttyFlag open_ str how (OpenFileFlags appendFlag exclusiveFlag nocttyFlag
nonBlockFlag truncateFlag) = do nonBlockFlag truncateFlag nofollowFlag
creatFlag cloexecFlag directoryFlag
syncFlag) = do
fd <- c_open str all_flags mode_w fd <- c_open str all_flags mode_w
return (Fd fd) return (Fd fd)
where where
all_flags = creat .|. flags .|. open_mode all_flags = creat .|. flags .|. open_mode
flags = flags =
(if appendFlag then (#const O_APPEND) else 0) .|. (if appendFlag then (#const O_APPEND) else 0) .|.
(if exclusiveFlag then (#const O_EXCL) else 0) .|. (if exclusiveFlag then (#const O_EXCL) else 0) .|.
(if nocttyFlag then (#const O_NOCTTY) else 0) .|. (if nocttyFlag then (#const O_NOCTTY) else 0) .|.
(if nonBlockFlag then (#const O_NONBLOCK) else 0) .|. (if nonBlockFlag then (#const O_NONBLOCK) else 0) .|.
(if truncateFlag then (#const O_TRUNC) else 0) (if truncateFlag then (#const O_TRUNC) else 0) .|.
(if nofollowFlag then (#const O_NOFOLLOW) else 0) .|.
(creat, mode_w) = case maybe_mode of (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) Nothing -> (0,0)
Just x -> ((#const O_CREAT), x) Just x -> ((#const O_CREAT), x)
......
...@@ -8,6 +8,13 @@ ...@@ -8,6 +8,13 @@
CRDLY(CR0,CR1,CR2,CR2), TABDLY(TAB0,TAB1,TAB2,TAB3) BSDLY(BS0,BS1), CRDLY(CR0,CR1,CR2,CR2), TABDLY(TAB0,TAB1,TAB2,TAB3) BSDLY(BS0,BS1),
VTDLY(VT0,VT1), FFDLY(FF0,FF1) 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* ## 2.7.2.2 *May 2017*
* Bundled with GHC 8.2.1 * Bundled with GHC 8.2.1
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment