Skip to content

Commit 5b91fca

Browse files
committed
Fix parsing of cas path adresses
1 parent 4fc581a commit 5b91fca

File tree

2 files changed

+37
-47
lines changed

2 files changed

+37
-47
lines changed

hnix-store-remote/src/System/Nix/Store/Remote.hs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,7 @@ querySubstitutablePaths ps = do
264264
putPaths ps
265265
sockGetPaths
266266

267-
queryPathInfoUncached :: forall a . NamedAlgo a
268-
=> StorePath
267+
queryPathInfoUncached :: StorePath
269268
-> MonadStore StorePathMetadata
270269
queryPathInfoUncached path = do
271270
runOpArgs QueryPathInfo $ do
@@ -277,9 +276,9 @@ queryPathInfoUncached path = do
277276
deriverPath <- sockGetPathMay
278277

279278
narHashText <- Data.Text.Encoding.decodeUtf8 <$> sockGetStr
280-
let narHash = case System.Nix.Hash.decodeBase32 narHashText of
279+
let narHash = case System.Nix.Hash.decodeBase32 @'System.Nix.Hash.SHA256 narHashText of
281280
Left e -> error e
282-
Right x -> SomeDigest @a x
281+
Right x -> SomeDigest x
283282

284283
references <- sockGetPaths
285284
registrationTime <- sockGet getTime
@@ -294,7 +293,7 @@ queryPathInfoUncached path = do
294293
sigs = Data.Set.empty
295294

296295
contentAddressableAddress =
297-
case System.Nix.Store.Remote.Parsers.parseContentAddressableAddress @a caString of
296+
case System.Nix.Store.Remote.Parsers.parseContentAddressableAddress caString of
298297
Left e -> error e
299298
Right x -> Just x
300299

hnix-store-remote/src/System/Nix/Store/Remote/Parsers.hs

Lines changed: 33 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,63 +2,54 @@
22
{-# LANGUAGE OverloadedStrings #-}
33
{-# LANGUAGE ScopedTypeVariables #-}
44
{-# LANGUAGE RankNTypes #-}
5+
{-# LANGUAGE DataKinds #-}
56
{-# LANGUAGE TypeApplications #-}
67

7-
module System.Nix.Store.Remote.Parsers (
8-
parseContentAddressableAddress
9-
) where
8+
module System.Nix.Store.Remote.Parsers
9+
( parseContentAddressableAddress
10+
)
11+
where
1012

11-
import Control.Applicative ((<|>))
12-
import Data.Attoparsec.ByteString.Char8 (Parser, (<?>))
13-
import Data.ByteString (ByteString)
14-
import System.Nix.Hash (Digest, NamedAlgo, SomeNamedDigest(SomeDigest))
15-
import System.Nix.StorePath (ContentAddressableAddress(..), NarHashMode(..))
16-
17-
import qualified Data.Attoparsec.ByteString.Char8
18-
import qualified Data.ByteString.Char8
13+
import Control.Applicative ( (<|>) )
14+
import Data.Attoparsec.ByteString.Char8
15+
import Data.ByteString.Char8
1916
import qualified Data.Text
20-
21-
import qualified System.Nix.Internal.Base32
22-
import qualified System.Nix.Hash
17+
import Data.Text.Encoding ( decodeUtf8 )
18+
import System.Nix.Hash
19+
import System.Nix.StorePath ( ContentAddressableAddress(..)
20+
, NarHashMode(..)
21+
)
2322

2423
-- | Parse `ContentAddressableAddress` from `ByteString`
25-
parseContentAddressableAddress :: forall hashAlgo . NamedAlgo hashAlgo
26-
=> ByteString
27-
-> Either String ContentAddressableAddress
24+
parseContentAddressableAddress
25+
:: ByteString -> Either String ContentAddressableAddress
2826
parseContentAddressableAddress =
29-
Data.Attoparsec.ByteString.Char8.parseOnly
30-
(contentAddressableAddressParser @hashAlgo)
27+
Data.Attoparsec.ByteString.Char8.parseOnly contentAddressableAddressParser
3128

3229
-- | Parser for content addressable field
33-
contentAddressableAddressParser :: forall hashAlgo . NamedAlgo hashAlgo
34-
=> Parser ContentAddressableAddress
35-
contentAddressableAddressParser =
36-
caText
37-
<|> caFixed @hashAlgo
30+
contentAddressableAddressParser :: Parser ContentAddressableAddress
31+
contentAddressableAddressParser = caText <|> caFixed
3832

3933
-- | Parser for @text:sha256:<h>@
4034
caText :: Parser ContentAddressableAddress
4135
caText = do
42-
_ <- "text:sha256:"
43-
digest <- parseDigest
44-
either fail return
45-
$ Text <$> digest
36+
_ <- "text:sha256:"
37+
digest <- decodeBase32 @'SHA256 <$> parseHash
38+
either fail return $ Text <$> digest
4639

4740
-- | Parser for @fixed:<r?>:<ht>:<h>@
48-
caFixed :: forall hashAlgo . NamedAlgo hashAlgo => Parser ContentAddressableAddress
41+
caFixed :: Parser ContentAddressableAddress
4942
caFixed = do
50-
_ <- "fixed:"
51-
narHashMode <- (pure Recursive <$> "true") <|> (pure RegularFile <$> "false")
52-
<?> "Invalid Base32 part"
53-
digest <- parseDigest
43+
_ <- "fixed:"
44+
narHashMode <- (pure Recursive <$> "r:") <|> (pure RegularFile <$> "")
45+
digest <- parseTypedDigest
46+
either fail return $ Fixed narHashMode <$> digest
47+
48+
parseTypedDigest :: Parser (Either String SomeNamedDigest)
49+
parseTypedDigest = mkNamedDigest <$> parseHashType <*> parseHash
5450

55-
either fail return
56-
$ Fixed <$> pure narHashMode <*> (SomeDigest @hashAlgo <$> digest)
51+
parseHashType :: Parser Data.Text.Text
52+
parseHashType = decodeUtf8 <$> ("sha256" <|> "sha1" <|> "md5") <* ":"
5753

58-
parseDigest :: forall a . Parser (Either String (Digest a))
59-
parseDigest =
60-
System.Nix.Hash.decodeBase32
61-
. Data.Text.pack
62-
. Data.ByteString.Char8.unpack
63-
<$> Data.Attoparsec.ByteString.Char8.takeWhile1
64-
(\c -> c `elem` System.Nix.Internal.Base32.digits32)
54+
parseHash :: Parser Data.Text.Text
55+
parseHash = decodeUtf8 <$> takeWhile1 (/= ':')

0 commit comments

Comments
 (0)