git-annex/Annex/LockPool.hs

55 lines
1.2 KiB
Haskell
Raw Normal View History

{- git-annex lock pool
-
- Copyright 2012 Joey Hess <joey@kitenet.net>
-
- Licensed under the GNU GPL version 3 or higher.
-}
{-# LANGUAGE CPP #-}
module Annex.LockPool where
import qualified Data.Map as M
import System.Posix.Types (Fd)
import Common.Annex
import Annex
import Annex.Perms
{- 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
#ifndef __WINDOWS__
2012-12-13 04:24:19 +00:00
mode <- annexFileMode
fd <- liftIO $ noUmask mode $
openFd file ReadOnly (Just mode) defaultFileFlags
liftIO $ waitToSetLock fd (ReadLock, AbsoluteSeek, 0, 0)
#else
liftIO $ writeFile file ""
let fd = 0
#endif
2012-12-13 04:24:19 +00:00
changePool $ M.insert file fd
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
go fd = do
#ifndef __WINDOWS__
2012-12-13 04:24:19 +00:00
liftIO $ closeFd fd
#endif
2012-12-13 04:24:19 +00:00
changePool $ M.delete file
getPool :: Annex (M.Map FilePath Fd)
getPool = getState lockpool
fromPool :: FilePath -> Annex (Maybe Fd)
fromPool file = M.lookup file <$> getPool
changePool :: (M.Map FilePath Fd -> M.Map FilePath Fd) -> Annex ()
changePool a = do
m <- getPool
changeState $ \s -> s { lockpool = a m }