haskell / haskell/bytestring

hGetContents closes handle

Open
#707 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
Haskell
Stars
301
Forks
144
Avg merge
7d 22h
Merged PRs (30d)
1

Description

hGetContents doesn't create Handle and therefore it is not obvious why it closes it. Handle is closed on a bracket.

```
data GitObject
= GitObject
{ gobHash :: !(Digest SHA1State)
, gobOrigin :: !FilePath
}
deriving (Show, Eq)

mkGitObject :: PhoenixM m => FilePath -> m (Maybe GitObject)
mkGitObject fp = do
s <- asks inHandlesSem
U.bracket_ (U.waitQSem s) (U.signalQSem s) $
U.withBinaryFile fp U.ReadMode $ \inH -> do
magicBs <- hGet inH 2
if zlibP magicBs
then do
!headerBs <- (magicBs <>) <$> hGet inH 10
(`U.catch` skipCorruptedFile inH) $ do
if gitObjectP $ Z.decompress (toLazy headerBs)
then do
!goh <- sha1 . Z.decompress . (toLazy headerBs <>) <$> hGetContents inH
pure . Just $ GitObject goh fp
else pure Nothing
else pure Nothing
where
skipCorruptedFile inH (e :: Z.DecompressError) =
case e of
Z.TruncatedInput -> do
fsz <- liftIO $ U.hFileSize inH
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.