Skip to content

Commit

Permalink
Merge #135: Clean-up: (return, (++), map) -> (pure, (<>), fmap)
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton-Latukha committed Feb 3, 2021
2 parents 792c76b + e2378e0 commit 57a2a65
Show file tree
Hide file tree
Showing 14 changed files with 77 additions and 77 deletions.
4 changes: 2 additions & 2 deletions hnix-store-core/src/System/Nix/Internal/Base32.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ digits32 = Vector.fromList "0123456789abcdfghijklmnpqrsvwxyz"

-- | Encode a 'BS.ByteString' in Nix's base32 encoding
encode :: ByteString -> Text
encode c = Data.Text.pack $ map char32 [nChar - 1, nChar - 2 .. 0]
encode c = Data.Text.pack $ fmap char32 [nChar - 1, nChar - 2 .. 0]
where
-- Each base32 character gives us 5 bits of information, while
-- each byte gives is 8. Because 'div' rounds down, we need to add
Expand Down Expand Up @@ -72,7 +72,7 @@ unsafeDecode what =
(Data.Text.unpack what)
of
[(i, _)] -> Right $ padded $ integerToBS i
x -> Left $ "Can't decode: readInt returned " ++ show x
x -> Left $ "Can't decode: readInt returned " <> show x
where
padded x
| Bytes.length x < decLen = x `Bytes.append` bstr
Expand Down
6 changes: 3 additions & 3 deletions hnix-store-core/src/System/Nix/Internal/Hash.hs
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,13 @@ mkNamedDigest name sriHash =
"sha1" -> SomeDigest <$> decodeGo @'SHA1 h
"sha256" -> SomeDigest <$> decodeGo @'SHA256 h
"sha512" -> SomeDigest <$> decodeGo @'SHA512 h
_ -> Left $ "Unknown hash name: " ++ T.unpack name
_ -> Left $ "Unknown hash name: " <> T.unpack name
decodeGo :: forall a . (NamedAlgo a, ValidAlgo a) => Text -> Either String (Digest a)
decodeGo h
| size == base16Len = decodeBase Base16 h
| size == base32Len = decodeBase Base32 h
| size == base64Len = decodeBase Base64 h
| otherwise = Left $ T.unpack sriHash ++ " is not a valid " ++ T.unpack name ++ " hash. Its length (" ++ show size ++ ") does not match any of " ++ show [base16Len, base32Len, base64Len]
| otherwise = Left $ T.unpack sriHash <> " is not a valid " <> T.unpack name <> " hash. Its length (" <> show size <> ") does not match any of " <> show [base16Len, base32Len, base64Len]
where
size = T.length h
hsize = hashSize @a
Expand Down Expand Up @@ -218,7 +218,7 @@ instance (ValidAlgo a, KnownNat n) => ValidAlgo ('Truncated n a) where
truncateDigest
:: forall n a.(KnownNat n) => Digest a -> Digest ('Truncated n a)
truncateDigest (Digest c) =
Digest $ BS.pack $ map truncOutputByte [0.. n-1]
Digest $ BS.pack $ fmap truncOutputByte [0.. n-1]
where
n = fromIntegral $ natVal (Proxy @n)

Expand Down
2 changes: 1 addition & 1 deletion hnix-store-core/src/System/Nix/Internal/Nar/Effects.hs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ streamStringOutIO f getChunk =
go handle = do
chunk <- getChunk
case chunk of
Nothing -> return ()
Nothing -> pure ()
Just c -> do
IO.liftIO $ Bytes.hPut handle c
go handle
Expand Down
28 changes: 14 additions & 14 deletions hnix-store-core/src/System/Nix/Internal/Nar/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ runParser effs (NarParser action) h target = do
Reader.runReaderT (Except.runExceptT $ State.evalStateT action state0) effs
`Exception.Lifted.catch` exceptionHandler
when (Either.isLeft unpackResult) cleanup
return unpackResult
pure unpackResult

where
state0 :: ParserState
Expand All @@ -103,7 +103,7 @@ runParser effs (NarParser action) h target = do

exceptionHandler :: Exception.Lifted.SomeException -> m (Either String a)
exceptionHandler e =
return $ Left $ "Exception while unpacking NAR file: " <> show e
pure $ Left $ "Exception while unpacking NAR file: " <> show e

cleanup :: m ()
cleanup =
Expand Down Expand Up @@ -141,7 +141,7 @@ data ParserState = ParserState

-- | Parse a NAR byte string, producing @()@.
-- Parsing a NAR is mostly used for its side-effect: producing
-- the file system objects packed in the NAR. That's why we return @()@
-- the file system objects packed in the NAR. That's why we pure @()@
parseNar :: (IO.MonadIO m, Fail.MonadFail m) => NarParser m ()
parseNar = do
expectStr "nix-archive-1"
Expand Down Expand Up @@ -179,7 +179,7 @@ parseSymlink = do
currentDirectoryAndFile :: Monad m => NarParser m (FilePath, FilePath)
currentDirectoryAndFile = do
dirStack <- State.gets directoryStack
return (List.foldr1 (</>) (List.reverse $ drop 1 dirStack), head dirStack)
pure (List.foldr1 (</>) (List.reverse $ drop 1 dirStack), head dirStack)


-- | Internal data type representing symlinks encountered in the NAR
Expand Down Expand Up @@ -224,7 +224,7 @@ parseFile = do
getChunk = do
bytesLeft <- IO.liftIO $ IORef.readIORef bytesLeftVar
if bytesLeft == 0
then return Nothing
then pure Nothing
else do
chunk <- IO.liftIO $ Bytes.hGetSome narHandle $ fromIntegral $ min 10000 bytesLeft
when (Bytes.null chunk) (Fail.fail "ZERO BYTES")
Expand All @@ -234,7 +234,7 @@ parseFile = do
-- clean up chunks from previous runs. Without it, heap memory usage can
-- quickly spike
IO.liftIO $ Concurrent.threadDelay 10
return $ Just chunk
pure $ Just chunk

target <- currentFile
streamFile <- Reader.asks Nar.narStreamFile
Expand Down Expand Up @@ -373,7 +373,7 @@ parens act = do
expectStr "("
r <- act
expectStr ")"
return r
pure r


-- | Sort links in the symlink stack according to their connectivity
Expand Down Expand Up @@ -406,15 +406,15 @@ createLinks = do
(linkPWD l </> linkTarget l)
fileAbsPath <- Directory.canonicalizePath
(linkFile l)
return (fileAbsPath, targetAbsPath)
pure (fileAbsPath, targetAbsPath)
let linkGraph = Graph.edges canonicalLinks
case Graph.topSort linkGraph of
Left _ -> error "Symlinks form a loop"
Right sortedNodes ->
let
sortedLinks = flip Map.lookup linkLocations <$> sortedNodes
in
return $ catMaybes sortedLinks
pure $ catMaybes sortedLinks


------------------------------------------------------------------------------
Expand All @@ -426,26 +426,26 @@ consume
:: (IO.MonadIO m, Fail.MonadFail m)
=> Int
-> NarParser m ByteString
consume 0 = return ""
consume 0 = pure ""
consume n = do
state0 <- State.get
newBytes <- IO.liftIO $ Bytes.hGetSome (handle state0) (max 0 n)
when (Bytes.length newBytes < n) $
Fail.fail $
"consume: Not enough bytes in handle. Wanted "
<> show n <> " got " <> show (Bytes.length newBytes)
return newBytes
pure newBytes


-- | Pop a string off the token stack
popStr :: Monad m => NarParser m (Maybe Text)
popStr = do
s <- State.get
case List.uncons (tokenStack s) of
Nothing -> return Nothing
Nothing -> pure Nothing
Just (x, xs) -> do
State.put $ s { tokenStack = xs }
return $ Just x
pure $ Just x


-- | Push a string onto the token stack
Expand All @@ -472,7 +472,7 @@ popFileName =
currentFile :: Monad m => NarParser m FilePath
currentFile = do
dirStack <- State.gets directoryStack
return $ List.foldr1 (</>) $ List.reverse dirStack
pure $ List.foldr1 (</>) $ List.reverse dirStack


-- | Add a link to the collection of encountered symlinks
Expand Down
2 changes: 1 addition & 1 deletion hnix-store-core/src/System/Nix/Internal/Nar/Streamer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ streamNarIO yield effs basePath = do
yield $ str "("
r <- act
yield $ str ")"
return r
pure r

-- Read, yield, and pad the file
yieldFile :: FilePath -> Int64 -> m ()
Expand Down
2 changes: 1 addition & 1 deletion hnix-store-core/src/System/Nix/ReadonlyStore.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ makeTextPath
makeTextPath fp nm h refs = makeStorePath fp ty h nm
where
ty =
BS.intercalate ":" ("text" : map storePathToRawFilePath (HS.toList refs))
BS.intercalate ":" ("text" : fmap storePathToRawFilePath (HS.toList refs))

makeFixedOutputPath
:: forall hashAlgo
Expand Down
50 changes: 25 additions & 25 deletions hnix-store-core/tests/NarFormat.hs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ unit_packSelfSrcDir = Temp.withSystemTempDirectory "nar-test" $ \tmpDir -> do
let go dir = do
srcHere <- doesDirectoryExist dir
case srcHere of
False -> return ()
False -> pure ()
True -> do
IO.withFile narFilePath IO.WriteMode $ \h ->
buildNarIO narEffectsIO "src" h
Expand Down Expand Up @@ -231,7 +231,7 @@ test_streamManyFilesToNar = HU.testCaseSteps "streamManyFilesToNar" $ \step ->
IO.withFile "hnar" IO.WriteMode $ \h ->
buildNarIO narEffectsIO narFilePath h
filesPostcount <- countProcessFiles
return $ filesPostcount - filesPrecount
pure $ filesPostcount - filesPrecount

step "create test files"
Directory.createDirectory packagePath
Expand Down Expand Up @@ -316,7 +316,7 @@ assertBoundedMemory = do
bytes <- max_live_bytes <$> getRTSStats
bytes < 100 * 1000 * 1000 `shouldBe` True
#else
return ()
pure ()
#endif


Expand All @@ -339,34 +339,34 @@ packThenExtract testName setup =
Left (_ :: SomeException) -> print ("No nix-store on system" :: String)
Right _ -> do
let
nixNarFile = narFilePath ++ ".nix"
hnixNarFile = narFilePath ++ ".hnix"
outputFile = narFilePath ++ ".out"
nixNarFile = narFilePath <> ".nix"
hnixNarFile = narFilePath <> ".hnix"
outputFile = narFilePath <> ".out"

step $ "Produce nix-store nar to " ++ nixNarFile
(_,_,_,handle) <- P.createProcess (P.shell $ "nix-store --dump " ++ narFilePath ++ " > " ++ nixNarFile)
step $ "Produce nix-store nar to " <> nixNarFile
(_,_,_,handle) <- P.createProcess (P.shell $ "nix-store --dump " <> narFilePath <> " > " <> nixNarFile)
void $ P.waitForProcess handle

step $ "Build NAR from " ++ narFilePath ++ " to " ++ hnixNarFile
step $ "Build NAR from " <> narFilePath <> " to " <> hnixNarFile
-- narBS <- buildNarIO narEffectsIO narFile
IO.withFile hnixNarFile IO.WriteMode $ \h ->
buildNarIO narEffectsIO narFilePath h

-- BSL.writeFile hnixNarFile narBS

step $ "Unpack NAR to " ++ outputFile
step $ "Unpack NAR to " <> outputFile
_narHandle <- IO.withFile nixNarFile IO.ReadMode $ \h ->
unpackNarIO narEffectsIO h outputFile

return ()
pure ()

-- | Count file descriptors owned by the current process
countProcessFiles :: IO Int
countProcessFiles = do
pid <- Unix.getProcessID
let fdDir = "/proc/" ++ show pid ++ "/fd"
let fdDir = "/proc/" <> show pid <> "/fd"
fds <- P.readProcess "ls" [fdDir] ""
return $ length $ words fds
pure $ length $ words fds


-- | Read the binary output of `nix-store --dump` for a filepath
Expand Down Expand Up @@ -437,12 +437,12 @@ sampleLargeDir fSize = Directory $ Map.fromList $ [
(FilePathPart "bf1", sampleLargeFile fSize)
, (FilePathPart "bf2", sampleLargeFile' fSize)
]
++ [ (FilePathPart (BSC.pack $ 'f' : show n),
<> [ (FilePathPart (BSC.pack $ 'f' : show n),
Regular Nar.NonExecutable 10000 (BSL.take 10000 (BSL.cycle "hi ")))
| n <- [1..100 :: Int]]
++ [
<> [
(FilePathPart "d", Directory $ Map.fromList
[ (FilePathPart (BSC.pack $ "df" ++ show n)
[ (FilePathPart (BSC.pack $ "df" <> show n)
, Regular Nar.NonExecutable 10000 (BSL.take 10000 (BSL.cycle "subhi ")))
| n <- [1..100 :: Int]]
)
Expand Down Expand Up @@ -595,13 +595,13 @@ instance Arbitrary FileSystemObject where
arbName :: Gen FilePathPart
arbName = fmap (FilePathPart . BS.pack . fmap (fromIntegral . fromEnum)) $ do
Positive n <- arbitrary
replicateM n (elements $ ['a'..'z'] ++ ['0'..'9'])
replicateM n (elements $ ['a'..'z'] <> ['0'..'9'])

arbDirectory :: Int -> Gen FileSystemObject
arbDirectory n = fmap (Directory . Map.fromList) $ replicateM n $ do
nm <- arbName
f <- oneof [arbFile, arbDirectory (n `div` 2)]
return (nm,f)
pure (nm,f)

------------------------------------------------------------------------------
-- | Serialize Nar to lazy ByteString
Expand All @@ -615,7 +615,7 @@ putNar (Nar file) = header <> parens (putFile file)
strs ["type", "regular"]
>> (if isExec == Nar.Executable
then strs ["executable", ""]
else return ())
else pure ())
>> putContents fSize contents

putFile (SymLink target) =
Expand Down Expand Up @@ -678,13 +678,13 @@ getNar = fmap Nar $ header >> parens getFile
>> assertStr "")
assertStr_ "contents"
(fSize, contents) <- sizedStr
return $ Regular (fromMaybe Nar.NonExecutable mExecutable) fSize contents
pure $ Regular (fromMaybe Nar.NonExecutable mExecutable) fSize contents

getDirectory = do
assertStr_ "type"
assertStr_ "directory"
fs <- many getEntry
return $ Directory (Map.fromList fs)
pure $ Directory (Map.fromList fs)

getSymLink = do
assertStr_ "type"
Expand All @@ -699,8 +699,8 @@ getNar = fmap Nar $ header >> parens getFile
name <- E.decodeUtf8 . BSL.toStrict <$> str
assertStr_ "node"
file <- parens getFile
maybe (fail $ "Bad FilePathPart: " ++ show name)
(return . (,file))
maybe (fail $ "Bad FilePathPart: " <> show name)
(pure . (,file))
(filePathPart $ E.encodeUtf8 name)

-- Fetch a length-prefixed, null-padded string
Expand All @@ -710,13 +710,13 @@ getNar = fmap Nar $ header >> parens getFile
n <- getInt64le
s <- getLazyByteString n
_ <- getByteString . fromIntegral $ padLen n
return (n,s)
pure (n,s)

parens m = assertStr "(" *> m <* assertStr ")"

assertStr_ = void . assertStr
assertStr s = do
s' <- str
if s == s'
then return s
then pure s
else fail "No"
12 changes: 6 additions & 6 deletions hnix-store-remote/src/System/Nix/Store/Remote.hs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ buildDerivation p drv buildMode = do
putInt 0

res <- getSocketIncremental $ getBuildResult
return res
pure res

ensurePath :: StorePath -> MonadStore ()
ensurePath pn = do
Expand All @@ -189,14 +189,14 @@ findRoots = do
<*> getPath sd

r <- catRights res
return $ Data.Map.Strict.fromList r
pure $ Data.Map.Strict.fromList r
where
catRights :: [(a, Either String b)] -> MonadStore [(a, b)]
catRights = mapM ex

ex :: (a, Either [Char] b) -> MonadStore (a, b)
ex (x , Right y) = return (x, y)
ex (_x, Left e ) = error $ "Unable to decode root: " ++ e
ex (x , Right y) = pure (x, y)
ex (_x, Left e ) = error $ "Unable to decode root: " <> e

isValidPathUncached :: StorePath -> MonadStore Bool
isValidPathUncached p = do
Expand Down Expand Up @@ -263,7 +263,7 @@ queryPathInfoUncached path = do

trust = if ultimate then BuiltLocally else BuiltElsewhere

return $ StorePathMetadata{..}
pure $ StorePathMetadata{..}

queryReferrers :: StorePath -> MonadStore StorePathSet
queryReferrers p = do
Expand Down Expand Up @@ -311,7 +311,7 @@ queryMissing ps = do
unknown <- sockGetPaths
downloadSize' <- sockGetInt
narSize' <- sockGetInt
return (willBuild, willSubstitute, unknown, downloadSize', narSize')
pure (willBuild, willSubstitute, unknown, downloadSize', narSize')

optimiseStore :: MonadStore ()
optimiseStore = void $ simpleOp OptimiseStore
Expand Down
Loading

0 comments on commit 57a2a65

Please sign in to comment.