use ByteString for hGet
Avoids the crazy low-level hGetBuf stuff. Also slightly faster.
This commit is contained in:
parent
899ecbfba1
commit
b089fba7b4
1 changed files with 3 additions and 17 deletions
20
Branch.hs
20
Branch.hs
|
@ -26,9 +26,7 @@ import System.Cmd.Utils
|
||||||
import Data.Maybe
|
import Data.Maybe
|
||||||
import Data.List
|
import Data.List
|
||||||
import System.IO
|
import System.IO
|
||||||
import System.IO.Unsafe
|
import qualified Data.ByteString.Char8 as B
|
||||||
import Foreign
|
|
||||||
import Data.Char
|
|
||||||
|
|
||||||
import Types.BranchState
|
import Types.BranchState
|
||||||
import qualified GitRepo as Git
|
import qualified GitRepo as Git
|
||||||
|
@ -258,23 +256,11 @@ catFile file = do
|
||||||
else do
|
else do
|
||||||
let [_sha, _type, size] = words header
|
let [_sha, _type, size] = words header
|
||||||
let bytes = read size
|
let bytes = read size
|
||||||
fp <- mallocForeignPtrBytes (fromIntegral bytes)
|
content <- B.hGet from bytes
|
||||||
len <- withForeignPtr fp $ \buf -> hGetBuf from buf (fromIntegral bytes)
|
|
||||||
when (len /= bytes) $
|
|
||||||
error "short read from git cat-file"
|
|
||||||
content <- lazySlurp fp 0 len
|
|
||||||
c <- hGetChar from
|
c <- hGetChar from
|
||||||
when (c /= '\n') $
|
when (c /= '\n') $
|
||||||
error "missing newline from git cat-file"
|
error "missing newline from git cat-file"
|
||||||
return content
|
return $ B.unpack content
|
||||||
|
|
||||||
lazySlurp :: ForeignPtr Word8 -> Int -> Int -> IO String
|
|
||||||
lazySlurp fp ix len
|
|
||||||
| ix == len = return []
|
|
||||||
| otherwise = do
|
|
||||||
c <- withForeignPtr fp $ \p -> peekElemOff p ix
|
|
||||||
cs <- unsafeInterleaveIO (lazySlurp fp (ix+1) len)
|
|
||||||
return $ chr (fromIntegral c) : cs
|
|
||||||
|
|
||||||
{- Lists all files on the branch. There may be duplicates in the list. -}
|
{- Lists all files on the branch. There may be duplicates in the list. -}
|
||||||
files :: Annex [FilePath]
|
files :: Annex [FilePath]
|
||||||
|
|
Loading…
Add table
Reference in a new issue