{-| The FileInfo module provides the definition of a class and methods for that class that can be used to store and obtain information about a file in a generic fashion. -} module FSOps.FileInfo ( -- ** File Node Classes and Types FileInfoClass (name, path_to, pathname, size, p_Directory), StdFileInfo, mkStdFileInfo, ) where import System.Posix import FSOps.FileBase {- | The 'FileInfoClass' is a class that can be used for each node in the 'FileTree'. The 'FileInfoClass' is a rich data structure that allows querying all sorts of information about the node in question. The minimal definition amongst @pathname@, @name@, and @path_to@ is either: * @pathname@ * @name@ and @path_to@. -} class FileInfoClass a where name :: a -> FilePath path_to :: a -> FilePath pathname :: a -> FilePath size :: a -> FileOffset p_Directory :: a -> Bool -- ^ Returns true if this entry is a directory name = snd . pathsplit . pathname path_to = fst . pathsplit . pathname pathname a = path_to a /// name a {- | The 'StdFileInfo' type is the default instance of the FileInfoClass. It supports all of the methods defined for the 'FileInfoClass', as well as subclassing the 'Show' class. -} data StdFileInfo = FileInfo { path::FilePath, info::FileStatus } instance Show StdFileInfo where showsPrec d finfo = let f_path = pathname finfo f_size = size finfo in showParen (d > 10) $ showString f_path . showString " (" . shows f_size . showString ")" -- instance Eq StdFileInfo where instance FileInfoClass StdFileInfo where pathname = path size = fileSize . info p_Directory = isDirectory . info {- | The 'mkStdFileInfo' function is used to convert a 'FilePath' (i.e. the path name string) of a file into a 'StdFileInfo' object. The default 'FileTree' constructed by 'get_filetree' or 'get_filetrees' is a @(FileTree FileSpec)@; this can be converted to an @IO (FileTree StdFileInfo)@ by calling: > mapTreeM mkStdFileInfo tree or directly via: > get_filetree "root" >>= mapTreeM mkStdFileInfo -} mkStdFileInfo :: FilePath -> IO StdFileInfo mkStdFileInfo p = return . FileInfo p =<< getFileStatus p