aboutsummaryrefslogtreecommitdiffhomepage
path: root/System
diff options
context:
space:
mode:
authorGravatar Herbert Valerio Riedel <hvr@gnu.org>2014-12-07 15:29:10 +0100
committerGravatar Herbert Valerio Riedel <hvr@gnu.org>2014-12-07 15:36:59 +0100
commit98eced86549def54dfb5057ef984a02c720be763 (patch)
treefb39e3abd988550c9590a3a66ea1912528924b78 /System
parent256b19184bcb05c3cd9a6061730b7d67d61c0763 (diff)
Wrap fsync(2) and fdatasync(2)
This adds two new functions in `System.Posix.Unistd` - `fileSynchronise` (aka `fsync(2)`), and - `fileSynchroniseDataOnly` (aka `fdatasync(2)`) This is based on part of #7 and has been heavily refactored from its original patch submission by Ricardo Catalinas Jiménez. This also bumps version to 2.7.1.0 as a minor version bump is now needed. Signed-off-by: Herbert Valerio Riedel <hvr@gnu.org>
Diffstat (limited to 'System')
-rw-r--r--System/Posix/Unistd.hsc52
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