summaryrefslogtreecommitdiff
path: root/KeyRing.hs
diff options
context:
space:
mode:
Diffstat (limited to 'KeyRing.hs')
-rw-r--r--KeyRing.hs52
1 files changed, 51 insertions, 1 deletions
diff --git a/KeyRing.hs b/KeyRing.hs
index c6b592f..ee3e139 100644
--- a/KeyRing.hs
+++ b/KeyRing.hs
@@ -5,6 +5,7 @@
5{-# LANGUAGE DeriveFunctor #-} 5{-# LANGUAGE DeriveFunctor #-}
6{-# LANGUAGE DoAndIfThenElse #-} 6{-# LANGUAGE DoAndIfThenElse #-}
7{-# LANGUAGE NoPatternGuards #-} 7{-# LANGUAGE NoPatternGuards #-}
8{-# LANGUAGE ForeignFunctionInterface #-}
8module KeyRing 9module KeyRing
9 ( runKeyRing 10 ( runKeyRing
10 , StreamInfo(..) 11 , StreamInfo(..)
@@ -81,7 +82,7 @@ import Data.ASN1.Types ( toASN1, ASN1Object, fromASN1
81import Data.ASN1.BitArray ( BitArray(..), toBitArray ) 82import Data.ASN1.BitArray ( BitArray(..), toBitArray )
82import Data.ASN1.Encoding ( encodeASN1, encodeASN1', decodeASN1, decodeASN1' ) 83import Data.ASN1.Encoding ( encodeASN1, encodeASN1', decodeASN1, decodeASN1' )
83import Data.ASN1.BinaryEncoding ( DER(..) ) 84import Data.ASN1.BinaryEncoding ( DER(..) )
84import Data.Time.Clock.POSIX ( getPOSIXTime ) 85import Data.Time.Clock.POSIX ( getPOSIXTime, POSIXTime )
85import qualified Data.Map as Map 86import qualified Data.Map as Map
86import qualified Data.ByteString.Lazy as L ( unpack, pack, null, readFile, writeFile 87import qualified Data.ByteString.Lazy as L ( unpack, pack, null, readFile, writeFile
87 , ByteString, toChunks, hGetContents, hPut, concat ) 88 , ByteString, toChunks, hGetContents, hPut, concat )
@@ -95,6 +96,15 @@ import qualified Data.Text as T ( Text, unpack, pack,
95import qualified System.Posix.Types as Posix 96import qualified System.Posix.Types as Posix
96import System.Posix.Files ( modificationTime, getFileStatus, getFdStatus 97import System.Posix.Files ( modificationTime, getFileStatus, getFdStatus
97 , setFileCreationMask, setFileTimes ) 98 , setFileCreationMask, setFileTimes )
99#if MIN_VERSION_unix(2,7,0)
100import System.Posix.Files ( setFdTimesHiRes )
101#else
102import Foreign.C.Types ( CTime(..), CLong, CInt(..) )
103import Foreign.Marshal.Array ( withArray )
104import Foreign.Ptr
105import Foreign.C.Error ( throwErrnoIfMinus1_ )
106import Foreign.Storable
107#endif
98import System.FilePath ( takeDirectory ) 108import System.FilePath ( takeDirectory )
99import System.IO (hPutStrLn,withFile,IOMode(..)) 109import System.IO (hPutStrLn,withFile,IOMode(..))
100import Foreign.C.Types ( CTime ) 110import Foreign.C.Types ( CTime )
@@ -852,6 +862,18 @@ writeInputFileL ctx inp bs = do
852 let fname = resolveInputFile ctx inp 862 let fname = resolveInputFile ctx inp
853 mapM_ (`L.writeFile` bs) fname 863 mapM_ (`L.writeFile` bs) fname
854 864
865writeStampedL :: InputFileContext -> InputFile -> Posix.EpochTime -> L.ByteString -> IO ()
866writeStampedL ctx (FileDesc fd) stamp bs = do
867 h <- fdToHandle fd
868 L.hPut h bs
869 handleIO_ (return ())
870 $ setFdTimesHiRes fd (realToFrac stamp) (realToFrac stamp)
871writeStampedL ctx inp stamp bs = do
872 let fname = resolveInputFile ctx inp
873 forM_ fname $ \fname -> do
874 L.writeFile fname bs
875 setFileTimes fname stamp stamp
876
855 877
856getInputFileTime :: InputFileContext -> InputFile -> IO CTime 878getInputFileTime :: InputFileContext -> InputFile -> IO CTime
857getInputFileTime ctx (FileDesc fd) = do 879getInputFileTime ctx (FileDesc fd) = do
@@ -2573,3 +2595,31 @@ socketFamily :: SockAddr -> Family
2573socketFamily (SockAddrInet _ _) = AF_INET 2595socketFamily (SockAddrInet _ _) = AF_INET
2574socketFamily (SockAddrInet6 {}) = AF_INET6 2596socketFamily (SockAddrInet6 {}) = AF_INET6
2575socketFamily (SockAddrUnix _) = AF_UNIX 2597socketFamily (SockAddrUnix _) = AF_UNIX
2598
2599#if ! MIN_VERSION_unix(2,7,0)
2600setFdTimesHiRes :: Posix.Fd -> POSIXTime -> POSIXTime -> IO ()
2601setFdTimesHiRes (Posix.Fd fd) atime mtime =
2602 withArray [toCTimeSpec atime, toCTimeSpec mtime] $ \times ->
2603 throwErrnoIfMinus1_ "setFdTimesHiRes" (c_futimens fd times)
2604
2605data CTimeSpec = CTimeSpec Posix.EpochTime CLong
2606instance Storable CTimeSpec where
2607 sizeOf _ = (16)
2608 alignment _ = alignment (undefined :: CInt)
2609 poke p (CTimeSpec sec nsec) = do
2610 ((\hsc_ptr -> pokeByteOff hsc_ptr 0)) p sec
2611 ((\hsc_ptr -> pokeByteOff hsc_ptr 8)) p nsec
2612 peek p = do
2613 sec <- (\hsc_ptr -> peekByteOff hsc_ptr 0) p
2614 nsec <- (\hsc_ptr -> peekByteOff hsc_ptr 8) p
2615 return $ CTimeSpec sec nsec
2616
2617toCTimeSpec :: POSIXTime -> CTimeSpec
2618toCTimeSpec t = CTimeSpec (CTime sec) (truncate $ 10^(9::Int) * frac)
2619 where
2620 (sec, frac) = if (frac' < 0) then (sec' - 1, frac' + 1) else (sec', frac')
2621 (sec', frac') = properFraction $ toRational t
2622
2623foreign import ccall unsafe "futimens"
2624 c_futimens :: CInt -> Ptr CTimeSpec -> IO CInt
2625#endif