module System.FileSystem
(
InApp
, DirName , FileName
, ByteString
, ClockTime
, File
, emptyFile , newFile
, getFileName , setFileName
, getFileContent , setFileContent
, getModifTime , setModifTime
, fileSize
, FileSystem , emptyFileSystem
, mapFileSystem
, mapFiles
, foldFileSystem
, foldFiles
, FSState
, FST , runFST
, FS, runFS
, DirPath , toDirPath , fromDirPath
, FPath , toFPath , fromFPath
, putFileL
, putFile
, getFileL
, getFile
, modFileL
, modFile
, moveFileL
, moveFile
, fs_writeFileL
, fs_writeFile
, fs_readFileL
, fs_readFile
, writeFileTimeL
, writeFileTime
, fs_getDirectoryContentsL
, fs_getDirectoryContents
, fs_doesFileExistL
, fs_doesFileExist
, fs_doesDirectoryExistL
, fs_doesDirectoryExist
, fs_createDirectoryL
, fs_createDirectory
, fs_removeDirectoryL
, fs_removeDirectory
, fs_removeFileL
, fs_removeFile
, fs_renameDirectoryL
, fs_renameDirectory
, fs_renameFileL
, fs_renameFile
, fs_copyFileL
, fs_copyFile
, fs_getModificationTimeL
, fs_getModificationTime
, module System.FileSystem.IO
, module System.FileSystem.Instances
, module System.FileSystem.Class
) where
import Data.ByteString (ByteString, length)
import System.Time (ClockTime,getClockTime)
import Control.Arrow (first , (***))
import System.FilePath (splitFileName)
import Data.Maybe (isJust,fromJust)
import Control.Monad.State (runStateT,MonadIO,liftIO)
import System.FileSystem.Types
import System.FileSystem.Across
import System.FileSystem.IO
import System.FileSystem.Computations
import System.FileSystem.Utils
import System.FileSystem.Instances
import System.FileSystem.Class
emptyFile :: FileName -> File
emptyFile = File emptyFD
newFile :: FileName -> ByteString -> File
newFile = curry $ uncurry (flip setFileContent) . first emptyFile
getFileName :: File -> FileName
getFileName = getFN
setFileName :: FileName -> File -> File
setFileName fn f = f { getFN = fn }
getFileContent :: File -> ByteString
getFileContent = getCnt . getFD
setFileContent :: ByteString -> File -> File
setFileContent c f = f { getFD = (getFD f) { getCnt = c } }
getModifTime :: File -> ClockTime
getModifTime = getLmt . getFD
setModifTime :: ClockTime -> File -> File
setModifTime t f = f { getFD = (getFD f) { getLmt = t } }
fileSize :: File -> Int
fileSize = Data.ByteString.length . getFileContent
mapFiles :: InApp File -> InApp FileSystem
mapFiles = mapFileSystem id
putFileL :: FSMonad m
=> DirPath
-> File
-> m ()
putFileL dp = putPath . Path dp . Just
putFile :: FSMonad m => FilePath -> File -> m ()
putFile = putFileL . toDirPath
getFileL :: FSMonad m
=> FPath
-> m (Maybe File)
getFileL = getFl
getFile :: FSMonad m => FilePath -> m (Maybe File)
getFile = getFileL . toFPath
modFileL :: FSMonad m => FPath -> InApp File -> m Bool
modFileL fp f =
do file <- getFileL fp
if isJust file
then do putFileL (fst fp) $ f $ fromJust file
return True
else return False
modFile :: FSMonad m => FilePath -> InApp File -> m Bool
modFile = modFileL . toFPath
moveFileL :: FSMonad m
=> FPath
-> FPath
-> m Bool
moveFileL fp1 (dp,fn) =
do f1 <- getFileL fp1
if isJust f1
then do putFileL dp $ setFileName fn $ fromJust f1
fs_removeFileL fp1
return True
else return False
moveFile :: FSMonad m => FilePath -> FilePath -> m Bool
moveFile = curry $ uncurry moveFileL . pairMap toFPath
fs_writeFileL :: FSMonad m => FPath -> ByteString -> m ()
fs_writeFileL = uncurry (.) . (putFileL *** newFile)
fs_writeFile :: FSMonad m => FilePath -> ByteString -> m ()
fs_writeFile = fs_writeFileL . toFPath
writeFileTimeL :: (FSMonad m, MonadIO m) => FPath -> ByteString -> m ()
writeFileTimeL (dp,fn) bs = do t <- liftIO getClockTime
putFileL dp . setModifTime t $ newFile fn bs
writeFileTime :: (FSMonad m, MonadIO m) => FilePath -> ByteString -> m ()
writeFileTime = writeFileTimeL . toFPath
fs_readFileL :: FSMonad m => FPath -> m (Maybe ByteString)
fs_readFileL = fmap (fmap getFileContent) . getFileL
fs_readFile :: FSMonad m => FilePath -> m (Maybe ByteString)
fs_readFile = fs_readFileL . toFPath
fs_getDirectoryContentsL :: FSMonad m => DirPath -> m (Maybe ([DirName],[FileName]))
fs_getDirectoryContentsL = getDirCnt
fs_getDirectoryContents :: FSMonad m => FilePath -> m (Maybe ([DirName],[FileName]))
fs_getDirectoryContents = fs_getDirectoryContentsL . toDirPath
fs_doesFileExistL :: FSMonad m => FPath -> m Bool
fs_doesFileExistL = checkFExist
fs_doesFileExist :: FSMonad m => FilePath -> m Bool
fs_doesFileExist = fs_doesFileExistL . toFPath
fs_doesDirectoryExistL :: FSMonad m => DirPath -> m Bool
fs_doesDirectoryExistL = checkDExist
fs_doesDirectoryExist :: FSMonad m => FilePath -> m Bool
fs_doesDirectoryExist = fs_doesDirectoryExistL . toDirPath
fs_createDirectoryL :: FSMonad m => DirPath -> m ()
fs_createDirectoryL = newDir
fs_createDirectory :: FSMonad m => FilePath -> m ()
fs_createDirectory = fs_createDirectoryL . toDirPath
fs_removeDirectoryL :: FSMonad m => DirPath -> m ()
fs_removeDirectoryL = rmvDir
fs_removeDirectory :: FSMonad m => FilePath -> m ()
fs_removeDirectory = fs_removeDirectoryL . toDirPath
fs_removeFileL :: FSMonad m => FPath -> m ()
fs_removeFileL = rmvFile
fs_removeFile :: FSMonad m => FilePath -> m ()
fs_removeFile = fs_removeFileL . toFPath
fs_renameDirectoryL :: FSMonad m => DirPath -> DirPath -> m Bool
fs_renameDirectoryL = curry rnmDir
fs_renameDirectory :: FSMonad m => FilePath -> FilePath -> m Bool
fs_renameDirectory = curry $ rnmDir . pairMap toDirPath
fs_renameFileL :: FSMonad m
=> DirPath
-> FileName
-> FileName
-> m Bool
fs_renameFileL dp fn1 fn2 = rnmFile ( (dp,fn1) , (dp,fn2) )
fs_renameFile :: FSMonad m => FilePath -> FileName -> FileName -> m Bool
fs_renameFile = fs_renameFileL . toDirPath
fs_copyFileL :: FSMonad m => FPath -> FPath -> m Bool
fs_copyFileL f1 f2 = fs_readFileL f1
>>= maybe ( return False )
( bind (const $ return True)
. fs_writeFileL f2)
fs_copyFile :: FSMonad m => FilePath -> FilePath -> m Bool
fs_copyFile = curry $ uncurry fs_copyFileL . pairMap toFPath
fs_getModificationTimeL :: FSMonad m => FPath -> m (Maybe ClockTime)
fs_getModificationTimeL = fmap (fmap getModifTime) . getFileL
fs_getModificationTime :: FSMonad m => FilePath -> m (Maybe ClockTime)
fs_getModificationTime = fs_getModificationTimeL . toFPath