LIB: add delete file/dir

This commit is contained in:
Julian Ospald 2015-12-17 16:25:37 +01:00
parent 0f1301622b
commit 25f620ad75
No known key found for this signature in database
GPG Key ID: 220CD1C5BDEED020
1 changed files with 48 additions and 6 deletions

View File

@ -1,9 +1,6 @@
{-# OPTIONS_HADDOCK ignore-exports #-}
module IO.File (
openFile
, executeFile
) where
module IO.File where
import Control.Exception
@ -18,9 +15,12 @@ import Control.Monad
import IO.Error
import System.Directory
(
doesFileExist
, getPermissions
doesDirectoryExist
, doesFileExist
, executable
, getPermissions
, removeDirectory
, removeFile
)
import System.FilePath.Posix
(
@ -33,6 +33,39 @@ import System.Process
)
data FileOperation = Copy
| Move
| Delete
| Open
| Execute
-- |Deletes the given file.
--
-- This will throw an exception if the filepath is not absolute
-- or the file does not exist.
--
-- It also throws exceptions from `removeFile`.
-- TODO: threaded, shouldn't block the GUI
deleteFile :: FilePath -> IO ()
deleteFile fp = do
fileSanityThrow fp
removeFile fp
-- |Deletes the given directory.
--
-- This will throw an exception if the filepath is not absolute
-- or the directory does not exist.
--
-- It also throws exceptions from `removeDirectory`.
-- TODO: threaded, shouldn't block the GUI
deleteDir :: FilePath -> IO ()
deleteDir fp = do
dirSanityThrow fp
removeDirectory fp
-- |Opens a file appropriately by invoking xdg-open.
--
-- This will throw an exception if the filepath is not absolute
@ -66,3 +99,12 @@ fileSanityThrow fp = do
unless (isAbsolute fp) (throw $ PathNotAbsolute fp)
exists <- doesFileExist fp
unless exists (throw $ FileDoesNotExist fp)
-- Throws an exception if the filepath is not absolute
-- or the dir does not exist.
dirSanityThrow :: FilePath -> IO ()
dirSanityThrow fp = do
unless (isAbsolute fp) (throw $ PathNotAbsolute fp)
exists <- doesDirectoryExist fp
unless exists (throw $ FileDoesNotExist fp)