avoid throwing exception when String is not encoded using the filesystem encoding

Since _encodeFilePath generates a String that doesn't use the filesystem
encoding, when this exception is caught, we know we already have such a
String, and can just return it as-is.
This commit is contained in:
Joey Hess 2015-08-12 10:57:48 -04:00
parent 4e4e11849a
commit e953be11af
2 changed files with 7 additions and 4 deletions

View file

@ -31,7 +31,8 @@ fromB64 = fromMaybe bad . fromB64Maybe
bad = error "bad base64 encoded data" bad = error "bad base64 encoded data"
-- Only ascii strings are tested, because an arbitrary string may contain -- Only ascii strings are tested, because an arbitrary string may contain
-- characters not encoded using the FileSystemEncoding. -- characters not encoded using the FileSystemEncoding, which would thus
-- not roundtrip, as fromB64 always generates an output encoded that way.
prop_b64_roundtrips :: String -> Bool prop_b64_roundtrips :: String -> Bool
prop_b64_roundtrips s prop_b64_roundtrips s
| all (isAscii) s = s == fromB64 (toB64 s) | all (isAscii) s = s == fromB64 (toB64 s)

View file

@ -35,6 +35,8 @@ import qualified Data.ByteString.Lazy as L
import qualified Data.ByteString.Lazy.UTF8 as L8 import qualified Data.ByteString.Lazy.UTF8 as L8
#endif #endif
import Utility.Exception
{- Sets a Handle to use the filesystem encoding. This causes data {- Sets a Handle to use the filesystem encoding. This causes data
- written or read from it to be encoded/decoded the same - written or read from it to be encoded/decoded the same
- as ghc 7.4 does to filenames etc. This special encoding - as ghc 7.4 does to filenames etc. This special encoding
@ -70,14 +72,14 @@ withFilePath fp f = Encoding.getFileSystemEncoding
- effects. - effects.
- -
- If the FilePath contains a value that is not legal in the filesystem - If the FilePath contains a value that is not legal in the filesystem
- encoding, this may throw an exception. For example, "\226" is not valid - encoding, rather than thowing an exception, it will be returned as-is.
- in the C locale, but is in utf locales.
-} -}
{-# NOINLINE _encodeFilePath #-} {-# NOINLINE _encodeFilePath #-}
_encodeFilePath :: FilePath -> String _encodeFilePath :: FilePath -> String
_encodeFilePath fp = unsafePerformIO $ do _encodeFilePath fp = unsafePerformIO $ do
enc <- Encoding.getFileSystemEncoding enc <- Encoding.getFileSystemEncoding
GHC.withCString enc fp $ GHC.peekCString Encoding.char8 GHC.withCString enc fp (GHC.peekCString Encoding.char8)
`catchNonAsync` (\_ -> return fp)
{- Encodes a FilePath into a Md5.Str, applying the filesystem encoding. -} {- Encodes a FilePath into a Md5.Str, applying the filesystem encoding. -}
md5FilePath :: FilePath -> MD5.Str md5FilePath :: FilePath -> MD5.Str