error handling

This commit is contained in:
Joey Hess 2010-10-13 03:20:05 -04:00
parent d1071bd1fe
commit 4b801b265a
2 changed files with 34 additions and 67 deletions

View file

@ -45,10 +45,9 @@ startAnnex = do
- the annex directory and setting up the symlink pointing to its content. -} - the annex directory and setting up the symlink pointing to its content. -}
annexFile :: State -> FilePath -> IO () annexFile :: State -> FilePath -> IO ()
annexFile state file = do annexFile state file = do
-- TODO check if already annexed r <- lookupFile file
let alreadyannexed = Nothing case (r) of
case (alreadyannexed) of Just _ -> error $ "already annexed " ++ file
Just _ -> error $ "already annexed: " ++ file
Nothing -> do Nothing -> do
checkLegal file checkLegal file
stored <- storeFile state file stored <- storeFile state file
@ -84,16 +83,14 @@ annexFile state file = do
{- Inverse of annexFile. -} {- Inverse of annexFile. -}
unannexFile :: State -> FilePath -> IO () unannexFile :: State -> FilePath -> IO ()
unannexFile state file = do unannexFile state file = do
-- TODO check if already annexed r <- lookupFile file
let alreadyannexed = Just 1 case (r) of
case (alreadyannexed) of
Nothing -> error $ "not annexed " ++ file Nothing -> error $ "not annexed " ++ file
Just _ -> do Just (key, backend) -> do
key <- fileKey file dropped <- dropFile state backend key
dropped <- dropFile state key if (not dropped)
case (dropped) of then error $ "backend refused to drop " ++ file
Nothing -> return () else do
Just (key, backend) -> do
let src = annexLocation state backend key let src = annexLocation state backend key
removeFile file removeFile file
gitRun (repo state) ["rm", file] gitRun (repo state) ["rm", file]
@ -110,20 +107,17 @@ unannexFile state file = do
{- Transfers the file from a remote. -} {- Transfers the file from a remote. -}
annexGetFile :: State -> FilePath -> IO () annexGetFile :: State -> FilePath -> IO ()
annexGetFile state file = do annexGetFile state file = do
-- TODO check if already annexed r <- lookupFile file
let alreadyannexed = Just 1 case (r) of
case (alreadyannexed) of
Nothing -> error $ "not annexed " ++ file Nothing -> error $ "not annexed " ++ file
Just _ -> do Just (key, backend) -> do
key <- fileKey file
backend <- fileBackend file
inannex <- inAnnex state backend key inannex <- inAnnex state backend key
if (inannex) if (inannex)
then return () then return ()
else do else do
let dest = annexLocation state backend key let dest = annexLocation state backend key
createDirectoryIfMissing True (parentDir dest) createDirectoryIfMissing True (parentDir dest)
success <- retrieveFile state key dest success <- retrieveFile state backend key dest
if (success) if (success)
then do then do
logStatus state key ValuePresent logStatus state key ValuePresent

View file

@ -14,14 +14,13 @@
- -} - -}
module Backend ( module Backend (
lookupBackend,
storeFile, storeFile,
dropFile, dropFile,
retrieveFile, retrieveFile,
fileKey, lookupFile
fileBackend
) where ) where
import Control.Exception
import System.Directory import System.Directory
import System.FilePath import System.FilePath
import Data.String.Utils import Data.String.Utils
@ -51,50 +50,24 @@ storeFile' (b:bs) state file = do
{- Attempts to retrieve an key from one of the backends, saving it to {- Attempts to retrieve an key from one of the backends, saving it to
- a specified location. -} - a specified location. -}
retrieveFile :: State -> Key -> FilePath -> IO Bool retrieveFile :: State -> Backend -> Key -> FilePath -> IO Bool
retrieveFile state key dest = do retrieveFile state backend key dest = (retrieveKeyFile backend) state key dest
result <- lookupBackend state key
case (result) of
Nothing -> return False
Just backend -> (retrieveKeyFile backend) state key dest
{- Drops a key from the backend that has it. -} {- Drops a key from a backend. -}
dropFile :: State -> Key -> IO (Maybe (Key, Backend)) dropFile :: State -> Backend -> Key -> IO Bool
dropFile state key = do dropFile state backend key = (removeKey backend) state key
result <- lookupBackend state key
case (result) of
Nothing -> return Nothing
Just backend -> do
(removeKey backend) state key
return $ Just (key, backend)
{- Looks up the backend that has a key. -} {- Looks up the key and backend corresponding to an annexed file,
lookupBackend :: State -> Key -> IO (Maybe Backend)
lookupBackend state key = lookupBackend' (backends state) state key
lookupBackend' [] _ _ = return Nothing
lookupBackend' (b:bs) state key = do
present <- checkBackend b state key
if present
then
return $ Just b
else
lookupBackend' bs state key
{- Checks if a key is available via a given backend. -}
checkBackend :: Backend -> State -> Key -> IO (Bool)
checkBackend backend state key =
doesFileExist $ annexLocation state backend key
{- Looks up the key corresponding to an annexed file,
- by examining what the file symlinks to. -} - by examining what the file symlinks to. -}
fileKey :: FilePath -> IO Key lookupFile :: FilePath -> IO (Maybe (Key, Backend))
fileKey file = do lookupFile file = do
l <- readSymbolicLink (file) result <- try (lookup)::IO (Either SomeException (Maybe (Key, Backend)))
return $ Key $ takeFileName $ l case (result) of
Left err -> return Nothing
{- Looks up the backend corresponding to an annexed file, Right succ -> return succ
- by examining what the file symlinks to. -} where
fileBackend :: FilePath -> IO Backend lookup = do
fileBackend file = do l <- readSymbolicLink file
l <- readSymbolicLink (file) return $ Just (k l, b l)
return $ lookupBackendName $ takeFileName $ parentDir $ l k l = Key $ takeFileName $ l
b l = lookupBackendName $ takeFileName $ parentDir $ l