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 )
-- 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) }
......@@ -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) }
......@@ -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)
......
......@@ -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
......
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