Skip to content

Commit

Permalink
Merge pull request #5804
Browse files Browse the repository at this point in the history
Fix corrupted config file header for non-ASCII package names
  • Loading branch information
hvr committed Mar 4, 2019
2 parents b69e471 + 89ab049 commit 1907a08
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
3 changes: 3 additions & 0 deletions Cabal/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
supports fully static linking;
[`glibc` has some issues](https://sourceware.org/glibc/wiki/FAQ#Even_statically_linked_programs_need_some_shared_libraries_which_is_not_acceptable_for_me.__What_can_I_do.3F)
with fully static linking.
* Fix corrupted config file header for non-ASCII package names
([2557](https://github.com/haskell/cabal/issues/2557)).
* Extend `Distribution.Simple.Utils.rewriteFileEx` from ASCII to UTF-8 encoding.

----

Expand Down
4 changes: 2 additions & 2 deletions Cabal/Distribution/Simple/Configure.hs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ parseHeader header = case BLC8.words header of
["Saved", "package", "config", "for", pkgId, "written", "by", cabalId,
"using", compId] ->
fromMaybe (throw ConfigStateFileBadHeader) $ do
_ <- simpleParsec (BLC8.unpack pkgId) :: Maybe PackageIdentifier
_ <- simpleParsec (fromUTF8LBS pkgId) :: Maybe PackageIdentifier
cabalId' <- simpleParsec (BLC8.unpack cabalId)
compId' <- simpleParsec (BLC8.unpack compId)
return (cabalId', compId')
Expand All @@ -287,7 +287,7 @@ showHeader :: PackageIdentifier -- ^ The processed package.
-> ByteString
showHeader pkgId = BLC8.unwords
[ "Saved", "package", "config", "for"
, BLC8.pack $ prettyShow pkgId
, toUTF8LBS $ prettyShow pkgId
, "written", "by"
, BLC8.pack $ prettyShow currentCabalId
, "using"
Expand Down
19 changes: 11 additions & 8 deletions Cabal/Distribution/Simple/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ import Control.Concurrent.MVar
( newEmptyMVar, putMVar, takeMVar )
import Data.Typeable
( cast )
import qualified Data.ByteString.Lazy.Char8 as BS.Char8
import qualified Data.ByteString.Lazy as BS

import System.Directory
( Permissions(executable), getDirectoryContents, getPermissions
Expand Down Expand Up @@ -1401,19 +1401,22 @@ rewriteFile = rewriteFileEx normal
-- the same as the existing content then leave the file as is so that we do not
-- update the file's modification time.
--
-- NB: the file is assumed to be ASCII-encoded.
-- NB: Before Cabal-3.0 the file content was assumed to be
-- ASCII-representable. Since Cabal-3.0 the file is assumed to be
-- UTF-8 encoded.
rewriteFileEx :: Verbosity -> FilePath -> String -> IO ()
rewriteFileEx verbosity path newContent =
flip catchIO mightNotExist $ do
existingContent <- annotateIO verbosity $ readFile path
_ <- evaluate (length existingContent)
unless (existingContent == newContent) $
existingContent <- annotateIO verbosity $ BS.readFile path
_ <- evaluate (BS.length existingContent)
unless (existingContent == newContent') $
annotateIO verbosity $
writeFileAtomic path (BS.Char8.pack newContent)
writeFileAtomic path newContent'
where
newContent' = toUTF8LBS newContent

mightNotExist e | isDoesNotExistError e
= annotateIO verbosity $ writeFileAtomic path
(BS.Char8.pack newContent)
= annotateIO verbosity $ writeFileAtomic path newContent'
| otherwise
= ioError e

Expand Down

0 comments on commit 1907a08

Please sign in to comment.