public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
* [Patch] BaseTools: Sometime write file not immediate to disk
@ 2019-04-11  4:57 Liming Gao
  2019-04-11  5:51 ` Zhu, Yonghong
  0 siblings, 1 reply; 2+ messages in thread
From: Liming Gao @ 2019-04-11  4:57 UTC (permalink / raw)
  To: devel; +Cc: Yunhua Feng, Bob Feng, Yonghong Zhu

From: Yunhua Feng <yunhuax.feng@intel.com>

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>
---
 BaseTools/Source/Python/Common/Misc.py | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

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


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [Patch] BaseTools: Sometime write file not immediate to disk
  2019-04-11  4:57 [Patch] BaseTools: Sometime write file not immediate to disk Liming Gao
@ 2019-04-11  5:51 ` Zhu, Yonghong
  0 siblings, 0 replies; 2+ messages in thread
From: Zhu, Yonghong @ 2019-04-11  5:51 UTC (permalink / raw)
  To: Gao, Liming, devel@edk2.groups.io
  Cc: Feng, YunhuaX, Feng, Bob C, Zhu, Yonghong

Reviewed-by: Yonghong Zhu <yonghong.zhu@intel.com>

Best Regards,
Zhu Yonghong

-----Original Message-----
From: Gao, Liming 
Sent: Thursday, April 11, 2019 12:57 PM
To: devel@edk2.groups.io
Cc: Feng, YunhuaX <yunhuax.feng@intel.com>; Feng, Bob C <bob.c.feng@intel.com>; Zhu, Yonghong <yonghong.zhu@intel.com>
Subject: [Patch] BaseTools: Sometime write file not immediate to disk

From: Yunhua Feng <yunhuax.feng@intel.com>

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>
---
 BaseTools/Source/Python/Common/Misc.py | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

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


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2019-04-11  5:51 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-04-11  4:57 [Patch] BaseTools: Sometime write file not immediate to disk Liming Gao
2019-04-11  5:51 ` Zhu, Yonghong

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox