Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nar: fix ordering of case-hacked paths on macOS #286

Merged
merged 2 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions hnix-store-nar/src/System/Nix/Nar/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ data NarOptions = NarOptions {

defaultNarOptions :: NarOptions
defaultNarOptions = NarOptions {
optUseCaseHack =
if System.Info.os == "darwin"
then True
else False
optUseCaseHack = System.Info.os == "darwin"
}

caseHackSuffix :: Text
Expand Down
2 changes: 1 addition & 1 deletion hnix-store-nar/src/System/Nix/Nar/Parser.hs
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ parseDirectory = do
conflictCount <- getFilePathConflictCount key
pure $
if conflictCount > 0 then
fName <> Nar.caseHackSuffix <> (Text.pack $ show conflictCount)
fName <> Nar.caseHackSuffix <> Text.pack (show conflictCount)
else
fName

Expand Down
40 changes: 24 additions & 16 deletions hnix-store-nar/src/System/Nix/Nar/Streamer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ module System.Nix.Nar.Streamer

import Data.ByteString (ByteString)
import Data.Int (Int64)
import Data.Text (Text)
import qualified Data.Map.Strict as Map

import Control.Monad ( forM_
, when
Expand All @@ -23,7 +23,7 @@ import qualified Data.ByteString.Lazy as Bytes.Lazy
import qualified Data.Foldable
import qualified Data.List
import qualified Data.Serialize as Serial
import qualified Data.Text as T (pack, breakOn)
import qualified Data.Text as T (pack, unpack)
import qualified Data.Text.Encoding as TE (encodeUtf8)
import System.FilePath ((</>))

Expand Down Expand Up @@ -92,18 +92,24 @@ streamNarIOWithOptions opts effs basePath yield = do
isDir <- IO.liftIO $ Nar.narIsDir effs path
if isDir then do
fs <- IO.liftIO (Nar.narListDir effs path)
let entries =
foldr (\f acc ->
let
name =
if Nar.optUseCaseHack opts
then undoCaseHack f
else f
in
case Map.insertLookupWithKey (\_ n _ -> n) name f acc of
(Nothing, newMap) -> newMap
(Just conflict, _) -> error $ "File name collision between " ++ (path </> name) ++ " and " ++ (path </> conflict)
Comment on lines +103 to +105
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nix throws an error if there are duplicate paths in the NAR.

) Map.empty fs
yield $ strs ["type", "directory"]
forM_ (Data.List.sort fs) $ \f -> do
forM_ (Map.toAscList entries) $ \(unhacked, original) -> do
yield $ str "entry"
parens $ do
let fullName = path </> f
let serializedPath =
if Nar.optUseCaseHack opts then
filePathToBSWithCaseHack f
else
filePathToBS f
yield $ strs ["name", serializedPath, "node"]
parens $ go fullName
yield $ strs ["name", filePathToBS unhacked, "node"]
parens $ go (path </> original)
else do
isExec <- IO.liftIO $ Nar.narIsExec effs path
yield $ strs ["type", "regular"]
Expand Down Expand Up @@ -148,8 +154,10 @@ strs xs = Bytes.concat $ str <$> xs
filePathToBS :: FilePath -> ByteString
filePathToBS = TE.encodeUtf8 . T.pack

filePathToBSWithCaseHack :: FilePath -> ByteString
filePathToBSWithCaseHack = TE.encodeUtf8 . undoCaseHack . T.pack

undoCaseHack :: Text -> Text
undoCaseHack = fst . T.breakOn Nar.caseHackSuffix
undoCaseHack :: FilePath -> FilePath
undoCaseHack f =
case Data.List.findIndex (caseHackSuffix `Data.List.isPrefixOf`) (Data.List.tails f) of
Just index -> take index f
Nothing -> f
where
caseHackSuffix = T.unpack Nar.caseHackSuffix
Loading