Commit 32b07c5f authored by Klaus Aehlig's avatar Klaus Aehlig

Provide means of locking a file

Two avoid two processes simultaneously accessing the same
on-file structure, like the job queue, file locks are used.
Therefore, provide this functionality in Haskell as well.
Signed-off-by: default avatarKlaus Aehlig <aehlig@google.com>
Reviewed-by: default avatarMichele Tartara <mtartara@google.com>
parent 02926b4b
......@@ -63,6 +63,7 @@ module Ganeti.Utils
, formatOrdinal
, atomicWriteFile
, tryAndLogIOError
, lockFile
) where
import Control.Exception (try)
......@@ -70,7 +71,7 @@ import Data.Char (toUpper, isAlphaNum, isDigit, isSpace)
import Data.Function (on)
import Data.List
import qualified Data.Map as M
import Control.Monad (foldM)
import Control.Monad (foldM, liftM)
import System.Directory (renameFile)
import System.FilePath.Posix (takeDirectory, takeBaseName)
......@@ -84,6 +85,7 @@ import Ganeti.Runtime
import System.IO
import System.Exit
import System.Posix.Files
import System.Posix.IO
import System.Time
-- * Debug functions
......@@ -517,3 +519,12 @@ atomicWriteFile path contents = do
hPutStr tmphandle contents
hClose tmphandle
renameFile tmppath path
-- | Attempt, in a non-blocking way, to obtain a lock on a given file; report
-- back success.
lockFile :: FilePath -> IO (Result ())
lockFile path = do
handle <- openFile path WriteMode
fd <- handleToFd handle
Control.Monad.liftM (either (Bad . show) Ok)
(try (setLock fd (WriteLock, AbsoluteSeek, 0, 0)) :: IO (Either IOError ()))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment