diff options
author | Joey Hess <joey@kitenet.net> | 2012-03-09 19:08:10 -0400 |
---|---|---|
committer | Joey Hess <joey@kitenet.net> | 2012-03-09 19:08:10 -0400 |
commit | d6e77595ba45762b3c2dfdcd47a2d6b5b70154ae (patch) | |
tree | 896615a59c8f67f3bfec97c55616b7e59017927b /Utility | |
parent | 789254747bceeaac004236275a6c1906f859945a (diff) |
factor out Utility.FileSystemEncoding
Diffstat (limited to 'Utility')
-rw-r--r-- | Utility/FileSystemEncoding.hs | 23 | ||||
-rw-r--r-- | Utility/Misc.hs | 8 | ||||
-rw-r--r-- | Utility/StatFS.hsc | 5 | ||||
-rw-r--r-- | Utility/Touch.hsc | 5 |
4 files changed, 25 insertions, 16 deletions
diff --git a/Utility/FileSystemEncoding.hs b/Utility/FileSystemEncoding.hs index 6970a10de..048323ee3 100644 --- a/Utility/FileSystemEncoding.hs +++ b/Utility/FileSystemEncoding.hs @@ -1,4 +1,4 @@ -{- File system encoding handling. +{- GHC File system encoding handling. - - Copyright 2012 Joey Hess <joey@kitenet.net> - @@ -7,8 +7,17 @@ module Utility.FileSystemEncoding where -import GHC.IO.Encoding (getFileSystemEncoding) +import System.IO +import Foreign.C 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 - storage. The FilePath is encoded using the filesystem encoding, @@ -16,3 +25,13 @@ import GHC.Foreign as GHC - was obtained. -} withFilePath :: FilePath -> (CString -> IO a) -> IO a 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 diff --git a/Utility/Misc.hs b/Utility/Misc.hs index 9c284c826..3ac5ca5c0 100644 --- a/Utility/Misc.hs +++ b/Utility/Misc.hs @@ -9,14 +9,6 @@ module Utility.Misc where import System.IO 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 - all read before it gets closed. -} diff --git a/Utility/StatFS.hsc b/Utility/StatFS.hsc index 51a6bda1e..58d0b3e02 100644 --- a/Utility/StatFS.hsc +++ b/Utility/StatFS.hsc @@ -47,15 +47,14 @@ module Utility.StatFS ( FileSystemStats(..), getFileSystemStats ) where +import Utility.FileSystemEncoding + import Foreign import Foreign.C.Types import Foreign.C.String import GHC.IO.Encoding (getFileSystemEncoding) 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__) # include <sys/param.h> # include <sys/mount.h> diff --git a/Utility/Touch.hsc b/Utility/Touch.hsc index 24ccd17a6..b84054cbc 100644 --- a/Utility/Touch.hsc +++ b/Utility/Touch.hsc @@ -13,15 +13,14 @@ module Utility.Touch ( touch ) where +import Utility.FileSystemEncoding + import Foreign import Foreign.C import Control.Monad (when) import GHC.IO.Encoding (getFileSystemEncoding) 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 {- Changes the access and modification times of an existing file. |