Skip to content

Commit 71991e5

Browse files
authored
Merge pull request #177 from soulomoon/dev
replace the filepath argument in `addToStore` with a more common type `NarSource` (The core part)
2 parents e95e8cb + e3723c7 commit 71991e5

File tree

3 files changed

+58
-30
lines changed

3 files changed

+58
-30
lines changed

hnix-store-core/src/System/Nix/Internal/Nar/Streamer.hs

Lines changed: 51 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
{-# language ScopedTypeVariables #-}
44

55
module System.Nix.Internal.Nar.Streamer
6-
( streamNarIO
6+
( NarSource
7+
, dumpString
8+
, dumpPath
9+
, streamNarIO
710
, IsExecutable(..)
811
)
912
where
@@ -19,22 +22,43 @@ import System.FilePath ( (</>) )
1922
import qualified System.Nix.Internal.Nar.Effects as Nar
2023

2124

25+
-- | NarSource
26+
-- The source to provide nar to the handler `(ByteString -> m ())`.
27+
-- It is isomorphic to ByteString by Yoneda lemma
28+
-- if the result is meant to be m ().
29+
-- It is done in CPS style so IO can be chunks.
30+
type NarSource m = (ByteString -> m ()) -> m ()
31+
32+
33+
-- | dumpString
34+
-- dump a string to nar in CPS style. The function takes in a `ByteString`,
35+
-- and build a `NarSource m`.
36+
dumpString
37+
:: forall m. IO.MonadIO m
38+
=> ByteString -- ^ the string you want to dump
39+
-> NarSource m -- ^ The nar result in CPS style
40+
dumpString text yield = traverse_ (yield . str)
41+
["nix-archive-1", "(", "type" , "regular", "contents", text, ")"]
42+
43+
44+
-- | dumpPath
45+
-- shorthand
46+
-- build a Source that turn file path to nar using the default narEffectsIO.
47+
dumpPath
48+
:: forall m . IO.MonadIO m
49+
=> FilePath -- ^ path for the file you want to dump to nar
50+
-> NarSource m -- ^ the nar result in CPS style
51+
dumpPath = streamNarIO Nar.narEffectsIO
52+
53+
2254
-- | This implementation of Nar encoding takes an arbitrary @yield@
2355
-- function from any streaming library, and repeatedly calls
2456
-- it while traversing the filesystem object to Nar encode
25-
streamNarIO
26-
:: forall m
27-
. (IO.MonadIO m)
28-
=> (ByteString -> m ())
29-
-> Nar.NarEffects IO
30-
-> FilePath
31-
-> m ()
32-
streamNarIO yield effs basePath = do
57+
streamNarIO :: forall m . IO.MonadIO m => Nar.NarEffects IO -> FilePath -> NarSource m
58+
streamNarIO effs basePath yield = do
3359
yield $ str "nix-archive-1"
3460
parens $ go basePath
35-
3661
where
37-
3862
go :: FilePath -> m ()
3963
go path = do
4064
isDir <- IO.liftIO $ Nar.narIsDir effs path
@@ -65,16 +89,6 @@ streamNarIO yield effs basePath = do
6589
yield $ strs ["name", Bytes.Char8.pack f, "node"]
6690
parens $ go fullName
6791

68-
str :: ByteString -> ByteString
69-
str t =
70-
let
71-
len = Bytes.length t
72-
in
73-
int len <> padBS len t
74-
75-
padBS :: Int -> ByteString -> ByteString
76-
padBS strSize bs = bs <> Bytes.replicate (padLen strSize) 0
77-
7892
parens act = do
7993
yield $ str "("
8094
r <- act
@@ -87,13 +101,6 @@ streamNarIO yield effs basePath = do
87101
mapM_ yield . Bytes.Lazy.toChunks =<< IO.liftIO (Bytes.Lazy.readFile path)
88102
yield $ Bytes.replicate (padLen $ fromIntegral fsize) 0
89103

90-
strs :: [ByteString] -> ByteString
91-
strs xs = Bytes.concat $ str <$> xs
92-
93-
int :: Integral a => a -> ByteString
94-
int n = Serial.runPut $ Serial.putInt64le $ fromIntegral n
95-
96-
97104
data IsExecutable = NonExecutable | Executable
98105
deriving (Eq, Show)
99106

@@ -107,3 +114,19 @@ isExecutable effs fp =
107114
-- | Distance to the next multiple of 8
108115
padLen :: Int -> Int
109116
padLen n = (8 - n) `mod` 8
117+
118+
int :: Integral a => a -> ByteString
119+
int n = Serial.runPut $ Serial.putInt64le $ fromIntegral n
120+
121+
str :: ByteString -> ByteString
122+
str t =
123+
let
124+
len = Bytes.length t
125+
in
126+
int len <> padBS len t
127+
128+
padBS :: Int -> ByteString -> ByteString
129+
padBS strSize bs = bs <> Bytes.replicate (padLen strSize) 0
130+
131+
strs :: [ByteString] -> ByteString
132+
strs xs = Bytes.concat $ str <$> xs

hnix-store-core/src/System/Nix/Nar.hs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ module System.Nix.Nar
2626
-- * Internal
2727
, Nar.streamNarIO
2828
, Nar.runParser
29+
, Nar.dumpString
30+
, Nar.dumpPath
31+
32+
-- * Type
33+
, Nar.NarSource
2934
)
3035
where
3136

@@ -52,9 +57,9 @@ buildNarIO
5257
-> IO ()
5358
buildNarIO effs basePath outHandle =
5459
Nar.streamNarIO
55-
(\chunk -> BS.hPut outHandle chunk >> Concurrent.threadDelay 10)
5660
effs
5761
basePath
62+
(\chunk -> BS.hPut outHandle chunk >> Concurrent.threadDelay 10)
5863

5964

6065
-- | Read NAR formatted bytes from the @IO.Handle@ and unpack them into

hnix-store-core/src/System/Nix/ReadonlyStore.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ computeStorePathForPath name pth recursive _pathFilter _repair = do
8787
recursiveContentHash :: IO (Digest SHA256)
8888
recursiveContentHash = hashFinalize <$> execStateT streamNarUpdate (hashInit @SHA256)
8989
streamNarUpdate :: StateT (Context SHA256) IO ()
90-
streamNarUpdate = streamNarIO (modify . flip (hashUpdate @ByteString @SHA256)) narEffectsIO pth
90+
streamNarUpdate = streamNarIO narEffectsIO pth (modify . flip (hashUpdate @ByteString @SHA256))
9191

9292
flatContentHash :: IO (Digest SHA256)
9393
flatContentHash = hashlazy <$> narReadFile narEffectsIO pth

0 commit comments

Comments
 (0)