2012-11-16 21:58:08 +00:00
|
|
|
{- git-annex chunked remotes
|
|
|
|
-
|
|
|
|
- Copyright 2012 Joey Hess <joey@kitenet.net>
|
|
|
|
-
|
|
|
|
- Licensed under the GNU GPL version 3 or higher.
|
|
|
|
-}
|
|
|
|
|
|
|
|
module Remote.Helper.Chunked where
|
|
|
|
|
|
|
|
import Common.Annex
|
|
|
|
import Utility.DataUnits
|
|
|
|
import Types.Remote
|
2012-11-19 02:49:07 +00:00
|
|
|
import Meters
|
2012-11-16 21:58:08 +00:00
|
|
|
|
|
|
|
import qualified Data.Map as M
|
|
|
|
import qualified Data.ByteString.Lazy as L
|
|
|
|
import Data.Int
|
|
|
|
import qualified Control.Exception as E
|
|
|
|
|
|
|
|
type ChunkSize = Maybe Int64
|
|
|
|
|
|
|
|
{- Gets a remote's configured chunk size. -}
|
2012-11-30 04:55:59 +00:00
|
|
|
chunkSize :: RemoteConfig -> ChunkSize
|
|
|
|
chunkSize m =
|
2012-11-16 21:58:08 +00:00
|
|
|
case M.lookup "chunksize" m of
|
|
|
|
Nothing -> Nothing
|
|
|
|
Just v -> case readSize dataUnits v of
|
|
|
|
Nothing -> error "bad chunksize"
|
|
|
|
Just size
|
|
|
|
| size <= 0 -> error "bad chunksize"
|
|
|
|
| otherwise -> Just $ fromInteger size
|
|
|
|
|
|
|
|
{- This is an extension that's added to the usual file (or whatever)
|
|
|
|
- where the remote stores a key. -}
|
|
|
|
type ChunkExt = String
|
|
|
|
|
|
|
|
{- A record of the number of chunks used.
|
|
|
|
-
|
|
|
|
- While this can be guessed at based on the size of the key, encryption
|
|
|
|
- makes that larger. Also, using this helps deal with changes to chunksize
|
|
|
|
- over the life of a remote.
|
|
|
|
-}
|
|
|
|
chunkCount :: ChunkExt
|
|
|
|
chunkCount = ".chunkcount"
|
|
|
|
|
|
|
|
{- Parses the String from the chunkCount file, and returns the files that
|
|
|
|
- are used to store the chunks. -}
|
2012-11-17 03:16:18 +00:00
|
|
|
listChunks :: FilePath -> String -> [FilePath]
|
|
|
|
listChunks basedest chunkcount = take count $ map (basedest ++) chunkStream
|
2012-11-16 21:58:08 +00:00
|
|
|
where
|
|
|
|
count = fromMaybe 0 $ readish chunkcount
|
|
|
|
|
|
|
|
{- An infinite stream of extensions to use for chunks. -}
|
|
|
|
chunkStream :: [ChunkExt]
|
|
|
|
chunkStream = map (\n -> ".chunk" ++ show n) [1 :: Integer ..]
|
|
|
|
|
|
|
|
{- Given the base destination to use to store a value,
|
|
|
|
- generates a stream of temporary destinations (just one when not chunking)
|
|
|
|
- and passes it to an action, which should chunk and store the data,
|
2012-11-19 17:18:23 +00:00
|
|
|
- and return the destinations it stored to, or [] on error. Then
|
|
|
|
- calls the storer to write the chunk count (if chunking). Finally, the
|
|
|
|
- fianlizer is called to rename the tmp into the dest
|
|
|
|
- (and do any other cleanup).
|
2012-11-16 21:58:08 +00:00
|
|
|
-}
|
2012-11-19 17:18:23 +00:00
|
|
|
storeChunks :: Key -> FilePath -> FilePath -> ChunkSize -> ([FilePath] -> IO [FilePath]) -> (FilePath -> String -> IO ()) -> (FilePath -> FilePath -> IO ()) -> IO Bool
|
|
|
|
storeChunks key tmp dest chunksize storer recorder finalizer =
|
2012-11-16 21:58:08 +00:00
|
|
|
either (const $ return False) return
|
|
|
|
=<< (E.try go :: IO (Either E.SomeException Bool))
|
|
|
|
where
|
|
|
|
go = do
|
|
|
|
stored <- storer tmpdests
|
|
|
|
when (chunksize /= Nothing) $ do
|
2012-11-19 17:18:23 +00:00
|
|
|
let chunkcount = basef ++ chunkCount
|
2012-11-16 21:58:08 +00:00
|
|
|
recorder chunkcount (show $ length stored)
|
2012-11-19 17:18:23 +00:00
|
|
|
finalizer tmp dest
|
2012-11-16 21:58:08 +00:00
|
|
|
return (not $ null stored)
|
|
|
|
|
2012-11-19 17:18:23 +00:00
|
|
|
basef = tmp ++ keyFile key
|
2012-11-16 21:58:08 +00:00
|
|
|
tmpdests
|
2012-11-19 17:18:23 +00:00
|
|
|
| chunksize == Nothing = [basef]
|
|
|
|
| otherwise = map (basef ++ ) chunkStream
|
2012-11-16 21:58:08 +00:00
|
|
|
|
|
|
|
{- Given a list of destinations to use, chunks the data according to the
|
|
|
|
- ChunkSize, and runs the storer action to store each chunk. Returns
|
|
|
|
- the destinations where data was stored, or [] on error.
|
|
|
|
-
|
|
|
|
- This buffers each chunk in memory.
|
|
|
|
- More optimal versions of this can be written, that rely
|
|
|
|
- on L.toChunks to split the lazy bytestring into chunks (typically
|
|
|
|
- smaller than the ChunkSize), and eg, write those chunks to a Handle.
|
|
|
|
- But this is the best that can be done with the storer interface that
|
|
|
|
- writes a whole L.ByteString at a time.
|
|
|
|
-}
|
|
|
|
storeChunked :: ChunkSize -> [FilePath] -> (FilePath -> L.ByteString -> IO ()) -> L.ByteString -> IO [FilePath]
|
|
|
|
storeChunked chunksize dests storer content =
|
|
|
|
either (const $ return []) return
|
|
|
|
=<< (E.try (go chunksize dests) :: IO (Either E.SomeException [FilePath]))
|
|
|
|
where
|
|
|
|
go _ [] = return [] -- no dests!?
|
|
|
|
|
|
|
|
go Nothing (d:_) = do
|
|
|
|
storer d content
|
|
|
|
return [d]
|
|
|
|
|
|
|
|
go (Just sz) _
|
|
|
|
-- always write a chunk, even if the data is 0 bytes
|
|
|
|
| L.null content = go Nothing dests
|
|
|
|
| otherwise = storechunks sz [] dests content
|
|
|
|
|
|
|
|
storechunks _ _ [] _ = return [] -- ran out of dests
|
|
|
|
storechunks sz useddests (d:ds) b
|
|
|
|
| L.null b = return $ reverse useddests
|
|
|
|
| otherwise = do
|
|
|
|
let (chunk, b') = L.splitAt sz b
|
|
|
|
storer d chunk
|
|
|
|
storechunks sz (d:useddests) ds b'
|
2012-11-17 03:16:18 +00:00
|
|
|
|
2012-11-19 02:49:07 +00:00
|
|
|
{- Writes a series of chunks to a file. The feeder is called to get
|
|
|
|
- each chunk. -}
|
2012-11-18 22:27:53 +00:00
|
|
|
meteredWriteFileChunks :: MeterUpdate -> FilePath -> [v] -> (v -> IO L.ByteString) -> IO ()
|
|
|
|
meteredWriteFileChunks meterupdate dest chunks feeder =
|
2012-11-19 02:49:07 +00:00
|
|
|
withBinaryFile dest WriteMode $ \h ->
|
|
|
|
forM_ chunks $ \c ->
|
|
|
|
meteredWrite meterupdate h =<< feeder c
|