2012-09-19 18:28:32 +00:00
|
|
|
{- various rsync stuff
|
2010-12-31 17:39:30 +00:00
|
|
|
-
|
2013-05-14 17:24:15 +00:00
|
|
|
- Copyright 2010-2013 Joey Hess <joey@kitenet.net>
|
2010-12-31 17:39:30 +00:00
|
|
|
-
|
2014-05-10 14:01:27 +00:00
|
|
|
- License: BSD-2-clause
|
2010-12-31 17:39:30 +00:00
|
|
|
-}
|
|
|
|
|
2014-12-30 19:04:36 +00:00
|
|
|
{-# LANGUAGE CPP #-}
|
|
|
|
|
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
|
2013-03-28 21:03:04 +00:00
|
|
|
import Utility.Metered
|
2012-09-19 19:21:52 +00:00
|
|
|
|
|
|
|
import Data.Char
|
2013-03-29 00:34:07 +00:00
|
|
|
import System.Console.GetOpt
|
2013-03-30 23:05:51 +00:00
|
|
|
import Data.Tuple.Utils
|
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)]
|
2012-12-13 04:24:19 +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. -}
|
2013-04-23 00:24:53 +00:00
|
|
|
escape s = "'" ++ intercalate "''" (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. -}
|
2013-03-29 00:34:07 +00:00
|
|
|
rsyncServerSend :: [CommandParam] -> FilePath -> IO Bool
|
|
|
|
rsyncServerSend options file = rsync $
|
|
|
|
rsyncServerParams ++ Param "--sender" : options ++ [File file]
|
2010-12-31 17:39:30 +00:00
|
|
|
|
|
|
|
{- Runs rsync in server mode to receive a file. -}
|
2013-03-29 00:34:07 +00:00
|
|
|
rsyncServerReceive :: [CommandParam] -> FilePath -> IO Bool
|
|
|
|
rsyncServerReceive options file = rsync $
|
|
|
|
rsyncServerParams ++ options ++ [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"
|
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
|
|
|
]
|
|
|
|
|
2013-05-09 17:49:47 +00:00
|
|
|
rsyncUseDestinationPermissions :: CommandParam
|
|
|
|
rsyncUseDestinationPermissions = Param "--chmod=ugo=rwX"
|
|
|
|
|
2011-02-28 20:25:31 +00:00
|
|
|
rsync :: [CommandParam] -> IO Bool
|
2013-05-14 17:24:15 +00:00
|
|
|
rsync = boolSystem "rsync" . rsyncParamsFixup
|
|
|
|
|
|
|
|
{- On Windows, rsync is from Cygwin, and expects to get Cygwin formatted
|
|
|
|
- paths to files. (It thinks that C:foo refers to a host named "C").
|
2014-12-30 19:04:36 +00:00
|
|
|
- Fix up the Params appropriately. -}
|
2013-05-14 17:24:15 +00:00
|
|
|
rsyncParamsFixup :: [CommandParam] -> [CommandParam]
|
2014-12-30 19:04:36 +00:00
|
|
|
#ifdef mingw32_HOST_OS
|
2013-05-14 17:24:15 +00:00
|
|
|
rsyncParamsFixup = map fixup
|
|
|
|
where
|
2014-10-09 18:53:13 +00:00
|
|
|
fixup (File f) = File (toCygPath f)
|
2014-12-30 19:04:36 +00:00
|
|
|
fixup (Param s)
|
|
|
|
| rsyncUrlIsPath s = Param (toCygPath s)
|
2013-05-14 17:24:15 +00:00
|
|
|
fixup p = p
|
2014-12-30 19:04:36 +00:00
|
|
|
#else
|
|
|
|
rsyncParamsFixup = id
|
|
|
|
#endif
|
2010-12-31 17:39:30 +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
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
|
|
|
-- host::dir is rsync protocol, while host:dir is ssh/rsh
|
|
|
|
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
|
2014-12-30 19:04:36 +00:00
|
|
|
#ifdef mingw32_HOST_OS
|
|
|
|
| not (null (takeDrive s)) = True
|
|
|
|
#endif
|
2012-07-22 17:48:50 +00:00
|
|
|
| rsyncUrlIsShell s = False
|
|
|
|
| otherwise = ':' `notElem` s
|
2012-09-19 19:21:52 +00:00
|
|
|
|
2014-12-17 17:21:55 +00:00
|
|
|
{- Runs rsync, but intercepts its progress output and updates a meter.
|
|
|
|
- The progress output is also output to stdout.
|
2012-09-19 19:21:52 +00:00
|
|
|
-
|
2014-12-17 17:21:55 +00:00
|
|
|
- The params must enable rsync's --progress mode for this to work.
|
|
|
|
-}
|
|
|
|
rsyncProgress :: MeterUpdate -> [CommandParam] -> IO Bool
|
2014-12-30 17:57:49 +00:00
|
|
|
rsyncProgress meterupdate = commandMeter parseRsyncProgress meterupdate "rsync" . rsyncParamsFixup
|
2014-12-17 17:21:55 +00:00
|
|
|
|
|
|
|
{- Strategy: Look for chunks prefixed with \r (rsync writes a \r before
|
2012-09-19 19:21:52 +00:00
|
|
|
- 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.
|
2014-04-10 19:36:51 +00:00
|
|
|
-
|
|
|
|
- In some locales, the number will have one or more commas in the middle
|
|
|
|
- of it.
|
2012-09-19 19:21:52 +00:00
|
|
|
-}
|
2014-12-17 17:21:55 +00:00
|
|
|
parseRsyncProgress :: ProgressParser
|
2012-09-19 19:21:52 +00:00
|
|
|
parseRsyncProgress = go [] . reverse . progresschunks
|
2012-12-13 04:24:19 +00:00
|
|
|
where
|
|
|
|
go remainder [] = (Nothing, remainder)
|
|
|
|
go remainder (x:xs) = case parsebytes (findbytesstart x) of
|
|
|
|
Nothing -> go (delim:x++remainder) xs
|
2014-12-17 17:21:55 +00:00
|
|
|
Just b -> (Just (toBytesProcessed b), remainder)
|
2012-09-19 19:21:52 +00:00
|
|
|
|
2012-12-13 04:24:19 +00:00
|
|
|
delim = '\r'
|
2014-12-17 17:21:55 +00:00
|
|
|
|
2012-12-13 04:24:19 +00:00
|
|
|
{- 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
|
2014-12-17 17:21:55 +00:00
|
|
|
|
|
|
|
parsebytes :: String -> Maybe Integer
|
2012-12-13 04:24:19 +00:00
|
|
|
parsebytes s = case break isSpace s of
|
|
|
|
([], _) -> Nothing
|
|
|
|
(_, []) -> Nothing
|
2014-04-10 19:36:51 +00:00
|
|
|
(b, _) -> readish $ filter (/= ',') b
|
2013-03-29 00:34:07 +00:00
|
|
|
|
2013-03-30 23:05:51 +00:00
|
|
|
{- Filters options to those that are safe to pass to rsync in server mode,
|
|
|
|
- without causing it to eg, expose files. -}
|
|
|
|
filterRsyncSafeOptions :: [String] -> [String]
|
|
|
|
filterRsyncSafeOptions = fst3 . getOpt Permute
|
|
|
|
[ Option [] ["bwlimit"] (reqArgLong "bwlimit") "" ]
|
2013-03-29 00:34:07 +00:00
|
|
|
where
|
|
|
|
reqArgLong x = ReqArg (\v -> "--" ++ x ++ "=" ++ v) ""
|