Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions System/IO/Temp.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module System.IO.Temp (
withSystemTempFile, withSystemTempDirectory,
withSystemTempFile, withSystemTempDirectory,withCanonicalisedSystemTempDirectory,
withTempFile, withTempDirectory,
module Distribution.Compat.TempFile
) where
Expand All @@ -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


Expand All @@ -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.
--
Expand Down Expand Up @@ -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))
2 changes: 1 addition & 1 deletion temporary.cabal
Original file line number Diff line number Diff line change
@@ -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
Expand Down