Skip to content

Commit

Permalink
Fix NAR encoding to always use lazy bytestring, not bytestring.char8
Browse files Browse the repository at this point in the history
  • Loading branch information
imalsogreg committed May 7, 2018
1 parent 43f5ea9 commit 9c93d04
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 18 deletions.
7 changes: 5 additions & 2 deletions hnix-store-core/src/System/Nix/Nar.hs
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,12 @@ localPackNar effs basePath = Nar <$> localPackFSO basePath
fType <- (,) <$> narIsDir effs path' <*> narIsSymLink effs path'
case fType of
(_, True) -> return $ SymLink (T.pack path')
(False, _) -> Regular <$> isExecutable effs path' <*> narReadFile effs path'
(False, _) -> Regular <$> isExecutable effs path'
<*> narReadFile effs path'
(True , _) -> fmap (Directory . Map.fromList) $ do
fs <- narListDir effs path'
forM fs $ \fp -> (FilePathPart (T.pack fp),) <$> localPackFSO (path' </> fp)
forM fs $ \fp ->
(FilePathPart (T.pack fp),) <$> localPackFSO (path' </> fp)



Expand All @@ -241,6 +243,7 @@ narEffectsIO = NarEffects {
, narIsSymLink = pathIsSymbolicLink
}


isExecutable :: Functor m => NarEffects m -> FilePath -> m IsExecutable
isExecutable effs fp =
bool NonExecutable Executable . executable <$> narGetPerms effs fp
27 changes: 11 additions & 16 deletions hnix-store-core/tests/NarFormat.hs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import Data.Binary.Get (Get (..), runGet)
import Data.Binary.Put (Put (..), runPut)
import qualified Data.ByteString.Base64.Lazy as B64
import qualified Data.ByteString.Lazy as BSL
import qualified Data.ByteString.Lazy.Char8 as BSC
-- import qualified Data.ByteString.Lazy.Char8 as BSC
import qualified Data.Map as Map
import Data.Maybe (isJust)
import qualified Data.Text as T
Expand Down Expand Up @@ -100,6 +100,7 @@ unit_packSelfSrcDir = do
nixStoreNar <- getNixStoreDump "src"
HU.assertEqual "src dir serializes the same between hnix-store and nix-store" hnixNar nixStoreNar


-- | Generate the ground-truth encoding on the fly with
-- `nix-store --dump`, rather than generating fixtures
-- beforehand
Expand All @@ -112,16 +113,16 @@ filesystemNixStore s n = do
Right _ ->
bracket (return ()) (\_ -> P.runCommand "rm -rf testfile") $ \_ -> do
localUnpackNar narEffectsIO "testfile" n
-- nixStoreNar <- getNixStoreDump "testfile"
nixStoreNar <- P.readProcess "nix-store" ["--dump", "testfile"] ""
nixStoreNar <- getNixStoreDump "testfile"
HU.assertEqual s (runPut (putNar n))
-- nixStoreNar
(BSC.pack nixStoreNar)
nixStoreNar


-- | Read the binary output of `nix-store --dump` for a filepath
getNixStoreDump :: String -> IO BSL.ByteString
getNixStoreDump fp = do
(_,Just h, _, _) <- P.createProcess
(P.proc "nix-store" ["--dump", "src"])
(P.proc "nix-store" ["--dump", fp])
{P.std_out = P.CreatePipe}
BSL.hGetContents h

Expand Down Expand Up @@ -170,19 +171,14 @@ sampleDirectory' = Directory $ Map.fromList [
])
]

-- NOTE: The `nixStoreBigFile` test fails at 9000000 bytes
sampleLargeFile :: FileSystemObject
sampleLargeFile =
Regular NonExecutable (BSL.take 1000000 (BSL.cycle "Lorem ipsum "))
Regular NonExecutable (BSL.take 9000000 (BSL.cycle "Lorem ipsum "))


-- NOTE: The `nixStoreBigDir` test fails at 9000000 bytes with the error:
-- nixStoreBigDir: error: writing to file: Broken pipe
-- FAIL
-- Exception: fd:5: hGetContents: invalid argument (invalid byte sequence)
sampleLargeFile' :: FileSystemObject
sampleLargeFile' =
Regular NonExecutable (BSL.take 1000000 (BSL.cycle "Lorems ipsums "))
Regular NonExecutable (BSL.take 9000000 (BSL.cycle "Lorems ipsums "))

sampleLargeDir :: FileSystemObject
sampleLargeDir = Directory $ Map.fromList $ [
Expand Down Expand Up @@ -287,9 +283,8 @@ instance Arbitrary FileSystemObject where
arbFile :: Gen FileSystemObject
arbFile = Regular
<$> elements [NonExecutable, Executable]
<*> fmap BSL.pack arbitrary
-- <*> oneof [fmap BSL.pack (arbitrary) , -- Binary File
-- fmap BSC.pack (arbitrary) ] -- ASCII File
<*> oneof [fmap BSL.pack arbitrary , -- Binary File
fmap BSC.pack arbitrary ] -- ASCII File

arbName :: Gen FilePathPart
arbName = fmap (FilePathPart . T.pack) $ do
Expand Down

0 comments on commit 9c93d04

Please sign in to comment.