| ... |
... |
@@ -336,11 +336,10 @@ withLockedPackageDb mode file = |
|
336
|
336
|
(lockPackageDbWith (lock_mode mode) file)
|
|
337
|
337
|
unlockPackageDb
|
|
338
|
338
|
where
|
|
339
|
|
- bracket_for_mode =
|
|
|
339
|
+ bracket_for_mode =
|
|
340
|
340
|
case mode of
|
|
341
|
|
- DbOpenReadOnly -> bracket
|
|
342
|
|
- DbOpenReadWrite -> bracketOnError
|
|
343
|
|
- where
|
|
|
341
|
+ DbOpenReadOnly -> bracket
|
|
|
342
|
+ DbOpenReadWrite{} -> bracketOnError
|
|
344
|
343
|
lock_mode :: DbOpenMode m t -> LockMode
|
|
345
|
344
|
lock_mode DbOpenReadOnly = SharedLock
|
|
346
|
345
|
lock_mode DbOpenReadWrite{} = ExclusiveLock
|
| ... |
... |
@@ -405,6 +404,14 @@ data DbMode = DbReadOnly | DbReadWrite deriving Eq |
|
405
|
404
|
-- | 'DbOpenMode' holds a value of type @t@ but only in 'DbReadWrite' mode. So
|
|
406
|
405
|
-- it is like 'Maybe' but with a type argument for the mode to enforce that the
|
|
407
|
406
|
-- mode is used consistently.
|
|
|
407
|
+--
|
|
|
408
|
+-- We use the argument to DbOpenReadWrite only when modifying the DB for two uses:
|
|
|
409
|
+-- * Storing the Din two cases:
|
|
|
410
|
+-- When modifying the DB however the lock is meant to outlive
|
|
|
411
|
+-- intermittent read operations. Which is why for writes we typically store the lock in DbOpenMode.
|
|
|
412
|
+-- and pass it along the call path.
|
|
|
413
|
+--
|
|
|
414
|
+-- See Note [ghc-pkg database locking] and withLockedPackageDb
|
|
408
|
415
|
data DbOpenMode (mode :: DbMode) t where
|
|
409
|
416
|
DbOpenReadOnly :: DbOpenMode 'DbReadOnly t
|
|
410
|
417
|
DbOpenReadWrite :: t -> DbOpenMode 'DbReadWrite t
|
| ... |
... |
@@ -430,7 +437,7 @@ isDbOpenReadMode = \case |
|
430
|
437
|
--
|
|
431
|
438
|
readPackageDbForGhc :: FilePath -> IO [DbUnitInfo]
|
|
432
|
439
|
readPackageDbForGhc file = do
|
|
433
|
|
- hPutStrLn stderr $ "readPackageDbForGhc:" ++ show file
|
|
|
440
|
+ -- hPutStrLn stderr $ "readPackageDbForGhc:" ++ show file
|
|
434
|
441
|
withLockedPackageDb DbOpenReadOnly file $ \_ -> do
|
|
435
|
442
|
decodeFromFile file DbOpenReadOnly getDbForGhc >>= \case
|
|
436
|
443
|
(pkgs, DbOpenReadOnly) -> return pkgs
|
| ... |
... |
@@ -450,10 +457,7 @@ readPackageDbForGhc file = do |
|
450
|
457
|
--
|
|
451
|
458
|
-- The incoming mode carries the exclusive lock if we are in R/W mode.
|
|
452
|
459
|
--
|
|
453
|
|
--- If we open the package db in read only mode, we get its contents. Otherwise
|
|
454
|
|
--- we additionally receive a PackageDbLock that represents a lock on the
|
|
455
|
|
--- database, so that we can safely update it later.
|
|
456
|
|
---
|
|
|
460
|
+-- Returns the lock on the db as-is.
|
|
457
|
461
|
readPackageDbForGhcPkg :: Binary pkgs => FilePath -> DbOpenMode mode PackageDbLock ->
|
|
458
|
462
|
IO (pkgs, DbOpenMode mode PackageDbLock)
|
|
459
|
463
|
readPackageDbForGhcPkg file mode =
|
| ... |
... |
@@ -545,15 +549,9 @@ headerMagic = BS.Char8.pack "\0ghcpkg\0" |
|
545
|
549
|
-- which it returns unchanged.
|
|
546
|
550
|
decodeFromFile :: FilePath -> DbOpenMode mode PackageDbLock -> Get pkgs ->
|
|
547
|
551
|
IO (pkgs, DbOpenMode mode PackageDbLock)
|
|
548
|
|
-decodeFromFile file mode decoder = case mode of
|
|
549
|
|
- -- DB is locked with shared access already, we just do the read.
|
|
550
|
|
- DbOpenReadOnly -> do
|
|
551
|
|
- (, DbOpenReadOnly) <$> decodeFileContents
|
|
552
|
|
- DbOpenReadWrite{} -> do
|
|
553
|
|
- -- When we open the package db in read/write mode, we receive an exclusive lock
|
|
554
|
|
- -- on the database via the mode and return it so we can keep it for the duration of the
|
|
555
|
|
- -- update.
|
|
556
|
|
- -- If an exception is raised the caller releases the lock.
|
|
|
552
|
+decodeFromFile file mode decoder =
|
|
|
553
|
+ -- Return incoming lock together with the result.
|
|
|
554
|
+ -- See Note [ghc-pkg database locking]
|
|
557
|
555
|
(, mode) <$> decodeFileContents
|
|
558
|
556
|
where
|
|
559
|
557
|
decodeFileContents = withBinaryFile file ReadMode $ \hnd ->
|