Revision 32b07c5f

b/src/Ganeti/Utils.hs
63 63
  , formatOrdinal
64 64
  , atomicWriteFile
65 65
  , tryAndLogIOError
66
  , lockFile
66 67
  ) where
67 68

  
68 69
import Control.Exception (try)
......
70 71
import Data.Function (on)
71 72
import Data.List
72 73
import qualified Data.Map as M
73
import Control.Monad (foldM)
74
import Control.Monad (foldM, liftM)
74 75
import System.Directory (renameFile)
75 76
import System.FilePath.Posix (takeDirectory, takeBaseName)
76 77

  
......
84 85
import System.IO
85 86
import System.Exit
86 87
import System.Posix.Files
88
import System.Posix.IO
87 89
import System.Time
88 90

  
89 91
-- * Debug functions
......
517 519
  hPutStr tmphandle contents
518 520
  hClose tmphandle
519 521
  renameFile tmppath path
522

  
523
-- | Attempt, in a non-blocking way, to obtain a lock on a given file; report
524
-- back success.
525
lockFile :: FilePath -> IO (Result ())
526
lockFile path = do
527
  handle <- openFile path WriteMode
528
  fd <- handleToFd handle
529
  Control.Monad.liftM (either (Bad . show) Ok)
530
    (try (setLock fd (WriteLock, AbsoluteSeek, 0, 0)) :: IO (Either IOError ()))

Also available in: Unified diff