module Core where import Prelude hiding ( readFile ) import System.IO.Strict import System.FilePath import System.Directory import Control.Applicative import Data.Time.Clock import Data.Time.Format import System.Locale import Storage.Hashed.Plain import Storage.Hashed.Darcs import Storage.Hashed.Hash import Storage.Hashed.Tree import Storage.Hashed.AnchoredPath import qualified Data.ByteString.Char8 as BSC data Backup = Backup { root :: Hash , date :: String , path :: String } deriving Show hencode = encodeBase64u hdecode = decodeBase64u showtime = formatTime defaultTimeLocale "%F.%R" readrepo = map (wibble . words) . lines where wibble [h, d, p] = Backup (hdecode $ BSC.pack h) d p wibble x = error $ show x repo path = getrepo path `catch` \_ -> makerepo path listrepo r = putStr $ unlines . map line $ r where line (Backup h d p) = unwords [BSC.unpack $ hencode h, d, p] listdir r h = do t <- expand =<< readDarcsHashed r (Nothing, h) putStr $ unlines [ unwords [ BSC.unpack $ hencode $ itemHash i , anchorPath "" p ] | (p, i) <- list t ] {- catpath r h p = do t <- expand =<< readDarcsHashed r (Nothing, h) case findBlob t p of Nothing -> fail $ showPath p ++ " does not exist." -} writerepo r to = do putStrLn $ "writing repo to " ++ to createDirectoryIfMissing False to writeFile (to "list") (unlines $ map line r) >> return r where line (Backup r d p) = unwords [BSC.unpack $ hencode r, d, p] makerepo path = writerepo [] path getrepo path = readrepo `fmap` (readFile $ path "list") make from to = do f <- darcsUpdateHashes =<< expand =<< readPlainTree from hash <- writeDarcsHashed f to dt <- getCurrentTime path <- canonicalizePath from r <- repo to writerepo (r ++ [Backup hash (showtime dt) path]) to putStrLn $ "new backup root: " ++ (BSC.unpack $ hencode hash) restore repo to root = do createDirectory to t <- expand =<< readDarcsHashed repo (Nothing, root) writePlainTree t to