module Filesystem
( IO.Handle
, IO.IOMode(..)
, rename
, isFile
, getModified
, getSize
, copyFile
, removeFile
, openFile
, withFile
, readFile
, writeFile
, appendFile
, openTextFile
, withTextFile
, readTextFile
, writeTextFile
, appendTextFile
, isDirectory
, canonicalizePath
, listDirectory
, createDirectory
, createTree
, removeDirectory
, removeTree
, getWorkingDirectory
, setWorkingDirectory
, getHomeDirectory
, getDesktopDirectory
, getDocumentsDirectory
, getAppDataDirectory
, getAppCacheDirectory
, getAppConfigDirectory
) where
import Prelude hiding (FilePath, readFile, writeFile, appendFile)
import qualified Control.Exception as Exc
import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as B8
import qualified Data.Text as T
import qualified Data.Text.IO as T
import Foreign.Ptr (nullPtr)
import Foreign.C (CString, withCString, peekCString)
import qualified System.Environment as SE
import Filesystem.Path (FilePath, append)
import Filesystem.Path.CurrentOS (currentOS)
import qualified Filesystem.Path.Rules as R
import qualified System.IO as IO
import System.IO.Error (isDoesNotExistError)
#ifdef CABAL_OS_WINDOWS
import Data.Bits ((.|.))
import Data.Time ( UTCTime(..)
, fromGregorian
, secondsToDiffTime
, picosecondsToDiffTime)
import qualified System.Win32 as Win32
#else
import Data.Time (UTCTime)
import Data.Time.Clock.POSIX (posixSecondsToUTCTime)
import qualified System.Posix as Posix
import qualified System.Posix.Error as Posix
#endif
import qualified System.Directory as SD
isFile :: FilePath -> IO Bool
isFile path = SD.doesFileExist (encodeString path)
isDirectory :: FilePath -> IO Bool
isDirectory path = SD.doesDirectoryExist (encodeString path)
rename :: FilePath -> FilePath -> IO ()
rename old new =
let old' = encodeString old in
let new' = encodeString new in
#ifdef CABAL_OS_WINDOWS
Win32.moveFileEx old' new' Win32.mOVEFILE_REPLACE_EXISTING
#else
Posix.rename old' new'
#endif
canonicalizePath :: FilePath -> IO FilePath
canonicalizePath path =
let path' = encodeString path in
#ifdef CABAL_OS_WINDOWS
fmap decodeString (Win32.getFullPathName path')
#else
withCString path' $ \cPath -> do
cOut <- Posix.throwErrnoPathIfNull "canonicalizePath" path' (c_realpath cPath nullPtr)
out <- peekCString cOut
return (decodeString out)
#endif
#ifndef CABAL_OS_WINDOWS
foreign import ccall unsafe "realpath"
c_realpath :: CString -> CString -> IO CString
#endif
createDirectory :: Bool
-> FilePath -> IO ()
createDirectory False path =
let path' = encodeString path in
#ifdef CABAL_OS_WINDOWS
Win32.createDirectory path' Nothing
#else
Posix.createDirectory path' 0o777
#endif
createDirectory True path = SD.createDirectoryIfMissing False (encodeString path)
createTree :: FilePath -> IO ()
createTree path = SD.createDirectoryIfMissing True (encodeString path)
listDirectory :: FilePath -> IO [FilePath]
listDirectory path = fmap cleanup contents where
contents = SD.getDirectoryContents (encodeString path)
cleanup = map (append path) . map decodeString . filter (`notElem` [".", ".."])
removeFile :: FilePath -> IO ()
removeFile path =
let path' = encodeString path in
#ifdef CABAL_OS_WINDOWS
Win32.deleteFile path'
#else
Posix.removeLink path'
#endif
removeDirectory :: FilePath -> IO ()
removeDirectory path =
let path' = encodeString path in
#ifdef CABAL_OS_WINDOWS
Win32.removeDirectory path'
#else
Posix.removeDirectory path'
#endif
removeTree :: FilePath -> IO ()
removeTree path = SD.removeDirectoryRecursive (encodeString path)
getWorkingDirectory :: IO FilePath
getWorkingDirectory = do
#ifdef CABAL_OS_WINDOWS
raw <- Win32.getCurrentDirectory
#else
raw <- Posix.getWorkingDirectory
#endif
return (decodeString raw)
setWorkingDirectory :: FilePath -> IO ()
setWorkingDirectory path =
let path' = encodeString path in
#ifdef CABAL_OS_WINDOWS
Win32.setCurrentDirectory path'
#else
Posix.changeWorkingDirectory path'
#endif
getHomeDirectory :: IO FilePath
getHomeDirectory = fmap decodeString SD.getHomeDirectory
getDesktopDirectory :: IO FilePath
getDesktopDirectory = xdg "XDG_DESKTOP_DIR" Nothing
(homeSlash "Desktop")
getDocumentsDirectory :: IO FilePath
getDocumentsDirectory = xdg "XDG_DOCUMENTS_DIR" Nothing
#ifdef CABAL_OS_WINDOWS
(fmap decodeString SD.getUserDocumentsDirectory)
#else
(homeSlash "Documents")
#endif
getAppDataDirectory :: T.Text -> IO FilePath
getAppDataDirectory label = xdg "XDG_DATA_HOME" (Just label)
#ifdef CABAL_OS_WINDOWS
(fmap decodeString (SD.getAppUserDataDirectory ""))
#else
(homeSlash ".local/share")
#endif
getAppCacheDirectory :: T.Text -> IO FilePath
getAppCacheDirectory label = xdg "XDG_CACHE_HOME" (Just label)
#ifdef CABAL_OS_WINDOWS
(homeSlash "Local Settings\\Cache")
#else
(homeSlash ".cache")
#endif
getAppConfigDirectory :: T.Text -> IO FilePath
getAppConfigDirectory label = xdg "XDG_CONFIG_HOME" (Just label)
#ifdef CABAL_OS_WINDOWS
(homeSlash "Local Settings")
#else
(homeSlash ".config")
#endif
homeSlash :: String -> IO FilePath
homeSlash path = do
home <- getHomeDirectory
return (append home (decodeString path))
getenv :: String -> IO (Maybe String)
getenv key = Exc.catch
(fmap Just (SE.getEnv key))
(\e -> if isDoesNotExistError e
then return Nothing
else Exc.throwIO e)
xdg :: String -> Maybe T.Text -> IO FilePath -> IO FilePath
xdg envkey label fallback = do
env <- getenv envkey
dir <- case env of
Just var -> return (decodeString var)
Nothing -> fallback
return $ case label of
Just text -> append dir (R.fromText currentOS text)
Nothing -> dir
copyFile :: FilePath
-> FilePath
-> IO ()
copyFile old new = SD.copyFile (encodeString old) (encodeString new)
getModified :: FilePath -> IO UTCTime
getModified path = do
#ifdef CABAL_OS_WINDOWS
info <- withHANDLE path Win32.getFileInformationByHandle
let ftime = Win32.bhfiLastWriteTime info
stime <- Win32.fileTimeToSystemTime ftime
let date = fromGregorian
(fromIntegral (Win32.wYear stime))
(fromIntegral (Win32.wMonth stime))
(fromIntegral (Win32.wDay stime))
let seconds = secondsToDiffTime $
(toInteger (Win32.wHour stime) * 3600) +
(toInteger (Win32.wMinute stime) * 60) +
(toInteger (Win32.wSecond stime))
let msecs = picosecondsToDiffTime $
(toInteger (Win32.wMilliseconds stime) * 1000000000)
return (UTCTime date (seconds + msecs))
#else
stat <- Posix.getFileStatus (encodeString path)
let mtime = Posix.modificationTime stat
return (posixSecondsToUTCTime (realToFrac mtime))
#endif
getSize :: FilePath -> IO Integer
getSize path = do
#ifdef CABAL_OS_WINDOWS
info <- withHANDLE path Win32.getFileInformationByHandle
return (toInteger (Win32.bhfiSize info))
#else
stat <- Posix.getFileStatus (encodeString path)
return (toInteger (Posix.fileSize stat))
#endif
openFile :: FilePath -> IO.IOMode -> IO IO.Handle
openFile path = IO.openBinaryFile (encodeString path)
withFile :: FilePath -> IO.IOMode -> (IO.Handle -> IO a) -> IO a
withFile path = IO.withBinaryFile (encodeString path)
readFile :: FilePath -> IO B.ByteString
readFile path = B.readFile (encodeString path)
writeFile :: FilePath -> B.ByteString -> IO ()
writeFile path = B.writeFile (encodeString path)
appendFile :: FilePath -> B.ByteString -> IO ()
appendFile path = B.appendFile (encodeString path)
openTextFile :: FilePath -> IO.IOMode -> IO IO.Handle
openTextFile path = IO.openFile (encodeString path)
withTextFile :: FilePath -> IO.IOMode -> (IO.Handle -> IO a) -> IO a
withTextFile path = IO.withFile (encodeString path)
readTextFile :: FilePath -> IO T.Text
readTextFile path = T.readFile (encodeString path)
writeTextFile :: FilePath -> T.Text -> IO ()
writeTextFile path = T.writeFile (encodeString path)
appendTextFile :: FilePath -> T.Text -> IO ()
appendTextFile path = T.appendFile (encodeString path)
#ifdef CABAL_OS_WINDOWS
withHANDLE :: FilePath -> (Win32.HANDLE -> IO a) -> IO a
withHANDLE path = Exc.bracket open close where
open = Win32.createFile
(encodeString path)
Win32.gENERIC_READ
(Win32.fILE_SHARE_READ .|. Win32.fILE_SHARE_WRITE)
Nothing
Win32.oPEN_EXISTING
0
Nothing
close = Win32.closeHandle
#endif
encodeString :: FilePath -> String
#ifdef CABAL_OS_WINDOWS
encodeString = T.unpack . R.encode R.windows
#else
encodeString = B8.unpack . R.encode R.posix
#endif
decodeString :: String -> FilePath
#ifdef CABAL_OS_WINDOWS
decodeString = R.decode R.windows . T.pack
#else
decodeString = R.decode R.posix . B8.pack
#endif