module Storage.Hashed.Plain( readPlainTree, writePlainTree,
plainTreeIO
) where
import Data.Maybe( catMaybes )
import qualified Data.ByteString as BS
import qualified Data.ByteString.Char8 as BS8
import qualified Data.ByteString.Lazy as BL
import System.FilePath( (</>) )
import System.Directory( getDirectoryContents
, createDirectoryIfMissing )
import Bundled.Posix( getFileStatus, isDirectory, isRegularFile, FileStatus )
import Control.Monad( forM_ )
import Storage.Hashed.AnchoredPath
import Storage.Hashed.Utils
import Storage.Hashed.Hash( Hash( NoHash) )
import Storage.Hashed.Tree( Tree(), TreeItem(..)
, Blob(..), makeTree
, list, readBlob, find, modifyTree, expand )
import Storage.Hashed.Monad( TreeIO, runTreeMonad, initialState, tree, replaceItem )
import qualified Data.Set as S
import Control.Monad.State( liftIO, gets, modify )
readPlainDir :: FilePath -> IO [(FilePath, FileStatus)]
readPlainDir dir =
withCurrentDirectory dir $ do
items <- getDirectoryContents "."
sequence [ do st <- getFileStatus s
return (s, st)
| s <- items, s `notElem` [ ".", ".." ] ]
readPlainTree :: FilePath -> IO (Tree IO)
readPlainTree dir = do
items <- readPlainDir dir
let subs = catMaybes [
let name = Name (BS8.pack name')
in case status of
_ | isDirectory status -> Just (name, Stub (readPlainTree (dir </> name')) NoHash)
_ | isRegularFile status -> Just (name, File $ Blob (readBlob' name) NoHash)
_ -> Nothing
| (name', status) <- items ]
return $ makeTree subs
where readBlob' (Name name) = readSegment (dir </> BS8.unpack name, Nothing)
writePlainTree :: Tree IO -> FilePath -> IO ()
writePlainTree t dir = do
createDirectoryIfMissing True dir
expand t >>= mapM_ write . list
where write (p, File b) = write' p b
write (p, SubTree _) =
createDirectoryIfMissing True (anchorPath dir p)
write _ = return ()
write' p b = readBlob b >>= BL.writeFile (anchorPath dir p)
plainTreeIO :: TreeIO a -> Tree IO -> FilePath -> IO (a, Tree IO)
plainTreeIO action t dir = runTreeMonad action $ initialState t (\_ -> return NoHash) updatePlain
where updatePlain path (File b) =
do liftIO $ createDirectoryIfMissing True (anchorPath "" $ parent path)
liftIO $ readBlob b >>= BL.writeFile (anchorPath "" path)
return $ File $ Blob (BL.readFile $ anchorPath "" path) NoHash
updatePlain _ x = return x