debian-binary-0.0.1: System/Debian/Binary/Utils/Temp.hs
-----------------------------------------------------------------------------
-- |
-- Module: System.Debian.Binary.Utils.Temp
-- Copyright: (c) 2008 Marco Túlio Gontijo e Silva <marcot@riseup.net>
-- License: Simple permissive license (see LICENSE)
--
-- Maintainer: Marco Túlio Gontijo e Silva <marcot@riseup.net>
-- Stability: unstable
-- Portability: unportable
--
-- This module provides functions to do an action and undo it after a function
-- passed as paramaeter is executed.
-----------------------------------------------------------------------------
module
System.Debian.Binary.Utils.Temp
(cdTemp, mkdirTemp, mkdirCdTemp, gzipTemp)
where
import Control.Exception
import System.Directory
import HSH
-- | Changes the current directory to @directory@, executes @action@, and then
-- changes the current directory to the original one.
cdTemp
:: FilePath -- ^ @directory@
-> IO result -- ^ @action@
-> IO result
cdTemp directory action
= do
current <- getCurrentDirectory
bracket_
(setCurrentDirectory directory)
(setCurrentDirectory current)
action
-- | Creates a @directory@, executes @action@, then removes it and all its
-- contents.
mkdirTemp
:: FilePath -- ^ @directory@
-> IO result -- ^ @action@
-> IO result
mkdirTemp directory
= bracket_ (createDirectory directory) $ removeDirectoryRecursive directory
-- | Utility function that runs 'mkdirTemp' inside a 'cdTemp'.
mkdirCdTemp :: FilePath -> IO a -> IO a
mkdirCdTemp dir = mkdirTemp dir . cdTemp dir
-- | Extracts a gziped @file@, executes @action@ and compact it again.
gzipTemp
:: FilePath -- ^ @file@
-> IO result -- ^ @action@
-> IO result
gzipTemp file
= bracket_ (runIO $ "gzip -d " ++ file ++ ".gz")
$ runIO
$ "gzip --best " ++ file