From ff6d2e6539e0a3672f0ec0cc807173045cac1941 Mon Sep 17 00:00:00 2001 From: Gary Oberbrunner Date: Sun, 15 Nov 2009 14:32:02 +0000 Subject: 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. --- src/CHANGES.txt | 3 +++ src/engine/SCons/Platform/__init__.py | 9 ++++++--- src/engine/SCons/compat/__init__.py | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/src/CHANGES.txt b/src/CHANGES.txt index 5db7a66..5c8c8b8 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 964ed67..5bcd91c 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 2a86324..553fcf3 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 -- cgit v0.12