diff options
Diffstat (limited to 'compiler/utils/Fingerprint.hsc')
-rw-r--r-- | compiler/utils/Fingerprint.hsc | 45 |
1 files changed, 41 insertions, 4 deletions
diff --git a/compiler/utils/Fingerprint.hsc b/compiler/utils/Fingerprint.hsc index 95f31c08bb..9a55e385b3 100644 --- a/compiler/utils/Fingerprint.hsc +++ b/compiler/utils/Fingerprint.hsc @@ -1,25 +1,33 @@ -- ---------------------------------------------------------------------------- --- +-- -- (c) The University of Glasgow 2006 -- -- Fingerprints for recompilation checking and ABI versioning. -- --- http://hackage.haskell.org/trac/ghc/wiki/Commentary/Compiler/RecompilationAvoidance +-- http://ghc.haskell.org/trac/ghc/wiki/Commentary/Compiler/RecompilationAvoidance -- -- ---------------------------------------------------------------------------- -{-# OPTIONS_GHC -fno-warn-orphans #-} module Fingerprint ( Fingerprint(..), fingerprint0, readHexFingerprint, fingerprintData, - fingerprintString + fingerprintString, + -- Re-exported from GHC.Fingerprint for GHC >= 7.7, local otherwise + getFileHash ) where #include "md5.h" ##include "HsVersions.h" import Numeric ( readHex ) +#if __GLASGOW_HASKELL__ < 707 +-- Only needed for getFileHash below. +import Foreign +import Panic +import System.IO +import Control.Monad ( when ) +#endif import GHC.Fingerprint @@ -30,3 +38,32 @@ readHexFingerprint s = Fingerprint w1 w2 [(w1,"")] = readHex s1 [(w2,"")] = readHex (take 16 s2) + +#if __GLASGOW_HASKELL__ < 707 +-- Only use this if we're smaller than GHC 7.7, otherwise +-- GHC.Fingerprint exports a better version of this function. + +-- | Computes the hash of a given file. +-- It loads the full file into memory an does not work with files bigger than +-- MAXINT. +getFileHash :: FilePath -> IO Fingerprint +getFileHash path = withBinaryFile path ReadMode $ \h -> do + + fileSize <- toIntFileSize `fmap` hFileSize h + + allocaBytes fileSize $ \bufPtr -> do + n <- hGetBuf h bufPtr fileSize + when (n /= fileSize) readFailedError + fingerprintData bufPtr fileSize + + where + toIntFileSize :: Integer -> Int + toIntFileSize size + | size > fromIntegral (maxBound :: Int) = throwGhcException $ + Sorry $ "Fingerprint.getFileHash: Tried to calculate hash of file " + ++ path ++ " with size > maxBound :: Int. This is not supported." + | otherwise = fromIntegral size + + readFailedError = throwGhcException $ + Panic $ "Fingerprint.getFileHash: hGetBuf failed on interface file" +#endif |