diff --git a/System/IO/Temp.hs b/System/IO/Temp.hs index fdb03cf..d0c9a23 100644 --- a/System/IO/Temp.hs +++ b/System/IO/Temp.hs @@ -1,5 +1,5 @@ module System.IO.Temp ( - withSystemTempFile, withSystemTempDirectory, + withSystemTempFile, withSystemTempDirectory,withCanonicalisedSystemTempDirectory, withTempFile, withTempDirectory, module Distribution.Compat.TempFile ) where @@ -16,7 +16,6 @@ import Control.Monad.Catch as Exception import Control.Monad.IO.Class import System.Directory import System.IO - import Distribution.Compat.TempFile @@ -40,6 +39,18 @@ withSystemTempDirectory :: (MonadIO m, MonadMask m) => -> m a withSystemTempDirectory template action = liftIO getTemporaryDirectory >>= \tmpDir -> withTempDirectory tmpDir template action +-- | Create and use a canonicalised temporary directory in the system standard temporary directory. +-- +-- Behaves exactly the same as 'withTempDirectory', except that the parent temporary directory +-- will be that returned by 'getTemporaryDirectory'. +withCanonicalisedSystemTempDirectory :: (MonadIO m, MonadMask m) => + String -- ^ Directory name template. See 'openTempFile'. + -> (FilePath -> m a) -- ^ Callback that can use the directory + -> m a +withCanonicalisedSystemTempDirectory template action = do + tmpDir <- liftIO getTemporaryDirectory + withTempDirectoryCanonicalised tmpDir template action + -- | Use a temporary filename that doesn't already exist. -- @@ -80,5 +91,25 @@ withTempDirectory targetDir template = (liftIO (createTempDirectory targetDir template)) (liftIO . ignoringIOErrors . removeDirectoryRecursive) +-- | Create and use a canonicalised temporary directory. +-- +-- Creates a new temporary directory inside the given directory, making use +-- of the template. The temp directory is deleted after use. For example: +-- +-- > withTempDirectoryCanonicalised "src" "sdist." $ \tmpDir -> do ... +-- +-- The @tmpDir@ will be a new subdirectory of the given directory, e.g. +-- @src/sdist.342@. +withTempDirectoryCanonicalised :: (MonadMask m, MonadIO m) => + FilePath -- ^ Temp directory to create the directory in + -> String -- ^ Directory name template. See 'openTempFile'. + -> (FilePath -> m a) -- ^ Callback that can use the directory + -> m a +withTempDirectoryCanonicalised targetDir template action = + Exception.bracket + (liftIO (createTempDirectory targetDir template)) + (liftIO . ignoringIOErrors . removeDirectoryRecursive) + (\path -> action =<< liftIO (canonicalizePath path)) + ignoringIOErrors :: MonadCatch m => m () -> m () ignoringIOErrors ioe = ioe `Exception.catch` (\e -> const (return ()) (e :: IOError)) diff --git a/temporary.cabal b/temporary.cabal index 8730c4f..87d1684 100644 --- a/temporary.cabal +++ b/temporary.cabal @@ -1,5 +1,5 @@ name: temporary -version: 1.2.0.3 +version: 1.2.0.4 cabal-version: >= 1.6 synopsis: Portable temporary file and directory support for Windows and Unix, based on code from Cabal description: The functions for creating temporary files and directories in the base library are quite limited. The unixutils