Avoid race on mkdir
authorDavid Neto <dneto@google.com>
Fri, 12 Aug 2016 18:19:17 +0000 (14:19 -0400)
committerDavid Neto <dneto@google.com>
Mon, 15 Aug 2016 15:14:34 +0000 (11:14 -0400)
Should fix https://github.com/KhronosGroup/SPIRV-Tools/issues/340

CHANGES
utils/generate_grammar_tables.py
utils/update_build_version.py

diff --git a/CHANGES b/CHANGES
index 9184a89..9e9e28f 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -9,6 +9,7 @@ v2016.3-dev 2016-08-11
  - Add optimization pass: Eliminate dead constants.
  - Fixes issues:
    #288: Check def-use dominance rules for OpPhi (variable,parent) operands
+   #340: Avoid race on mkdir during build
 
 v2016.2 2016-08-05
  - Validator is incomplete
index f91f3ad..85c2f47 100755 (executable)
@@ -3,6 +3,7 @@
 
 from __future__ import print_function
 
+import errno
 import functools
 import json
 import os.path
@@ -24,8 +25,11 @@ def make_path_to_file(f):
     dir = os.path.dirname(os.path.abspath(f))
     try:
         os.makedirs(dir)
-    except:
-        pass
+    except OSError as e:
+        if e.errno == errno.EEXIST and os.path.isdir(dir):
+            pass
+        else:
+            raise
 
 
 def populate_capability_bit_mapping_dict(cap_dict):
index 9a3d5ac..fd0608e 100755 (executable)
 from __future__ import print_function
 
 import datetime
+import errno
 import os.path
 import re
 import subprocess
 import sys
 
 
+def mkdir_p(directory):
+    """Make the directory, and all its ancestors as required.  Any of the
+    directories are allowed to already exist."""
+
+    try:
+        os.makedirs(directory)
+    except OSError as e:
+        if e.errno == errno.EEXIST and os.path.isdir(directory):
+            pass
+        else:
+            raise
+
+
 def command_output(cmd, directory):
     """Runs a command in a directory and returns its standard output stream.
 
@@ -99,9 +113,7 @@ def main():
         sys.exit(1)
 
     output_file = sys.argv[2]
-    output_dir = os.path.dirname(output_file)
-    if not os.path.isdir(output_dir):
-        os.makedirs(output_dir)
+    mkdir_p(os.path.dirname(output_file))
 
     software_version = deduce_software_version(sys.argv[1])
     new_content = '"{}", "SPIRV-Tools {} {}"\n'.format(