{-# 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 ()