aboutsummaryrefslogtreecommitdiff
path: root/Commands.hs
diff options
context:
space:
mode:
authorGravatar Joey Hess <joey@kitenet.net>2010-10-14 17:37:20 -0400
committerGravatar Joey Hess <joey@kitenet.net>2010-10-14 17:38:13 -0400
commit508a3b65ed675c9322940578614f088ea2c74e7f (patch)
treec3564f59aee2351d8bd7e8ce42ade05f8207e6b8 /Commands.hs
parentaa2f4bd81049e3bcaad6f5f1334864ce14887527 (diff)
annex.numcopies works
Diffstat (limited to 'Commands.hs')
-rw-r--r--Commands.hs41
1 files changed, 40 insertions, 1 deletions
diff --git a/Commands.hs b/Commands.hs
index 730663b0d..6128b76aa 100644
--- a/Commands.hs
+++ b/Commands.hs
@@ -8,6 +8,7 @@ import System.Posix.Files
import System.Directory
import Data.String.Utils
import List
+import IO
import qualified GitRepo as Git
import qualified Annex
import Utility
@@ -18,6 +19,7 @@ import UUID
import LocationLog
import Types
import Core
+import qualified Remotes
options :: [OptDescr (String -> Annex ())]
options =
@@ -138,7 +140,7 @@ wantCmd file = do error "not implemented" -- TODO
{- Indicates a file is not wanted. -}
dropCmd :: FilePath -> Annex ()
dropCmd file = notinBackend file err $ \(key, backend) -> do
- -- TODO only remove if enough copies are present elsewhere
+ requireEnoughCopies key
success <- Backend.removeKey backend key
if (success)
then do
@@ -181,3 +183,40 @@ inBackend file yes no = do
Just v -> yes v
Nothing -> no
notinBackend file yes no = inBackend file no yes
+
+{- Checks remotes to verify that enough copies of a key exist to allow
+ - for a key to be safely removed (with no data loss), and fails with an
+ - error if not. -}
+requireEnoughCopies :: Key -> Annex ()
+requireEnoughCopies key = do
+ g <- Annex.gitRepo
+ let numcopies = read $ Git.configGet g config "1"
+ remotes <- Remotes.withKey key
+ if (numcopies > length remotes)
+ then error $ "I only know about " ++ (show $ length remotes) ++
+ " out of " ++ (show numcopies) ++
+ " necessary copies of: " ++ (keyFile key) ++
+ unsafe
+ else findcopies numcopies remotes []
+ where
+ findcopies 0 _ _ = return () -- success, enough copies found
+ findcopies _ [] bad = die bad
+ findcopies n (r:rs) bad = do
+ result <- liftIO $ try $ haskey r
+ case (result) of
+ Right True -> findcopies (n-1) rs bad
+ Left _ -> findcopies n rs (r:bad)
+ haskey r = do
+ -- To check if a remote has a key, construct a new
+ -- Annex monad and query its backend.
+ a <- Annex.new r
+ (result, _) <- Annex.run a (Backend.hasKey key)
+ return result
+ die bad =
+ error $ "I failed to find enough other copies of: " ++
+ (keyFile key) ++ "\n" ++
+ "I was unable to access these remotes: " ++
+ (Remotes.list bad) ++ unsafe
+ unsafe = "\n -- According to the " ++ config ++
+ " setting, it is not safe to remove it!"
+ config = "annex.numcopies"