Commit 104c6852 authored by Mikhail Glushenkov's avatar Mikhail Glushenkov
Browse files

Merge pull request #2150 from erikd/master

cabal-install/IndexUtils : Expose function getIndexFileAge.
parents e4abefff 26eb1c31
......@@ -11,7 +11,7 @@ import Data.Time.Clock.POSIX (utcTimeToPOSIXSeconds, posixDayLength)
import Data.Time (getCurrentTime, diffUTCTime)
#else
import System.Time (ClockTime(..), getClockTime
,diffClockTimes, normalizeTimeDiff, tdDay)
,diffClockTimes, normalizeTimeDiff, tdDay, tdHour)
#endif
#if defined mingw32_HOST_OS
......@@ -115,17 +115,17 @@ getModTime path = do
#endif
-- | Return age of given file in days.
getFileAge :: FilePath -> IO Int
getFileAge :: FilePath -> IO Double
getFileAge file = do
t0 <- getModificationTime file
#if MIN_VERSION_directory(1,2,0)
t1 <- getCurrentTime
let days = truncate $ (t1 `diffUTCTime` t0) / posixDayLength
return $ realToFrac (t1 `diffUTCTime` t0) / realToFrac posixDayLength
#else
t1 <- getClockTime
let days = (tdDay . normalizeTimeDiff) (t1 `diffClockTimes` t0)
let dt = normalizeTimeDiff (t1 `diffClockTimes` t0)
return $ fromIntegral ((24 * tdDay dt) + tdHour dt) / 24.0
#endif
return days
getCurTime :: IO EpochTime
getCurTime = do
......
......@@ -11,6 +11,7 @@
-- Extra utils related to the package indexes.
-----------------------------------------------------------------------------
module Distribution.Client.IndexUtils (
getIndexFileAge,
getInstalledPackages,
getSourcePackages,
getSourcePackagesStrict,
......@@ -81,6 +82,7 @@ import System.FilePath.Posix as FilePath.Posix
import System.IO
import System.IO.Unsafe (unsafeInterleaveIO)
import System.IO.Error (isDoesNotExistError)
import Numeric (showFFloat)
getInstalledPackages :: Verbosity -> Compiler
......@@ -180,7 +182,7 @@ readRepoIndex verbosity repo mode =
let indexFile = repoLocalDir repo </> "00-index.tar"
cacheFile = repoLocalDir repo </> "00-index.cache"
in handleNotFound $ do
warnIfIndexIsOld indexFile
warnIfIndexIsOld =<< getIndexFileAge repo
whenCacheOutOfDate indexFile cacheFile $ do
info verbosity "Updating the index cache file..."
updatePackageIndexCacheFile indexFile cacheFile
......@@ -214,15 +216,20 @@ readRepoIndex verbosity repo mode =
else ioError e
isOldThreshold = 15 --days
warnIfIndexIsOld indexFile = do
dt <- getFileAge indexFile
warnIfIndexIsOld dt = do
when (dt >= isOldThreshold) $ case repoKind repo of
Left remoteRepo -> warn verbosity $
"The package list for '" ++ remoteRepoName remoteRepo
++ "' is " ++ show dt ++ " days old.\nRun "
++ "' is " ++ showFFloat (Just 1) dt " days old.\nRun "
++ "'cabal update' to get the latest list of available packages."
Right _localRepo -> return ()
-- | Return the age of the index file in days (as a Double).
getIndexFileAge :: Repo -> IO Double
getIndexFileAge repo = getFileAge $ repoLocalDir repo </> "00-index.tar"
-- | It is not necessary to call this, as the cache will be updated when the
-- index is read normally. However you can do the work earlier if you like.
--
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment