summaryrefslogtreecommitdiff
path: root/lib/DotLock.hs
blob: af05f5d480f7c83de5b368a67854a9fcecf9b4f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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 ()