git-annex/Command/DropUnused.hs
Joey Hess e6da7eb177 Improved temp file handling
* Improved temp file handling. Transfers of content can now be resumed
  from temp files later; the resume does not have to be the immediate
  next git-annex run.
* unused: Include partially transferred content in the list.
2011-01-28 14:10:50 -04:00

61 lines
1.4 KiB
Haskell

{- git-annex command
-
- Copyright 2010 Joey Hess <joey@kitenet.net>
-
- Licensed under the GNU GPL version 3 or higher.
-}
module Command.DropUnused where
import Control.Monad (when)
import Control.Monad.State (liftIO)
import qualified Data.Map as M
import System.Directory
import Command
import Types
import Messages
import Locations
import qualified Annex
import qualified Command.Drop
import Backend
command :: [Command]
command = [Command "dropunused" (paramRepeating paramNumber) seek
"drop unused file content"]
seek :: [CommandSeek]
seek = [withStrings start]
{- Drops unused content by number. -}
start :: CommandStartString
start s = do
m <- readUnusedLog
case M.lookup s m of
Nothing -> return Nothing
Just key -> do
g <- Annex.gitRepo
showStart "dropunused" s
backend <- keyBackend key
-- drop both content in the backend and any tmp
-- file for the key
let tmp = gitAnnexTmpLocation g key
tmp_exists <- liftIO $ doesFileExist tmp
when tmp_exists $ liftIO $ removeFile tmp
return $ Just $ Command.Drop.perform key backend (Just 0)
readUnusedLog :: Annex (M.Map String Key)
readUnusedLog = do
g <- Annex.gitRepo
let f = gitAnnexUnusedLog g
e <- liftIO $ doesFileExist f
if e
then do
l <- liftIO $ readFile f
return $ M.fromList $ map parse $ lines l
else return $ M.empty
where
parse line = (head ws, tokey $ unwords $ tail ws)
where
ws = words line
tokey s = read s :: Key