git-annex/Utility/FileSystemEncoding.hs

38 lines
1.3 KiB
Haskell
Raw Normal View History

2012-03-09 23:08:10 +00:00
{- GHC File system encoding handling.
2012-03-09 22:52:03 +00:00
-
- Copyright 2012 Joey Hess <joey@kitenet.net>
-
- Licensed under the GNU GPL version 3 or higher.
-}
module Utility.FileSystemEncoding where
2012-03-09 23:08:10 +00:00
import System.IO
import Foreign.C
2012-03-09 22:52:03 +00:00
import GHC.Foreign as GHC
2012-03-09 23:08:10 +00:00
import GHC.IO.Encoding
{- Sets a Handle to use the filesystem encoding. This causes data
- written or read from it to be encoded/decoded the same
- as ghc 7.4 does to filenames etc. This special encoding
- allows "arbitrary undecodable bytes to be round-tripped through it". -}
fileEncoding :: Handle -> IO ()
fileEncoding h = hSetEncoding h =<< getFileSystemEncoding
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
withFilePath fp f = getFileSystemEncoding >>= \enc -> GHC.withCString enc fp f
2012-03-09 23:08:10 +00:00
{- Encodes a FilePath into a String of encoded bytes, applying the
- filesystem encoding.
-
- This does not do any IO, beyond allocating a C buffer. GHC does not
- seem to provide a pure way to do this conversion. -}
encodeFilePath :: FilePath -> IO String
encodeFilePath fp = do
enc <- getFileSystemEncoding
GHC.withCString enc fp $ GHC.peekCString enc