fix ByteString conversion on windows

the encode' and decode' functions on Windows should not apply the
filesystem encoding, which does not work there. Instead, convert to and
from UTF-8.

Also, avoid exporting encodeW8 and decodeW8. Both use the filesystem
encoding, so won't work as expected on windows.
This commit is contained in:
Joey Hess 2019-12-18 13:26:06 -04:00
parent 0246ecbe94
commit 322c542b5c
No known key found for this signature in database
GPG key ID: DB12DB0FF05F8F38
4 changed files with 23 additions and 8 deletions

View file

@ -43,6 +43,7 @@ import Annex.LockPool
#endif
import Control.Concurrent.STM
import qualified Data.ByteString as S
{- Some ssh commands are fed stdin on a pipe and so should be allowed to
- consume it. But ssh commands that are not piped stdin should generally
@ -325,7 +326,7 @@ sizeof_sockaddr_un_sun_path = 100
{- Note that this looks at the true length of the path in bytes, as it will
- appear on disk. -}
valid_unix_socket_path :: FilePath -> Bool
valid_unix_socket_path f = length (decodeW8 f) < sizeof_sockaddr_un_sun_path
valid_unix_socket_path f = S.length (encodeBS f) < sizeof_sockaddr_un_sun_path
{- Parses the SSH port, and returns the other OpenSSH options. If
- several ports are found, the last one takes precedence. -}

View file

@ -11,6 +11,7 @@ import Annex.Common
import Utility.Hash
import qualified Data.ByteString as S
import qualified Data.ByteString.Lazy as L
{- Generates a keyName from an input string. Takes care of sanitizing it.
- If it's not too long, the full string is used as the keyName.
@ -21,11 +22,12 @@ genKeyName s
-- Avoid making keys longer than the length of a SHA256 checksum.
| bytelen > sha256len = encodeBS' $
truncateFilePath (sha256len - md5len - 1) s' ++ "-" ++
show (md5 (encodeBL s))
show (md5 bl)
| otherwise = encodeBS' s'
where
s' = preSanitizeKeyName s
bytelen = length (decodeW8 s')
bl = encodeBL s
bytelen = fromIntegral $ L.length bl
sha256len = 64
md5len = 32

View file

@ -23,10 +23,6 @@ module Utility.FileSystemEncoding (
encodeBL',
decodeBS',
encodeBS',
decodeW8,
encodeW8,
encodeW8NUL,
decodeW8NUL,
truncateFilePath,
s2w8,
w82s,
@ -148,16 +144,32 @@ encodeBS = S8.fromString
{- Faster version that assumes the string does not contain NUL;
- if it does it will be truncated before the NUL. -}
decodeBS' :: S.ByteString -> FilePath
#ifndef mingw32_HOST_OS
decodeBS' = encodeW8 . S.unpack
#else
decodeBS' = S8.toString
#endif
encodeBS' :: FilePath -> S.ByteString
#ifndef mingw32_HOST_OS
encodeBS' = S.pack . decodeW8
#else
encodeBS' = S8.fromString
#endif
decodeBL' :: L.ByteString -> FilePath
#ifndef mingw32_HOST_OS
decodeBL' = encodeW8 . L.unpack
#else
decodeBL' = L8.toString
#endif
encodeBL' :: FilePath -> L.ByteString
#ifndef mingw32_HOST_OS
encodeBL' = L.pack . decodeW8
#else
encodeBL' = L8.fromString
#endif
{- Recent versions of the unix package have this alias; defined here
- for backwards compatibility. -}

View file

@ -258,7 +258,7 @@ commandMeter' progressparser oh meterupdate cmd params =
unless (quietMode oh) $ do
S.hPut stdout b
hFlush stdout
let s = encodeW8 (S.unpack b)
let s = decodeBS b
let (mbytes, buf') = progressparser (buf++s)
case mbytes of
Nothing -> feedprogress prev buf' h