git-annex/Utility/RsyncFile.hs

70 lines
2 KiB
Haskell
Raw Normal View History

2011-10-16 04:31:25 +00:00
{- file copying with rsync
-
- 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
import Data.String.Utils
import Data.List
import Utility.SafeCommand
{- 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)]
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) ++ "'"
{- Runs rsync in server mode to send a file. -}
rsyncServerSend :: FilePath -> IO Bool
rsyncServerSend file = rsync $
rsyncServerParams ++ [Param "--sender", File file]
{- Runs rsync in server mode to receive a file. -}
rsyncServerReceive :: FilePath -> IO Bool
rsyncServerReceive file = rsync $ rsyncServerParams ++ [File file]
2011-02-28 20:25:31 +00:00
rsyncServerParams :: [CommandParam]
rsyncServerParams =
[ Param "--server"
-- preserve permissions
, Param "-p"
-- preserve timestamps
, Param "-t"
-- allow resuming of transfers of big files
, Param "--inplace"
-- other options rsync normally uses in server mode
, Params "-e.Lsf ."
]
2011-02-28 20:25:31 +00:00
rsync :: [CommandParam] -> IO Bool
rsync = boolSystem "rsync"
{- Checks if an rsync url involves the remote shell (ssh or rsh).
- Use of such urls with rsync 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
go [] = False
go (c:cs)
| c == '/' = False -- got to directory with no colon
| c == ':' = not $ ":" `isPrefixOf` cs
| otherwise = go cs
{- Checks if a rsync url is really just a local path. -}
rsyncUrlIsPath :: String -> Bool
rsyncUrlIsPath s
| rsyncUrlIsShell s = False
| otherwise = ':' `notElem` s