FileSystem-1.0.0: File system data structure and monad transformer.




Several type synonyms

type InApp a = a -> aSource

Internal Application: An application from somewhere over itself.

type DirName = StringSource

A name for a directory.

type FileName = StringSource

A name for a file.



A re-export of the ByteString type. The content of a File is stored in a ByteString.

data ByteString

A space-efficient representation of a Word8 vector, supporting many efficient operations. A ByteString contains 8-bit characters only.

Instances of Eq, Ord, Read, Show, Data, Typeable


A re-export of the ClockTime type. ClockTime is used to represents modification times.

data ClockTime

A representation of the internal clock time. Clock times may be compared, converted to strings, or converted to an external calendar time CalendarTime for I/O or other manipulations.


The File type and basic functions to operate with it.

data File Source

A complete file.


emptyFile :: FileName -> FileSource

Create an empty file with the given FileName.

newFile :: FileName -> ByteString -> FileSource

Build a new file with an initial FileName and content.

getFileName :: File -> FileNameSource

Get the name of a File.

setFileName :: FileName -> File -> FileSource

Rename a File with the given FileName.

getFileContent :: File -> ByteStringSource

Extract the content of a File.

setFileContent :: ByteString -> File -> FileSource

Set the content of a File to the given ByteString.

getModifTime :: File -> ClockTimeSource

Get the last modification time of a File.

setModifTime :: ClockTime -> File -> FileSource

Set the last modification time of a File.

fileSize :: File -> IntSource

Calculate the size of a File.


data FileSystem Source

The file system structure. It stores a directory with files and subdirectories.

emptyFileSystem :: FileSystemSource

An empty file system.

Across the FileSystem


mapFileSystem :: InApp DirName -> InApp File -> InApp FileSystemSource

Map a pair of applications (one over DirName, and the other over File) through a FileSystem.

mapFiles :: InApp File -> InApp FileSystemSource

Map a function over all the Files of a FileSystem.




:: FilePath

Root path

-> Either (FilePath -> t -> Either DirName File -> t) (FilePath -> Either DirName File -> t -> t)

Folding operator, with current FilePath reference

-> t

The initial value

-> FileSystem

The FileSystem to fold

-> t


Folding function for FileSystems.

foldFiles :: Either (t -> File -> t) (File -> t -> t) -> t -> FileSystem -> tSource

An usage of foldFileSystem, folding only Files, ignoring the FilePath where they are.

Computations over a FileSystem


FST monad transformer

type FSState = FileSystemSource

The state of file system computations.

Currently, a FileSystem structure.

data FST m a Source

Monadic transformer which adds a FSState environment.


MonadTrans FST 
Monad m => Monad (FST m) 
Functor m => Functor (FST m) 
MonadIO m => MonadIO (FST m) 
(Functor m, Monad m) => FSMonad (FST m) 

runFST :: Monad m => FST m a -> FSState -> m (a, FSState)Source

Run an FST computation, given an initial state.

type FS = FST IdentitySource

Application of the FST monad transformer to the Identity monad.

runFS :: FS a -> FSState -> (a, FSState)Source

Just a composition of runIdentity and runFST.

List-based paths

List-based paths is an alternative to FilePath that represents a path as a list of names.

While directory paths are a simple list of DirNames, file paths are a pair: (DirPath,FileName). First component is the directory where the file is, and the second is the name of the file.

Below you can see a list of examples of how to represent several FilePaths:

 |  FilePath  |   List-based path   |
 | "aa\bb\cc" |   ["aa","bb",cc"]   |
 | "dir\file" |   (["dir"],"file")  |
 | "a\b\f.hs" |  (["a","b"],"f.hs") |

type DirPath = [DirName]Source

A list-based directory path.

toDirPath :: FilePath -> DirPathSource

Translation between FilePath and DirPath.

fromDirPath :: DirPath -> FilePathSource

Translation between DirPath and FilePath.

type FPath = (DirPath, FileName)Source

A file path, composed by the path of the directory which contains it, and its file name.

toFPath :: FilePath -> FPathSource

Translation between FilePath and FPath.

fromFPath :: FPath -> FilePathSource

Translation between FPath and FilePath.


Computations within a FSMonad environment. All functions have a list-based path version (with L at the end), and a normal FilePath version. Use toDirPath (toFPath) and fromDirPath (fromFPath) to change between both formats.

Also, to avoid name collisions, function with existing names have fs_ as preffix.

Put/Get File operations



:: FSMonad m 
=> DirPath

Directory where put the File.

-> File 
-> m () 

Puts a file in the given directory. It creates the parent directory if missing.



:: FSMonad m 
=> FPath

Path where search the File.

-> m (Maybe File) 

Gets a file from the file system. It returns Nothing if the File is not found.

modFileL :: FSMonad m => FPath -> InApp File -> m BoolSource

Modifies a file with the given application. It returns True if the file was found and modified.



:: FSMonad m 
=> FPath

Original path of the File.

-> FPath

New path for the File.

-> m Bool 

Moves a file. It returns True if the file exists and has been moved.

Writing and reading

Writing and reading files in a FSMonad environment.

Pure operations

fs_writeFileL :: FSMonad m => FPath -> ByteString -> m ()Source

Writes a file. If the files already exists, it is overwritten.

fs_readFileL :: FSMonad m => FPath -> m (Maybe ByteString)Source

Reads a file. It returns Nothing if the file can't be found.

IO monad operations

writeFileTimeL :: (FSMonad m, MonadIO m) => FPath -> ByteString -> m ()Source

Writes a file, changing its last modification time to the current time. If the file already exists, it is overwritten. Note that MonadIO instance is needed.

System.Directory operations

All these functions are analogous to those defined in System.Directory.

fs_getDirectoryContentsL :: FSMonad m => DirPath -> m (Maybe ([DirName], [FileName]))Source

Returns all directory names and file names located in the given directory. It returns Nothing if the directory does not exist.

fs_doesFileExistL :: FSMonad m => FPath -> m BoolSource

Checks if a file exists.

fs_doesDirectoryExistL :: FSMonad m => DirPath -> m BoolSource

Checks if a directory exists.

fs_createDirectoryL :: FSMonad m => DirPath -> m ()Source

Creates a new directory. If the directory already exists, it does nothing.

fs_removeDirectoryL :: FSMonad m => DirPath -> m ()Source

Removes a directory, with all its content. If the directory does not exist, it does nothing.

fs_removeFileL :: FSMonad m => FPath -> m ()Source

Removes a file. If the file does not exist, it does nothing.

fs_renameDirectoryL :: FSMonad m => DirPath -> DirPath -> m BoolSource

Renames a directory. If the directory can't be found, it returns False.



:: FSMonad m 
=> DirPath

Directory where the file is.

-> FileName

Original name.

-> FileName

New name.

-> m Bool 

Renames a file. First, you must specify the directory where the file is. If the file can't be found, it returns False.

fs_copyFileL :: FSMonad m => FPath -> FPath -> m BoolSource

Copies a file from a location to another. Returns True if the file was found and copied.

fs_getModificationTimeL :: FSMonad m => FPath -> m (Maybe ClockTime)Source

Gets the last modification time of a file. It returns Nothing if the file doesn't exist.


IO interface

Class instances

Some needed class instances.

FSMonad class

If you want to use another monad, instead of FST, make your type instance of the class here defined, FSMonad.