Skip to content

hGetContents closes handle #707

@yaitskov

Description

@yaitskov

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions