33 lines
1.0 KiB
Haskell
33 lines
1.0 KiB
Haskell
module Files (
|
|
File(..)
|
|
, absoluteLink
|
|
, filePath
|
|
, find
|
|
) where
|
|
|
|
import System.Directory (doesDirectoryExist, doesFileExist, listDirectory)
|
|
import System.FilePath ((</>))
|
|
|
|
data File = File FilePath | Dir FilePath
|
|
|
|
absoluteLink :: FilePath -> FilePath
|
|
absoluteLink ('.':path) = path
|
|
absoluteLink path = "/" </> path
|
|
|
|
filePath :: File -> IO (Either String FilePath)
|
|
filePath = filePathAux
|
|
where
|
|
filePathAux (File path) = ifIO doesFileExist path Right (notExist . File)
|
|
filePathAux (Dir path) = ifIO doesDirectoryExist path Right (notExist . Dir)
|
|
ifIO predicate value whenTrue whenFalse = do
|
|
result <- predicate value
|
|
return $ if result then whenTrue value else whenFalse value
|
|
notExist (File path) = Left $ path ++ ": no such file"
|
|
notExist (Dir path) = Left $ path ++ ": no such directory"
|
|
|
|
find :: FilePath -> IO [FilePath]
|
|
find path =
|
|
filePath (Dir path) >>= emptyIfMissing (fmap ((path </>) <$>) . listDirectory)
|
|
where
|
|
emptyIfMissing = either (\_ -> return [])
|