Skip to content

Commit b2fa5ec

Browse files
authored
Merge pull request #10988 from haskell/avoid-init-and-last
Avoid partial functions Data.List.init and Data.List.last
2 parents fecbefb + 96a72d2 commit b2fa5ec

File tree

1 file changed

+3
-98
lines changed

1 file changed

+3
-98
lines changed

Cabal/src/Distribution/Compat/Internal/TempFile.hs

Lines changed: 3 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -12,113 +12,18 @@ import Distribution.Compat.Exception
1212

1313
import System.FilePath ((</>))
1414

15-
import System.IO (Handle, openBinaryTempFile, openTempFile)
16-
#if defined(__IO_MANAGER_WINIO__)
17-
import System.IO (openBinaryTempFileWithDefaultPermissions)
18-
import System.Posix.Internals (c_getpid)
19-
#else
20-
import Control.Exception (onException)
21-
import Data.Bits ((.|.))
22-
import Foreign.C (CInt, eEXIST, getErrno, errnoToIOError)
23-
import GHC.IO.Handle.FD (fdToHandle)
24-
import System.Posix.Internals (c_getpid, c_open, c_close, o_EXCL, o_BINARY, withFilePath,
25-
o_CREAT, o_RDWR, o_NONBLOCK, o_NOCTTY)
26-
#endif
27-
15+
import System.IO (Handle, openBinaryTempFile, openBinaryTempFileWithDefaultPermissions, openTempFile)
2816
import System.IO.Error (isAlreadyExistsError)
17+
import System.Posix.Internals (c_getpid)
2918

3019
#if defined(mingw32_HOST_OS) || defined(ghcjs_HOST_OS)
3120
import System.Directory ( createDirectory )
3221
#else
3322
import qualified System.Posix
3423
#endif
3524

36-
-- ------------------------------------------------------------
37-
38-
-- * temporary files
39-
40-
-- ------------------------------------------------------------
41-
42-
-- This is here for Haskell implementations that do not come with
43-
-- System.IO.openTempFile. This includes nhc-1.20, hugs-2006.9.
44-
-- TODO: This file should probably be removed.
45-
46-
-- This is a copy/paste of the openBinaryTempFile definition, but
47-
-- it uses 666 rather than 600 for the permissions. Newer versions
48-
-- of base have a new function with this behavior which we use on
49-
-- Windows when the new IO manager is used.
5025
openNewBinaryFile :: FilePath -> String -> IO (FilePath, Handle)
51-
openNewBinaryFile dir template = do
52-
53-
-- This method can't be used under WINIO. Also the current implementation has
54-
-- thread safety issues depending on which GHC is used. On newer GHC's let's
55-
-- use the built in one.
56-
#if defined(__IO_MANAGER_WINIO__)
57-
openBinaryTempFileWithDefaultPermissions dir template
58-
#else
59-
pid <- c_getpid
60-
findTempName pid
61-
where
62-
-- We split off the last extension, so we can use .foo.ext files
63-
-- for temporary files (hidden on Unix OSes). Unfortunately we're
64-
-- below file path in the hierarchy here.
65-
(prefix,suffix) =
66-
case break (== '.') $ reverse template of
67-
-- First case: template contains no '.'s. Just re-reverse it.
68-
(rev_suffix, "") -> (reverse rev_suffix, "")
69-
-- Second case: template contains at least one '.'. Strip the
70-
-- dot from the prefix and prepend it to the suffix (if we don't
71-
-- do this, the unique number will get added after the '.' and
72-
-- thus be part of the extension, which is wrong.)
73-
(rev_suffix, '.':rest) -> (reverse rest, '.':reverse rev_suffix)
74-
-- Otherwise, something is wrong, because (break (== '.')) should
75-
-- always return a pair with either the empty string or a string
76-
-- beginning with '.' as the second component.
77-
_ -> error "bug in System.IO.openTempFile"
78-
79-
oflags = rw_flags .|. o_EXCL .|. o_BINARY
80-
81-
findTempName x = do
82-
fd <- withFilePath filepath $ \ f ->
83-
c_open f oflags 0o666
84-
if fd < 0
85-
then do
86-
errno <- getErrno
87-
if errno == eEXIST
88-
then findTempName (x+1)
89-
else ioError (errnoToIOError "openNewBinaryFile" errno Nothing (Just dir))
90-
else do
91-
-- TODO: We want to tell fdToHandle what the file path is,
92-
-- as any exceptions etc will only be able to report the
93-
-- FD currently
94-
h <- fdToHandle fd `onException` c_close fd
95-
return (filepath, h)
96-
where
97-
filename = prefix ++ show x ++ suffix
98-
filepath = dir `combine` filename
99-
100-
-- FIXME: bits copied from System.FilePath
101-
combine a b
102-
| null b = a
103-
| null a = b
104-
| last a == pathSeparator = a ++ b
105-
| otherwise = a ++ [pathSeparator] ++ b
106-
107-
-- FIXME: Copied from GHC.Handle
108-
std_flags, output_flags, rw_flags :: CInt
109-
std_flags = o_NONBLOCK .|. o_NOCTTY
110-
output_flags = std_flags .|. o_CREAT
111-
rw_flags = output_flags .|. o_RDWR
112-
113-
-- FIXME: Should use System.FilePath library
114-
pathSeparator :: Char
115-
#ifdef mingw32_HOST_OS
116-
pathSeparator = '\\'
117-
#else
118-
pathSeparator = '/'
119-
#endif
120-
-- /* __IO_MANAGER_WINIO__ */
121-
#endif
26+
openNewBinaryFile = openBinaryTempFileWithDefaultPermissions
12227

12328
createTempDirectory :: FilePath -> String -> IO FilePath
12429
createTempDirectory dir template = do

0 commit comments

Comments
 (0)