From ec1b9da72f13d74272bc5d48022746af4e943c86 Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Tue, 1 Jan 2019 22:44:04 -0400 Subject: [PATCH] avoid abusing from/toRawFilePath for non-FilePaths --- Types/UUID.hs | 4 ++-- Utility/FileSystemEncoding.hs | 32 +++++++++++++++++++++----------- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/Types/UUID.hs b/Types/UUID.hs index 443cbb1e57..b49c439ca1 100644 --- a/Types/UUID.hs +++ b/Types/UUID.hs @@ -44,10 +44,10 @@ instance ToUUID B.ByteString where | otherwise = UUID b instance FromUUID String where - fromUUID s = fromRawFilePath (fromUUID s) + fromUUID s = decodeBS' (fromUUID s) instance ToUUID String where - toUUID s = toUUID (toRawFilePath s) + toUUID s = toUUID (encodeBS' s) -- There is no matching FromUUID U.UUID because a git-annex UUID may -- be NoUUID or perhaps contain something not allowed in a canonical UUID. diff --git a/Utility/FileSystemEncoding.hs b/Utility/FileSystemEncoding.hs index 1c0442f308..9e7e47e49f 100644 --- a/Utility/FileSystemEncoding.hs +++ b/Utility/FileSystemEncoding.hs @@ -16,9 +16,11 @@ module Utility.FileSystemEncoding ( fromRawFilePath, toRawFilePath, decodeBL, - decodeBS, encodeBL, + decodeBS, encodeBS, + decodeBS', + encodeBS', decodeW8, encodeW8, encodeW8NUL, @@ -119,13 +121,6 @@ decodeBL = encodeW8NUL . L.unpack decodeBL = L8.toString #endif -decodeBS :: S.ByteString -> FilePath -#ifndef mingw32_HOST_OS -decodeBS = encodeW8NUL . S.unpack -#else -decodeBS = S8.toString -#endif - {- Encodes a FilePath into a ByteString, applying the filesystem encoding. -} encodeBL :: FilePath -> L.ByteString #ifndef mingw32_HOST_OS @@ -134,6 +129,13 @@ encodeBL = L.pack . decodeW8NUL encodeBL = L8.fromString #endif +decodeBS :: S.ByteString -> FilePath +#ifndef mingw32_HOST_OS +decodeBS = encodeW8NUL . S.unpack +#else +decodeBS = S8.toString +#endif + encodeBS :: FilePath -> S.ByteString #ifndef mingw32_HOST_OS encodeBS = S.pack . decodeW8NUL @@ -141,6 +143,14 @@ encodeBS = S.pack . decodeW8NUL encodeBS = S8.fromString #endif +{- Faster version that assumes the string does not contain NUL; + - if it does it will be truncated before the NUL. -} +decodeBS' :: S.ByteString -> FilePath +decodeBS' = encodeW8 . S.unpack + +encodeBS' :: FilePath -> S.ByteString +encodeBS' = S.pack . decodeW8 + {- Recent versions of the unix package have this alias; defined here - for backwards compatibility. -} type RawFilePath = S.ByteString @@ -149,13 +159,13 @@ type RawFilePath = S.ByteString - since filename's don't. This should only be used with actual - RawFilePaths not arbitrary ByteString that may contain NUL. -} fromRawFilePath :: RawFilePath -> FilePath -fromRawFilePath = encodeW8 . S.unpack +fromRawFilePath = decodeBS' {- Note that the FilePath is assumed to never contain NUL, - since filename's don't. This should only be used with actual FilePaths - not arbitrary String that may contain NUL. -} toRawFilePath :: FilePath -> RawFilePath -toRawFilePath = S.pack . decodeW8 +toRawFilePath = encodeBS' {- Converts a [Word8] to a FilePath, encoding using the filesystem encoding. - @@ -164,7 +174,7 @@ toRawFilePath = S.pack . decodeW8 - file system encoding, only complicated by GHC's interface to doing so. - - Note that the encoding stops at any NUL in the input. FilePaths - - do not normally contain embedded NUL, but Haskell Strings may. + - cannot contain embedded NUL, but Haskell Strings may. -} {-# NOINLINE encodeW8 #-} encodeW8 :: [Word8] -> FilePath