2012-03-09 23:08:10 +00:00
|
|
|
{- GHC File system encoding handling.
|
2012-03-09 22:52:03 +00:00
|
|
|
-
|
2016-12-24 18:46:31 +00:00
|
|
|
- Copyright 2012-2016 Joey Hess <id@joeyh.name>
|
2012-03-09 22:52:03 +00:00
|
|
|
-
|
2014-05-10 14:01:27 +00:00
|
|
|
- License: BSD-2-clause
|
2012-03-09 22:52:03 +00:00
|
|
|
-}
|
|
|
|
|
2014-03-19 18:49:01 +00:00
|
|
|
{-# LANGUAGE CPP #-}
|
2015-05-10 20:31:50 +00:00
|
|
|
{-# OPTIONS_GHC -fno-warn-tabs #-}
|
2014-03-19 18:49:01 +00:00
|
|
|
|
2012-09-13 23:14:00 +00:00
|
|
|
module Utility.FileSystemEncoding (
|
2016-12-24 18:46:31 +00:00
|
|
|
useFileSystemEncoding,
|
2016-12-30 22:14:19 +00:00
|
|
|
fileEncoding,
|
2012-09-13 23:14:00 +00:00
|
|
|
withFilePath,
|
2018-05-08 18:26:57 +00:00
|
|
|
RawFilePath,
|
|
|
|
fromRawFilePath,
|
|
|
|
toRawFilePath,
|
2019-01-01 18:54:06 +00:00
|
|
|
decodeBL,
|
|
|
|
encodeBL,
|
2019-01-02 02:44:04 +00:00
|
|
|
decodeBS,
|
Fix setting/setting/viewing metadata that contains unicode or other special characters, when in a non-unicode locale.
Oh boy, not again. So, another place that the filesystem encoding needs to
be applied. Yay.
In passing, I changed decodeBS so if a NUL is embedded in the input, the
resulting FilePath doesn't get truncated at that NUL. This was needed to
make prop_b64_roundtrips pass, and on reviewing the callers of decodeBS, I
didn't see any where this wouldn't make sense. When a FilePath is used to
operate on the filesystem, it'll get truncated at a NUL anyway, whereas if
a String is being used for something else, it might conceivably have a NUL
in it, and we wouldn't want it to get truncated when going through
decodeBS.
(NB: There may be a speed impact from this change.)
2015-08-11 22:40:59 +00:00
|
|
|
encodeBS,
|
2019-01-11 23:56:31 +00:00
|
|
|
decodeBL',
|
|
|
|
encodeBL',
|
2019-01-02 02:44:04 +00:00
|
|
|
decodeBS',
|
|
|
|
encodeBS',
|
Fix a few bugs involving filenames that are at or near the filesystem's maximum filename length limit.
Started with a problem when running addurl on a really long url,
because the whole url is munged into the filename. Ended up doing
a fairly extensive review for places where filenames could get too large,
although it's hard to say I'm not missed any..
Backend.Url had a 128 character limit, which is fine when the limit is 255,
but not if it's a lot shorter on some systems. So check the pathconf()
limit. Note that this could result in fromUrl creating different keys
for the same url, if run on systems with different limits. I don't see
this is likely to cause any problems. That can already happen when using
addurl --fast, or if the content of an url changes.
Both Command.AddUrl and Backend.Url assumed that urls don't contain a
lot of multi-byte unicode, and would fail to truncate an url that did
properly.
A few places use a filename as the template to make a temp file.
While that's nice in that the temp file name can be easily related back to
the original filename, it could lead to `git annex add` failing to add a
filename that was at or close to the maximum length.
Note that in Command.Add.lockdown, the template is still derived from the
filename, just with enough space left to turn it into a temp file.
This is an important optimisation, because the assistant may lock down
a bunch of files all at once, and using the same template for all of them
would cause openTempFile to iterate through the same set of names,
looking for an unused temp file. I'm not very happy with the relatedTemplate
hack, but it avoids that slowdown.
Backend.WORM does not limit the filename stored in the key.
I have not tried to change that; so git annex add will fail on really long
filenames when using the WORM backend. It seems better to preserve the
invariant that a WORM key always contains the complete filename, since
the filename is the only unique material in the key, other than mtime and
size. Since nobody has complained about add failing (I think I saw it
once?) on WORM, probably it's ok, or nobody but me uses it.
There may be compatability problems if using git annex addurl --fast
or the WORM backend on a system with the 255 limit and then trying to use
that repo in a system with a smaller limit. I have not tried to deal with
those.
This commit was sponsored by Alexander Brem. Thanks!
2013-07-30 21:49:11 +00:00
|
|
|
truncateFilePath,
|
2017-05-16 03:32:17 +00:00
|
|
|
s2w8,
|
|
|
|
w82s,
|
|
|
|
c2w8,
|
|
|
|
w82c,
|
2012-09-13 23:14:00 +00:00
|
|
|
) where
|
2012-03-09 22:52:03 +00:00
|
|
|
|
fix key directory hash calculation code
Fix Key directory hash calculation code to behave as it did before version
3.20120227 when a key contains non-ascii.
The hash directories for a given Key are based on its md5sum.
Prior to ghc 7.4, Keys contained raw, undecoded bytes, so the md5sum was
taken of each byte in turn. With the ghc 7.4 filename encoding change,
keys contains decoded unicode characters (possibly with surrigates for
undecodable bytes). This changes the result of the md5sum, since the md5sum
used is pure haskell and supports unicode. And that won't do, as git-annex
will start looking in a different hash directory for the content of a key.
The surrigates are particularly bad, since that's essentially a ghc
implementation detail, so could change again at any time. Also, changing
the locale changes how the bytes are decoded, which can also change
the md5sum.
Symptoms would include things like:
* git annex fsck would complain that no copies existed of a file,
despite its symlink pointing to the content that was locally present
* git annex fix would change the symlink to use the wrong hash
directory.
Only WORM backend is likely to have been affected, since only it tends
to include much filename data (SHA1E could in theory also be affected).
I have not tried to support the hash directories used by git-annex versions
3.20120227 to 3.20120308, so things added with those versions with WORM
will require manual fixups. Sorry for the inconvenience!
2012-03-09 23:26:02 +00:00
|
|
|
import qualified GHC.Foreign as GHC
|
|
|
|
import qualified GHC.IO.Encoding as Encoding
|
2012-03-09 23:08:10 +00:00
|
|
|
import Foreign.C
|
fix key directory hash calculation code
Fix Key directory hash calculation code to behave as it did before version
3.20120227 when a key contains non-ascii.
The hash directories for a given Key are based on its md5sum.
Prior to ghc 7.4, Keys contained raw, undecoded bytes, so the md5sum was
taken of each byte in turn. With the ghc 7.4 filename encoding change,
keys contains decoded unicode characters (possibly with surrigates for
undecodable bytes). This changes the result of the md5sum, since the md5sum
used is pure haskell and supports unicode. And that won't do, as git-annex
will start looking in a different hash directory for the content of a key.
The surrigates are particularly bad, since that's essentially a ghc
implementation detail, so could change again at any time. Also, changing
the locale changes how the bytes are decoded, which can also change
the md5sum.
Symptoms would include things like:
* git annex fsck would complain that no copies existed of a file,
despite its symlink pointing to the content that was locally present
* git annex fix would change the symlink to use the wrong hash
directory.
Only WORM backend is likely to have been affected, since only it tends
to include much filename data (SHA1E could in theory also be affected).
I have not tried to support the hash directories used by git-annex versions
3.20120227 to 3.20120308, so things added with those versions with WORM
will require manual fixups. Sorry for the inconvenience!
2012-03-09 23:26:02 +00:00
|
|
|
import System.IO
|
|
|
|
import System.IO.Unsafe
|
2012-06-20 16:51:25 +00:00
|
|
|
import Data.Word
|
2015-11-17 21:27:24 +00:00
|
|
|
import Data.List
|
2018-05-08 18:26:57 +00:00
|
|
|
import qualified Data.ByteString as S
|
2014-03-19 18:49:01 +00:00
|
|
|
import qualified Data.ByteString.Lazy as L
|
|
|
|
#ifdef mingw32_HOST_OS
|
2019-01-01 18:54:06 +00:00
|
|
|
import qualified Data.ByteString.UTF8 as S8
|
2014-03-19 18:49:01 +00:00
|
|
|
import qualified Data.ByteString.Lazy.UTF8 as L8
|
|
|
|
#endif
|
2020-01-05 00:18:40 +00:00
|
|
|
import System.FilePath.ByteString (RawFilePath, encodeFilePath, decodeFilePath)
|
2012-03-09 23:08:10 +00:00
|
|
|
|
2015-08-12 14:57:48 +00:00
|
|
|
import Utility.Exception
|
2017-05-16 03:32:17 +00:00
|
|
|
import Utility.Split
|
2015-08-12 14:57:48 +00:00
|
|
|
|
2016-12-24 18:46:31 +00:00
|
|
|
{- Makes all subsequent Handles that are opened, as well as stdio Handles,
|
|
|
|
- use the filesystem encoding, instead of the encoding of the current
|
|
|
|
- locale.
|
|
|
|
-
|
|
|
|
- The filesystem encoding allows "arbitrary undecodable bytes to be
|
|
|
|
- round-tripped through it". This avoids encoded failures when data is not
|
|
|
|
- encoded matching the current locale.
|
|
|
|
-
|
|
|
|
- Note that code can still use hSetEncoding to change the encoding of a
|
|
|
|
- Handle. This only affects the default encoding.
|
2014-03-19 18:49:01 +00:00
|
|
|
-}
|
2016-12-24 18:46:31 +00:00
|
|
|
useFileSystemEncoding :: IO ()
|
|
|
|
useFileSystemEncoding = do
|
2014-03-19 18:49:01 +00:00
|
|
|
#ifndef mingw32_HOST_OS
|
2016-12-24 18:46:31 +00:00
|
|
|
e <- Encoding.getFileSystemEncoding
|
2014-03-19 18:49:01 +00:00
|
|
|
#else
|
2016-12-24 18:46:31 +00:00
|
|
|
{- The file system encoding does not work well on Windows,
|
|
|
|
- and Windows only has utf FilePaths anyway. -}
|
|
|
|
let e = Encoding.utf8
|
2014-03-19 18:49:01 +00:00
|
|
|
#endif
|
2016-12-24 18:46:31 +00:00
|
|
|
hSetEncoding stdin e
|
|
|
|
hSetEncoding stdout e
|
|
|
|
hSetEncoding stderr e
|
|
|
|
Encoding.setLocaleEncoding e
|
2012-03-09 22:52:03 +00:00
|
|
|
|
2016-12-30 22:14:19 +00:00
|
|
|
fileEncoding :: Handle -> IO ()
|
|
|
|
#ifndef mingw32_HOST_OS
|
|
|
|
fileEncoding h = hSetEncoding h =<< Encoding.getFileSystemEncoding
|
|
|
|
#else
|
|
|
|
fileEncoding h = hSetEncoding h Encoding.utf8
|
|
|
|
#endif
|
|
|
|
|
2012-03-09 22:52:03 +00:00
|
|
|
{- Marshal a Haskell FilePath into a NUL terminated C string using temporary
|
|
|
|
- storage. The FilePath is encoded using the filesystem encoding,
|
|
|
|
- reversing the decoding that should have been done when the FilePath
|
|
|
|
- was obtained. -}
|
|
|
|
withFilePath :: FilePath -> (CString -> IO a) -> IO a
|
fix key directory hash calculation code
Fix Key directory hash calculation code to behave as it did before version
3.20120227 when a key contains non-ascii.
The hash directories for a given Key are based on its md5sum.
Prior to ghc 7.4, Keys contained raw, undecoded bytes, so the md5sum was
taken of each byte in turn. With the ghc 7.4 filename encoding change,
keys contains decoded unicode characters (possibly with surrigates for
undecodable bytes). This changes the result of the md5sum, since the md5sum
used is pure haskell and supports unicode. And that won't do, as git-annex
will start looking in a different hash directory for the content of a key.
The surrigates are particularly bad, since that's essentially a ghc
implementation detail, so could change again at any time. Also, changing
the locale changes how the bytes are decoded, which can also change
the md5sum.
Symptoms would include things like:
* git annex fsck would complain that no copies existed of a file,
despite its symlink pointing to the content that was locally present
* git annex fix would change the symlink to use the wrong hash
directory.
Only WORM backend is likely to have been affected, since only it tends
to include much filename data (SHA1E could in theory also be affected).
I have not tried to support the hash directories used by git-annex versions
3.20120227 to 3.20120308, so things added with those versions with WORM
will require manual fixups. Sorry for the inconvenience!
2012-03-09 23:26:02 +00:00
|
|
|
withFilePath fp f = Encoding.getFileSystemEncoding
|
|
|
|
>>= \enc -> GHC.withCString enc fp f
|
2012-03-09 23:08:10 +00:00
|
|
|
|
2012-09-13 23:14:00 +00:00
|
|
|
{- Encodes a FilePath into a String, applying the filesystem encoding.
|
|
|
|
-
|
|
|
|
- There are very few things it makes sense to do with such an encoded
|
|
|
|
- string. It's not a legal filename; it should not be displayed.
|
|
|
|
- So this function is not exported, but instead used by the few functions
|
|
|
|
- that can usefully consume it.
|
2012-03-09 23:08:10 +00:00
|
|
|
-
|
fix key directory hash calculation code
Fix Key directory hash calculation code to behave as it did before version
3.20120227 when a key contains non-ascii.
The hash directories for a given Key are based on its md5sum.
Prior to ghc 7.4, Keys contained raw, undecoded bytes, so the md5sum was
taken of each byte in turn. With the ghc 7.4 filename encoding change,
keys contains decoded unicode characters (possibly with surrigates for
undecodable bytes). This changes the result of the md5sum, since the md5sum
used is pure haskell and supports unicode. And that won't do, as git-annex
will start looking in a different hash directory for the content of a key.
The surrigates are particularly bad, since that's essentially a ghc
implementation detail, so could change again at any time. Also, changing
the locale changes how the bytes are decoded, which can also change
the md5sum.
Symptoms would include things like:
* git annex fsck would complain that no copies existed of a file,
despite its symlink pointing to the content that was locally present
* git annex fix would change the symlink to use the wrong hash
directory.
Only WORM backend is likely to have been affected, since only it tends
to include much filename data (SHA1E could in theory also be affected).
I have not tried to support the hash directories used by git-annex versions
3.20120227 to 3.20120308, so things added with those versions with WORM
will require manual fixups. Sorry for the inconvenience!
2012-03-09 23:26:02 +00:00
|
|
|
- This use of unsafePerformIO is belived to be safe; GHC's interface
|
|
|
|
- only allows doing this conversion with CStrings, and the CString buffer
|
|
|
|
- is allocated, used, and deallocated within the call, with no side
|
|
|
|
- effects.
|
2015-08-12 14:36:51 +00:00
|
|
|
-
|
|
|
|
- If the FilePath contains a value that is not legal in the filesystem
|
2015-08-12 14:57:48 +00:00
|
|
|
- encoding, rather than thowing an exception, it will be returned as-is.
|
fix key directory hash calculation code
Fix Key directory hash calculation code to behave as it did before version
3.20120227 when a key contains non-ascii.
The hash directories for a given Key are based on its md5sum.
Prior to ghc 7.4, Keys contained raw, undecoded bytes, so the md5sum was
taken of each byte in turn. With the ghc 7.4 filename encoding change,
keys contains decoded unicode characters (possibly with surrigates for
undecodable bytes). This changes the result of the md5sum, since the md5sum
used is pure haskell and supports unicode. And that won't do, as git-annex
will start looking in a different hash directory for the content of a key.
The surrigates are particularly bad, since that's essentially a ghc
implementation detail, so could change again at any time. Also, changing
the locale changes how the bytes are decoded, which can also change
the md5sum.
Symptoms would include things like:
* git annex fsck would complain that no copies existed of a file,
despite its symlink pointing to the content that was locally present
* git annex fix would change the symlink to use the wrong hash
directory.
Only WORM backend is likely to have been affected, since only it tends
to include much filename data (SHA1E could in theory also be affected).
I have not tried to support the hash directories used by git-annex versions
3.20120227 to 3.20120308, so things added with those versions with WORM
will require manual fixups. Sorry for the inconvenience!
2012-03-09 23:26:02 +00:00
|
|
|
-}
|
2012-09-13 23:14:00 +00:00
|
|
|
{-# NOINLINE _encodeFilePath #-}
|
|
|
|
_encodeFilePath :: FilePath -> String
|
|
|
|
_encodeFilePath fp = unsafePerformIO $ do
|
fix key directory hash calculation code
Fix Key directory hash calculation code to behave as it did before version
3.20120227 when a key contains non-ascii.
The hash directories for a given Key are based on its md5sum.
Prior to ghc 7.4, Keys contained raw, undecoded bytes, so the md5sum was
taken of each byte in turn. With the ghc 7.4 filename encoding change,
keys contains decoded unicode characters (possibly with surrigates for
undecodable bytes). This changes the result of the md5sum, since the md5sum
used is pure haskell and supports unicode. And that won't do, as git-annex
will start looking in a different hash directory for the content of a key.
The surrigates are particularly bad, since that's essentially a ghc
implementation detail, so could change again at any time. Also, changing
the locale changes how the bytes are decoded, which can also change
the md5sum.
Symptoms would include things like:
* git annex fsck would complain that no copies existed of a file,
despite its symlink pointing to the content that was locally present
* git annex fix would change the symlink to use the wrong hash
directory.
Only WORM backend is likely to have been affected, since only it tends
to include much filename data (SHA1E could in theory also be affected).
I have not tried to support the hash directories used by git-annex versions
3.20120227 to 3.20120308, so things added with those versions with WORM
will require manual fixups. Sorry for the inconvenience!
2012-03-09 23:26:02 +00:00
|
|
|
enc <- Encoding.getFileSystemEncoding
|
2015-08-12 14:57:48 +00:00
|
|
|
GHC.withCString enc fp (GHC.peekCString Encoding.char8)
|
|
|
|
`catchNonAsync` (\_ -> return fp)
|
2012-06-20 16:51:25 +00:00
|
|
|
|
2014-03-19 18:49:01 +00:00
|
|
|
{- Decodes a ByteString into a FilePath, applying the filesystem encoding. -}
|
2019-01-01 18:54:06 +00:00
|
|
|
decodeBL :: L.ByteString -> FilePath
|
2014-03-19 18:49:01 +00:00
|
|
|
#ifndef mingw32_HOST_OS
|
2019-01-01 18:54:06 +00:00
|
|
|
decodeBL = encodeW8NUL . L.unpack
|
2014-03-19 18:49:01 +00:00
|
|
|
#else
|
|
|
|
{- On Windows, we assume that the ByteString is utf-8, since Windows
|
|
|
|
- only uses unicode for filenames. -}
|
2019-01-01 18:54:06 +00:00
|
|
|
decodeBL = L8.toString
|
|
|
|
#endif
|
|
|
|
|
Fix setting/setting/viewing metadata that contains unicode or other special characters, when in a non-unicode locale.
Oh boy, not again. So, another place that the filesystem encoding needs to
be applied. Yay.
In passing, I changed decodeBS so if a NUL is embedded in the input, the
resulting FilePath doesn't get truncated at that NUL. This was needed to
make prop_b64_roundtrips pass, and on reviewing the callers of decodeBS, I
didn't see any where this wouldn't make sense. When a FilePath is used to
operate on the filesystem, it'll get truncated at a NUL anyway, whereas if
a String is being used for something else, it might conceivably have a NUL
in it, and we wouldn't want it to get truncated when going through
decodeBS.
(NB: There may be a speed impact from this change.)
2015-08-11 22:40:59 +00:00
|
|
|
{- Encodes a FilePath into a ByteString, applying the filesystem encoding. -}
|
2019-01-01 18:54:06 +00:00
|
|
|
encodeBL :: FilePath -> L.ByteString
|
|
|
|
#ifndef mingw32_HOST_OS
|
|
|
|
encodeBL = L.pack . decodeW8NUL
|
|
|
|
#else
|
|
|
|
encodeBL = L8.fromString
|
|
|
|
#endif
|
|
|
|
|
2019-01-02 02:44:04 +00:00
|
|
|
decodeBS :: S.ByteString -> FilePath
|
|
|
|
#ifndef mingw32_HOST_OS
|
|
|
|
decodeBS = encodeW8NUL . S.unpack
|
|
|
|
#else
|
|
|
|
decodeBS = S8.toString
|
|
|
|
#endif
|
|
|
|
|
2019-01-01 18:54:06 +00:00
|
|
|
encodeBS :: FilePath -> S.ByteString
|
Fix setting/setting/viewing metadata that contains unicode or other special characters, when in a non-unicode locale.
Oh boy, not again. So, another place that the filesystem encoding needs to
be applied. Yay.
In passing, I changed decodeBS so if a NUL is embedded in the input, the
resulting FilePath doesn't get truncated at that NUL. This was needed to
make prop_b64_roundtrips pass, and on reviewing the callers of decodeBS, I
didn't see any where this wouldn't make sense. When a FilePath is used to
operate on the filesystem, it'll get truncated at a NUL anyway, whereas if
a String is being used for something else, it might conceivably have a NUL
in it, and we wouldn't want it to get truncated when going through
decodeBS.
(NB: There may be a speed impact from this change.)
2015-08-11 22:40:59 +00:00
|
|
|
#ifndef mingw32_HOST_OS
|
2019-01-01 18:54:06 +00:00
|
|
|
encodeBS = S.pack . decodeW8NUL
|
Fix setting/setting/viewing metadata that contains unicode or other special characters, when in a non-unicode locale.
Oh boy, not again. So, another place that the filesystem encoding needs to
be applied. Yay.
In passing, I changed decodeBS so if a NUL is embedded in the input, the
resulting FilePath doesn't get truncated at that NUL. This was needed to
make prop_b64_roundtrips pass, and on reviewing the callers of decodeBS, I
didn't see any where this wouldn't make sense. When a FilePath is used to
operate on the filesystem, it'll get truncated at a NUL anyway, whereas if
a String is being used for something else, it might conceivably have a NUL
in it, and we wouldn't want it to get truncated when going through
decodeBS.
(NB: There may be a speed impact from this change.)
2015-08-11 22:40:59 +00:00
|
|
|
#else
|
2019-01-01 18:54:06 +00:00
|
|
|
encodeBS = S8.fromString
|
Fix setting/setting/viewing metadata that contains unicode or other special characters, when in a non-unicode locale.
Oh boy, not again. So, another place that the filesystem encoding needs to
be applied. Yay.
In passing, I changed decodeBS so if a NUL is embedded in the input, the
resulting FilePath doesn't get truncated at that NUL. This was needed to
make prop_b64_roundtrips pass, and on reviewing the callers of decodeBS, I
didn't see any where this wouldn't make sense. When a FilePath is used to
operate on the filesystem, it'll get truncated at a NUL anyway, whereas if
a String is being used for something else, it might conceivably have a NUL
in it, and we wouldn't want it to get truncated when going through
decodeBS.
(NB: There may be a speed impact from this change.)
2015-08-11 22:40:59 +00:00
|
|
|
#endif
|
|
|
|
|
2019-01-02 02:44:04 +00:00
|
|
|
{- Faster version that assumes the string does not contain NUL;
|
|
|
|
- if it does it will be truncated before the NUL. -}
|
|
|
|
decodeBS' :: S.ByteString -> FilePath
|
2019-12-18 17:26:06 +00:00
|
|
|
#ifndef mingw32_HOST_OS
|
2019-01-02 02:44:04 +00:00
|
|
|
decodeBS' = encodeW8 . S.unpack
|
2019-12-18 17:26:06 +00:00
|
|
|
#else
|
|
|
|
decodeBS' = S8.toString
|
|
|
|
#endif
|
2019-01-02 02:44:04 +00:00
|
|
|
|
|
|
|
encodeBS' :: FilePath -> S.ByteString
|
2019-12-18 17:26:06 +00:00
|
|
|
#ifndef mingw32_HOST_OS
|
2019-01-02 02:44:04 +00:00
|
|
|
encodeBS' = S.pack . decodeW8
|
2019-12-18 17:26:06 +00:00
|
|
|
#else
|
|
|
|
encodeBS' = S8.fromString
|
|
|
|
#endif
|
2019-01-02 02:44:04 +00:00
|
|
|
|
2019-01-11 23:56:31 +00:00
|
|
|
decodeBL' :: L.ByteString -> FilePath
|
2019-12-18 17:26:06 +00:00
|
|
|
#ifndef mingw32_HOST_OS
|
2019-01-11 23:56:31 +00:00
|
|
|
decodeBL' = encodeW8 . L.unpack
|
2019-12-18 17:26:06 +00:00
|
|
|
#else
|
|
|
|
decodeBL' = L8.toString
|
|
|
|
#endif
|
2019-01-11 23:56:31 +00:00
|
|
|
|
|
|
|
encodeBL' :: FilePath -> L.ByteString
|
2019-12-18 17:26:06 +00:00
|
|
|
#ifndef mingw32_HOST_OS
|
2019-01-11 23:56:31 +00:00
|
|
|
encodeBL' = L.pack . decodeW8
|
2019-12-18 17:26:06 +00:00
|
|
|
#else
|
|
|
|
encodeBL' = L8.fromString
|
|
|
|
#endif
|
2019-01-11 23:56:31 +00:00
|
|
|
|
2018-05-08 18:26:57 +00:00
|
|
|
fromRawFilePath :: RawFilePath -> FilePath
|
2020-01-05 00:18:40 +00:00
|
|
|
fromRawFilePath = decodeFilePath
|
2018-05-08 18:26:57 +00:00
|
|
|
|
|
|
|
toRawFilePath :: FilePath -> RawFilePath
|
2020-01-05 00:18:40 +00:00
|
|
|
toRawFilePath = encodeFilePath
|
2018-05-08 18:26:57 +00:00
|
|
|
|
2012-06-20 16:51:25 +00:00
|
|
|
{- Converts a [Word8] to a FilePath, encoding using the filesystem encoding.
|
|
|
|
-
|
2019-01-02 02:48:14 +00:00
|
|
|
- w82s produces a String, which may contain Chars that are invalid
|
2012-06-20 16:51:25 +00:00
|
|
|
- unicode. From there, this is really a simple matter of applying the
|
|
|
|
- file system encoding, only complicated by GHC's interface to doing so.
|
metadata: Fix encoding problem that led to mojibake when storing metadata strings that contained both unicode characters and a space (or '!') character.
The fix is to stop using w82s, which does not properly reconstitute unicode
strings. Instrad, use utf8 bytestring to get the [Word8] to base64. This
passes unicode through perfectly, including any invalid filesystem encoded
characters.
Note that toB64 / fromB64 are also used for creds and cipher
embedding. It would be unfortunate if this change broke those uses.
For cipher embedding, note that ciphers can contain arbitrary bytes (should
really be using ByteString.Char8 there). Testing indicated it's not safe to
use the new fromB64 there; I think that characters were incorrectly
combined.
For credpair embedding, the username or password could contain unicode.
Before, that unicode would fail to round-trip through the b64.
So, I guess this is not going to break any embedded creds that worked
before.
This bug may have affected some creds before, and if so,
this change will not fix old ones, but should fix new ones at least.
2015-03-04 15:16:03 +00:00
|
|
|
-
|
|
|
|
- Note that the encoding stops at any NUL in the input. FilePaths
|
2019-01-02 02:44:04 +00:00
|
|
|
- cannot contain embedded NUL, but Haskell Strings may.
|
2012-06-20 16:51:25 +00:00
|
|
|
-}
|
|
|
|
{-# NOINLINE encodeW8 #-}
|
|
|
|
encodeW8 :: [Word8] -> FilePath
|
|
|
|
encodeW8 w8 = unsafePerformIO $ do
|
|
|
|
enc <- Encoding.getFileSystemEncoding
|
|
|
|
GHC.withCString Encoding.char8 (w82s w8) $ GHC.peekCString enc
|
2012-09-13 23:14:00 +00:00
|
|
|
|
|
|
|
decodeW8 :: FilePath -> [Word8]
|
|
|
|
decodeW8 = s2w8 . _encodeFilePath
|
Fix a few bugs involving filenames that are at or near the filesystem's maximum filename length limit.
Started with a problem when running addurl on a really long url,
because the whole url is munged into the filename. Ended up doing
a fairly extensive review for places where filenames could get too large,
although it's hard to say I'm not missed any..
Backend.Url had a 128 character limit, which is fine when the limit is 255,
but not if it's a lot shorter on some systems. So check the pathconf()
limit. Note that this could result in fromUrl creating different keys
for the same url, if run on systems with different limits. I don't see
this is likely to cause any problems. That can already happen when using
addurl --fast, or if the content of an url changes.
Both Command.AddUrl and Backend.Url assumed that urls don't contain a
lot of multi-byte unicode, and would fail to truncate an url that did
properly.
A few places use a filename as the template to make a temp file.
While that's nice in that the temp file name can be easily related back to
the original filename, it could lead to `git annex add` failing to add a
filename that was at or close to the maximum length.
Note that in Command.Add.lockdown, the template is still derived from the
filename, just with enough space left to turn it into a temp file.
This is an important optimisation, because the assistant may lock down
a bunch of files all at once, and using the same template for all of them
would cause openTempFile to iterate through the same set of names,
looking for an unused temp file. I'm not very happy with the relatedTemplate
hack, but it avoids that slowdown.
Backend.WORM does not limit the filename stored in the key.
I have not tried to change that; so git annex add will fail on really long
filenames when using the WORM backend. It seems better to preserve the
invariant that a WORM key always contains the complete filename, since
the filename is the only unique material in the key, other than mtime and
size. Since nobody has complained about add failing (I think I saw it
once?) on WORM, probably it's ok, or nobody but me uses it.
There may be compatability problems if using git annex addurl --fast
or the WORM backend on a system with the 255 limit and then trying to use
that repo in a system with a smaller limit. I have not tried to deal with
those.
This commit was sponsored by Alexander Brem. Thanks!
2013-07-30 21:49:11 +00:00
|
|
|
|
metadata: Fix encoding problem that led to mojibake when storing metadata strings that contained both unicode characters and a space (or '!') character.
The fix is to stop using w82s, which does not properly reconstitute unicode
strings. Instrad, use utf8 bytestring to get the [Word8] to base64. This
passes unicode through perfectly, including any invalid filesystem encoded
characters.
Note that toB64 / fromB64 are also used for creds and cipher
embedding. It would be unfortunate if this change broke those uses.
For cipher embedding, note that ciphers can contain arbitrary bytes (should
really be using ByteString.Char8 there). Testing indicated it's not safe to
use the new fromB64 there; I think that characters were incorrectly
combined.
For credpair embedding, the username or password could contain unicode.
Before, that unicode would fail to round-trip through the b64.
So, I guess this is not going to break any embedded creds that worked
before.
This bug may have affected some creds before, and if so,
this change will not fix old ones, but should fix new ones at least.
2015-03-04 15:16:03 +00:00
|
|
|
{- Like encodeW8 and decodeW8, but NULs are passed through unchanged. -}
|
|
|
|
encodeW8NUL :: [Word8] -> FilePath
|
2017-05-16 03:32:17 +00:00
|
|
|
encodeW8NUL = intercalate [nul] . map encodeW8 . splitc (c2w8 nul)
|
metadata: Fix encoding problem that led to mojibake when storing metadata strings that contained both unicode characters and a space (or '!') character.
The fix is to stop using w82s, which does not properly reconstitute unicode
strings. Instrad, use utf8 bytestring to get the [Word8] to base64. This
passes unicode through perfectly, including any invalid filesystem encoded
characters.
Note that toB64 / fromB64 are also used for creds and cipher
embedding. It would be unfortunate if this change broke those uses.
For cipher embedding, note that ciphers can contain arbitrary bytes (should
really be using ByteString.Char8 there). Testing indicated it's not safe to
use the new fromB64 there; I think that characters were incorrectly
combined.
For credpair embedding, the username or password could contain unicode.
Before, that unicode would fail to round-trip through the b64.
So, I guess this is not going to break any embedded creds that worked
before.
This bug may have affected some creds before, and if so,
this change will not fix old ones, but should fix new ones at least.
2015-03-04 15:16:03 +00:00
|
|
|
where
|
2017-05-16 03:32:17 +00:00
|
|
|
nul = '\NUL'
|
metadata: Fix encoding problem that led to mojibake when storing metadata strings that contained both unicode characters and a space (or '!') character.
The fix is to stop using w82s, which does not properly reconstitute unicode
strings. Instrad, use utf8 bytestring to get the [Word8] to base64. This
passes unicode through perfectly, including any invalid filesystem encoded
characters.
Note that toB64 / fromB64 are also used for creds and cipher
embedding. It would be unfortunate if this change broke those uses.
For cipher embedding, note that ciphers can contain arbitrary bytes (should
really be using ByteString.Char8 there). Testing indicated it's not safe to
use the new fromB64 there; I think that characters were incorrectly
combined.
For credpair embedding, the username or password could contain unicode.
Before, that unicode would fail to round-trip through the b64.
So, I guess this is not going to break any embedded creds that worked
before.
This bug may have affected some creds before, and if so,
this change will not fix old ones, but should fix new ones at least.
2015-03-04 15:16:03 +00:00
|
|
|
|
|
|
|
decodeW8NUL :: FilePath -> [Word8]
|
2017-05-16 03:32:17 +00:00
|
|
|
decodeW8NUL = intercalate [c2w8 nul] . map decodeW8 . splitc nul
|
metadata: Fix encoding problem that led to mojibake when storing metadata strings that contained both unicode characters and a space (or '!') character.
The fix is to stop using w82s, which does not properly reconstitute unicode
strings. Instrad, use utf8 bytestring to get the [Word8] to base64. This
passes unicode through perfectly, including any invalid filesystem encoded
characters.
Note that toB64 / fromB64 are also used for creds and cipher
embedding. It would be unfortunate if this change broke those uses.
For cipher embedding, note that ciphers can contain arbitrary bytes (should
really be using ByteString.Char8 there). Testing indicated it's not safe to
use the new fromB64 there; I think that characters were incorrectly
combined.
For credpair embedding, the username or password could contain unicode.
Before, that unicode would fail to round-trip through the b64.
So, I guess this is not going to break any embedded creds that worked
before.
This bug may have affected some creds before, and if so,
this change will not fix old ones, but should fix new ones at least.
2015-03-04 15:16:03 +00:00
|
|
|
where
|
2017-05-16 03:32:17 +00:00
|
|
|
nul = '\NUL'
|
|
|
|
|
|
|
|
c2w8 :: Char -> Word8
|
|
|
|
c2w8 = fromIntegral . fromEnum
|
|
|
|
|
|
|
|
w82c :: Word8 -> Char
|
|
|
|
w82c = toEnum . fromIntegral
|
|
|
|
|
|
|
|
s2w8 :: String -> [Word8]
|
|
|
|
s2w8 = map c2w8
|
|
|
|
|
|
|
|
w82s :: [Word8] -> String
|
|
|
|
w82s = map w82c
|
metadata: Fix encoding problem that led to mojibake when storing metadata strings that contained both unicode characters and a space (or '!') character.
The fix is to stop using w82s, which does not properly reconstitute unicode
strings. Instrad, use utf8 bytestring to get the [Word8] to base64. This
passes unicode through perfectly, including any invalid filesystem encoded
characters.
Note that toB64 / fromB64 are also used for creds and cipher
embedding. It would be unfortunate if this change broke those uses.
For cipher embedding, note that ciphers can contain arbitrary bytes (should
really be using ByteString.Char8 there). Testing indicated it's not safe to
use the new fromB64 there; I think that characters were incorrectly
combined.
For credpair embedding, the username or password could contain unicode.
Before, that unicode would fail to round-trip through the b64.
So, I guess this is not going to break any embedded creds that worked
before.
This bug may have affected some creds before, and if so,
this change will not fix old ones, but should fix new ones at least.
2015-03-04 15:16:03 +00:00
|
|
|
|
Fix a few bugs involving filenames that are at or near the filesystem's maximum filename length limit.
Started with a problem when running addurl on a really long url,
because the whole url is munged into the filename. Ended up doing
a fairly extensive review for places where filenames could get too large,
although it's hard to say I'm not missed any..
Backend.Url had a 128 character limit, which is fine when the limit is 255,
but not if it's a lot shorter on some systems. So check the pathconf()
limit. Note that this could result in fromUrl creating different keys
for the same url, if run on systems with different limits. I don't see
this is likely to cause any problems. That can already happen when using
addurl --fast, or if the content of an url changes.
Both Command.AddUrl and Backend.Url assumed that urls don't contain a
lot of multi-byte unicode, and would fail to truncate an url that did
properly.
A few places use a filename as the template to make a temp file.
While that's nice in that the temp file name can be easily related back to
the original filename, it could lead to `git annex add` failing to add a
filename that was at or close to the maximum length.
Note that in Command.Add.lockdown, the template is still derived from the
filename, just with enough space left to turn it into a temp file.
This is an important optimisation, because the assistant may lock down
a bunch of files all at once, and using the same template for all of them
would cause openTempFile to iterate through the same set of names,
looking for an unused temp file. I'm not very happy with the relatedTemplate
hack, but it avoids that slowdown.
Backend.WORM does not limit the filename stored in the key.
I have not tried to change that; so git annex add will fail on really long
filenames when using the WORM backend. It seems better to preserve the
invariant that a WORM key always contains the complete filename, since
the filename is the only unique material in the key, other than mtime and
size. Since nobody has complained about add failing (I think I saw it
once?) on WORM, probably it's ok, or nobody but me uses it.
There may be compatability problems if using git annex addurl --fast
or the WORM backend on a system with the 255 limit and then trying to use
that repo in a system with a smaller limit. I have not tried to deal with
those.
This commit was sponsored by Alexander Brem. Thanks!
2013-07-30 21:49:11 +00:00
|
|
|
{- Truncates a FilePath to the given number of bytes (or less),
|
|
|
|
- as represented on disk.
|
|
|
|
-
|
|
|
|
- Avoids returning an invalid part of a unicode byte sequence, at the
|
|
|
|
- cost of efficiency when running on a large FilePath.
|
|
|
|
-}
|
|
|
|
truncateFilePath :: Int -> FilePath -> FilePath
|
2014-03-19 18:49:01 +00:00
|
|
|
#ifndef mingw32_HOST_OS
|
Fix a few bugs involving filenames that are at or near the filesystem's maximum filename length limit.
Started with a problem when running addurl on a really long url,
because the whole url is munged into the filename. Ended up doing
a fairly extensive review for places where filenames could get too large,
although it's hard to say I'm not missed any..
Backend.Url had a 128 character limit, which is fine when the limit is 255,
but not if it's a lot shorter on some systems. So check the pathconf()
limit. Note that this could result in fromUrl creating different keys
for the same url, if run on systems with different limits. I don't see
this is likely to cause any problems. That can already happen when using
addurl --fast, or if the content of an url changes.
Both Command.AddUrl and Backend.Url assumed that urls don't contain a
lot of multi-byte unicode, and would fail to truncate an url that did
properly.
A few places use a filename as the template to make a temp file.
While that's nice in that the temp file name can be easily related back to
the original filename, it could lead to `git annex add` failing to add a
filename that was at or close to the maximum length.
Note that in Command.Add.lockdown, the template is still derived from the
filename, just with enough space left to turn it into a temp file.
This is an important optimisation, because the assistant may lock down
a bunch of files all at once, and using the same template for all of them
would cause openTempFile to iterate through the same set of names,
looking for an unused temp file. I'm not very happy with the relatedTemplate
hack, but it avoids that slowdown.
Backend.WORM does not limit the filename stored in the key.
I have not tried to change that; so git annex add will fail on really long
filenames when using the WORM backend. It seems better to preserve the
invariant that a WORM key always contains the complete filename, since
the filename is the only unique material in the key, other than mtime and
size. Since nobody has complained about add failing (I think I saw it
once?) on WORM, probably it's ok, or nobody but me uses it.
There may be compatability problems if using git annex addurl --fast
or the WORM backend on a system with the 255 limit and then trying to use
that repo in a system with a smaller limit. I have not tried to deal with
those.
This commit was sponsored by Alexander Brem. Thanks!
2013-07-30 21:49:11 +00:00
|
|
|
truncateFilePath n = go . reverse
|
|
|
|
where
|
2014-10-09 18:53:13 +00:00
|
|
|
go f =
|
Fix a few bugs involving filenames that are at or near the filesystem's maximum filename length limit.
Started with a problem when running addurl on a really long url,
because the whole url is munged into the filename. Ended up doing
a fairly extensive review for places where filenames could get too large,
although it's hard to say I'm not missed any..
Backend.Url had a 128 character limit, which is fine when the limit is 255,
but not if it's a lot shorter on some systems. So check the pathconf()
limit. Note that this could result in fromUrl creating different keys
for the same url, if run on systems with different limits. I don't see
this is likely to cause any problems. That can already happen when using
addurl --fast, or if the content of an url changes.
Both Command.AddUrl and Backend.Url assumed that urls don't contain a
lot of multi-byte unicode, and would fail to truncate an url that did
properly.
A few places use a filename as the template to make a temp file.
While that's nice in that the temp file name can be easily related back to
the original filename, it could lead to `git annex add` failing to add a
filename that was at or close to the maximum length.
Note that in Command.Add.lockdown, the template is still derived from the
filename, just with enough space left to turn it into a temp file.
This is an important optimisation, because the assistant may lock down
a bunch of files all at once, and using the same template for all of them
would cause openTempFile to iterate through the same set of names,
looking for an unused temp file. I'm not very happy with the relatedTemplate
hack, but it avoids that slowdown.
Backend.WORM does not limit the filename stored in the key.
I have not tried to change that; so git annex add will fail on really long
filenames when using the WORM backend. It seems better to preserve the
invariant that a WORM key always contains the complete filename, since
the filename is the only unique material in the key, other than mtime and
size. Since nobody has complained about add failing (I think I saw it
once?) on WORM, probably it's ok, or nobody but me uses it.
There may be compatability problems if using git annex addurl --fast
or the WORM backend on a system with the 255 limit and then trying to use
that repo in a system with a smaller limit. I have not tried to deal with
those.
This commit was sponsored by Alexander Brem. Thanks!
2013-07-30 21:49:11 +00:00
|
|
|
let bytes = decodeW8 f
|
|
|
|
in if length bytes <= n
|
|
|
|
then reverse f
|
|
|
|
else go (drop 1 f)
|
2014-03-19 18:49:01 +00:00
|
|
|
#else
|
|
|
|
{- On Windows, count the number of bytes used by each utf8 character. -}
|
|
|
|
truncateFilePath n = reverse . go [] n . L8.fromString
|
|
|
|
where
|
|
|
|
go coll cnt bs
|
|
|
|
| cnt <= 0 = coll
|
|
|
|
| otherwise = case L8.decode bs of
|
|
|
|
Just (c, x) | c /= L8.replacement_char ->
|
|
|
|
let x' = fromIntegral x
|
|
|
|
in if cnt - x' < 0
|
|
|
|
then coll
|
|
|
|
else go (c:coll) (cnt - x') (L8.drop 1 bs)
|
|
|
|
_ -> coll
|
|
|
|
#endif
|