diff options
Diffstat (limited to 'System/Posix/Unistd.hsc')
-rw-r--r-- | System/Posix/Unistd.hsc | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/System/Posix/Unistd.hsc b/System/Posix/Unistd.hsc index 0a13d6d..afb8c08 100644 --- a/System/Posix/Unistd.hsc +++ b/System/Posix/Unistd.hsc @@ -1,3 +1,4 @@ +{-# LANGUAGE CApiFFI #-} {-# LANGUAGE NondecreasingIndentation #-} #ifdef __GLASGOW_HASKELL__ {-# LANGUAGE Trustworthy #-} @@ -27,6 +28,10 @@ module System.Posix.Unistd ( -- * Sleeping sleep, usleep, nanosleep, + -- * File synchronisation + fileSynchronise, + fileSynchroniseDataOnly, + {- ToDo from unistd.h: confstr, @@ -55,8 +60,14 @@ import Foreign.C.Error import Foreign.C.String ( peekCString ) import Foreign.C.Types import Foreign +import System.Posix.Types import System.Posix.Internals +#if !(HAVE_FSYNC && HAVE_FDATASYNC) +import System.IO.Error ( ioeSetLocation ) +import GHC.IO.Exception ( unsupportedOperation ) +#endif + -- ----------------------------------------------------------------------------- -- System environment (uname()) @@ -206,3 +217,44 @@ sysconf n = do foreign import ccall unsafe "sysconf" c_sysconf :: CInt -> IO CLong + +-- ----------------------------------------------------------------------------- +-- File synchronization + +-- | Performs @fsync(2)@ operation on file-descriptor. +-- +-- Throws 'IOError' (\"unsupported operation\") if platform does not +-- provide @fsync(2)@ (use @#if HAVE_FSYNC@ CPP guard to +-- detect availability). +fileSynchronise :: Fd -> IO () +#if HAVE_FSYNC +fileSynchronise fd = do + throwErrnoIfMinus1_ "fileSynchronise" (c_fsync fd) + +foreign import capi safe "unistd.h fsync" + c_fsync :: Fd -> IO CInt +#else +{-# WARNING fileSynchronise + "operation will throw exception (CPP guard: @#if HAVE_FSYNC@)" #-} +fileSynchronise _ = ioError (ioeSetLocation unsupportedOperation + "fileSynchronise") +#endif + +-- | Performs @fdatasync(2)@ operation on file-descriptor. +-- +-- Throws 'IOError' (\"unsupported operation\") if platform does not +-- provide @fdatasync(2)@ (use @#if HAVE_FDATASYNC@ CPP guard to +-- detect availability). +fileSynchroniseDataOnly :: Fd -> IO () +#if HAVE_FDATASYNC +fileSynchroniseDataOnly fd = do + throwErrnoIfMinus1_ "fileSynchroniseDataOnly" (c_fdatasync fd) + +foreign import capi safe "unistd.h fdatasync" + c_fdatasync :: Fd -> IO CInt +#else +{-# WARNING fileSynchroniseDataOnly + "operation will throw exception (CPP guard: @#if HAVE_FDATASYNC@)" #-} +fileSynchroniseDataOnly _ = ioError (ioeSetLocation unsupportedOperation + "fileSynchroniseDataOnly") +#endif |