Skip to content

Commit 547165d

Browse files
committed
Add useful garbage
1 parent 759abd9 commit 547165d

File tree

4 files changed

+97
-12
lines changed

4 files changed

+97
-12
lines changed

hnix-store-remote/app/Main.hs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
import qualified Data.ByteString.Lazy as LBS
33
import qualified Data.HashSet as HS
44
import Data.Maybe
5+
import Data.Proxy
56
import Control.Monad.Reader
67
import Text.Pretty.Simple
78

89
import qualified System.Nix.GC as GC
10+
import System.Nix.Path (PathHashAlgo)
911
import System.Nix.Store.Remote
1012
import System.Nix.Store.Remote.Util
1113

@@ -17,19 +19,27 @@ main = do
1719

1820
verifyStore False False
1921

20-
(Just path) <- addTextToStore "hnix-store" "test" (HS.fromList []) False
22+
(Just path) <- addTextToStore "hnix-store" "test" (HS.fromList []) False
23+
24+
-- (Just path2) <- addTextToStore "hnix-store2" "test2" (HS.fromList []) False
25+
path2 <- addToStore "hi-test-file"
26+
"/home/greghale/code/hnix-store/hnix-store-remote/hi"
27+
False (Proxy :: Proxy PathHashAlgo) (const True) False
2128

2229
valid <- isValidPathUncached path
23-
case valid of
24-
True -> do
30+
valid2 <- isValidPathUncached path2
31+
32+
case (valid, valid2) of
33+
(True, True) -> do
2534
info <- queryPathInfoUncached path
26-
return (path, info)
35+
info2 <- queryPathInfoUncached path2
36+
return (path, info, path2, info2)
2737
_ -> error "shouldn't happen"
2838

2939
pPrint x
3040
case x of
3141
(Left err, log) -> putStrLn err >> print log
32-
(Right (path, pathinfo), log) -> do
42+
(Right (path, pathinfo, path2, pathinfo2), log) -> do
3343
gcres <- runStore $ do
3444
collectGarbage $ GC.Options
3545
{ GC.operation = GC.DeleteSpecific

hnix-store-remote/hnix-store-remote.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ library
2121
, System.Nix.Store.Remote.Util
2222

2323
build-depends: base >=4.10 && <4.12
24+
, base64-bytestring
2425
, bytestring
2526
, binary
2627
, bytestring

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

Lines changed: 79 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ import System.Nix.Store.Remote.Types
5959
import System.Nix.Store.Remote.Protocol
6060
import System.Nix.Store.Remote.Util
6161

62+
-- tmp
63+
import qualified Data.ByteString.Base64.Lazy as B64
64+
6265
type RepairFlag = Bool
6366
type CheckFlag = Bool
6467
type CheckSigsFlag = Bool
@@ -194,7 +197,50 @@ printHashType SHA1 = "SHA1"
194197
printHashType SHA256 = "SHA256"
195198
printHashType (Truncated _ a) = printHashType a
196199

200+
201+
-- **********************************************************
202+
-- ** This is the c++ code we are porting for `addToStore` **
203+
-- **********************************************************
204+
--
205+
-- Path RemoteStore::addToStore(const string & name, const Path & _srcPath,
206+
-- bool recursive, HashType hashAlgo, PathFilter & filter, RepairFlag repair)
207+
-- {
208+
-- if (repair) throw Error("repairing is not supported when building through the Nix daemon");
209+
210+
-- auto conn(getConnection());
211+
212+
-- Path srcPath(absPath(_srcPath));
213+
214+
-- conn->to << wopAddToStore << name
215+
-- << ((hashAlgo == htSHA256 && recursive) ? 0 : 1) /* backwards compatibility hack */
216+
-- << (recursive ? 1 : 0)
217+
-- << printHashType(hashAlgo);
218+
219+
-- try {
220+
-- conn->to.written = 0;
221+
-- conn->to.warn = true;
222+
-- connections->incCapacity();
223+
-- {
224+
-- Finally cleanup([&]() { connections->decCapacity(); });
225+
-- dumpPath(srcPath, conn->to, filter);
226+
-- }
227+
-- conn->to.warn = false;
228+
-- conn.processStderr();
229+
-- } catch (SysError & e) {
230+
-- /* Daemon closed while we were sending the path. Probably OOM
231+
-- or I/O error. */
232+
-- if (e.errNo == EPIPE)
233+
-- try {
234+
-- conn.processStderr();
235+
-- } catch (EndOfFile & e) { }
236+
-- throw;
237+
-- }
238+
239+
-- return readStorePath(*this, conn->from);
240+
-- }
241+
197242
type PathFilter = Path -> Bool
243+
198244
addToStore
199245
:: forall a. (HasDigest a, AlgoVal a)
200246
=> LBS.ByteString
@@ -206,26 +252,53 @@ addToStore
206252
-> MonadStore Path
207253
addToStore name pth recursive algoProxy pfilter repair = do
208254
-- Get length first
209-
len <- liftIO $ LBS.length . B.runPut . putNar <$> localPackNar narEffectsIO pth
255+
-- len <- liftIO $ LBS.length . B.runPut . putNar <$> localPackNar narEffectsIO pth
210256
-- Fetch full NAR bytestring separately. We are trying to
211257
-- avoid forcing the full string in memory
212258
bs :: LBS.ByteString <- liftIO $ B.runPut . putNar <$> localPackNar narEffectsIO pth
259+
liftIO $ print (LBS.length bs)
260+
bs' <- liftIO $ putNar <$> localPackNar narEffectsIO pth
261+
let bs'' = putByteStringLen "nix-archive-1"
262+
let bs = sampleRegularBaseline
263+
let len = LBS.length bs
213264
runOpArgs AddToStore $ do
214265
putByteStringLen name
215266
-- TODO: really send the string 0 or 1? Or is this Word8's 0 and 1?
216267
putByteStringLen $ if algoVal @a `elem` [SHA256, Truncated 20 SHA256]
217268
&& recursive
218-
then "0"
219-
else "1"
269+
then (LBS.pack [0])
270+
else (LBS.pack [1])
271+
-- then "0"
272+
-- else "1"
220273
-- TODO: really send the string 0 or 1? Or is this Word8's 0 and 1?
221-
putByteStringLen $ if recursive then "0" else "1"
222-
putByteStringLen (T.encodeUtf8 . printHashType $ algoVal @a)
274+
putByteStringLen $ if recursive
275+
then (LBS.pack [1])
276+
else (LBS.pack [0])
277+
-- then "1"
278+
-- else "0"
223279

224-
putInt len
280+
-- putByteStringLen (T.encodeUtf8 . T.toLower . printHashType $ algoVal @a)
281+
282+
-- putByteStringLen bs
283+
-- putInt len
225284
B.putLazyByteString bs
285+
-- bs''
286+
-- when (len `mod` 8 /= 0) $
287+
-- let pad x = forM_ (take x $ cycle [0]) B.putWord8
288+
-- in pad $ fromIntegral $ 8 - (len `mod` 8)
289+
226290
fmap (fromMaybe $ error "TODO: Error") sockGetPath
227291

228292

293+
-- "hi" file turned to a NAR with `nix-store --dump`, Base64 encoded
294+
sampleRegularBaseline :: LBS.ByteString
295+
sampleRegularBaseline = B64.decodeLenient $ LBS.concat
296+
["DQAAAAAAAABuaXgtYXJjaGl2ZS0xAAAAAQAAAAAAAAAoAAAAAAA"
297+
,"AAAQAAAAAAAAAdHlwZQAAAAAHAAAAAAAAAHJlZ3VsYXIACAAAAA"
298+
,"AAAABjb250ZW50cwMAAAAAAAAAaGkKAAAAAAABAAAAAAAAACkAA"
299+
,"AAAAAAA"
300+
]
301+
229302
addTextToStore :: LBS.ByteString -> LBS.ByteString -> PathSet -> RepairFlag -> MonadStore (Maybe Path)
230303
addTextToStore name text references' repair = do
231304
runOpArgs AddTextToStore $ do

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import qualified Data.HashSet as HashSet
1515
import Network.Socket.ByteString (recv, sendAll)
1616

1717
import System.Nix.Store.Remote.Types
18+
import System.Nix.Hash
1819
import System.Nix.Path
1920
import System.Nix.Util
2021

@@ -73,7 +74,7 @@ mkPath p = case (pathName $ lBSToText p) of
7374
-- TODO: replace `undefined` with digest encoding function when
7475
-- [issue 24](https://github.com/haskell-nix/hnix-store/issues/24)
7576
-- is closed
76-
Just x -> Just $ Path (undefined $ LBS.toStrict p) x --XXX: hash
77+
Just x -> Just $ Path (hash $ LBS.toStrict p) x --XXX: hash
7778
Nothing -> Nothing
7879

7980
-- WOOT

0 commit comments

Comments
 (0)