on master help sign in
Merge branch '90-safe-intl-filename' of https://github.com/Blaisorblade/yaml
commit7112a0775578d5595c715541bde707e301ab2897 authorMichael Snoyman <michael@snoyman.com> dateWed 31 Aug 2016, 10:34 (10 years ago) parent1f592635c2b6b2
6 files changed+48−13
ChangeLog.md+4 Text/Libyaml.hs+26−12 examples/Config.hs+1 test/Data/YamlSpec.hs+13 test/resources/accenté/foo.yaml+1 yaml.cabal+3−1
ChangeLog.md+4
@@ -1,3 +1,7 @@
1+## 0.8.18.2
2+
3+* Handle non-ASCII filenames correctly on Windows [#91](https://github.com/snoyberg/yaml/pull/91)
4+
15## 0.8.18.1
26
37* Improve prettyPrintParseException when context is empty [#89](https://github.com/snoyberg/yaml/pull/89)
Text/Libyaml.hs+26−12
@@ -29,6 +29,7 @@ module Text.Libyaml
2929
3030import Prelude hiding (pi)
3131
32+import Data.Bits ((.|.))
3233import Foreign.C
3334import Foreign.Ptr
3435import Foreign.ForeignPtr
@@ -36,6 +37,7 @@ import Foreign.ForeignPtr
3637import Foreign.ForeignPtr.Unsafe
3738#endif
3839import Foreign.Marshal.Alloc
40+import qualified System.Posix.Internals as Posix
3941
4042#if !MIN_VERSION_base(4,8,0)
4143import Control.Applicative
@@ -147,11 +149,10 @@ foreign import ccall unsafe "yaml_parser_set_input_file"
147149data FileStruct
148150type File = Ptr FileStruct
149151
150−foreign import ccall unsafe "fopen"
151− c_fopen :: Ptr CChar
152− -> Ptr CChar
153− -> IO File
154−
152+foreign import ccall unsafe "fdopen"
153+ c_fdopen :: CInt
154+ -> Ptr CChar
155+ -> IO File
155156foreign import ccall unsafe "fclose"
156157 c_fclose :: File
157158 -> IO ()
@@ -503,6 +504,24 @@ decode bs =
503504 c_yaml_parser_delete ptr
504505 free ptr
505506
507+-- XXX copied from GHC.IO.FD
508+std_flags, read_flags, output_flags, write_flags :: CInt
509+std_flags = Posix.o_NOCTTY
510+output_flags = std_flags .|. Posix.o_CREAT
511+read_flags = std_flags .|. Posix.o_RDONLY
512+write_flags = output_flags .|. Posix.o_WRONLY
513+
514+-- | Open a C FILE* from a file path, using internal GHC API to work correctly
515+-- on all platforms, even on non-ASCII filenames. The opening mode must be
516+-- indicated via both 'rawOpenFlags' and 'openMode'.
517+openFile :: FilePath -> CInt -> String -> IO File
518+openFile file rawOpenFlags openMode = do
519+ fd <- liftIO $ Posix.withFilePath file $ \file' ->
520+ Posix.c_open file' rawOpenFlags 0o666
521+ if fd /= (-1)
522+ then withCString openMode $ \openMode' -> c_fdopen fd openMode'
523+ else return nullPtr
524+
506525decodeFile :: MonadResource m => FilePath
507526#if MIN_VERSION_conduit(1, 0, 0)
508527 -> Producer m Event
@@ -521,12 +540,9 @@ decodeFile file =
521540 free ptr
522541 throwIO $ YamlException "Yaml out of memory"
523542 else do
524− file' <- liftIO
525− $ withCString file $ \file' -> withCString "r" $ \r' ->
526− c_fopen file' r'
543+ file' <- openFile file read_flags "r"
527544 if file' == nullPtr
528545 then do
529− c_fclose_helper file'
530546 c_yaml_parser_delete ptr
531547 free ptr
532548 throwIO $ YamlException
@@ -599,9 +615,7 @@ encodeFile filePath =
599615 bracketP getFile c_fclose $ \file -> runEmitter (alloc file) (\u _ -> return u)
600616 where
601617 getFile = do
602− file <- withCString filePath $
603− \filePath' -> withCString "w" $
604− \w' -> c_fopen filePath' w'
618+ file <- openFile filePath write_flags "w"
605619 if file == nullPtr
606620 then throwIO $ YamlException $ "could not open file for write: " ++ filePath
607621 else return file
examples/Config.hs+1
@@ -9,6 +9,7 @@ import Data.Yaml (FromJSON(..), (.:))
99import Text.RawString.QQ
1010import Data.ByteString (ByteString)
1111import Control.Applicative
12+import Prelude -- Ensure Applicative is in scope and we have no warnings, before/after AMP.
1213
1314configYaml :: ByteString
1415configYaml = [r|
test/Data/YamlSpec.hs+13
@@ -19,6 +19,7 @@ import Control.Monad
1919import Control.Exception (try, SomeException)
2020import Test.Hspec
2121import Data.Either.Compat
22+import System.Directory (createDirectory)
2223import Test.Mockery.Directory
2324
2425import qualified Data.Yaml as D
@@ -63,6 +64,7 @@ spec = do
6364 describe "Data.Yaml" $ do
6465 it "encode/decode" caseEncodeDecodeData
6566 it "encode/decode file" caseEncodeDecodeFileData
67+ it "encode/decode files with non-ASCII names" caseEncodeDecodeNonAsciiFileData
6668 it "encode/decode strings" caseEncodeDecodeStrings
6769 it "decode invalid file" caseDecodeInvalid
6870 it "processes datatypes" caseDataTypes
@@ -317,6 +319,17 @@ caseEncodeDecodeFileData = withFile "" $ \fp -> do
317319 out <- D.decodeFile fp
318320 out @?= Just sample
319321
322+caseEncodeDecodeNonAsciiFileData :: Assertion
323+caseEncodeDecodeNonAsciiFileData = do
324+ let mySample = (object ["foo" .= True])
325+ inTempDirectory $ do
326+ createDirectory "accenté"
327+ D.encodeFile "accenté/bar.yaml" mySample
328+ out1 <- D.decodeFile "accenté/bar.yaml"
329+ out1 @?= Just mySample
330+ out2 <- D.decodeFile "test/resources/accenté/foo.yaml"
331+ out2 @?= Just mySample
332+
320333caseEncodeDecodeStrings :: Assertion
321334caseEncodeDecodeStrings = do
322335 let out = D.decode $ D.encode sample
test/resources/accenté/foo.yamlnew file+1
@@ -0,0 +1 @@
1+foo: true
yaml.cabal+3−1
@@ -1,5 +1,5 @@
11name: yaml
2−version: 0.8.18.1
2+version: 0.8.18.2
33license: BSD3
44license-file: LICENSE
55author: Michael Snoyman <michael@snoyman.com>, Anton Ageev <antage@gmail.com>,Kirill Simonov
@@ -20,6 +20,7 @@ extra-source-files: c/helper.h
2020 test/resources/foo.yaml
2121 test/resources/bar.yaml
2222 test/resources/baz.yaml
23+ test/resources/accenté/foo.yaml
2324 test/resources/loop/foo.yaml
2425 test/resources/loop/bar.yaml
2526 README.md
@@ -130,6 +131,7 @@ test-suite spec
130131 , text
131132 , aeson >= 0.7
132133 , unordered-containers
134+ , directory
133135 , vector
134136 , resourcet
135137 , aeson-qq