From: garyo Date: Sun, 15 Nov 2009 14:32:02 +0000 (+0000) Subject: Apply patch submitted in issue #947 to fix race condition in X-Git-Url: http://git.tremily.us/gitweb.cgi?a=commitdiff_plain;h=00a915c7a102cd3d7513a2b3802217f692fcee06;p=scons.git Apply patch submitted in issue #947 to fix race condition in TempFileMunge by using mkstemp instead of mktemp. Includes pre-Python-2.3 compat version of mkstemp. Thanks to Jim Randall. git-svn-id: http://scons.tigris.org/svn/scons/trunk@4392 fdb21ef1-2011-0410-befe-b5e4ea1792b1 --- diff --git a/src/CHANGES.txt b/src/CHANGES.txt index 5db7a66d..5c8c8b81 100644 --- a/src/CHANGES.txt +++ b/src/CHANGES.txt @@ -10,6 +10,9 @@ RELEASE X.X.X - XXX + From Jim Randall: + - Fixed temp filename race condition on Windows with long cmd lines. + From David Cournapeau: - Fixed tryRun when sconf directory is in a variant dir. - Do not add -fPIC for ifort tool on non-posix platforms (darwin and diff --git a/src/engine/SCons/Platform/__init__.py b/src/engine/SCons/Platform/__init__.py index 964ed67f..5bcd91ce 100644 --- a/src/engine/SCons/Platform/__init__.py +++ b/src/engine/SCons/Platform/__init__.py @@ -44,6 +44,8 @@ their own platform definition. __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__" +import SCons.compat + import imp import os import string @@ -176,8 +178,8 @@ class TempFileMunge: # We use the .lnk suffix for the benefit of the Phar Lap # linkloc linker, which likes to append an .lnk suffix if # none is given. - tmp = os.path.normpath(tempfile.mktemp('.lnk')) - native_tmp = SCons.Util.get_native_path(tmp) + (fd, tmp) = tempfile.mkstemp('.lnk', text=True) + native_tmp = SCons.Util.get_native_path(os.path.normpath(tmp)) if env['SHELL'] and env['SHELL'] == 'sh': # The sh shell will try to escape the backslashes in the @@ -197,7 +199,8 @@ class TempFileMunge: prefix = '@' args = map(SCons.Subst.quote_spaces, cmd[1:]) - open(tmp, 'w').write(string.join(args, " ") + "\n") + os.write(fd, string.join(args, " ") + "\n") + os.close(fd) # XXX Using the SCons.Action.print_actions value directly # like this is bogus, but expedient. This class should # really be rewritten as an Action that defines the diff --git a/src/engine/SCons/compat/__init__.py b/src/engine/SCons/compat/__init__.py index 2a863242..553fcf3f 100644 --- a/src/engine/SCons/compat/__init__.py +++ b/src/engine/SCons/compat/__init__.py @@ -250,6 +250,41 @@ except ImportError: # Pre-1.6 Python has no UserString module. import_as('_scons_UserString', 'UserString') +import tempfile +try: + tempfile.mkstemp +except AttributeError: + # Pre-2.3 Python has no tempfile.mkstemp function, so try to simulate it. + # adapted from the mkstemp implementation in python 3. + import os + import errno + def mkstemp( *args, **kw ) : + text = False + if 'text' in kw : + text = kw['text'] + del kw['text'] + elif len( args ) == 4 : + text = args[3] + args = args[:3] + flags = os.O_RDWR | os.O_CREAT | os.O_EXCL + if not text and hasattr( os, 'O_BINARY' ) : + flags |= os.O_BINARY + while True: + try : + name = tempfile.mktemp( *args, **kw ) + fd = os.open( name, flags, 0600 ) + return (fd, os.path.abspath(name)) + except OSError, e: + if e.errno == errno.EEXIST: + continue + raise + + tempfile.mkstemp = mkstemp + del mkstemp + + + + # Local Variables: # tab-width:4 # indent-tabs-mode:nil