Skip to content

Commit ba466e4

Browse files
committed
ww
1 parent 11557dd commit ba466e4

File tree

11 files changed

+31
-58
lines changed

11 files changed

+31
-58
lines changed

Cabal-syntax/src/Distribution/Utils/Generic.hs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,14 @@ import qualified Data.Set as Set
100100

101101
import qualified Control.Exception as Exception
102102
import System.Directory
103-
( getTemporaryDirectory
103+
( copyFile
104+
, getTemporaryDirectory
104105
, removeFile
105106
, renameFile
106107
)
107108
import System.FilePath
108-
( splitFileName
109+
( takeFileName
110+
, takeDrive
109111
, (<.>)
110112
)
111113
import System.IO
@@ -172,15 +174,17 @@ withFileContents name action =
172174
-- This case will give an IO exception but the atomic property is not affected.
173175
writeFileAtomic :: FilePath -> LBS.ByteString -> IO ()
174176
writeFileAtomic targetPath content = do
175-
let (_, targetFile) = splitFileName targetPath
177+
let targetFile = takeFileName targetPath
176178
tmpDir <- getTemporaryDirectory
177179
Exception.bracketOnError
178180
(openBinaryTempFileWithDefaultPermissions tmpDir $ targetFile <.> "tmp")
179181
(\(tmpPath, handle) -> hClose handle >> removeFile tmpPath)
180182
( \(tmpPath, handle) -> do
181183
LBS.hPut handle content
182184
hClose handle
183-
renameFile tmpPath targetPath
185+
if takeDrive targetPath == takeDrive tmpDir
186+
then renameFile tmpPath targetPath
187+
else copyFile tmpPath targetPath >> removeFile tmpPath
184188
)
185189

186190
-- ------------------------------------------------------------

Cabal-tests/tests/UnitTests/Distribution/Simple/Utils.hs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,14 @@ import Test.Tasty.HUnit
2323
withTempFileTest :: Assertion
2424
withTempFileTest = do
2525
fileName <- newIORef ""
26-
tempDir <- getTemporaryDirectory
27-
withTempFile tempDir ".foo" $ \fileName' _handle -> do
26+
withTempFile ".foo" $ \fileName' _handle -> do
2827
writeIORef fileName fileName'
2928
fileExists <- readIORef fileName >>= doesFileExist
3029
assertBool "Temporary file not deleted by 'withTempFile'!" (not fileExists)
3130

3231
withTempFileRemovedTest :: Assertion
3332
withTempFileRemovedTest = do
34-
tempDir <- getTemporaryDirectory
35-
withTempFile tempDir ".foo" $ \fileName handle -> do
33+
withTempFile ".foo" $ \fileName handle -> do
3634
hClose handle
3735
removeFile fileName
3836

@@ -58,9 +56,8 @@ rawSystemStdInOutTextDecodingTest ghcPath
5856
-- so skip the test if it's not.
5957
| show localeEncoding /= "UTF-8" = return ()
6058
| otherwise = do
61-
tempDir <- getTemporaryDirectory
62-
res <- withTempFile tempDir ".hs" $ \filenameHs handleHs -> do
63-
withTempFile tempDir ".exe" $ \filenameExe handleExe -> do
59+
res <- withTempFile ".hs" $ \filenameHs handleHs -> do
60+
withTempFile ".exe" $ \filenameExe handleExe -> do
6461
-- Small program printing not utf8
6562
hPutStrLn handleHs "import Data.ByteString"
6663
hPutStrLn handleHs "main = Data.ByteString.putStr (Data.ByteString.pack [32, 32, 255])"

Cabal/src/Distribution/Simple/Configure.hs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,6 @@ import System.Directory
154154
( canonicalizePath
155155
, createDirectoryIfMissing
156156
, doesFileExist
157-
, getTemporaryDirectory
158157
, removeFile
159158
)
160159
import System.FilePath
@@ -2674,10 +2673,8 @@ checkForeignDeps pkg lbi verbosity =
26742673

26752674
builds :: String -> [ProgArg] -> IO Bool
26762675
builds program args =
2677-
do
2678-
tempDir <- makeSymbolicPath <$> getTemporaryDirectory
2679-
withTempFileCwd mbWorkDir tempDir ".c" $ \cName cHnd ->
2680-
withTempFileCwd mbWorkDir tempDir "" $ \oNname oHnd -> do
2676+
withTempFileCwd ".c" $ \cName cHnd ->
2677+
withTempFileCwd "" $ \oNname oHnd -> do
26812678
hPutStrLn cHnd program
26822679
hClose cHnd
26832680
hClose oHnd

Cabal/src/Distribution/Simple/GHC/Internal.hs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ import Distribution.Utils.Path
8585
import Distribution.Verbosity
8686
import Distribution.Version (Version)
8787
import Language.Haskell.Extension
88-
import System.Directory (getDirectoryContents, getTemporaryDirectory)
88+
import System.Directory (getDirectoryContents)
8989
import System.Environment (getEnv)
9090
import System.FilePath
9191
( takeDirectory
@@ -221,9 +221,8 @@ configureToolchain _implInfo ghcProg ghcInfo =
221221
-- we need to find out if ld supports the -x flag
222222
configureLd' :: Verbosity -> ConfiguredProgram -> IO ConfiguredProgram
223223
configureLd' verbosity ldProg = do
224-
tempDir <- getTemporaryDirectory
225-
ldx <- withTempFile tempDir ".c" $ \testcfile testchnd ->
226-
withTempFile tempDir ".o" $ \testofile testohnd -> do
224+
ldx <- withTempFile ".c" $ \testcfile testchnd ->
225+
withTempFile ".o" $ \testofile testohnd -> do
227226
hPutStrLn testchnd "int foo() { return 0; }"
228227
hClose testchnd
229228
hClose testohnd
@@ -236,7 +235,7 @@ configureToolchain _implInfo ghcProg ghcInfo =
236235
, "-o"
237236
, testofile
238237
]
239-
withTempFile tempDir ".o" $ \testofile' testohnd' ->
238+
withTempFile ".o" $ \testofile' testohnd' ->
240239
do
241240
hClose testohnd'
242241
_ <-

Cabal/src/Distribution/Simple/Haddock.hs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,8 +1133,6 @@ renderArgs verbosity mbWorkDir tmpFileOpts version comp platform args k = do
11331133
withResponseFile
11341134
verbosity
11351135
tmpFileOpts
1136-
mbWorkDir
1137-
outputDir
11381136
"haddock-response.txt"
11391137
(if haddockSupportsUTF8 then Just utf8 else Nothing)
11401138
renderedArgs
@@ -1144,7 +1142,7 @@ renderArgs verbosity mbWorkDir tmpFileOpts version comp platform args k = do
11441142
(Flag pfile, _) ->
11451143
withPrologueArgs ["--prologue=" ++ pfile]
11461144
(_, Flag prologueText) ->
1147-
withTempFileEx tmpFileOpts mbWorkDir outputDir "haddock-prologue.txt" $
1145+
withTempFileEx tmpFileOpts "haddock-prologue.txt" $
11481146
\prologueFileName h -> do
11491147
when haddockSupportsUTF8 (hSetEncoding h utf8)
11501148
hPutStrLn h prologueText

Cabal/src/Distribution/Simple/PreProcess.hs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -511,8 +511,6 @@ ppHsc2hs bi lbi clbi =
511511
withResponseFile
512512
verbosity
513513
defaultTempFileOptions
514-
mbWorkDir
515-
(makeSymbolicPath $ takeDirectory outFile)
516514
"hsc2hs-response.txt"
517515
Nothing
518516
pureArgs

Cabal/src/Distribution/Simple/Program/Ar.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ createArLibArchive verbosity lbi targetPath files = do
154154
(initial, middle, final)
155155
(map getSymbolicPath files)
156156
]
157-
else withResponseFile verbosity defaultTempFileOptions mbWorkDir tmpDir "ar.rsp" Nothing (map getSymbolicPath files) $
157+
else withResponseFile verbosity defaultTempFileOptions "ar.rsp" Nothing (map getSymbolicPath files) $
158158
\path -> runProgramInvocation verbosity $ invokeWithResponseFile path
159159

160160
unless

Cabal/src/Distribution/Simple/Program/Ld.hs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,6 @@ combineObjectFiles verbosity lbi ldProg target files = do
8383
middle = ld middleArgs
8484
final = ld finalArgs
8585

86-
targetDir = takeDirectorySymbolicPath target
87-
8886
invokeWithResponseFile :: FilePath -> ProgramInvocation
8987
invokeWithResponseFile atFile =
9088
ld $ simpleArgs ++ ['@' : atFile]
@@ -106,7 +104,7 @@ combineObjectFiles verbosity lbi ldProg target files = do
106104

107105
if oldVersionManualOverride || responseArgumentsNotSupported
108106
then run $ multiStageProgramInvocation simple (initial, middle, final) (map getSymbolicPath files)
109-
else withResponseFile verbosity defaultTempFileOptions mbWorkDir targetDir "ld.rsp" Nothing (map getSymbolicPath files) $
107+
else withResponseFile verbosity defaultTempFileOptions "ld.rsp" Nothing (map getSymbolicPath files) $
110108
\path -> runProgramInvocation verbosity $ invokeWithResponseFile path
111109
where
112110
tmpfile = target <.> "tmp" -- perhaps should use a proper temp file

Cabal/src/Distribution/Simple/Program/ResponseFile.hs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@ import Distribution.Verbosity
2727
withResponseFile
2828
:: Verbosity
2929
-> TempFileOptions
30-
-> Maybe (SymbolicPath CWD (Dir Pkg))
31-
-- ^ Working directory
32-
-> SymbolicPath Pkg (Dir Response)
33-
-- ^ Directory to create response file in.
3430
-> String
3531
-- ^ Template for response file name.
3632
-> Maybe TextEncoding
@@ -39,8 +35,8 @@ withResponseFile
3935
-- ^ Arguments to put into response file.
4036
-> (FilePath -> IO a)
4137
-> IO a
42-
withResponseFile verbosity tmpFileOpts mbWorkDir responseDir fileNameTemplate encoding arguments f =
43-
withTempFileEx tmpFileOpts mbWorkDir responseDir fileNameTemplate $ \responsePath hf -> do
38+
withResponseFile verbosity tmpFileOpts fileNameTemplate encoding arguments f =
39+
withTempFileEx tmpFileOpts fileNameTemplate $ \responsePath hf -> do
4440
let responseFileName = getSymbolicPath responsePath
4541
traverse_ (hSetEncoding hf) encoding
4642
let responseContents =

Cabal/src/Distribution/Simple/Utils.hs

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1734,23 +1734,17 @@ defaultTempFileOptions = TempFileOptions{optKeepTempFiles = False}
17341734

17351735
-- | Use a temporary filename that doesn't already exist
17361736
withTempFile
1737-
:: FilePath
1738-
-- ^ Temp dir to create the file in
1739-
-> String
1737+
:: String
17401738
-- ^ File name template. See 'openTempFile'.
17411739
-> (FilePath -> Handle -> IO a)
17421740
-> IO a
1743-
withTempFile tmpDir template f = withFrozenCallStack $
1744-
withTempFileCwd Nothing (makeSymbolicPath tmpDir) template $
1741+
withTempFile template f = withFrozenCallStack $
1742+
withTempFileCwd template $
17451743
\fp h -> f (getSymbolicPath fp) h
17461744

17471745
-- | Use a temporary filename that doesn't already exist.
17481746
withTempFileCwd
1749-
:: Maybe (SymbolicPath CWD (Dir Pkg))
1750-
-- ^ Working directory
1751-
-> SymbolicPath Pkg (Dir tmpDir)
1752-
-- ^ Temp dir to create the file in
1753-
-> String
1747+
:: String
17541748
-- ^ File name template. See 'openTempFile'.
17551749
-> (SymbolicPath Pkg File -> Handle -> IO a)
17561750
-> IO a
@@ -1759,17 +1753,13 @@ withTempFileCwd = withFrozenCallStack $ withTempFileEx defaultTempFileOptions
17591753
-- | A version of 'withTempFile' that additionally takes a 'TempFileOptions'
17601754
-- argument.
17611755
withTempFileEx
1762-
:: forall a tmpDir
1756+
:: forall a
17631757
. TempFileOptions
1764-
-> Maybe (SymbolicPath CWD (Dir Pkg))
1765-
-- ^ Working directory
1766-
-> SymbolicPath Pkg (Dir tmpDir)
1767-
-- ^ Temp dir to create the file in
17681758
-> String
17691759
-- ^ File name template. See 'openTempFile'.
17701760
-> (SymbolicPath Pkg File -> Handle -> IO a)
17711761
-> IO a
1772-
withTempFileEx opts _mbWorkDir _tmpDir template action = do
1762+
withTempFileEx opts template action = do
17731763
tmp <- getTemporaryDirectory
17741764
withFrozenCallStack $
17751765
Exception.bracket
@@ -1783,7 +1773,6 @@ withTempFileEx opts _mbWorkDir _tmpDir template action = do
17831773
)
17841774
(withLexicalCallStack (\(fn, h) -> action (mkRelToPkg tmp fn) h))
17851775
where
1786-
-- i = interpretSymbolicPath mbWorkDir -- See Note [Symbolic paths] in Distribution.Utils.Path
17871776
mkRelToPkg :: FilePath -> FilePath -> SymbolicPath Pkg File
17881777
mkRelToPkg tmp fp =
17891778
makeSymbolicPath tmp </> makeRelativePathEx (takeFileName fp)

0 commit comments

Comments
 (0)