Skip to content

Instantly share code, notes, and snippets.

@jml
Created February 28, 2015 21:58
Show Gist options
  • Save jml/c832355ff1642fba8af9 to your computer and use it in GitHub Desktop.
Save jml/c832355ff1642fba8af9 to your computer and use it in GitHub Desktop.
{-# LANGUAGE CPP #-}
{-# LANGUAGE ForeignFunctionInterface #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
import Prelude hiding (FilePath)
import qualified Data.ByteString
import Data.ByteString (ByteString)
import Foreign
import Foreign.C
import Filesystem
import Filesystem.Path
import qualified Filesystem.Path.Rules as Rules
decode :: ByteString -> FilePath
decode = Rules.decode Rules.posix
encode :: FilePath -> ByteString
encode = Rules.encode Rules.posix
oldName = decode "old_\xA1\xA2\xA3.txt"
newName = decode "new_\xA1\xA2\xA3.txt"
withPathCString :: FilePath -> (CString -> IO a) -> IO a
withPathCString p = Data.ByteString.useAsCString (encode p)
foreign import ccall unsafe "fopen"
c_fopen :: CString -> CString -> IO (Ptr ())
foreign import ccall unsafe "fclose"
c_fclose :: Ptr () -> IO CInt
foreign import ccall unsafe "fwrite"
c_fwrite :: CString -> CSize -> CSize -> Ptr () -> IO CSize
touch_ffi :: FilePath -> Data.ByteString.ByteString -> IO ()
touch_ffi path contents = do
fp <- withPathCString path $ \path_cstr ->
Foreign.C.withCString "wb" $ \mode_cstr ->
c_fopen path_cstr mode_cstr
_ <- Data.ByteString.useAsCStringLen contents $ \(buf, len) ->
c_fwrite buf 1 (fromIntegral len) fp
_ <- c_fclose fp
return ()
main = do
touch_ffi oldName ""
Filesystem.copyFile oldName newName
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment