add decodeW8

This commit is contained in:
Joey Hess 2012-09-13 19:14:00 -04:00
parent 61c3ce6562
commit 60c31afc38
3 changed files with 28 additions and 8 deletions

View file

@ -267,7 +267,7 @@ mergeFile file key
++ takeExtension file
shortHash :: String -> String
shortHash = take 4 . md5s . encodeFilePath
shortHash = take 4 . md5s . md5FilePath
changed :: Remote -> Git.Ref -> Annex Bool
changed remote b = do

View file

@ -248,12 +248,12 @@ hashDirMixed :: Hasher
hashDirMixed k = addTrailingPathSeparator $ take 2 dir </> drop 2 dir
where
dir = take 4 $ display_32bits_as_dir =<< [a,b,c,d]
ABCD (a,b,c,d) = md5 $ encodeFilePath $ key2file k
ABCD (a,b,c,d) = md5 $ md5FilePath $ key2file k
hashDirLower :: Hasher
hashDirLower k = addTrailingPathSeparator $ take 3 dir </> drop 3 dir
where
dir = take 6 $ md5s $ encodeFilePath $ key2file k
dir = take 6 $ md5s $ md5FilePath $ key2file k
{- modified version of display_32bits_as_hex from Data.Hash.MD5
- Copyright (C) 2001 Ian Lynagh

View file

@ -5,7 +5,13 @@
- Licensed under the GNU GPL version 3 or higher.
-}
module Utility.FileSystemEncoding where
module Utility.FileSystemEncoding (
fileEncoding,
withFilePath,
md5FilePath,
decodeW8,
encodeW8
) where
import qualified GHC.Foreign as GHC
import qualified GHC.IO.Encoding as Encoding
@ -31,19 +37,28 @@ withFilePath :: FilePath -> (CString -> IO a) -> IO a
withFilePath fp f = Encoding.getFileSystemEncoding
>>= \enc -> GHC.withCString enc fp f
{- Encodes a FilePath into a Md5.Str, applying the filesystem encoding.
{- 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.
-
- 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.
-}
{-# NOINLINE encodeFilePath #-}
encodeFilePath :: FilePath -> MD5.Str
encodeFilePath fp = MD5.Str $ unsafePerformIO $ do
{-# NOINLINE _encodeFilePath #-}
_encodeFilePath :: FilePath -> String
_encodeFilePath fp = unsafePerformIO $ do
enc <- Encoding.getFileSystemEncoding
GHC.withCString enc fp $ GHC.peekCString Encoding.char8
{- Encodes a FilePath into a Md5.Str, applying the filesystem encoding. -}
md5FilePath :: FilePath -> MD5.Str
md5FilePath = MD5.Str . _encodeFilePath
{- Converts a [Word8] to a FilePath, encoding using the filesystem encoding.
-
- w82c produces a String, which may contain Chars that are invalid
@ -55,3 +70,8 @@ encodeW8 :: [Word8] -> FilePath
encodeW8 w8 = unsafePerformIO $ do
enc <- Encoding.getFileSystemEncoding
GHC.withCString Encoding.char8 (w82s w8) $ GHC.peekCString enc
{- Useful when you want the actual number of bytes that will be used to
- represent the FilePath on disk. -}
decodeW8 :: FilePath -> [Word8]
decodeW8 = s2w8 . _encodeFilePath