2011-10-16 04:31:25 +00:00
|
|
|
{- file copying with rsync
|
2010-12-31 17:39:30 +00:00
|
|
|
-
|
|
|
|
- Copyright 2010 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
2011-07-06 00:24:10 +00:00
|
|
|
module Utility.RsyncFile where
|
2010-12-31 17:39:30 +00:00
|
|
|
|
2010-12-31 23:09:17 +00:00
|
|
|
import Data.String.Utils
|
2011-11-18 16:53:48 +00:00
|
|
|
import Data.List
|
2010-12-31 23:09:17 +00:00
|
|
|
|
2011-08-22 20:14:12 +00:00
|
|
|
import Utility.SafeCommand
|
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
|
|
|
|
|
|
|
{- Runs rsync in server mode to send a file, and exits. -}
|
|
|
|
rsyncServerSend :: FilePath -> IO ()
|
2011-02-25 05:13:01 +00:00
|
|
|
rsyncServerSend file = rsyncExec $
|
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
|
|
|
|
2011-02-28 20:25:31 +00:00
|
|
|
rsyncExec :: [CommandParam] -> IO ()
|
|
|
|
rsyncExec params = executeFile "rsync" True (toCommand params) Nothing
|
2011-11-18 16:53:48 +00:00
|
|
|
|
|
|
|
{- Checks if an rsync url involves the remote shell (ssh or rsh).
|
|
|
|
- Use of such urls with rsync or rsyncExec requires additional shell
|
|
|
|
- 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
|