2010-10-10 04:18:10 +00:00
|
|
|
{- git-annex utility functions
|
|
|
|
-}
|
|
|
|
|
2010-10-11 21:52:46 +00:00
|
|
|
module Utility (
|
|
|
|
withFileLocked,
|
|
|
|
hGetContentsStrict,
|
2010-10-15 20:09:30 +00:00
|
|
|
parentDir,
|
|
|
|
relPathCwdToDir,
|
2010-10-19 05:45:45 +00:00
|
|
|
relPathDirToDir,
|
2010-10-22 18:57:02 +00:00
|
|
|
boolSystem,
|
|
|
|
shellEscape
|
2010-10-11 21:52:46 +00:00
|
|
|
) where
|
2010-10-10 04:18:10 +00:00
|
|
|
|
|
|
|
import System.IO
|
2010-10-19 05:45:45 +00:00
|
|
|
import System.Cmd
|
|
|
|
import System.Exit
|
|
|
|
import System.Posix.Signals
|
|
|
|
import Data.Typeable
|
2010-10-10 04:18:10 +00:00
|
|
|
import System.Posix.IO
|
|
|
|
import Data.String.Utils
|
2010-10-15 20:09:30 +00:00
|
|
|
import System.Path
|
2010-10-16 17:38:59 +00:00
|
|
|
import System.IO.HVFS
|
2010-10-15 23:01:20 +00:00
|
|
|
import System.FilePath
|
2010-10-15 20:09:30 +00:00
|
|
|
import System.Directory
|
2010-10-10 04:18:10 +00:00
|
|
|
|
|
|
|
{- 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 =
|
2010-10-23 00:47:14 +00:00
|
|
|
if (not $ null dirs)
|
2010-10-15 23:01:20 +00:00
|
|
|
then slash ++ (join s $ take ((length dirs) - 1) dirs)
|
2010-10-10 04:18:10 +00:00
|
|
|
else ""
|
|
|
|
where
|
2010-10-15 23:01:20 +00:00
|
|
|
dirs = filter (\x -> length x > 0) $
|
|
|
|
split s dir
|
|
|
|
slash = if (not $ isAbsolute dir) then "" else s
|
|
|
|
s = [pathSeparator]
|
2010-10-15 20:09:30 +00:00
|
|
|
|
|
|
|
{- Constructs a relative path from the CWD to a directory.
|
|
|
|
-
|
|
|
|
- For example, assuming CWD is /tmp/foo/bar:
|
|
|
|
- relPathCwdToDir "/tmp/foo" == "../"
|
|
|
|
- relPathCwdToDir "/tmp/foo/bar" == ""
|
|
|
|
- relPathCwdToDir "/tmp/foo/bar" == ""
|
|
|
|
-}
|
|
|
|
relPathCwdToDir :: FilePath -> IO FilePath
|
|
|
|
relPathCwdToDir dir = do
|
|
|
|
cwd <- getCurrentDirectory
|
|
|
|
let absdir = abs cwd dir
|
|
|
|
return $ relPathDirToDir cwd absdir
|
|
|
|
where
|
|
|
|
-- absolute, normalized form of the directory
|
|
|
|
abs cwd dir =
|
|
|
|
case (absNormPath cwd dir) of
|
|
|
|
Just d -> d
|
|
|
|
Nothing -> error $ "unable to normalize " ++ dir
|
|
|
|
|
|
|
|
{- Constructs a relative path from one directory to another.
|
|
|
|
-
|
|
|
|
- Both directories must be absolute, and normalized (eg with absNormpath).
|
|
|
|
-
|
|
|
|
- The path will end with "/", unless it is empty.
|
2010-10-15 23:01:20 +00:00
|
|
|
-}
|
2010-10-15 20:09:30 +00:00
|
|
|
relPathDirToDir :: FilePath -> FilePath -> FilePath
|
|
|
|
relPathDirToDir from to =
|
2010-10-23 00:47:14 +00:00
|
|
|
if (not $ null path)
|
2010-10-15 23:01:20 +00:00
|
|
|
then addTrailingPathSeparator path
|
2010-10-15 20:09:30 +00:00
|
|
|
else ""
|
|
|
|
where
|
2010-10-15 23:01:20 +00:00
|
|
|
s = [pathSeparator]
|
|
|
|
pfrom = split s from
|
|
|
|
pto = split s to
|
2010-10-15 20:09:30 +00:00
|
|
|
common = map fst $ filter same $ zip pfrom pto
|
|
|
|
same (c,d) = c == d
|
|
|
|
uncommon = drop numcommon pto
|
|
|
|
dotdots = take ((length pfrom) - numcommon) $ repeat ".."
|
|
|
|
numcommon = length $ common
|
2010-10-15 23:01:20 +00:00
|
|
|
path = join s $ dotdots ++ uncommon
|
2010-10-19 05:45:45 +00:00
|
|
|
|
|
|
|
{- Run a system command, and returns True or False
|
|
|
|
- if it succeeded or failed.
|
|
|
|
-
|
|
|
|
- An error is thrown if the command exits due to SIGINT,
|
|
|
|
- to propigate ctrl-c.
|
|
|
|
-}
|
|
|
|
boolSystem :: FilePath -> [String] -> IO Bool
|
|
|
|
boolSystem command params = do
|
|
|
|
r <- rawSystem command params
|
|
|
|
case r of
|
|
|
|
ExitSuccess -> return True
|
|
|
|
ExitFailure e -> if Just e == cast sigINT
|
|
|
|
then error $ command ++ "interrupted"
|
|
|
|
else return False
|
2010-10-22 18:57:02 +00:00
|
|
|
|
|
|
|
{- Escapes a filename to be safely able to be exposed to the shell. -}
|
|
|
|
shellEscape f = "'" ++ quote ++ "'"
|
|
|
|
where
|
|
|
|
-- replace ' with '"'"'
|
|
|
|
quote = join "'\"'\"'" $ split "'" f
|