summaryrefslogtreecommitdiff
path: root/Meters.hs
blob: 0ea5d3af61f0165d4ad269ab588f39c8a29417e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
{- git-annex meters
 -
 - Copyright 2012 Joey Hess <joey@kitenet.net>
 -
 - Licensed under the GNU GPL version 3 or higher.
 -}

module Meters where

import Common
import Types.Meters
import Utility.Observed

import qualified Data.ByteString.Lazy as L

{- Sends the content of a file to an action, updating the meter as it's
 - consumed. -}
withMeteredFile :: FilePath -> MeterUpdate -> (L.ByteString -> IO a) -> IO a
withMeteredFile f meterupdate a = withBinaryFile f ReadMode $ \h ->
	hGetContentsObserved h (meterupdate . toInteger) >>= a

{- Sends the content of a file to a Handle, updating the meter as it's
 - written. -}
sendMeteredFile :: FilePath -> MeterUpdate -> Handle -> IO ()
sendMeteredFile f meterupdate h = withMeteredFile f meterupdate $ L.hPut h