2012-09-19 18:28:32 +00:00
|
|
|
{- various rsync stuff
|
2010-12-31 17:39:30 +00:00
|
|
|
-
|
2012-09-19 18:28:32 +00:00
|
|
|
- Copyright 2010-2012 Joey Hess <joey@kitenet.net>
|
2010-12-31 17:39:30 +00:00
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2012-09-19 18:28:32 +00:00
|
|
|
module Utility.Rsync where
|
2010-12-31 17:39:30 +00:00
|
|
|
|
2012-09-19 20:55:08 +00:00
|
|
|
import Common
|
2012-09-19 19:21:52 +00:00
|
|
|
|
|
|
|
import Data.Char
|
2010-12-31 23:09:17 +00:00
|
|
|
|
|
|
|
{- Generates parameters to make rsync use a specified command as its remote
|
|
|
|
- shell. -}
|
2011-02-28 20:25:31 +00:00
|
|
|
rsyncShell :: [CommandParam] -> [CommandParam]
|
|
|
|
rsyncShell command = [Param "-e", Param $ unwords $ map escape (toCommand command)]
|
2010-12-31 23:09:17 +00:00
|
|
|
where
|
|
|
|
{- rsync requires some weird, non-shell like quoting in
|
|
|
|
- here. A doubled single quote inside the single quoted
|
|
|
|
- string is a single quote. -}
|
2011-07-15 16:47:14 +00:00
|
|
|
escape s = "'" ++ join "''" (split "'" s) ++ "'"
|
2010-12-31 17:39:30 +00:00
|
|
|
|
2012-07-02 05:31:10 +00:00
|
|
|
{- Runs rsync in server mode to send a file. -}
|
|
|
|
rsyncServerSend :: FilePath -> IO Bool
|
|
|
|
rsyncServerSend file = rsync $
|
2011-02-28 20:10:16 +00:00
|
|
|
rsyncServerParams ++ [Param "--sender", File file]
|
2010-12-31 17:39:30 +00:00
|
|
|
|
|
|
|
{- Runs rsync in server mode to receive a file. -}
|
|
|
|
rsyncServerReceive :: FilePath -> IO Bool
|
2011-02-28 20:10:16 +00:00
|
|
|
rsyncServerReceive file = rsync $ rsyncServerParams ++ [File file]
|
2010-12-31 17:39:30 +00:00
|
|
|
|
2011-02-28 20:25:31 +00:00
|
|
|
rsyncServerParams :: [CommandParam]
|
2010-12-31 17:39:30 +00:00
|
|
|
rsyncServerParams =
|
2011-02-28 20:10:16 +00:00
|
|
|
[ Param "--server"
|
|
|
|
-- preserve permissions
|
|
|
|
, Param "-p"
|
2011-11-04 23:38:17 +00:00
|
|
|
-- preserve timestamps
|
|
|
|
, Param "-t"
|
2011-02-28 20:10:16 +00:00
|
|
|
-- allow resuming of transfers of big files
|
|
|
|
, Param "--inplace"
|
|
|
|
-- other options rsync normally uses in server mode
|
|
|
|
, Params "-e.Lsf ."
|
2010-12-31 17:39:30 +00:00
|
|
|
]
|
|
|
|
|
2011-02-28 20:25:31 +00:00
|
|
|
rsync :: [CommandParam] -> IO Bool
|
2011-02-28 20:10:16 +00:00
|
|
|
rsync = boolSystem "rsync"
|
2010-12-31 17:39:30 +00:00
|
|
|
|
2012-09-19 20:55:08 +00:00
|
|
|
{- Runs rsync, but intercepts its progress output and feeds bytes
|
|
|
|
- complete values into the callback. The progress output is also output
|
2012-09-20 17:46:07 +00:00
|
|
|
- to stdout.
|
|
|
|
-
|
|
|
|
- The params must enable rsync's --progress mode for this to work.
|
|
|
|
-}
|
2012-09-19 20:55:08 +00:00
|
|
|
rsyncProgress :: (Integer -> IO ()) -> [CommandParam] -> IO Bool
|
2012-10-17 04:39:45 +00:00
|
|
|
rsyncProgress callback params = do
|
|
|
|
r <- withHandle StdoutHandle createProcessSuccess p (feedprogress 0 [])
|
|
|
|
{- For an unknown reason, piping rsync's output like this does
|
|
|
|
- causes it to run a second ssh process, which it neglects to wait
|
|
|
|
- on. Reap the resulting zombie. -}
|
|
|
|
reapZombies
|
|
|
|
return r
|
2012-09-19 20:55:08 +00:00
|
|
|
where
|
|
|
|
p = proc "rsync" (toCommand params)
|
2012-09-20 21:30:38 +00:00
|
|
|
feedprogress prev buf h = do
|
2012-09-20 20:01:31 +00:00
|
|
|
s <- hGetSomeString h 80
|
|
|
|
if null s
|
|
|
|
then return True
|
|
|
|
else do
|
|
|
|
putStr s
|
2012-09-19 20:55:08 +00:00
|
|
|
hFlush stdout
|
2012-09-20 20:01:31 +00:00
|
|
|
let (mbytes, buf') = parseRsyncProgress (buf++s)
|
2012-09-20 21:30:38 +00:00
|
|
|
case mbytes of
|
|
|
|
Nothing -> feedprogress prev buf' h
|
|
|
|
(Just bytes) -> do
|
|
|
|
when (bytes /= prev) $
|
|
|
|
callback bytes
|
|
|
|
feedprogress bytes buf' h
|
2012-09-19 20:55:08 +00:00
|
|
|
|
2011-11-18 16:53:48 +00:00
|
|
|
{- Checks if an rsync url involves the remote shell (ssh or rsh).
|
2012-07-02 05:31:10 +00:00
|
|
|
- Use of such urls with rsync requires additional shell
|
2011-11-18 16:53:48 +00:00
|
|
|
- escaping. -}
|
|
|
|
rsyncUrlIsShell :: String -> Bool
|
|
|
|
rsyncUrlIsShell s
|
|
|
|
| "rsync://" `isPrefixOf` s = False
|
|
|
|
| otherwise = go s
|
|
|
|
where
|
2012-05-02 15:43:30 +00:00
|
|
|
-- host::dir is rsync protocol, while host:dir is ssh/rsh
|
2011-11-18 16:53:48 +00:00
|
|
|
go [] = False
|
|
|
|
go (c:cs)
|
|
|
|
| c == '/' = False -- got to directory with no colon
|
|
|
|
| c == ':' = not $ ":" `isPrefixOf` cs
|
|
|
|
| otherwise = go cs
|
2012-07-22 17:48:50 +00:00
|
|
|
|
|
|
|
{- Checks if a rsync url is really just a local path. -}
|
|
|
|
rsyncUrlIsPath :: String -> Bool
|
|
|
|
rsyncUrlIsPath s
|
|
|
|
| rsyncUrlIsShell s = False
|
|
|
|
| otherwise = ':' `notElem` s
|
2012-09-19 19:21:52 +00:00
|
|
|
|
|
|
|
{- Parses the String looking for rsync progress output, and returns
|
|
|
|
- Maybe the number of bytes rsynced so far, and any any remainder of the
|
|
|
|
- string that could be an incomplete progress output. That remainder
|
|
|
|
- should be prepended to future output, and fed back in. This interface
|
|
|
|
- allows the output to be read in any desired size chunk, or even one
|
|
|
|
- character at a time.
|
|
|
|
-
|
|
|
|
- Strategy: Look for chunks prefixed with \r (rsync writes a \r before
|
|
|
|
- the first progress output, and each thereafter). The first number
|
|
|
|
- after the \r is the number of bytes processed. After the number,
|
|
|
|
- there must appear some whitespace, or we didn't get the whole number,
|
|
|
|
- and return the \r and part we did get, for later processing.
|
|
|
|
-}
|
|
|
|
parseRsyncProgress :: String -> (Maybe Integer, String)
|
|
|
|
parseRsyncProgress = go [] . reverse . progresschunks
|
|
|
|
where
|
2012-09-19 21:10:13 +00:00
|
|
|
go remainder [] = (Nothing, remainder)
|
|
|
|
go remainder (x:xs) = case parsebytes (findbytesstart x) of
|
|
|
|
Nothing -> go (delim:x++remainder) xs
|
|
|
|
Just b -> (Just b, remainder)
|
2012-09-19 19:21:52 +00:00
|
|
|
|
|
|
|
delim = '\r'
|
|
|
|
{- Find chunks that each start with delim.
|
|
|
|
- The first chunk doesn't start with it
|
|
|
|
- (it's empty when delim is at the start of the string). -}
|
|
|
|
progresschunks = drop 1 . split [delim]
|
|
|
|
findbytesstart s = dropWhile isSpace s
|
|
|
|
parsebytes s = case break isSpace s of
|
|
|
|
([], _) -> Nothing
|
|
|
|
(_, []) -> Nothing
|
|
|
|
(b, _) -> readish b
|