Skip to content

Commit

Permalink
Bring sanity to openTempFile
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bgamari committed Feb 26, 2017
1 parent 517ad20 commit ad617a3
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions libraries/base/System/IO.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ad617a3

Please sign in to comment.