]> git.proxmox.com Git - mirror_edk2.git/commitdiff
BaseTools: Sometime write file not immediate to disk
authorYunhua Feng <yunhuax.feng@intel.com>
Thu, 11 Apr 2019 04:57:15 +0000 (12:57 +0800)
committerLiming Gao <liming.gao@intel.com>
Mon, 22 Apr 2019 02:05:52 +0000 (10:05 +0800)
BZ: https://bugzilla.tianocore.org/process_bug.cgi

On Windows OS, sometime the generated file is not immediate saved to disk.
When run nmake, prompt AutoGen.h not found, and stop build.
Below blog shows Write-Replace to fix it. This patch uses this way to write
temp file, then rename the temp file to the real file.
https://blog.gocept.com/2013/07/15/reliable-file-updates-with-python/

Cc: Bob Feng <bob.c.feng@intel.com>
Cc: Liming Gao <liming.gao@intel.com>
Cc: Yonghong Zhu <yonghong.zhu@intel.com>
Signed-off-by: Yunhua Feng <yunhuax.feng@intel.com>
Reviewed-by: Yonghong Zhu <yonghong.zhu@intel.com>
BaseTools/Source/Python/Common/Misc.py

index 5db9405ddc06264c53a2909b3f08e25efbc29ad7..3b3ab2d6df84b2ea9ede5a88ff5e9d7c9b8ae818 100644 (file)
@@ -22,6 +22,7 @@ from random import sample
 from struct import pack\r
 import uuid\r
 import subprocess\r
 from struct import pack\r
 import uuid\r
 import subprocess\r
+import tempfile\r
 from collections import OrderedDict\r
 \r
 import Common.LongFilePathOs as os\r
 from collections import OrderedDict\r
 \r
 import Common.LongFilePathOs as os\r
@@ -476,15 +477,23 @@ def SaveFileOnChange(File, Content, IsBinaryFile=True):
         if not os.access(DirName, os.W_OK):\r
             EdkLogger.error(None, PERMISSION_FAILURE, "Do not have write permission on directory %s" % DirName)\r
 \r
         if not os.access(DirName, os.W_OK):\r
             EdkLogger.error(None, PERMISSION_FAILURE, "Do not have write permission on directory %s" % DirName)\r
 \r
+    OpenMode = "w"\r
     if IsBinaryFile:\r
     if IsBinaryFile:\r
+        OpenMode = "wb"\r
+\r
+    if GlobalData.gIsWindows and not os.path.exists(File):\r
+        # write temp file, then rename the temp file to the real file\r
+        # to make sure the file be immediate saved to disk\r
+        with tempfile.NamedTemporaryFile(OpenMode, dir=os.path.dirname(File), delete=False) as tf:\r
+            tf.write(Content)\r
+            tempname = tf.name\r
         try:\r
         try:\r
-            with open(File, "wb") as Fd:\r
-                Fd.write(Content)\r
-        except IOError as X:\r
+            os.rename(tempname, File)\r
+        except:\r
             EdkLogger.error(None, FILE_CREATE_FAILURE, ExtraData='IOError %s' % X)\r
     else:\r
         try:\r
             EdkLogger.error(None, FILE_CREATE_FAILURE, ExtraData='IOError %s' % X)\r
     else:\r
         try:\r
-            with open(File, 'w') as Fd:\r
+            with open(File, OpenMode) as Fd:\r
                 Fd.write(Content)\r
         except IOError as X:\r
             EdkLogger.error(None, FILE_CREATE_FAILURE, ExtraData='IOError %s' % X)\r
                 Fd.write(Content)\r
         except IOError as X:\r
             EdkLogger.error(None, FILE_CREATE_FAILURE, ExtraData='IOError %s' % X)\r