module System.Path.Directory
(
createDirectory,
createDirectoryIfMissing,
removeDirectory,
removeDirectoryRecursive,
renameDirectory,
getDirectoryContents,
absDirectoryContents,
relDirectoryContents,
filesInDir,
dirsInDir,
getCurrentDirectory,
setCurrentDirectory,
getHomeDirectory,
getAppUserDataDirectory,
getUserDocumentsDirectory,
getTemporaryDirectory,
removeFile,
renameFile,
copyFile,
canonicalizePath,
makeRelativeToCurrentDirectory,
findExecutable,
doesFileExist,
doesDirectoryExist,
Permissions,
getPermissions,
setPermissions,
getModificationTime
)
where
import Prelude hiding (FilePath)
import System.Path
import Control.Applicative
import Control.Arrow
import Data.List
import Data.Time
import GHC.Exts(IsString(..))
import System.Directory (Permissions)
import qualified System.Directory as SD
import System.IO hiding (FilePath)
import System.IO.Error
import Test.QuickCheck
import Text.Printf
#if !(MIN_VERSION_directory(1,2,0))
import System.Time
#endif
createDirectory :: AbsRelClass ar => DirPath ar -> IO ()
createDirectory = SD.createDirectory . getPathString
createDirectoryIfMissing :: AbsRelClass ar => Bool -> DirPath ar -> IO ()
createDirectoryIfMissing flag = SD.createDirectoryIfMissing flag . getPathString
removeDirectory :: AbsRelClass ar => DirPath ar -> IO ()
removeDirectory = SD.removeDirectory . getPathString
removeDirectoryRecursive :: AbsRelClass ar => DirPath ar -> IO ()
removeDirectoryRecursive = SD.removeDirectoryRecursive . getPathString
renameDirectory :: (AbsRelClass ar1, AbsRelClass ar2) => DirPath ar1 -> DirPath ar2 -> IO ()
renameDirectory p1 p2 = SD.renameDirectory (getPathString p1) (getPathString p2)
getDirectoryContents :: AbsRelClass ar => DirPath ar -> IO ([RelDir], [RelFile])
getDirectoryContents = relDirectoryContents
absDirectoryContents :: AbsRelClass ar => DirPath ar -> IO ([AbsDir], [AbsFile])
absDirectoryContents p = do
cd <- asAbsDir <$> SD.getCurrentDirectory
let dir = absRel id (cd </>) p
(rds, rfs) <- relDirectoryContents dir
return (map (dir </>) rds, map (dir </>) rfs)
relDirectoryContents :: AbsRelClass ar => DirPath ar -> IO ([RelDir], [RelFile])
relDirectoryContents dir = do
filenames <- filter (not . flip elem [".",".."]) <$> SD.getDirectoryContents (getPathString dir)
dirFlags <- mapM (doesDirectoryExist . (dir </>) . asRelPath) filenames
let fileinfo = zip filenames dirFlags
(dirs, files) = partition snd fileinfo
return (map (combine currentDir . asRelDir . fst) dirs,
map (combine currentDir . asRelFile . fst) files)
filesInDir :: AbsRelClass ar => DirPath ar -> IO [RelFile]
filesInDir dir = snd <$> relDirectoryContents dir
dirsInDir :: AbsRelClass ar => DirPath ar -> IO [RelDir]
dirsInDir dir = fst <$> relDirectoryContents dir
getCurrentDirectory :: IO AbsDir
getCurrentDirectory = asAbsDir <$> SD.getCurrentDirectory
setCurrentDirectory :: AbsRelClass ar => DirPath ar -> IO ()
setCurrentDirectory = SD.setCurrentDirectory . getPathString
getHomeDirectory :: IO AbsDir
getHomeDirectory = asAbsDir <$> SD.getHomeDirectory
getAppUserDataDirectory :: String -> IO AbsDir
getAppUserDataDirectory user = asAbsDir <$> SD.getAppUserDataDirectory user
getUserDocumentsDirectory :: IO AbsDir
getUserDocumentsDirectory = asAbsDir <$> SD.getUserDocumentsDirectory
getTemporaryDirectory :: IO AbsDir
getTemporaryDirectory = asAbsDir <$> SD.getTemporaryDirectory
removeFile :: AbsRelClass ar => FilePath ar -> IO ()
removeFile = SD.removeFile . getPathString
renameFile :: (AbsRelClass ar1, AbsRelClass ar2) => FilePath ar1 -> FilePath ar2 -> IO ()
renameFile p1 p2 = SD.renameFile (getPathString p1) (getPathString p2)
copyFile :: (AbsRelClass ar1, AbsRelClass ar2) => FilePath ar1 -> FilePath ar2 -> IO ()
copyFile p1 p2 = SD.copyFile (getPathString p1) (getPathString p2)
canonicalizePath :: AbsRelClass ar => Path ar fd -> IO (AbsPath fd)
canonicalizePath p = asPath <$> SD.canonicalizePath (getPathString p)
makeRelativeToCurrentDirectory :: AbsRelClass ar => Path ar fd -> IO (RelPath fd)
makeRelativeToCurrentDirectory p = asPath <$> SD.makeRelativeToCurrentDirectory (getPathString p)
findExecutable :: String -> IO (Maybe AbsFile)
findExecutable s = fmap asPath <$> SD.findExecutable s
doesFileExist :: AbsRelClass ar => FilePath ar -> IO Bool
doesFileExist = SD.doesFileExist . getPathString
doesDirectoryExist :: AbsRelClass ar => DirPath ar -> IO Bool
doesDirectoryExist = SD.doesDirectoryExist . getPathString
getPermissions :: AbsRelClass ar => Path ar fd -> IO Permissions
getPermissions p = SD.getPermissions (getPathString p)
setPermissions :: AbsRelClass ar => Path ar fd -> Permissions -> IO ()
setPermissions p perms = SD.setPermissions (getPathString p) perms
getModificationTime :: AbsRelClass ar => Path ar fd -> IO UTCTime
#if MIN_VERSION_directory(1,2,0)
getModificationTime p = SD.getModificationTime (getPathString p)
#else
getModificationTime p = convertTime <$> SD.getModificationTime (getPathString p)
where
convertTime clock = UTCTime day (fromIntegral sec)
where
calendar = toUTCTime clock
day = addDays (toInteger (ctYDay calendar)) yearStart
yearStart = fromGregorian (toInteger (ctYear calendar)) 1 1
hour = ctHour calendar
min = hour * 60 + ctMin calendar
sec = min * 60 + ctSec calendar
#endif
testall = do
putStrLn "Running QuickCheck tests..."
putStrLn "Tests completed."
vectorOf :: Gen a -> Int -> Gen [a]
vectorOf gen n = sequence [ gen | i <- [1..n] ]