Skip to content

Commit a0ec94e

Browse files
committed
Remove locationAbsolute code
1 parent 2a7d00c commit a0ec94e

File tree

1 file changed

+62
-86
lines changed

1 file changed

+62
-86
lines changed

src/GHC/Utils/GhcPkg/Main/Compat.hs

Lines changed: 62 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ import Path ( Abs, Dir, Path, toFilePath )
5353
import Prelude
5454
import System.Directory
5555
( createDirectoryIfMissing, doesDirectoryExist
56-
, getCurrentDirectory, getDirectoryContents, removeFile
56+
, getDirectoryContents, removeFile
5757
)
58-
import System.Exit ( exitWith, ExitCode(..) )
58+
import System.Exit ( exitWith, ExitCode (..) )
5959
import System.Environment ( getProgName, getEnv )
6060
import System.FilePath as FilePath
6161
import System.IO ( hFlush, hPutStrLn, stderr, stdout )
@@ -83,28 +83,31 @@ ghcPkgUnregisterForce globalDb pkgDb hasIpid pkgarg_strs = do
8383
-- -----------------------------------------------------------------------------
8484
-- Command-line syntax
8585

86-
data Verbosity = Silent | Normal | Verbose
87-
deriving (Show, Eq, Ord)
86+
data Verbosity
87+
= Silent
88+
| Normal
89+
| Verbose
90+
deriving (Show, Eq, Ord)
8891

8992
-- -----------------------------------------------------------------------------
9093
-- Do the business
9194

9295
-- | Enum flag representing argument type
9396
data AsPackageArg
94-
= AsUnitId
95-
| AsDefault
97+
= AsUnitId
98+
| AsDefault
9699

97100
-- | Represents how a package may be specified by a user on the command line.
98101
data PackageArg
99102
-- | A package identifier foo-0.1, or a glob foo-*
100-
= Id GlobPackageIdentifier
103+
= Id GlobPackageIdentifier
101104
-- | An installed package ID foo-0.1-HASH. This is guaranteed to uniquely
102105
-- match a single entry in the package database.
103-
| IUId UnitId
106+
| IUId UnitId
104107
-- | A glob against the package name. The first string is the literal
105108
-- glob, the second is a function which returns @True@ if the argument
106109
-- matches.
107-
| Substring String (String->Bool)
110+
| Substring String (String -> Bool)
108111

109112
parseCheck :: Cabal.Parsec a => String -> String -> IO a
110113
parseCheck str what =
@@ -115,8 +118,8 @@ parseCheck str what =
115118
-- | Either an exact 'PackageIdentifier', or a glob for all packages
116119
-- matching 'PackageName'.
117120
data GlobPackageIdentifier
118-
= ExactPackageIdentifier MungedPackageId
119-
| GlobPackageIdentifier MungedPackageName
121+
= ExactPackageIdentifier MungedPackageId
122+
| GlobPackageIdentifier MungedPackageName
120123

121124
displayGlobPkgId :: GlobPackageIdentifier -> String
122125
displayGlobPkgId (ExactPackageIdentifier pid) = display pid
@@ -135,23 +138,12 @@ readPackageArg AsDefault str = Id <$> readGlobPkgId str
135138
-- -----------------------------------------------------------------------------
136139
-- Package databases
137140

138-
-- Some commands operate on a single database:
139-
-- register, unregister, expose, hide, trust, distrust
140-
-- however these commands also check the union of the available databases
141-
-- in order to check consistency. For example, register will check that
142-
-- dependencies exist before registering a package.
143-
--
144-
-- Some commands operate on multiple databases, with overlapping semantics:
145-
-- list, describe, field
146-
147141
data PackageDB (mode :: GhcPkg.DbMode)
148142
= PackageDB {
149-
location, locationAbsolute :: !FilePath,
150-
-- We need both possibly-relative and definitely-absolute package
151-
-- db locations. This is because the relative location is used as
152-
-- an identifier for the db, so it is important we do not modify it.
153-
-- On the other hand we need the absolute path in a few places
154-
-- particularly in relation to the ${pkgroot} stuff.
143+
location :: !FilePath,
144+
-- We only need possibly-relative package db location. The relative
145+
-- location is used as an identifier for the db, so it is important we do
146+
-- not modify it.
155147

156148
packageDbLock :: !(GhcPkg.DbOpenMode mode GhcPkg.PackageDbLock),
157149
-- If package db is open in read write mode, we keep its lock around for
@@ -164,9 +156,8 @@ type PackageDBStack = [PackageDB 'GhcPkg.DbReadOnly]
164156
-- A stack of package databases. Convention: head is the topmost
165157
-- in the stack.
166158

167-
-- | Selector for picking the right package DB to modify as 'register' and
168-
-- 'recache' operate on the database on top of the stack, whereas 'modify'
169-
-- changes the first database that contains a specific package.
159+
-- | Selector for picking the right package DB to modify as 'modify' changes the
160+
-- first database that contains a specific package.
170161
newtype DbModifySelector = ContainsPkg PackageArg
171162

172163
getPkgDatabases :: Path Abs Dir
@@ -217,8 +208,8 @@ getPkgDatabases globalDb verbosity pkgarg pkgDb = do
217208
infoLn ("modifying: " ++ location db)
218209
infoLn ("flag db stack: " ++ show (map location flag_db_stack))
219210

220-
return (db_stack, db_to_operate_on, flag_db_stack)
221-
where
211+
pure (db_stack, db_to_operate_on, flag_db_stack)
212+
where
222213
getDatabases flag_db_names final_stack = do
223214
-- The package db we open in read write mode is the first one included in
224215
-- flag_db_names that contains specified package. Therefore we need to
@@ -237,7 +228,7 @@ getPkgDatabases globalDb verbosity pkgarg pkgDb = do
237228
db <- readDatabase db_path
238229
if hasPkg db
239230
then couldntOpenDbForModification db_path e
240-
else return (db, Nothing)
231+
else pure (db, Nothing)
241232

242233
-- If we fail to open the database in read/write mode, we need
243234
-- to check if it's for modification first before throwing an
@@ -246,22 +237,22 @@ getPkgDatabases globalDb verbosity pkgarg pkgDb = do
246237
db <- readParseDatabase verbosity (GhcPkg.DbOpenReadWrite $ ContainsPkg pkgarg) db_path
247238
let ro_db = db { packageDbLock = GhcPkg.DbOpenReadOnly }
248239
if hasPkg db
249-
then return (ro_db, Just db)
240+
then pure (ro_db, Just db)
250241
else do
251242
-- If the database is not for modification after all,
252243
-- drop the write lock as we are already finished with
253244
-- the database.
254245
case packageDbLock db of
255246
GhcPkg.DbOpenReadWrite lock ->
256247
GhcPkg.unlockPackageDb lock
257-
return (ro_db, Nothing)
248+
pure (ro_db, Nothing)
258249
| db_path <- final_stack ]
259250

260251
to_modify <- case mto_modify of
261-
Just db -> return db
252+
Just db -> pure db
262253
Nothing -> cannotFindPackage pkgarg Nothing
263254

264-
return (db_stack, GhcPkg.DbOpenReadWrite to_modify)
255+
pure (db_stack, GhcPkg.DbOpenReadWrite to_modify)
265256
where
266257
couldntOpenDbForModification :: FilePath -> IOError -> IO a
267258
couldntOpenDbForModification db_path e = die $ "Couldn't open database "
@@ -291,7 +282,7 @@ readParseDatabase verbosity mode path = do
291282
-- old single-file style db:
292283
mdb <- tryReadParseOldFileStyleDatabase verbosity mode path
293284
case mdb of
294-
Just db -> return db
285+
Just db -> pure db
295286
Nothing ->
296287
die $ "ghc no longer supports single-file style package "
297288
++ "databases (" ++ path ++ ") use 'ghc-pkg init'"
@@ -300,33 +291,31 @@ readParseDatabase verbosity mode path = do
300291
| otherwise -> ioError err
301292
Right fs -> ignore_cache (const $ return ())
302293
where
303-
confs = map (path </>) $ filter (".conf" `isSuffixOf`) fs
304-
305-
ignore_cache :: (FilePath -> IO ()) -> IO (PackageDB mode)
306-
ignore_cache checkTime = do
307-
-- If we're opening for modification, we need to acquire a
308-
-- lock even if we don't open the cache now, because we are
309-
-- going to modify it later.
310-
lock <- F.mapM (const $ GhcPkg.lockPackageDb cache) mode
311-
let doFile f = do checkTime f
312-
parseSingletonPackageConf verbosity f
313-
pkgs <- mapM doFile confs
314-
mkPackageDB pkgs lock
294+
confs = map (path </>) $ filter (".conf" `isSuffixOf`) fs
295+
296+
ignore_cache :: (FilePath -> IO ()) -> IO (PackageDB mode)
297+
ignore_cache checkTime = do
298+
-- If we're opening for modification, we need to acquire a
299+
-- lock even if we don't open the cache now, because we are
300+
-- going to modify it later.
301+
lock <- F.mapM (const $ GhcPkg.lockPackageDb cache) mode
302+
let doFile f = do checkTime f
303+
parseSingletonPackageConf verbosity f
304+
pkgs <- mapM doFile confs
305+
mkPackageDB pkgs lock
315306

316-
where
317-
cache = path </> cachefilename
318-
319-
mkPackageDB :: [InstalledPackageInfo]
320-
-> GhcPkg.DbOpenMode mode GhcPkg.PackageDbLock
321-
-> IO (PackageDB mode)
322-
mkPackageDB pkgs lock = do
323-
path_abs <- absolutePath path
324-
return $ PackageDB {
325-
location = path,
326-
locationAbsolute = path_abs,
327-
packageDbLock = lock,
328-
packages = pkgs
329-
}
307+
where
308+
cache = path </> cachefilename
309+
310+
mkPackageDB :: [InstalledPackageInfo]
311+
-> GhcPkg.DbOpenMode mode GhcPkg.PackageDbLock
312+
-> IO (PackageDB mode)
313+
mkPackageDB pkgs lock = do
314+
pure $ PackageDB
315+
{ location = path
316+
, packageDbLock = lock
317+
, packages = pkgs
318+
}
330319

331320
parseSingletonPackageConf :: Verbosity -> FilePath -> IO InstalledPackageInfo
332321
parseSingletonPackageConf verbosity file = do
@@ -360,27 +349,22 @@ tryReadParseOldFileStyleDatabase verbosity mode path = do
360349
content <- readFile path `catchIO` \_ -> return ""
361350
if take 2 content == "[]"
362351
then do
363-
path_abs <- absolutePath path
364352
let path_dir = adjustOldDatabasePath path
365353
warn $ "Warning: ignoring old file-style db and trying " ++ path_dir
366354
direxists <- doesDirectoryExist path_dir
367355
if direxists
368356
then do
369357
db <- readParseDatabase verbosity mode path_dir
370358
-- but pretend it was at the original location
371-
return $ Just db {
372-
location = path,
373-
locationAbsolute = path_abs
374-
}
359+
pure $ Just db { location = path }
375360
else do
376361
lock <- F.forM mode $ \_ -> do
377362
createDirectoryIfMissing True path_dir
378363
GhcPkg.lockPackageDb $ path_dir </> cachefilename
379-
return $ Just PackageDB {
380-
location = path,
381-
locationAbsolute = path_abs,
382-
packageDbLock = lock,
383-
packages = []
364+
pure $ Just PackageDB
365+
{ location = path
366+
, packageDbLock = lock
367+
, packages = []
384368
}
385369

386370
-- if the path is not a file, or is not an empty db then we fail
@@ -392,23 +376,20 @@ adjustOldFileStylePackageDB db = do
392376
mcontent <- fmap Just (readFile (location db)) `catchIO` \_ -> return Nothing
393377
case fmap (take 2) mcontent of
394378
-- it is an old style and empty db, so look for a dir kind in location.d/
395-
Just "[]" -> return db {
396-
location = adjustOldDatabasePath $ location db,
397-
locationAbsolute = adjustOldDatabasePath $ locationAbsolute db
398-
}
379+
Just "[]" -> pure db
380+
{ location = adjustOldDatabasePath $ location db }
399381
-- it is old style but not empty, we have to bail
400382
Just _ -> die $ "ghc no longer supports single-file style package "
401383
++ "databases (" ++ location db ++ ") use 'ghc-pkg init'"
402384
++ "to create the database with the correct format."
403385
-- probably not old style, carry on as normal
404-
Nothing -> return db
386+
Nothing -> pure db
405387

406388
adjustOldDatabasePath :: FilePath -> FilePath
407389
adjustOldDatabasePath = (<.> "d")
408390

409-
parsePackageInfo
410-
:: BS.ByteString
411-
-> IO (InstalledPackageInfo, [ValidateWarning])
391+
parsePackageInfo :: BS.ByteString
392+
-> IO (InstalledPackageInfo, [ValidateWarning])
412393
parsePackageInfo str =
413394
case parseInstalledPackageInfo str of
414395
Right (warnings, ok) -> pure (mungePackageInfo ok, ws)
@@ -606,8 +587,3 @@ removeFileSafe :: FilePath -> IO ()
606587
removeFileSafe fn =
607588
removeFile fn `catchIO` \ e ->
608589
unless (isDoesNotExistError e) $ ioError e
609-
610-
-- | Turn a path relative to the current directory into a (normalised)
611-
-- absolute path.
612-
absolutePath :: FilePath -> IO FilePath
613-
absolutePath path = normalise . (</> path) <$> getCurrentDirectory

0 commit comments

Comments
 (0)