From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: mx.groups.io; dkim=missing; spf=pass (domain: intel.com, ip: 134.134.136.100, mailfrom: liming.gao@intel.com) Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by groups.io with SMTP; Wed, 10 Apr 2019 21:57:59 -0700 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga006.jf.intel.com ([10.7.209.51]) by orsmga105.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 10 Apr 2019 21:57:59 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.60,336,1549958400"; d="scan'208";a="134772426" Received: from shwde7172.ccr.corp.intel.com ([10.239.158.22]) by orsmga006.jf.intel.com with ESMTP; 10 Apr 2019 21:57:57 -0700 From: "Liming Gao" To: devel@edk2.groups.io Cc: Yunhua Feng , Bob Feng , Yonghong Zhu Subject: [Patch] BaseTools: Sometime write file not immediate to disk Date: Thu, 11 Apr 2019 12:57:15 +0800 Message-Id: <1554958635-9588-1-git-send-email-liming.gao@intel.com> X-Mailer: git-send-email 2.8.0.windows.1 From: Yunhua Feng 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 Cc: Liming Gao Cc: Yonghong Zhu Signed-off-by: Yunhua Feng --- 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