Commit 6fd8ceff authored by Klaus Aehlig's avatar Klaus Aehlig

Move FStat related function to Utils

In this way, the functions to to decide, based on fstat, whether
a file needs to be reloaded can used by other parts as well,
in particular to monitor progress in the job queue.
Signed-off-by: default avatarKlaus Aehlig <aehlig@google.com>
Reviewed-by: default avatarPetr Pudlak <pudlak@google.com>
parent ac0c5c6d
......@@ -32,10 +32,8 @@ module Ganeti.ConfigReader
import Control.Concurrent
import Control.Exception
import Control.Monad (liftM, unless)
import Control.Monad (unless)
import Data.IORef
import System.Posix.Files
import System.Posix.Types
import System.INotify
import Ganeti.BasicTypes
......@@ -51,12 +49,6 @@ import Ganeti.Utils
-- executed.
type ConfigReader = IO (Result ConfigData)
-- | File stat identifier.
type FStat = (EpochTime, FileID, FileOffset)
-- | Null 'FStat' value.
nullFStat :: FStat
nullFStat = (-1, -1, -1)
-- | Reload model data type.
data ReloadModel = ReloadNotify -- ^ We are using notifications
......@@ -151,27 +143,6 @@ safeUpdateConfig path oldfstat save_fn =
return (nullFStat, ConfigIOError)
)
-- | Computes the file cache data from a FileStatus structure.
buildFileStatus :: FileStatus -> FStat
buildFileStatus ofs =
let modt = modificationTime ofs
inum = fileID ofs
fsize = fileSize ofs
in (modt, inum, fsize)
-- | Wrapper over 'buildFileStatus'. This reads the data from the
-- filesystem and then builds our cache structure.
getFStat :: FilePath -> IO FStat
getFStat p = liftM buildFileStatus (getFileStatus p)
-- | Check if the file needs reloading
needsReload :: FStat -> FilePath -> IO (Maybe FStat)
needsReload oldstat path = do
newstat <- getFStat path
return $ if newstat /= oldstat
then Just newstat
else Nothing
-- ** Watcher threads
-- $watcher
......
......@@ -64,6 +64,9 @@ module Ganeti.Utils
, atomicWriteFile
, tryAndLogIOError
, lockFile
, FStat
, nullFStat
, needsReload
) where
import Control.Exception (try)
......@@ -74,6 +77,7 @@ import qualified Data.Map as M
import Control.Monad (foldM, liftM)
import System.Directory (renameFile)
import System.FilePath.Posix (takeDirectory, takeBaseName)
import System.Posix.Types
import Debug.Trace
import Network.Socket
......@@ -528,3 +532,31 @@ lockFile path = do
fd <- handleToFd handle
Control.Monad.liftM (either (Bad . show) Ok)
(try (setLock fd (WriteLock, AbsoluteSeek, 0, 0)) :: IO (Either IOError ()))
-- | File stat identifier.
type FStat = (EpochTime, FileID, FileOffset)
-- | Null 'FStat' value.
nullFStat :: FStat
nullFStat = (-1, -1, -1)
-- | Computes the file cache data from a FileStatus structure.
buildFileStatus :: FileStatus -> FStat
buildFileStatus ofs =
let modt = modificationTime ofs
inum = fileID ofs
fsize = fileSize ofs
in (modt, inum, fsize)
-- | Wrapper over 'buildFileStatus'. This reads the data from the
-- filesystem and then builds our cache structure.
getFStat :: FilePath -> IO FStat
getFStat p = liftM buildFileStatus (getFileStatus p)
-- | Check if the file needs reloading
needsReload :: FStat -> FilePath -> IO (Maybe FStat)
needsReload oldstat path = do
newstat <- getFStat path
return $ if newstat /= oldstat
then Just newstat
else Nothing
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