2010-10-10 04:18:10 +00:00
|
|
|
{- git-annex utility functions
|
|
|
|
-}
|
|
|
|
|
2010-10-11 21:52:46 +00:00
|
|
|
module Utility (
|
|
|
|
withFileLocked,
|
|
|
|
hGetContentsStrict,
|
|
|
|
parentDir
|
|
|
|
) where
|
2010-10-10 04:18:10 +00:00
|
|
|
|
|
|
|
import System.IO
|
|
|
|
import System.Posix.IO
|
|
|
|
import Data.String.Utils
|
|
|
|
|
|
|
|
{- Let's just say that Haskell makes reading/writing a file with
|
|
|
|
- file locking excessively difficult. -}
|
2010-10-10 15:08:40 +00:00
|
|
|
withFileLocked file mode action = do
|
|
|
|
-- TODO: find a way to use bracket here
|
2010-10-10 04:18:10 +00:00
|
|
|
handle <- openFile file mode
|
|
|
|
lockfd <- handleToFd handle -- closes handle
|
|
|
|
waitToSetLock lockfd (lockType mode, AbsoluteSeek, 0, 0)
|
|
|
|
handle' <- fdToHandle lockfd
|
2010-10-10 15:08:40 +00:00
|
|
|
ret <- action handle'
|
|
|
|
hClose handle'
|
|
|
|
return ret
|
2010-10-10 04:18:10 +00:00
|
|
|
where
|
|
|
|
lockType ReadMode = ReadLock
|
|
|
|
lockType _ = WriteLock
|
|
|
|
|
2010-10-10 06:22:35 +00:00
|
|
|
{- A version of hgetContents that is not lazy. Ensures file is
|
|
|
|
- all read before it gets closed. -}
|
|
|
|
hGetContentsStrict h = hGetContents h >>= \s -> length s `seq` return s
|
|
|
|
|
2010-10-10 04:18:10 +00:00
|
|
|
{- Returns the parent directory of a path. Parent of / is "" -}
|
|
|
|
parentDir :: String -> String
|
|
|
|
parentDir dir =
|
|
|
|
if length dirs > 0
|
2010-10-13 05:49:21 +00:00
|
|
|
then absolute ++ (join "/" $ take ((length dirs) - 1) dirs)
|
2010-10-10 04:18:10 +00:00
|
|
|
else ""
|
|
|
|
where
|
|
|
|
dirs = filter (\x -> length x > 0) $ split "/" dir
|
2010-10-13 05:49:21 +00:00
|
|
|
absolute = if ((dir !! 0) == '/') then "/" else ""
|