diff options
Diffstat (limited to 'KeyRing.hs')
-rw-r--r-- | KeyRing.hs | 52 |
1 files changed, 51 insertions, 1 deletions
@@ -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 #-} | ||
8 | module KeyRing | 9 | module KeyRing |
9 | ( runKeyRing | 10 | ( runKeyRing |
10 | , StreamInfo(..) | 11 | , StreamInfo(..) |
@@ -81,7 +82,7 @@ import Data.ASN1.Types ( toASN1, ASN1Object, fromASN1 | |||
81 | import Data.ASN1.BitArray ( BitArray(..), toBitArray ) | 82 | import Data.ASN1.BitArray ( BitArray(..), toBitArray ) |
82 | import Data.ASN1.Encoding ( encodeASN1, encodeASN1', decodeASN1, decodeASN1' ) | 83 | import Data.ASN1.Encoding ( encodeASN1, encodeASN1', decodeASN1, decodeASN1' ) |
83 | import Data.ASN1.BinaryEncoding ( DER(..) ) | 84 | import Data.ASN1.BinaryEncoding ( DER(..) ) |
84 | import Data.Time.Clock.POSIX ( getPOSIXTime ) | 85 | import Data.Time.Clock.POSIX ( getPOSIXTime, POSIXTime ) |
85 | import qualified Data.Map as Map | 86 | import qualified Data.Map as Map |
86 | import qualified Data.ByteString.Lazy as L ( unpack, pack, null, readFile, writeFile | 87 | import 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, | |||
95 | import qualified System.Posix.Types as Posix | 96 | import qualified System.Posix.Types as Posix |
96 | import System.Posix.Files ( modificationTime, getFileStatus, getFdStatus | 97 | import System.Posix.Files ( modificationTime, getFileStatus, getFdStatus |
97 | , setFileCreationMask, setFileTimes ) | 98 | , setFileCreationMask, setFileTimes ) |
99 | #if MIN_VERSION_unix(2,7,0) | ||
100 | import System.Posix.Files ( setFdTimesHiRes ) | ||
101 | #else | ||
102 | import Foreign.C.Types ( CTime(..), CLong, CInt(..) ) | ||
103 | import Foreign.Marshal.Array ( withArray ) | ||
104 | import Foreign.Ptr | ||
105 | import Foreign.C.Error ( throwErrnoIfMinus1_ ) | ||
106 | import Foreign.Storable | ||
107 | #endif | ||
98 | import System.FilePath ( takeDirectory ) | 108 | import System.FilePath ( takeDirectory ) |
99 | import System.IO (hPutStrLn,withFile,IOMode(..)) | 109 | import System.IO (hPutStrLn,withFile,IOMode(..)) |
100 | import Foreign.C.Types ( CTime ) | 110 | import 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 | ||
865 | writeStampedL :: InputFileContext -> InputFile -> Posix.EpochTime -> L.ByteString -> IO () | ||
866 | writeStampedL 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) | ||
871 | writeStampedL 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 | ||
856 | getInputFileTime :: InputFileContext -> InputFile -> IO CTime | 878 | getInputFileTime :: InputFileContext -> InputFile -> IO CTime |
857 | getInputFileTime ctx (FileDesc fd) = do | 879 | getInputFileTime ctx (FileDesc fd) = do |
@@ -2573,3 +2595,31 @@ socketFamily :: SockAddr -> Family | |||
2573 | socketFamily (SockAddrInet _ _) = AF_INET | 2595 | socketFamily (SockAddrInet _ _) = AF_INET |
2574 | socketFamily (SockAddrInet6 {}) = AF_INET6 | 2596 | socketFamily (SockAddrInet6 {}) = AF_INET6 |
2575 | socketFamily (SockAddrUnix _) = AF_UNIX | 2597 | socketFamily (SockAddrUnix _) = AF_UNIX |
2598 | |||
2599 | #if ! MIN_VERSION_unix(2,7,0) | ||
2600 | setFdTimesHiRes :: Posix.Fd -> POSIXTime -> POSIXTime -> IO () | ||
2601 | setFdTimesHiRes (Posix.Fd fd) atime mtime = | ||
2602 | withArray [toCTimeSpec atime, toCTimeSpec mtime] $ \times -> | ||
2603 | throwErrnoIfMinus1_ "setFdTimesHiRes" (c_futimens fd times) | ||
2604 | |||
2605 | data CTimeSpec = CTimeSpec Posix.EpochTime CLong | ||
2606 | instance 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 | |||
2617 | toCTimeSpec :: POSIXTime -> CTimeSpec | ||
2618 | toCTimeSpec 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 | |||
2623 | foreign import ccall unsafe "futimens" | ||
2624 | c_futimens :: CInt -> Ptr CTimeSpec -> IO CInt | ||
2625 | #endif | ||