Skip to content

Commit 2beb057

Browse files
committed
Refactor getPackageSourceHashes in cabal-install
Try to leverage the fact that repoTarballPkgsWithMetadata only contains secure Repos by construction. This allows us to avoid relying on partial functions or maybes. Add a new data type that contains only the fields that the RepoSecure constructor has in order to localize changes. Add convenience functions that convert between Repo and SecureRepo.
1 parent c25ab80 commit 2beb057

File tree

3 files changed

+40
-16
lines changed

3 files changed

+40
-16
lines changed

cabal-install/src/Distribution/Client/FetchUtils.hs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE NamedFieldPuns #-}
12
-----------------------------------------------------------------------------
23
-----------------------------------------------------------------------------
34
{-# LANGUAGE RecordWildCards #-}
@@ -156,29 +157,32 @@ checkRepoTarballFetched repo pkgid = do
156157
verifyFetchedTarballs
157158
:: Verbosity
158159
-> RepoContext
159-
-> Repo
160+
-> SecureRepo
160161
-> [PackageId]
161162
-> IO
162163
( [ Either
163-
(Repo, PackageId) -- Verified
164+
(SecureRepo, PackageId) -- Verified SecureRepo
164165
(Repo, PackageId) -- unverified)
165166
]
166167
)
167-
verifyFetchedTarballs verbosity repoCtxt repo pkgids =
168+
verifyFetchedTarballs verbosity repoCtxt secureRepo pkgids =
168169
-- Establish the context once per repo (see #10110), this codepath is important
169170
-- to be fast as it can happen when no other building happens.
170171
let establishContext k =
171-
case repo of
172-
RepoSecure{} ->
173-
repoContextWithSecureRepo repoCtxt repo $ \repoSecure ->
174-
Sec.withIndex repoSecure $ \callbacks -> k (Just callbacks)
175-
_ -> k Nothing
172+
repoContextWithSecureRepo repoCtxt repo $ \repoSecure ->
173+
Sec.withIndex repoSecure $ \callbacks ->
174+
k (Just callbacks)
175+
176+
repo = secureRepoToRepo secureRepo
176177
in do
177178
establishContext $ \mCallbacks ->
178179
forM pkgids $ \pkgid -> do
179180
let file = packageFile repo pkgid
180181
res <- verifyFetchedTarball verbosity file mCallbacks pkgid
181-
return $ if res then Left (repo, pkgid) else Right (repo, pkgid)
182+
return $
183+
if res
184+
then Left (secureRepo, pkgid)
185+
else Right (repo, pkgid)
182186

183187
verifyFetchedTarball :: Verbosity -> FilePath -> Maybe Sec.IndexCallbacks -> PackageId -> IO Bool
184188
verifyFetchedTarball verbosity file mCallbacks pkgid =

cabal-install/src/Distribution/Client/ProjectPlanning.hs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1171,14 +1171,14 @@ getPackageSourceHashes verbosity withRepoCtx solverPlan = do
11711171
-- Tarballs from repositories, either where the repository provides
11721172
-- hashes as part of the repo metadata, or where we will have to
11731173
-- download and hash the tarball.
1174-
repoTarballPkgsWithMetadataUnvalidated :: [(Repo, [PackageId])]
1174+
repoTarballPkgsWithMetadataUnvalidated :: [(SecureRepo, [PackageId])]
11751175
repoTarballPkgsWithoutMetadata :: [(Repo, PackageId)]
11761176
( repoTarballPkgsWithMetadataUnvalidated
11771177
, repoTarballPkgsWithoutMetadata
11781178
) =
11791179
partitionEithers
11801180
[ case repo of
1181-
RepoSecure{} -> Left (repo, [pkgid])
1181+
RepoSecure r dir -> Left (SecureRepo r dir, [pkgid])
11821182
_ -> Right (repo, pkgid)
11831183
| (pkgid, RepoTarballPackage repo _ _) <- allPkgLocations
11841184
]
@@ -1230,8 +1230,8 @@ getPackageSourceHashes verbosity withRepoCtx solverPlan = do
12301230
fmap (Map.fromList . concat) $
12311231
sequence
12321232
-- Reading the repo index is expensive so we group the packages by repo
1233-
[ repoContextWithSecureRepo repoctx repo $ \secureRepo ->
1234-
Sec.withIndex secureRepo $ \repoIndex ->
1233+
[ repoContextWithSecureRepo repoctx (secureRepoToRepo secureRepo) $ \repo ->
1234+
Sec.withIndex repo $ \repoIndex ->
12351235
sequence
12361236
[ do
12371237
hash <-
@@ -1244,12 +1244,12 @@ getPackageSourceHashes verbosity withRepoCtx solverPlan = do
12441244
return (pkgid, hashFromTUF hash)
12451245
| pkgid <- pkgids
12461246
]
1247-
| (repo, pkgids) <-
1247+
| (secureRepo, pkgids) <-
12481248
-- All Repos here are SecureRepos (and will have a name), so we're
12491249
-- sorting Justs
12501250
map (\grp@((repo, _) :| _) -> (repo, map snd (NE.toList grp)))
1251-
. NE.groupBy ((==) `on` (fmap remoteRepoName . maybeRepoRemote . fst))
1252-
. sortBy (compare `on` (fmap remoteRepoName . maybeRepoRemote . fst))
1251+
. NE.groupBy ((==) `on` (remoteRepoName . secureRemote . fst))
1252+
. sortBy (compare `on` (remoteRepoName . secureRemote . fst))
12531253
$ repoTarballPkgsWithMetadata
12541254
]
12551255

cabal-install/src/Distribution/Client/Types/Repo.hs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ module Distribution.Client.Types.Repo
1919
, repoName
2020
, isRepoRemote
2121
, maybeRepoRemote
22+
, SecureRepo (..)
23+
, mkSecureRepo
24+
, secureRepoToRepo
2225

2326
-- * Windows
2427
, asPosixPath
@@ -223,6 +226,23 @@ repoName (RepoLocalNoIndex r _) = localRepoName r
223226
repoName (RepoRemote r _) = remoteRepoName r
224227
repoName (RepoSecure r _) = remoteRepoName r
225228

229+
-- | Secure repositories
230+
--
231+
-- This contains the same fields as `Repo`'s constructor `RepoSecure`, but is kept
232+
-- separate to keep API breakages low
233+
data SecureRepo = SecureRepo
234+
{ secureRemote :: RemoteRepo
235+
, secureLocalDir :: FilePath
236+
}
237+
deriving (Show, Eq, Ord, Generic)
238+
239+
mkSecureRepo :: Repo -> Maybe SecureRepo
240+
mkSecureRepo (RepoSecure r dir) = Just (SecureRepo r dir)
241+
mkSecureRepo _ = Nothing
242+
243+
secureRepoToRepo :: SecureRepo -> Repo
244+
secureRepoToRepo (SecureRepo r dir) = RepoSecure r dir
245+
226246
-------------------------------------------------------------------------------
227247

228248
-- * Windows utils

0 commit comments

Comments
 (0)