import {-# SOURCE #-} Module
import FastString
import Unique
+import Panic
import UniqFM
#if __GLASGOW_HASKELL__ < 503
import Data.Char ( ord, chr )
import Data.Array.Base ( unsafeRead, unsafeWrite )
import Control.Monad ( when )
-import Control.Exception ( throw )
+import Control.Exception ( throw, throwDyn )
import System.IO as IO
import System.IO.Unsafe ( unsafeInterleaveIO )
import System.IO.Error ( mkIOError, eofErrorType )
import GHC.Exts
import GHC.IOBase ( IO(..) )
import GHC.Word ( Word8(..) )
+import GHC.Handle ( openFileEx, IOModeEx(..) )
#endif
#if __GLASGOW_HASKELL__ < 503
writeBinMem :: BinHandle -> FilePath -> IO ()
writeBinMem (BinIO _ _ _) _ = error "Data.Binary.writeBinMem: not a memory handle"
writeBinMem (BinMem _ ix_r sz_r arr_r) fn = do
- h <- openFile fn WriteMode
+ h <- openFileEx fn (BinaryMode WriteMode)
arr <- readIORef arr_r
ix <- readFastMutInt ix_r
hPutArray h arr ix
readBinMem :: FilePath -> IO BinHandle
readBinMem filename = do
- h <- openFile filename ReadMode
+ h <- openFileEx filename (BinaryMode ReadMode)
filesize' <- hFileSize h
let filesize = fromIntegral filesize'
arr <- newArray_ (0,filesize-1)
getBinFileWithDict :: Binary a => FilePath -> IO a
getBinFileWithDict file_path = do
bh <- Binary.readBinMem file_path
+ magic <- get bh
+ when (magic /= binaryInterfaceMagic) $
+ throwDyn (ProgramError (
+ "magic number mismatch: old/corrupt interface file?"))
dict_p <- Binary.get bh -- get the dictionary ptr
data_p <- tellBin bh
seekBin bh dict_p
initBinMemSize = (1024*1024) :: Int
+binaryInterfaceMagic = 0x1face :: Word32
+
putBinFileWithDict :: Binary a => FilePath -> Module -> a -> IO ()
putBinFileWithDict file_path mod a = do
bh <- openBinMem initBinMemSize mod
+ put_ bh binaryInterfaceMagic
p <- tellBin bh
put_ bh p -- placeholder for ptr to dictionary
put_ bh a