From c8751be1514c1dcc24976b3b83af727c99f1637d Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Sun, 18 Nov 2012 18:27:53 -0400 Subject: [PATCH] simplify --- Remote/Directory.hs | 7 +------ Remote/Helper/Chunked.hs | 32 ++++++++++++++++---------------- Remote/WebDAV.hs | 14 +++++--------- 3 files changed, 22 insertions(+), 31 deletions(-) diff --git a/Remote/Directory.hs b/Remote/Directory.hs index 0527270a1b..a61ef83c01 100644 --- a/Remote/Directory.hs +++ b/Remote/Directory.hs @@ -180,13 +180,8 @@ retrieve :: FilePath -> ChunkSize -> Key -> AssociatedFile -> FilePath -> Annex retrieve d chunksize k _ f = metered Nothing k $ \meterupdate -> liftIO $ withStoredFiles chunksize d k $ \files -> catchBoolIO $ do - meteredWriteFileChunks meterupdate f files feeder + meteredWriteFileChunks meterupdate f files $ L.readFile return True - where - feeder [] = return ([], []) - feeder (x:xs) = do - chunks <- L.toChunks <$> L.readFile x - return (xs, chunks) retrieveEncrypted :: FilePath -> ChunkSize -> (Cipher, Key) -> Key -> FilePath -> Annex Bool retrieveEncrypted d chunksize (cipher, enck) k f = metered Nothing k $ \meterupdate -> diff --git a/Remote/Helper/Chunked.hs b/Remote/Helper/Chunked.hs index dd6e3eb0db..f6a4308b3b 100644 --- a/Remote/Helper/Chunked.hs +++ b/Remote/Helper/Chunked.hs @@ -125,21 +125,21 @@ storeChunked chunksize dests storer content = - after each chunk of the L.ByteString, typically every 64 kb or so. -} meteredWriteFile :: MeterUpdate -> FilePath -> L.ByteString -> IO () meteredWriteFile meterupdate dest b = - meteredWriteFileChunks meterupdate dest (L.toChunks b) feeder - where - feeder chunks = return ([], chunks) + meteredWriteFileChunks meterupdate dest [b] return -{- Writes a series of S.ByteString chunks to a file, updating a progress - - meter after each chunk. The feeder is called to get more chunks. -} -meteredWriteFileChunks :: MeterUpdate -> FilePath -> s -> (s -> IO (s, [S.ByteString])) -> IO () -meteredWriteFileChunks meterupdate dest startstate feeder = - E.bracket (openFile dest WriteMode) hClose (feed startstate []) +{- Writes a series of major chunks to a file. The feeder is called to get + - each major chunk. Then each chunk of the L.ByteString is written, + - with the meter updated after each chunk. -} +meteredWriteFileChunks :: MeterUpdate -> FilePath -> [v] -> (v -> IO L.ByteString) -> IO () +meteredWriteFileChunks meterupdate dest chunks feeder = + E.bracket (openFile dest WriteMode) hClose (feed chunks []) where - feed state [] h = do - (state', cs) <- feeder state - unless (null cs) $ - feed state' cs h - feed state (c:cs) h = do - S.hPut h c - meterupdate $ toInteger $ S.length c - feed state cs h + feed [] [] _ = noop + feed (c:cs) [] h = do + bs <- L.toChunks <$> feeder c + unless (null bs) $ + feed cs bs h + feed cs (b:bs) h = do + S.hPut h b + meterupdate $ toInteger $ S.length b + feed cs bs h diff --git a/Remote/WebDAV.hs b/Remote/WebDAV.hs index ea4800c9de..ed7b82b642 100644 --- a/Remote/WebDAV.hs +++ b/Remote/WebDAV.hs @@ -116,19 +116,15 @@ retrieve :: Remote -> Key -> AssociatedFile -> FilePath -> Annex Bool retrieve r k _f d = metered Nothing k $ \meterupdate -> davAction r False $ \(baseurl, user, pass) -> liftIO $ catchBoolIO $ withStoredFiles r k baseurl user pass onerr $ \urls -> do - meteredWriteFileChunks meterupdate d urls $ - feeder user pass + meteredWriteFileChunks meterupdate d urls $ \url -> do + mb <- davGetUrlContent url user pass + case mb of + Nothing -> throwIO "download failed" + Just b -> return b return True where onerr _ = return False - feeder _ _ [] = return ([], []) - feeder user pass (url:urls) = do - mb <- davGetUrlContent url user pass - case mb of - Nothing -> throwIO "download failed" - Just b -> return (urls, L.toChunks b) - retrieveEncrypted :: Remote -> (Cipher, Key) -> Key -> FilePath -> Annex Bool retrieveEncrypted r (cipher, enck) k d = metered Nothing k $ \meterupdate -> davAction r False $ \(baseurl, user, pass) -> liftIO $ catchBoolIO $