temporary-1.3/System/0000755000000000000000000000000012601201313013036 5ustar0000000000000000temporary-1.3/System/IO/0000755000000000000000000000000013263144051013357 5ustar0000000000000000temporary-1.3/tests/0000755000000000000000000000000013117272647012741 5ustar0000000000000000temporary-1.3/System/IO/Temp.hs0000644000000000000000000002055213263144046014630 0ustar0000000000000000{-# LANGUAGE CPP, ScopedTypeVariables #-} -- | Functions to create temporary files and directories. -- -- Most functions come in two flavours: those that create files/directories -- under the system standard temporary directory and those that use the -- user-supplied directory. -- -- The functions that create files/directories under the system standard -- temporary directory will return canonical absolute paths (see -- 'getCanonicalTemporaryDirectory'). The functions use the user-supplied -- directory do not canonicalize the returned path. -- -- The action inside 'withTempFile' or 'withTempDirectory' is allowed to -- remove the temporary file/directory if it needs to. -- -- == Templates and file names -- -- The treatment of templates differs somewhat for files vs directories. -- -- For files, the template has form @name.ext@, and a random number will be -- placed between between the name and the extension to yield a unique file -- name, e.g. @name1804289383846930886.ext@. -- -- For directories, no extension is recognized. -- A random hexadecimal string (whose length depends on the system's word -- size) is appended to the end of the template. -- For instance, -- the directory template @dir@ may result in a directory named -- @dir-e4bd89e5d00acdee@. -- -- You shouldn't rely on the specific form of file or directory names -- generated by the library; it has changed in the past and may change in the future. module System.IO.Temp ( withSystemTempFile, withSystemTempDirectory, withTempFile, withTempDirectory, openNewBinaryFile, createTempDirectory, writeTempFile, writeSystemTempFile, emptyTempFile, emptySystemTempFile, -- * Re-exports from System.IO openTempFile, openBinaryTempFile, -- * Auxiliary functions getCanonicalTemporaryDirectory ) where import qualified Control.Monad.Catch as MC import Control.Monad.IO.Class import Data.Bits -- no import list: we use different functions -- depending on the base version #if !MIN_VERSION_base(4,8,0) import Data.Word (Word) #endif import System.Directory import System.IO (Handle, hClose, openTempFile, openBinaryTempFile, openBinaryTempFileWithDefaultPermissions, hPutStr) import System.IO.Error (isAlreadyExistsError) import System.FilePath (()) import System.Random #ifdef mingw32_HOST_OS import System.Directory ( createDirectory ) #else import qualified System.Posix #endif import Text.Printf -- | Create, open, and use a temporary file in the system standard temporary directory. -- -- The temp file is deleted after use. -- -- Behaves exactly the same as 'withTempFile', except that the parent temporary directory -- will be that returned by 'getCanonicalTemporaryDirectory'. withSystemTempFile :: (MonadIO m, MC.MonadMask m) => String -- ^ File name template -> (FilePath -> Handle -> m a) -- ^ Callback that can use the file -> m a withSystemTempFile template action = liftIO getCanonicalTemporaryDirectory >>= \tmpDir -> withTempFile tmpDir template action -- | Create and use a 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 'getCanonicalTemporaryDirectory'. withSystemTempDirectory :: (MonadIO m, MC.MonadMask m) => String -- ^ Directory name template -> (FilePath -> m a) -- ^ Callback that can use the directory -> m a withSystemTempDirectory template action = liftIO getCanonicalTemporaryDirectory >>= \tmpDir -> withTempDirectory tmpDir template action -- | Create, open, and use a temporary file in the given directory. -- -- The temp file is deleted after use. withTempFile :: (MonadIO m, MC.MonadMask m) => FilePath -- ^ Parent directory to create the file in -> String -- ^ File name template -> (FilePath -> Handle -> m a) -- ^ Callback that can use the file -> m a withTempFile tmpDir template action = MC.bracket (liftIO (openTempFile tmpDir template)) (\(name, handle) -> liftIO (hClose handle >> ignoringIOErrors (removeFile name))) (uncurry action) -- | Create and use a temporary directory inside the given directory. -- -- The directory is deleted after use. withTempDirectory :: (MC.MonadMask m, MonadIO m) => FilePath -- ^ Parent directory to create the directory in -> String -- ^ Directory name template -> (FilePath -> m a) -- ^ Callback that can use the directory -> m a withTempDirectory targetDir template = MC.bracket (liftIO (createTempDirectory targetDir template)) (liftIO . ignoringIOErrors . removeDirectoryRecursive) -- | Create a unique new file, write (text mode) a given data string to it, -- and close the handle again. The file will not be deleted automatically, -- and only the current user will have permission to access the file. -- -- @since 1.2.1 writeTempFile :: FilePath -- ^ Parent directory to create the file in -> String -- ^ File name template -> String -- ^ Data to store in the file -> IO FilePath -- ^ Path to the (written and closed) file writeTempFile targetDir template content = MC.bracket (openTempFile targetDir template) (\(_, handle) -> hClose handle) (\(filePath, handle) -> hPutStr handle content >> return filePath) -- | Like 'writeTempFile', but use the system directory for temporary files. -- -- @since 1.2.1 writeSystemTempFile :: String -- ^ File name template -> String -- ^ Data to store in the file -> IO FilePath -- ^ Path to the (written and closed) file writeSystemTempFile template content = getCanonicalTemporaryDirectory >>= \tmpDir -> writeTempFile tmpDir template content -- | Create a unique new empty file. (Equivalent to 'writeTempFile' with empty data string.) -- This is useful if the actual content is provided by an external process. -- -- @since 1.2.1 emptyTempFile :: FilePath -- ^ Parent directory to create the file in -> String -- ^ File name template -> IO FilePath -- ^ Path to the (written and closed) file emptyTempFile targetDir template = MC.bracket (openTempFile targetDir template) (\(_, handle) -> hClose handle) (\(filePath, _) -> return filePath) -- | Like 'emptyTempFile', but use the system directory for temporary files. -- -- @since 1.2.1 emptySystemTempFile :: String -- ^ File name template -> IO FilePath -- ^ Path to the (written and closed) file emptySystemTempFile template = getCanonicalTemporaryDirectory >>= \tmpDir -> emptyTempFile tmpDir template ignoringIOErrors :: MC.MonadCatch m => m () -> m () ignoringIOErrors ioe = ioe `MC.catch` (\e -> const (return ()) (e :: IOError)) -- | Like 'openBinaryTempFile', but uses 666 rather than 600 for the -- permissions. -- -- Equivalent to 'openBinaryTempFileWithDefaultPermissions'. openNewBinaryFile :: FilePath -> String -> IO (FilePath, Handle) openNewBinaryFile = openBinaryTempFileWithDefaultPermissions -- | Create a temporary directory. createTempDirectory :: FilePath -- ^ Parent directory to create the directory in -> String -- ^ Directory name template -> IO FilePath createTempDirectory dir template = findTempName where findTempName = do x :: Word <- randomIO let dirpath = dir template ++ printf "-%.*x" (wordSize `div` 4) x r <- MC.try $ mkPrivateDir dirpath case r of Right _ -> return dirpath Left e | isAlreadyExistsError e -> findTempName | otherwise -> ioError e -- | Word size in bits wordSize :: Int wordSize = #if MIN_VERSION_base(4,7,0) finiteBitSize (undefined :: Word) #else bitSize (undefined :: Word) #endif mkPrivateDir :: String -> IO () #ifdef mingw32_HOST_OS mkPrivateDir s = createDirectory s #else mkPrivateDir s = System.Posix.createDirectory s 0o700 #endif -- | Return the absolute and canonical path to the system temporary -- directory. -- -- >>> setCurrentDirectory "/home/feuerbach/" -- >>> setEnv "TMPDIR" "." -- >>> getTemporaryDirectory -- "." -- >>> getCanonicalTemporaryDirectory -- "/home/feuerbach" getCanonicalTemporaryDirectory :: IO FilePath getCanonicalTemporaryDirectory = getTemporaryDirectory >>= canonicalizePath temporary-1.3/tests/test.hs0000644000000000000000000001040613117272355014251 0ustar0000000000000000{-# LANGUAGE CPP #-} import Test.Tasty import Test.Tasty.HUnit import Control.Monad import Control.Concurrent import Control.Concurrent.MVar import Control.Exception import System.Directory import System.IO import System.FilePath import System.Environment.Compat import Data.Bits import Data.List import GHC.IO.Handle #ifndef mingw32_HOST_OS import System.Posix.Files #endif import System.IO.Temp main = do -- force single-thread execution, because changing TMPDIR in one of the -- tests may leak to the other tests setEnv "TASTY_NUM_THREADS" "1" #ifndef mingw32_HOST_OS setFileCreationMask 0 #endif sys_tmp_dir <- getCanonicalTemporaryDirectory defaultMain $ testGroup "Tests" [ testCase "openNewBinaryFile" $ do (fp, fh) <- openNewBinaryFile sys_tmp_dir "test.txt" let fn = takeFileName fp assertBool ("Does not match template: " ++ fn) $ ("test" `isPrefixOf` fn) && (".txt" `isSuffixOf` fn) assertBool (fp ++ " is not in the right directory " ++ sys_tmp_dir) $ takeDirectory fp `equalFilePath` sys_tmp_dir hClose fh assertBool "File does not exist" =<< doesFileExist fp #ifndef mingw32_HOST_OS status <- getFileStatus fp fileMode status .&. 0o777 @?= 0o666 #endif removeFile fp , testCase "withSystemTempFile" $ do (fp, fh) <- withSystemTempFile "test.txt" $ \fp fh -> do let fn = takeFileName fp assertBool ("Does not match template: " ++ fn) $ ("test" `isPrefixOf` fn) && (".txt" `isSuffixOf` fn) assertBool (fp ++ " is not in the right directory " ++ sys_tmp_dir) $ takeDirectory fp `equalFilePath` sys_tmp_dir assertBool "File not open" =<< hIsOpen fh hPutStrLn fh "hi" assertBool "File does not exist" =<< doesFileExist fp #ifndef mingw32_HOST_OS status <- getFileStatus fp fileMode status .&. 0o777 @?= 0o600 #endif return (fp, fh) assertBool "File still exists" . not =<< doesFileExist fp assertBool "File not closed" =<< hIsClosed fh , testCase "withSystemTempDirectory" $ do fp <- withSystemTempDirectory "test.dir" $ \fp -> do let fn = takeFileName fp assertBool ("Does not match template: " ++ fn) $ ("test.dir" `isPrefixOf` fn) assertBool (fp ++ " is not in the right directory " ++ sys_tmp_dir) $ takeDirectory fp `equalFilePath` sys_tmp_dir assertBool "Directory does not exist" =<< doesDirectoryExist fp #ifndef mingw32_HOST_OS status <- getFileStatus fp fileMode status .&. 0o777 @?= 0o700 #endif return fp assertBool "Directory still exists" . not =<< doesDirectoryExist fp , testCase "writeSystemTempFile" $ do fp <- writeSystemTempFile "blah.txt" "hello" str <- readFile fp "hello" @?= str removeFile fp , testCase "emptySystemTempFile" $ do fp <- emptySystemTempFile "empty.txt" assertBool "File doesn't exist" =<< doesFileExist fp removeFile fp , testCase "withSystemTempFile returns absolute path" $ do bracket_ (setEnv "TMPDIR" ".") (unsetEnv "TMPDIR") $ do withSystemTempFile "temp.txt" $ \fp _ -> assertBool "Not absolute" $ isAbsolute fp , testCase "withSystemTempDirectory is not interrupted" $ do -- this mvar is both a channel to pass the name of the directory -- and a signal that we finished creating files and are ready -- to be killed mvar1 <- newEmptyMVar -- this mvar signals that the withSystemTempDirectory function -- returned and we can check whether the directory has survived mvar2 <- newEmptyMVar threadId <- forkIO $ (withSystemTempDirectory "temp.test." $ \dir -> do replicateM_ 100 $ emptyTempFile dir "file.xyz" putMVar mvar1 dir threadDelay $ 10^6 ) `finally` (putMVar mvar2 ()) dir <- readMVar mvar1 -- start sending exceptions replicateM_ 10 $ forkIO $ killThread threadId -- wait for the thread to finish readMVar mvar2 -- check whether the directory was successfully removed assertBool "Directory was not removed" . not =<< doesDirectoryExist dir ] temporary-1.3/LICENSE0000644000000000000000000000312313117274031012570 0ustar0000000000000000Copyright (c) 2003-2006, Isaac Jones (c) 2005-2009, Duncan Coutts (c) 2008, Maximilian Bolingbroke ... and other contributors All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of Maximilian Bolingbroke nor the names of other contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. temporary-1.3/Setup.lhs0000644000000000000000000000011312601201313013355 0ustar0000000000000000#!/usr/bin/env runhaskell > import Distribution.Simple > main = defaultMaintemporary-1.3/temporary.cabal0000644000000000000000000000271513263143432014601 0ustar0000000000000000name: temporary version: 1.3 cabal-version: >= 1.10 synopsis: Portable temporary file and directory support description: Functions for creating temporary files and directories. category: System, Utils license: BSD3 license-file: LICENSE maintainer: Mateusz Kowalczyk , Roman Cheplyaka homepage: https://github.com/feuerbach/temporary build-type: Simple extra-source-files: CHANGELOG.md source-repository head type: git location: git://github.com/feuerbach/temporary.git Library default-language: Haskell2010 exposed-modules: System.IO.Temp build-depends: base >= 3 && < 10, filepath >= 1.1, directory >= 1.0, transformers >= 0.2.0.0, exceptions >= 0.6, random >= 1.1 -- note: the transformers dependency is needed for MonadIO -- on older GHCs; on newer ones, it is included in base. ghc-options: -Wall if !os(windows) build-depends: unix >= 2.3 test-suite test default-language: Haskell2010 type: exitcode-stdio-1.0 hs-source-dirs: tests main-is: test.hs ghc-options: -threaded -with-rtsopts=-N2 build-depends: base >= 4.3 && < 5 , directory , tasty , tasty-hunit , temporary , filepath , base-compat if !os(windows) build-depends: unix >= 2.3 temporary-1.3/CHANGELOG.md0000644000000000000000000000120313263143415013374 0ustar0000000000000000## 1.3 * Generated directory names are now based on random hex strings rather than PIDs. This got a major version bump as a courtesy to users who may depend on the specific form of generated names, but that form is not part of the API contract and should not be depended upon. ## 1.2.1.1 * Improve the docs ## 1.2.1 * Limit support to GHC 7.0+ * Add new functions: `writeTempFile,` `writeSystemTempFile,` `emptyTempFile,` `emptySystemTempFile` * Make sure that system* functions return canonicalized paths * Modernize the code base, add tests and documentation ## 1.2.0.4 * Update maintainership information * Fix the docs