module WASH.Utility.Locking (obtainLock, releaseLock) where
import System.Cmd (system)
import System.Directory (doesDirectoryExist)
import WASH.Utility.Auxiliary
import System.Directory (getModificationTime, removeDirectory)
import System.Time (getClockTime, diffClockTimes, tdSec)
obtainLock :: FilePath -> IO ()
releaseLock :: FilePath -> IO ()
lockPath name = name ++ ".lockdir"
obtainLock name =
assertDirectoryExists (lockPath name)
(system "sleep 1" >> obtainLockLoop name)
releaseLock name =
removeDirectory (lockPath name)
obtainLockLoop name =
let lp = lockPath name in
do b <- doesDirectoryExist lp
if b then do
mtime <- getModificationTime lp
ftime <- getModificationTime name
ctime <- getClockTime
let td = diffClockTimes ctime mtime
tf = diffClockTimes ctime ftime
if tdSec td > 60 && tdSec tf > 60
then do removeDirectory lp
obtainLock name
else do system "sleep 1"
obtainLockLoop name
else obtainLock name