Skip to content

L.hGetContents should not close handle #343

@yaitskov

Description

@yaitskov

Data.ByteString.Lazy.hGetContents closes handle. This is not obvious and redundant, because handle is usually created in a bracket which takes care of closing handle.

hGetContents should be deprecated and
hGetContents' introduced with new semantics.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    out-of-scopeUnrelated to CLC or does not need a CLC approval

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions