forked from commercialhaskell/stack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUser.hs
More file actions
404 lines (371 loc) · 14.1 KB
/
User.hs
File metadata and controls
404 lines (371 loc) · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -Wno-unused-top-binds -Wno-identities #-}
-- | Work with SQLite database used for caches across an entire user account.
module Stack.Storage.User
( initUserStorage
, PrecompiledCacheKey
, precompiledCacheKey
, loadPrecompiledCache
, savePrecompiledCache
, loadDockerImageExeCache
, saveDockerImageExeCache
, loadCompilerPaths
, saveCompilerPaths
, upgradeChecksSince
, logUpgradeCheck
) where
import qualified Data.Set as Set
import qualified Data.Text as T
import Data.Time.Clock ( UTCTime )
import Database.Persist.Sqlite
( Entity (..), SqlBackend, Unique, (=.), (==.), (>=.), count
, deleteBy, getBy, insert, insert_, selectList, update
, upsert
)
import Database.Persist.TH
( mkMigrate, mkPersist, persistLowerCase, share
, sqlSettings
)
import Distribution.Text ( simpleParse, display )
import Foreign.C.Types ( CTime (..) )
import qualified Pantry.Internal as SQLite
import Path ( (</>), mkRelFile, parseRelFile )
import Path.IO ( resolveFile', resolveDir' )
import qualified RIO.FilePath as FP
import Stack.Prelude
import Stack.Storage.Util ( handleMigrationException, updateSet )
import Stack.Types.Build ( PrecompiledCache (..) )
import Stack.Types.Cache ( Action (..) )
import Stack.Types.Compiler ( ActualCompiler, compilerVersionText )
import Stack.Types.CompilerBuild ( CompilerBuild )
import Stack.Types.CompilerPaths
( CompilerPaths (..), GhcPkgExe (..) )
import Stack.Types.Config ( Config (..), HasConfig (..) )
import Stack.Types.Storage ( UserStorage (..) )
import System.Posix.Types ( COff (..) )
import System.PosixCompat.Files
( fileSize, getFileStatus, modificationTime )
-- | Type representing exceptions thrown by functions exported by the
-- "Stack.Storage.User" module.
data StorageUserException
= CompilerFileMetadataMismatch
| GlobalPackageCacheFileMetadataMismatch
| GlobalDumpParseFailure
| CompilerCacheArchitectureInvalid Text
deriving (Show, Typeable)
instance Exception StorageUserException where
displayException CompilerFileMetadataMismatch =
"Error: [S-8196]\n"
++ "Compiler file metadata mismatch, ignoring cache."
displayException GlobalPackageCacheFileMetadataMismatch =
"Error: [S-5378]\n"
++ "Global package cache file metadata mismatch, ignoring cache."
displayException GlobalDumpParseFailure =
"Error: [S-2673]\n"
++ "Global dump did not parse correctly."
displayException
(CompilerCacheArchitectureInvalid compilerCacheArch) = concat
[ "Error: [S-8441]\n"
, "Invalid arch: "
, show compilerCacheArch
]
share [ mkPersist sqlSettings
, mkMigrate "migrateAll"
]
[persistLowerCase|
PrecompiledCacheParent sql="precompiled_cache"
platformGhcDir FilePath "default=(hex(randomblob(16)))"
compiler Text
cabalVersion Text
packageKey Text
optionsHash ByteString
haddock Bool default=0
library FilePath Maybe
UniquePrecompiledCacheParent platformGhcDir compiler cabalVersion packageKey optionsHash haddock sql="unique_precompiled_cache"
deriving Show
PrecompiledCacheSubLib
parent PrecompiledCacheParentId sql="precompiled_cache_id" OnDeleteCascade
value FilePath sql="sub_lib"
UniquePrecompiledCacheSubLib parent value
deriving Show
PrecompiledCacheExe
parent PrecompiledCacheParentId sql="precompiled_cache_id" OnDeleteCaseCascade
value FilePath sql="exe"
UniquePrecompiledCacheExe parent value
deriving Show
DockerImageExeCache
imageHash Text
exePath FilePath
exeTimestamp UTCTime
compatible Bool
DockerImageExeCacheUnique imageHash exePath exeTimestamp
deriving Show
CompilerCache
actualVersion ActualCompiler
arch Text
-- Include ghc executable size and modified time for sanity checking entries
ghcPath FilePath
ghcSize Int64
ghcModified Int64
ghcPkgPath FilePath
runghcPath FilePath
haddockPath FilePath
cabalVersion Text
globalDb FilePath
globalDbCacheSize Int64
globalDbCacheModified Int64
info ByteString
-- This is the ugliest part of this table, simply storing a Show/Read version of the
-- data. We could do a better job with normalized data and proper table structure.
-- However, recomputing this value in the future if the data representation changes
-- is very cheap, so we'll take the easy way out for now.
globalDump Text
UniqueCompilerInfo ghcPath
-- Last time certain actions were performed
LastPerformed
action Action
timestamp UTCTime
UniqueAction action
|]
-- | Initialize the database.
initUserStorage ::
HasLogFunc env
=> Path Abs File -- ^ storage file
-> (UserStorage -> RIO env a)
-> RIO env a
initUserStorage fp f = handleMigrationException $
SQLite.initStorage "Stack" migrateAll fp $ f . UserStorage
-- | Run an action in a database transaction
withUserStorage ::
(HasConfig env, HasLogFunc env)
=> ReaderT SqlBackend (RIO env) a
-> RIO env a
withUserStorage inner = do
storage <- view (configL . to configUserStorage . to unUserStorage)
SQLite.withStorage_ storage inner
-- | Key used to retrieve the precompiled cache
type PrecompiledCacheKey = Unique PrecompiledCacheParent
-- | Build key used to retrieve the precompiled cache
precompiledCacheKey ::
Path Rel Dir
-> ActualCompiler
-> Version
-> Text
-> ByteString
-> Bool
-> PrecompiledCacheKey
precompiledCacheKey platformGhcDir compiler cabalVersion =
UniquePrecompiledCacheParent
(toFilePath platformGhcDir)
(compilerVersionText compiler)
(T.pack $ versionString cabalVersion)
-- | Internal helper to read the 'PrecompiledCache' from the database
readPrecompiledCache ::
(HasConfig env, HasLogFunc env)
=> PrecompiledCacheKey
-> ReaderT SqlBackend (RIO env) (Maybe ( PrecompiledCacheParentId
, PrecompiledCache Rel))
readPrecompiledCache key = do
mparent <- getBy key
forM mparent $ \(Entity parentId PrecompiledCacheParent {..}) -> do
pcLibrary <- mapM parseRelFile precompiledCacheParentLibrary
pcSubLibs <-
mapM (parseRelFile . precompiledCacheSubLibValue . entityVal) =<<
selectList [PrecompiledCacheSubLibParent ==. parentId] []
pcExes <-
mapM (parseRelFile . precompiledCacheExeValue . entityVal) =<<
selectList [PrecompiledCacheExeParent ==. parentId] []
pure (parentId, PrecompiledCache {..})
-- | Load 'PrecompiledCache' from the database.
loadPrecompiledCache ::
(HasConfig env, HasLogFunc env)
=> PrecompiledCacheKey
-> RIO env (Maybe (PrecompiledCache Rel))
loadPrecompiledCache key =
withUserStorage $ fmap snd <$> readPrecompiledCache key
-- | Insert or update 'PrecompiledCache' to the database.
savePrecompiledCache ::
(HasConfig env, HasLogFunc env)
=> PrecompiledCacheKey
-> PrecompiledCache Rel
-> RIO env ()
savePrecompiledCache
key@( UniquePrecompiledCacheParent
precompiledCacheParentPlatformGhcDir
precompiledCacheParentCompiler
precompiledCacheParentCabalVersion
precompiledCacheParentPackageKey
precompiledCacheParentOptionsHash
precompiledCacheParentHaddock
)
new
= withUserStorage $ do
let precompiledCacheParentLibrary = fmap toFilePath (pcLibrary new)
mIdOld <- readPrecompiledCache key
(parentId, mold) <-
case mIdOld of
Nothing -> (, Nothing) <$> insert PrecompiledCacheParent {..}
Just (parentId, old) -> do
update
parentId
[ PrecompiledCacheParentLibrary =.
precompiledCacheParentLibrary
]
pure (parentId, Just old)
updateSet
PrecompiledCacheSubLib
PrecompiledCacheSubLibParent
parentId
PrecompiledCacheSubLibValue
(maybe Set.empty (toFilePathSet . pcSubLibs) mold)
(toFilePathSet $ pcSubLibs new)
updateSet
PrecompiledCacheExe
PrecompiledCacheExeParent
parentId
PrecompiledCacheExeValue
(maybe Set.empty (toFilePathSet . pcExes) mold)
(toFilePathSet $ pcExes new)
where
toFilePathSet = Set.fromList . map toFilePath
-- | Get the record of whether an executable is compatible with a Docker image
loadDockerImageExeCache ::
(HasConfig env, HasLogFunc env)
=> Text
-> Path Abs File
-> UTCTime
-> RIO env (Maybe Bool)
loadDockerImageExeCache imageId exePath exeTimestamp = withUserStorage $
fmap (dockerImageExeCacheCompatible . entityVal) <$>
getBy (DockerImageExeCacheUnique imageId (toFilePath exePath) exeTimestamp)
-- | Sets the record of whether an executable is compatible with a Docker image
saveDockerImageExeCache ::
(HasConfig env, HasLogFunc env)
=> Text
-> Path Abs File
-> UTCTime
-> Bool
-> RIO env ()
saveDockerImageExeCache imageId exePath exeTimestamp compatible = void $
withUserStorage $
upsert
( DockerImageExeCache
imageId
(toFilePath exePath)
exeTimestamp
compatible
)
[]
-- | Type-restricted version of 'fromIntegral' to ensure we're making the value
-- bigger, not smaller.
sizeToInt64 :: COff -> Int64
sizeToInt64 (COff i) = fromIntegral i -- fromIntegral added for 32-bit systems
-- | Type-restricted version of 'fromIntegral' to ensure we're making the value
-- bigger, not smaller.
timeToInt64 :: CTime -> Int64
timeToInt64 (CTime i) = fromIntegral i -- fromIntegral added for 32-bit systems
-- | Load compiler information, if available, and confirm that the referenced
-- files are unchanged. May throw exceptions!
loadCompilerPaths ::
HasConfig env
=> Path Abs File -- ^ compiler executable
-> CompilerBuild
-> Bool -- ^ sandboxed?
-> RIO env (Maybe CompilerPaths)
loadCompilerPaths compiler build sandboxed = do
mres <- withUserStorage $ getBy $ UniqueCompilerInfo $ toFilePath compiler
for mres $ \(Entity _ CompilerCache {..}) -> do
compilerStatus <- liftIO $ getFileStatus $ toFilePath compiler
when
( compilerCacheGhcSize /= sizeToInt64 (fileSize compilerStatus)
|| compilerCacheGhcModified /=
timeToInt64 (modificationTime compilerStatus)
)
(throwIO CompilerFileMetadataMismatch)
globalDbStatus <-
liftIO $ getFileStatus $ compilerCacheGlobalDb FP.</> "package.cache"
when
( compilerCacheGlobalDbCacheSize /= sizeToInt64 (fileSize globalDbStatus)
|| compilerCacheGlobalDbCacheModified /=
timeToInt64 (modificationTime globalDbStatus)
)
(throwIO GlobalPackageCacheFileMetadataMismatch)
-- We could use parseAbsFile instead of resolveFile' below to bypass some
-- system calls, at the cost of some really wonky error messages in case
-- someone screws up their GHC installation
pkgexe <- resolveFile' compilerCacheGhcPkgPath
runghc <- resolveFile' compilerCacheRunghcPath
haddock <- resolveFile' compilerCacheHaddockPath
globaldb <- resolveDir' compilerCacheGlobalDb
cabalVersion <- parseVersionThrowing $ T.unpack compilerCacheCabalVersion
globalDump <-
case readMaybe $ T.unpack compilerCacheGlobalDump of
Nothing -> throwIO GlobalDumpParseFailure
Just globalDump -> pure globalDump
arch <-
case simpleParse $ T.unpack compilerCacheArch of
Nothing -> throwIO $ CompilerCacheArchitectureInvalid compilerCacheArch
Just arch -> pure arch
pure CompilerPaths
{ cpCompiler = compiler
, cpCompilerVersion = compilerCacheActualVersion
, cpArch = arch
, cpBuild = build
, cpPkg = GhcPkgExe pkgexe
, cpInterpreter = runghc
, cpHaddock = haddock
, cpSandboxed = sandboxed
, cpCabalVersion = cabalVersion
, cpGlobalDB = globaldb
, cpGhcInfo = compilerCacheInfo
, cpGlobalDump = globalDump
}
-- | Save compiler information. May throw exceptions!
saveCompilerPaths ::
HasConfig env
=> CompilerPaths
-> RIO env ()
saveCompilerPaths CompilerPaths {..} = withUserStorage $ do
deleteBy $ UniqueCompilerInfo $ toFilePath cpCompiler
compilerStatus <- liftIO $ getFileStatus $ toFilePath cpCompiler
globalDbStatus <-
liftIO $
getFileStatus $ toFilePath $ cpGlobalDB </> $(mkRelFile "package.cache")
let GhcPkgExe pkgexe = cpPkg
insert_ CompilerCache
{ compilerCacheActualVersion = cpCompilerVersion
, compilerCacheGhcPath = toFilePath cpCompiler
, compilerCacheGhcSize = sizeToInt64 $ fileSize compilerStatus
, compilerCacheGhcModified = timeToInt64 $ modificationTime compilerStatus
, compilerCacheGhcPkgPath = toFilePath pkgexe
, compilerCacheRunghcPath = toFilePath cpInterpreter
, compilerCacheHaddockPath = toFilePath cpHaddock
, compilerCacheCabalVersion = T.pack $ versionString cpCabalVersion
, compilerCacheGlobalDb = toFilePath cpGlobalDB
, compilerCacheGlobalDbCacheSize = sizeToInt64 $ fileSize globalDbStatus
, compilerCacheGlobalDbCacheModified =
timeToInt64 $ modificationTime globalDbStatus
, compilerCacheInfo = cpGhcInfo
, compilerCacheGlobalDump = tshow cpGlobalDump
, compilerCacheArch = T.pack $ Distribution.Text.display cpArch
}
-- | How many upgrade checks have occurred since the given timestamp?
upgradeChecksSince :: HasConfig env => UTCTime -> RIO env Int
upgradeChecksSince since = withUserStorage $ count
[ LastPerformedAction ==. UpgradeCheck
, LastPerformedTimestamp >=. since
]
-- | Log in the database that an upgrade check occurred at the given time.
logUpgradeCheck :: HasConfig env => UTCTime -> RIO env ()
logUpgradeCheck time = withUserStorage $ void $ upsert
(LastPerformed UpgradeCheck time)
[LastPerformedTimestamp =. time]