{-# LANGUAGE CPP #-}
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Distribution.Compat.Time
( ModTime(..)
, getModTime, getFileAge, getCurTime
, posixSecondsToModTime
, calibrateMtimeChangeDelay )
where
import Prelude ()
import Distribution.Compat.Prelude
import System.Directory ( getModificationTime )
import Distribution.Simple.Utils ( withTempDirectory )
import Distribution.Verbosity ( silent )
import System.FilePath
import Data.Time.Clock.POSIX ( POSIXTime, getPOSIXTime )
import Data.Time ( diffUTCTime, getCurrentTime )
import Data.Time.Clock.POSIX ( posixDayLength )
#if defined mingw32_HOST_OS
import qualified Prelude
import Data.Bits ((.|.), unsafeShiftL)
#if MIN_VERSION_base(4,7,0)
import Data.Bits (finiteBitSize)
#else
import Data.Bits (bitSize)
#endif
import Foreign ( allocaBytes, peekByteOff )
import System.IO.Error ( mkIOError, doesNotExistErrorType )
import System.Win32.Types ( BOOL, DWORD, LPCTSTR, LPVOID, withTString )
#else
import System.Posix.Files ( FileStatus, getFileStatus )
#if MIN_VERSION_unix(2,6,0)
import System.Posix.Files ( modificationTimeHiRes )
#else
import System.Posix.Files ( modificationTime )
#endif
#endif
newtype ModTime = ModTime Word64
deriving (Get ModTime
[ModTime] -> Put
ModTime -> Put
forall t. (t -> Put) -> Get t -> ([t] -> Put) -> Binary t
putList :: [ModTime] -> Put
$cputList :: [ModTime] -> Put
get :: Get ModTime
$cget :: Get ModTime
put :: ModTime -> Put
$cput :: ModTime -> Put
Binary, forall x. Rep ModTime x -> ModTime
forall x. ModTime -> Rep ModTime x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep ModTime x -> ModTime
$cfrom :: forall x. ModTime -> Rep ModTime x
Generic, ModTime
forall a. a -> a -> Bounded a
maxBound :: ModTime
$cmaxBound :: ModTime
minBound :: ModTime
$cminBound :: ModTime
Bounded, ModTime -> ModTime -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: ModTime -> ModTime -> Bool
$c/= :: ModTime -> ModTime -> Bool
== :: ModTime -> ModTime -> Bool
$c== :: ModTime -> ModTime -> Bool
Eq, Eq ModTime
ModTime -> ModTime -> Bool
ModTime -> ModTime -> Ordering
ModTime -> ModTime -> ModTime
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: ModTime -> ModTime -> ModTime
$cmin :: ModTime -> ModTime -> ModTime
max :: ModTime -> ModTime -> ModTime
$cmax :: ModTime -> ModTime -> ModTime
>= :: ModTime -> ModTime -> Bool
$c>= :: ModTime -> ModTime -> Bool
> :: ModTime -> ModTime -> Bool
$c> :: ModTime -> ModTime -> Bool
<= :: ModTime -> ModTime -> Bool
$c<= :: ModTime -> ModTime -> Bool
< :: ModTime -> ModTime -> Bool
$c< :: ModTime -> ModTime -> Bool
compare :: ModTime -> ModTime -> Ordering
$ccompare :: ModTime -> ModTime -> Ordering
Ord, Typeable)
instance Structured ModTime
instance Show ModTime where
show :: ModTime -> String
show (ModTime Word64
x) = forall a. Show a => a -> String
show Word64
x
instance Read ModTime where
readsPrec :: Int -> ReadS ModTime
readsPrec Int
p String
str = forall a b. (a -> b) -> [a] -> [b]
map (forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (b, d) (c, d)
first Word64 -> ModTime
ModTime) (forall a. Read a => Int -> ReadS a
readsPrec Int
p String
str)
getModTime :: FilePath -> IO ModTime
#if defined mingw32_HOST_OS
getModTime path = allocaBytes size_WIN32_FILE_ATTRIBUTE_DATA $ \info -> do
res <- getFileAttributesEx path info
if not res
then do
let err = mkIOError doesNotExistErrorType
"Distribution.Compat.Time.getModTime"
Nothing (Just path)
ioError err
else do
dwLow <- peekByteOff info
index_WIN32_FILE_ATTRIBUTE_DATA_ftLastWriteTime_dwLowDateTime
dwHigh <- peekByteOff info
index_WIN32_FILE_ATTRIBUTE_DATA_ftLastWriteTime_dwHighDateTime
#if MIN_VERSION_base(4,7,0)
let qwTime =
(fromIntegral (dwHigh :: DWORD) `unsafeShiftL` finiteBitSize dwHigh)
.|. (fromIntegral (dwLow :: DWORD))
#else
let qwTime =
(fromIntegral (dwHigh :: DWORD) `unsafeShiftL` bitSize dwHigh)
.|. (fromIntegral (dwLow :: DWORD))
#endif
return $! ModTime (qwTime :: Word64)
#ifdef x86_64_HOST_ARCH
#define CALLCONV ccall
#else
#define CALLCONV stdcall
#endif
foreign import CALLCONV "windows.h GetFileAttributesExW"
c_getFileAttributesEx :: LPCTSTR -> Int32 -> LPVOID -> Prelude.IO BOOL
getFileAttributesEx :: String -> LPVOID -> IO BOOL
getFileAttributesEx path lpFileInformation =
withTString path $ \c_path ->
c_getFileAttributesEx c_path getFileExInfoStandard lpFileInformation
getFileExInfoStandard :: Int32
getFileExInfoStandard = 0
size_WIN32_FILE_ATTRIBUTE_DATA :: Int
size_WIN32_FILE_ATTRIBUTE_DATA = 36
index_WIN32_FILE_ATTRIBUTE_DATA_ftLastWriteTime_dwLowDateTime :: Int
index_WIN32_FILE_ATTRIBUTE_DATA_ftLastWriteTime_dwLowDateTime = 20
index_WIN32_FILE_ATTRIBUTE_DATA_ftLastWriteTime_dwHighDateTime :: Int
index_WIN32_FILE_ATTRIBUTE_DATA_ftLastWriteTime_dwHighDateTime = 24
#else
getModTime :: String -> IO ModTime
getModTime String
path = do
FileStatus
st <- String -> IO FileStatus
getFileStatus String
path
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$! (FileStatus -> ModTime
extractFileTime FileStatus
st)
extractFileTime :: FileStatus -> ModTime
FileStatus
x = NominalDiffTime -> ModTime
posixTimeToModTime (FileStatus -> NominalDiffTime
modificationTimeHiRes FileStatus
x)
#endif
windowsTick, secToUnixEpoch :: Word64
windowsTick :: Word64
windowsTick = Word64
10000000
secToUnixEpoch :: Word64
secToUnixEpoch = Word64
11644473600
posixSecondsToModTime :: Int64 -> ModTime
posixSecondsToModTime :: Int64 -> ModTime
posixSecondsToModTime Int64
s =
Word64 -> ModTime
ModTime forall a b. (a -> b) -> a -> b
$ ((forall a b. (Integral a, Num b) => a -> b
fromIntegral Int64
s :: Word64) forall a. Num a => a -> a -> a
+ Word64
secToUnixEpoch) forall a. Num a => a -> a -> a
* Word64
windowsTick
posixTimeToModTime :: POSIXTime -> ModTime
posixTimeToModTime :: NominalDiffTime -> ModTime
posixTimeToModTime NominalDiffTime
p = Word64 -> ModTime
ModTime forall a b. (a -> b) -> a -> b
$ (forall a b. (RealFrac a, Integral b) => a -> b
ceiling forall a b. (a -> b) -> a -> b
$ NominalDiffTime
p forall a. Num a => a -> a -> a
* NominalDiffTime
1e7)
forall a. Num a => a -> a -> a
+ (Word64
secToUnixEpoch forall a. Num a => a -> a -> a
* Word64
windowsTick)
getFileAge :: FilePath -> IO Double
getFileAge :: String -> IO Double
getFileAge String
file = do
UTCTime
t0 <- String -> IO UTCTime
getModificationTime String
file
UTCTime
t1 <- IO UTCTime
getCurrentTime
forall (m :: * -> *) a. Monad m => a -> m a
return forall a b. (a -> b) -> a -> b
$ forall a b. (Real a, Fractional b) => a -> b
realToFrac (UTCTime
t1 UTCTime -> UTCTime -> NominalDiffTime
`diffUTCTime` UTCTime
t0) forall a. Fractional a => a -> a -> a
/ forall a b. (Real a, Fractional b) => a -> b
realToFrac NominalDiffTime
posixDayLength
getCurTime :: IO ModTime
getCurTime :: IO ModTime
getCurTime = NominalDiffTime -> ModTime
posixTimeToModTime forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` IO NominalDiffTime
getPOSIXTime
calibrateMtimeChangeDelay :: IO (Int, Int)
calibrateMtimeChangeDelay :: IO (Int, Int)
calibrateMtimeChangeDelay =
forall a. Verbosity -> String -> String -> (String -> IO a) -> IO a
withTempDirectory Verbosity
silent String
"." String
"calibration-" forall a b. (a -> b) -> a -> b
$ \String
dir -> do
let fileName :: String
fileName = String
dir String -> ShowS
</> String
"probe"
[Int]
mtimes <- forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
t a -> (a -> f b) -> f (t b)
for [Int
1..Int
25] forall a b. (a -> b) -> a -> b
$ \(Int
i::Int) -> IO () -> IO Int
time forall a b. (a -> b) -> a -> b
$ do
String -> String -> IO ()
writeFile String
fileName forall a b. (a -> b) -> a -> b
$ forall a. Show a => a -> String
show Int
i
ModTime
t0 <- String -> IO ModTime
getModTime String
fileName
let spin :: a -> IO ()
spin a
j = do
String -> String -> IO ()
writeFile String
fileName forall a b. (a -> b) -> a -> b
$ forall a. Show a => a -> String
show (Int
i,a
j)
ModTime
t1 <- String -> IO ModTime
getModTime String
fileName
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (ModTime
t0 forall a. Ord a => a -> a -> Bool
< ModTime
t1) (a -> IO ()
spin forall a b. (a -> b) -> a -> b
$ a
j forall a. Num a => a -> a -> a
+ a
1)
forall {a}. (Show a, Num a) => a -> IO ()
spin (Int
0::Int)
let mtimeChange :: Int
mtimeChange = forall (t :: * -> *) a. (Foldable t, Ord a) => t a -> a
maximum [Int]
mtimes
mtimeChange' :: Int
mtimeChange' = forall a. Ord a => a -> a -> a
min Int
1000000 forall a b. (a -> b) -> a -> b
$ (forall a. Ord a => a -> a -> a
max Int
10000 Int
mtimeChange) forall a. Num a => a -> a -> a
* Int
2
forall (m :: * -> *) a. Monad m => a -> m a
return (Int
mtimeChange, Int
mtimeChange')
where
time :: IO () -> IO Int
time :: IO () -> IO Int
time IO ()
act = do
UTCTime
t0 <- IO UTCTime
getCurrentTime
IO ()
act
UTCTime
t1 <- IO UTCTime
getCurrentTime
forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (RealFrac a, Integral b) => a -> b
ceiling forall a b. (a -> b) -> a -> b
$! (UTCTime
t1 UTCTime -> UTCTime -> NominalDiffTime
`diffUTCTime` UTCTime
t0) forall a. Num a => a -> a -> a
* NominalDiffTime
1e6