Commit ddf84bbd authored by Edward Z. Yang's avatar Edward Z. Yang
Browse files

Ensure handle to temporary is closed before temporary file expires.



Fixes bug on Windows.
Signed-off-by: default avatarEdward Z. Yang <ezyang@cs.stanford.edu>
parent fa91dd5e
{-# LANGUAGE NondecreasingIndentation #-}
-- Implements the \"@.\/cabal sdist@\" command, which creates a source -- Implements the \"@.\/cabal sdist@\" command, which creates a source
-- distribution for this package. That is, packs up the source code -- distribution for this package. That is, packs up the source code
-- into a tarball, making use of the corresponding Cabal module. -- into a tarball, making use of the corresponding Cabal module.
...@@ -35,15 +36,15 @@ import Distribution.Verbosity (Verbosity, normal, lessVerbose) ...@@ -35,15 +36,15 @@ import Distribution.Verbosity (Verbosity, normal, lessVerbose)
import Distribution.Version (Version(..), orLaterVersion) import Distribution.Version (Version(..), orLaterVersion)
import Distribution.Client.Utils import Distribution.Client.Utils
(removeExistingFile, tryFindAddSourcePackageDesc) (tryFindAddSourcePackageDesc)
import Distribution.Compat.Exception (catchIO) import Distribution.Compat.Exception (catchIO)
import System.FilePath ((</>), (<.>)) import System.FilePath ((</>), (<.>))
import Control.Monad (when, unless, liftM) import Control.Monad (when, unless, liftM)
import System.Directory (doesFileExist, removeFile, canonicalizePath) import System.Directory (doesFileExist, removeFile, canonicalizePath, getTemporaryDirectory)
import System.Process (runProcess, waitForProcess) import System.Process (runProcess, waitForProcess)
import System.Exit (ExitCode(..)) import System.Exit (ExitCode(..))
import Control.Exception (IOException) import Control.Exception (IOException, evaluate)
-- |Create a source distribution. -- |Create a source distribution.
sdist :: SDistFlags -> SDistExFlags -> IO () sdist :: SDistFlags -> SDistExFlags -> IO ()
...@@ -152,8 +153,9 @@ allPackageSourceFiles verbosity packageDir = do ...@@ -152,8 +153,9 @@ allPackageSourceFiles verbosity packageDir = do
let err = "Error reading source files of package." let err = "Error reading source files of package."
desc <- tryFindAddSourcePackageDesc packageDir err desc <- tryFindAddSourcePackageDesc packageDir err
flattenPackageDescription `fmap` readPackageDescription verbosity desc flattenPackageDescription `fmap` readPackageDescription verbosity desc
let -- TODO: allocate a temporary directory for this, more thread safe. globalTmp <- getTemporaryDirectory
file = packageDir </> "cabal-sdist-list-sources" withTempDirectory verbosity globalTmp "cabal-list-sources." $ \tempDir -> do
let file = tempDir </> "cabal-sdist-list-sources"
flags = defaultSDistFlags { flags = defaultSDistFlags {
sDistVerbosity = Flag $ if verbosity == normal sDistVerbosity = Flag $ if verbosity == normal
then lessVerbose verbosity else verbosity, then lessVerbose verbosity else verbosity,
...@@ -179,6 +181,8 @@ allPackageSourceFiles verbosity packageDir = do ...@@ -179,6 +181,8 @@ allPackageSourceFiles verbosity packageDir = do
"Exception was: " ++ show e "Exception was: " ++ show e
-- Run setup sdist --list-sources=TMPFILE -- Run setup sdist --list-sources=TMPFILE
ret <- doListSources `catchIO` (\e -> onFailedListSources e >> return []) r <- doListSources `catchIO` (\e -> onFailedListSources e >> return [])
removeExistingFile file -- Ensure that we've closed the 'readFile' handle before we exit the
return ret -- temporary directory.
_ <- evaluate (length r)
return r
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