Commit ad617a3e authored by Ben Gamari's avatar Ben Gamari Committed by Ben Gamari

Bring sanity to openTempFile

Test Plan: Run test of `openTempFile` under `strace` to verify
reasonably random filenames

Reviewers: austin, hvr, trofi, rwbarton

Reviewed By: trofi, rwbarton

Subscribers: rwbarton, thomie

Differential Revision: https://phabricator.haskell.org/D3188
parent 517ad201
......@@ -233,6 +233,8 @@ import System.Posix.Types
import GHC.Base
import GHC.List
import GHC.IORef
import GHC.Num
import GHC.IO hiding ( bracket, onException )
import GHC.IO.IOMode
import GHC.IO.Handle.FD
......@@ -508,15 +510,16 @@ openTempFile' loc tmp_dir template binary mode = findTempName
| last a == pathSeparator = a ++ b
| otherwise = a ++ [pathSeparator] ++ b
-- int rand(void) from <stdlib.h>, limited by RAND_MAX (small value, 32768)
foreign import capi "stdlib.h rand" c_rand :: IO CInt
tempCounter :: IORef Int
tempCounter = unsafePerformIO $ newIORef 0
{-# NOINLINE tempCounter #-}
-- build large digit-alike number
rand_string :: IO String
rand_string = do
r1 <- c_rand
r2 <- c_rand
return $ show r1 ++ show r2
r1 <- c_getpid
r2 <- atomicModifyIORef tempCounter (\n -> (n+1, n))
return $ show r1 ++ "-" ++ show r2
data OpenNewFileResult
= NewFileCreated CInt
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment