From fbf425fbef1c1e60fcdddfbd9b25976162725f97 Mon Sep 17 00:00:00 2001 From: joe Date: Sun, 24 Apr 2016 18:43:00 -0400 Subject: Refactored build of executable and library. --- lib/DotLock.hs | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 lib/DotLock.hs (limited to 'lib/DotLock.hs') diff --git a/lib/DotLock.hs b/lib/DotLock.hs new file mode 100644 index 0000000..af05f5d --- /dev/null +++ b/lib/DotLock.hs @@ -0,0 +1,45 @@ +{-# LANGUAGE ForeignFunctionInterface #-} +module DotLock + ( DotLock + , Flags + , dotlock_init + , dotlock_create + , dotlock_take + , dotlock_release + , dotlock_destroy + , dotlock_remove_lockfiles + , dotlock_set_fd + , dotlock_get_fd + , dotlock_disable + ) where + +import System.Posix.Types (Fd(..)) +import Foreign.C.String +import Foreign.C.Types +import Foreign.Ptr + +newtype DotLock = DotLockPtr (Ptr ()) +type Flags = Int + +foreign import ccall "dotlock_create" _dotlock_create_ptr :: Ptr Char -> Flags -> IO (Ptr ()) + +foreign import ccall "dotlock_create" _dotlock_create :: CString -> Flags -> IO (Ptr ()) + +dotlock_init :: IO () +dotlock_init = do + null_ptr <- _dotlock_create_ptr nullPtr 0 + return () + +dotlock_create :: FilePath -> Flags -> IO (Maybe DotLock) +dotlock_create file flags = do + ptr <- withCString file (flip _dotlock_create flags) + if ptr == nullPtr then return Nothing else return (Just $ DotLockPtr ptr) + + +foreign import ccall "dotlock_take" dotlock_take :: DotLock -> CLong -> IO CInt +foreign import ccall "dotlock_release" dotlock_release :: DotLock -> IO CInt +foreign import ccall "dotlock_destroy" dotlock_destroy :: DotLock -> IO () +foreign import ccall "dotlock_remove_lockfiles" dotlock_remove_lockfiles ::DotLock -> IO () +foreign import ccall "dotlock_set_fd" dotlock_set_fd :: DotLock -> Fd -> IO () +foreign import ccall "dotlock_get_fd" dotlock_get_fd :: DotLock -> IO Fd +foreign import ccall "dotlock_disable" dotlock_disable :: IO () -- cgit v1.2.3