factor out Utility.FileSystemEncoding

This commit is contained in:
Joey Hess 2012-03-09 19:08:10 -04:00
parent 789254747b
commit d6e77595ba
5 changed files with 26 additions and 16 deletions

View file

@ -26,5 +26,6 @@ import Utility.SafeCommand as X
import Utility.Path as X import Utility.Path as X
import Utility.Directory as X import Utility.Directory as X
import Utility.Monad as X import Utility.Monad as X
import Utility.FileSystemEncoding as X
import Utility.PartialPrelude as X import Utility.PartialPrelude as X

View file

@ -1,4 +1,4 @@
{- File system encoding handling. {- GHC File system encoding handling.
- -
- Copyright 2012 Joey Hess <joey@kitenet.net> - Copyright 2012 Joey Hess <joey@kitenet.net>
- -
@ -7,8 +7,17 @@
module Utility.FileSystemEncoding where module Utility.FileSystemEncoding where
import GHC.IO.Encoding (getFileSystemEncoding) import System.IO
import Foreign.C
import GHC.Foreign as GHC import GHC.Foreign as GHC
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
{- Marshal a Haskell FilePath into a NUL terminated C string using temporary {- Marshal a Haskell FilePath into a NUL terminated C string using temporary
- storage. The FilePath is encoded using the filesystem encoding, - storage. The FilePath is encoded using the filesystem encoding,
@ -16,3 +25,13 @@ import GHC.Foreign as GHC
- was obtained. -} - was obtained. -}
withFilePath :: FilePath -> (CString -> IO a) -> IO a withFilePath :: FilePath -> (CString -> IO a) -> IO a
withFilePath fp f = getFileSystemEncoding >>= \enc -> GHC.withCString enc fp f withFilePath fp f = getFileSystemEncoding >>= \enc -> GHC.withCString enc fp f
{- 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

View file

@ -9,14 +9,6 @@ module Utility.Misc where
import System.IO import System.IO
import Control.Monad import Control.Monad
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 et. This special encoding
- allows "arbitrary undecodable bytes to be round-tripped through it". -}
fileEncoding :: Handle -> IO ()
fileEncoding h = hSetEncoding h =<< getFileSystemEncoding
{- A version of hgetContents that is not lazy. Ensures file is {- A version of hgetContents that is not lazy. Ensures file is
- all read before it gets closed. -} - all read before it gets closed. -}

View file

@ -47,15 +47,14 @@
module Utility.StatFS ( FileSystemStats(..), getFileSystemStats ) where module Utility.StatFS ( FileSystemStats(..), getFileSystemStats ) where
import Utility.FileSystemEncoding
import Foreign import Foreign
import Foreign.C.Types import Foreign.C.Types
import Foreign.C.String import Foreign.C.String
import GHC.IO.Encoding (getFileSystemEncoding) import GHC.IO.Encoding (getFileSystemEncoding)
import GHC.Foreign as GHC import GHC.Foreign as GHC
withFilePath :: FilePath -> (CString -> IO a) -> IO a
withFilePath fp f = getFileSystemEncoding >>= \enc -> GHC.withCString enc fp f
#if defined (__FreeBSD__) || defined (__FreeBSD_kernel__) || defined (__APPLE__) #if defined (__FreeBSD__) || defined (__FreeBSD_kernel__) || defined (__APPLE__)
# include <sys/param.h> # include <sys/param.h>
# include <sys/mount.h> # include <sys/mount.h>

View file

@ -13,15 +13,14 @@ module Utility.Touch (
touch touch
) where ) where
import Utility.FileSystemEncoding
import Foreign import Foreign
import Foreign.C import Foreign.C
import Control.Monad (when) import Control.Monad (when)
import GHC.IO.Encoding (getFileSystemEncoding) import GHC.IO.Encoding (getFileSystemEncoding)
import GHC.Foreign as GHC import GHC.Foreign as GHC
withFilePath :: FilePath -> (CString -> IO a) -> IO a
withFilePath fp f = getFileSystemEncoding >>= \enc -> GHC.withCString enc fp f
newtype TimeSpec = TimeSpec CTime newtype TimeSpec = TimeSpec CTime
{- Changes the access and modification times of an existing file. {- Changes the access and modification times of an existing file.