2012-01-20 19:34:52 +00:00
|
|
|
{- git-annex lock pool
|
|
|
|
-
|
2014-01-28 18:17:14 +00:00
|
|
|
- Copyright 2012, 2014 Joey Hess <joey@kitenet.net>
|
2012-01-20 19:34:52 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2013-05-11 20:03:00 +00:00
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
|
2012-01-20 19:34:52 +00:00
|
|
|
module Annex.LockPool where
|
|
|
|
|
|
|
|
import Common.Annex
|
|
|
|
import Annex
|
2014-01-28 18:17:14 +00:00
|
|
|
import Types.LockPool
|
|
|
|
|
|
|
|
import qualified Data.Map as M
|
|
|
|
|
2013-08-04 17:12:18 +00:00
|
|
|
#ifndef mingw32_HOST_OS
|
2012-04-21 20:59:49 +00:00
|
|
|
import Annex.Perms
|
2014-01-28 18:17:14 +00:00
|
|
|
#else
|
|
|
|
import Utility.WinLock
|
2013-08-04 17:12:18 +00:00
|
|
|
#endif
|
2012-01-20 19:34:52 +00:00
|
|
|
|
|
|
|
{- Create a specified lock file, and takes a shared lock. -}
|
|
|
|
lockFile :: FilePath -> Annex ()
|
|
|
|
lockFile file = go =<< fromPool file
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
|
|
|
go (Just _) = noop -- already locked
|
|
|
|
go Nothing = do
|
2013-08-02 16:27:32 +00:00
|
|
|
#ifndef mingw32_HOST_OS
|
2012-12-13 04:24:19 +00:00
|
|
|
mode <- annexFileMode
|
2014-01-28 18:17:14 +00:00
|
|
|
lockhandle <- liftIO $ noUmask mode $
|
2012-12-13 04:24:19 +00:00
|
|
|
openFd file ReadOnly (Just mode) defaultFileFlags
|
2014-01-28 18:17:14 +00:00
|
|
|
liftIO $ waitToSetLock lockhandle (ReadLock, AbsoluteSeek, 0, 0)
|
2013-05-11 20:03:00 +00:00
|
|
|
#else
|
2014-01-28 18:17:14 +00:00
|
|
|
lockhandle <- liftIO $ waitToLock $ lockShared file
|
2013-05-11 20:03:00 +00:00
|
|
|
#endif
|
2014-01-28 18:17:14 +00:00
|
|
|
changePool $ M.insert file lockhandle
|
2012-01-20 19:34:52 +00:00
|
|
|
|
|
|
|
unlockFile :: FilePath -> Annex ()
|
2012-04-22 03:32:33 +00:00
|
|
|
unlockFile file = maybe noop go =<< fromPool file
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
2014-01-28 18:17:14 +00:00
|
|
|
go lockhandle = do
|
2013-08-02 16:27:32 +00:00
|
|
|
#ifndef mingw32_HOST_OS
|
2014-01-28 18:17:14 +00:00
|
|
|
liftIO $ closeFd lockhandle
|
|
|
|
#else
|
|
|
|
liftIO $ dropLock lockhandle
|
2013-05-11 20:03:00 +00:00
|
|
|
#endif
|
2012-12-13 04:24:19 +00:00
|
|
|
changePool $ M.delete file
|
2012-01-20 19:34:52 +00:00
|
|
|
|
2014-01-28 18:17:14 +00:00
|
|
|
getPool :: Annex LockPool
|
2012-01-20 19:34:52 +00:00
|
|
|
getPool = getState lockpool
|
|
|
|
|
2014-01-28 18:17:14 +00:00
|
|
|
fromPool :: FilePath -> Annex (Maybe LockHandle)
|
2012-01-20 19:34:52 +00:00
|
|
|
fromPool file = M.lookup file <$> getPool
|
|
|
|
|
2014-01-28 18:17:14 +00:00
|
|
|
changePool :: (LockPool -> LockPool) -> Annex ()
|
2012-01-20 19:34:52 +00:00
|
|
|
changePool a = do
|
|
|
|
m <- getPool
|
|
|
|
changeState $ \s -> s { lockpool = a m }
|