[commit: ghc] master: Bring sanity to openTempFile (ad617a3)

git at git.haskell.org git at git.haskell.org
Sun Feb 26 19:56:39 UTC 2017


Repository : ssh://git@git.haskell.org/ghc

On branch  : master
Link       : http://ghc.haskell.org/trac/ghc/changeset/ad617a3edf832b5368146e0bbf0cf2780d9355e1/ghc

>---------------------------------------------------------------

commit ad617a3edf832b5368146e0bbf0cf2780d9355e1
Author: Ben Gamari <bgamari.foss at gmail.com>
Date:   Sun Feb 26 13:44:44 2017 -0500

    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


>---------------------------------------------------------------

ad617a3edf832b5368146e0bbf0cf2780d9355e1
 libraries/base/System/IO.hs | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/libraries/base/System/IO.hs b/libraries/base/System/IO.hs
index 04e976a..656f852 100644
--- a/libraries/base/System/IO.hs
+++ b/libraries/base/System/IO.hs
@@ -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



More information about the ghc-commits mailing list