git-annex/Backend/File.hs

101 lines
2.9 KiB
Haskell
Raw Normal View History

2010-10-10 13:47:04 -04:00
{- git-annex "file" backend
- -}
2010-10-14 03:50:28 -04:00
module Backend.File (backend) where
2010-10-10 13:47:04 -04:00
2010-10-13 21:28:47 -04:00
import Control.Monad.State
2010-10-13 16:21:50 -04:00
import System.IO
import System.Cmd
2010-10-14 13:17:43 -04:00
import System.Exit
2010-10-13 16:21:50 -04:00
import Control.Exception
2010-10-14 02:52:17 -04:00
import BackendTypes
import LocationLog
import Locations
2010-10-14 02:41:54 -04:00
import qualified Remotes
2010-10-14 02:36:41 -04:00
import qualified GitRepo as Git
2010-10-14 15:31:44 -04:00
import Utility
2010-10-14 16:13:43 -04:00
import Core
2010-10-14 17:37:20 -04:00
import qualified Annex
import UUID
2010-10-10 13:47:04 -04:00
backend = Backend {
name = "file",
2010-10-10 15:04:18 -04:00
getKey = keyValue,
2010-10-10 15:41:35 -04:00
storeFileKey = dummyStore,
retrieveKeyFile = copyKeyFile,
2010-10-14 15:31:44 -04:00
removeKey = dummyRemove,
hasKey = checkKeyFile
2010-10-10 13:47:04 -04:00
}
-- direct mapping from filename to key
2010-10-13 21:28:47 -04:00
keyValue :: FilePath -> Annex (Maybe Key)
2010-10-14 19:36:11 -04:00
keyValue file = return $ Just $ Key ((name backend), file)
2010-10-10 15:04:18 -04:00
{- This backend does not really do any independant data storage,
- it relies on the file contents in .git/annex/ in this repo,
2010-10-14 14:14:19 -04:00
- and other accessible repos. So storing a key is
2010-10-14 15:31:44 -04:00
- a no-op. -}
2010-10-13 21:28:47 -04:00
dummyStore :: FilePath -> Key -> Annex (Bool)
dummyStore file key = return True
2010-10-14 14:14:19 -04:00
{- Allow keys to be removed. -}
2010-10-13 21:28:47 -04:00
dummyRemove :: Key -> Annex Bool
2010-10-14 14:14:19 -04:00
dummyRemove url = return True
2010-10-10 15:04:18 -04:00
2010-10-14 15:31:44 -04:00
{- Just check if the .git/annex/ file for the key exists. -}
checkKeyFile :: Key -> Annex Bool
2010-10-14 19:36:11 -04:00
checkKeyFile k = inAnnex k
2010-10-14 15:31:44 -04:00
{- Try to find a copy of the file in one of the remotes,
- and copy it over to this one. -}
2010-10-13 21:28:47 -04:00
copyKeyFile :: Key -> FilePath -> Annex (Bool)
copyKeyFile key file = do
2010-10-14 02:41:54 -04:00
remotes <- Remotes.withKey key
2010-10-14 17:37:20 -04:00
if (0 == length remotes)
then cantfind
else return ()
2010-10-13 23:18:58 -04:00
trycopy remotes remotes
where
trycopy full [] = error $ "unable to get: " ++ (keyFile key) ++ "\n" ++
"To get that file, need access to one of these remotes: " ++
2010-10-14 02:41:54 -04:00
(Remotes.list full)
trycopy full (r:rs) = do
2010-10-13 22:59:43 -04:00
-- annexLocation needs the git config to have been
-- read for a remote, so do that now,
-- if it hasn't been already
result <- Remotes.tryGitConfigRead r
case (result) of
Nothing -> trycopy full rs
Just r' -> do
result <- liftIO $ (try (copyFromRemote r' key file)::IO (Either SomeException ()))
case (result) of
Left err -> do
liftIO $ hPutStrLn stderr (show err)
trycopy full rs
Right succ -> return True
2010-10-14 17:37:20 -04:00
cantfind = do
g <- Annex.gitRepo
uuids <- liftIO $ keyLocations g key
error $ "no available git remotes have: " ++
(keyFile key) ++ (uuidlist uuids)
uuidlist [] = ""
uuidlist uuids = "\nIt has been seen before in these repositories:\n" ++
prettyPrintUUIDs uuids
2010-10-13 16:21:50 -04:00
{- Tries to copy a file from a remote, exception on error. -}
2010-10-14 02:36:41 -04:00
copyFromRemote :: Git.Repo -> Key -> FilePath -> IO ()
copyFromRemote r key file = do
2010-10-14 02:36:41 -04:00
putStrLn $ "copy from " ++ (Git.repoDescribe r ) ++ " " ++ file
2010-10-13 16:32:16 -04:00
2010-10-14 02:36:41 -04:00
if (Git.repoIsLocal r)
2010-10-13 22:59:43 -04:00
then getlocal
else getremote
2010-10-13 16:21:50 -04:00
where
getlocal = do
2010-10-14 13:17:43 -04:00
res <-rawSystem "cp" ["-a", location, file]
if (res == ExitSuccess)
then return ()
else error "cp failed"
2010-10-13 22:59:43 -04:00
getremote = error "get via network not yet implemented!"
2010-10-14 19:36:11 -04:00
location = annexLocation r key