From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: mx.groups.io; dkim=missing; spf=pass (domain: intel.com, ip: 192.55.52.115, mailfrom: yonghong.zhu@intel.com) Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by groups.io with SMTP; Wed, 10 Apr 2019 22:51:26 -0700 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga103.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 10 Apr 2019 22:51:25 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.60,336,1549958400"; d="scan'208";a="133293497" Received: from fmsmsx106.amr.corp.intel.com ([10.18.124.204]) by orsmga008.jf.intel.com with ESMTP; 10 Apr 2019 22:51:25 -0700 Received: from fmsmsx121.amr.corp.intel.com (10.18.125.36) by FMSMSX106.amr.corp.intel.com (10.18.124.204) with Microsoft SMTP Server (TLS) id 14.3.408.0; Wed, 10 Apr 2019 22:51:24 -0700 Received: from shsmsx107.ccr.corp.intel.com (10.239.4.96) by fmsmsx121.amr.corp.intel.com (10.18.125.36) with Microsoft SMTP Server (TLS) id 14.3.408.0; Wed, 10 Apr 2019 22:51:25 -0700 Received: from shsmsx103.ccr.corp.intel.com ([169.254.4.93]) by SHSMSX107.ccr.corp.intel.com ([169.254.9.153]) with mapi id 14.03.0415.000; Thu, 11 Apr 2019 13:51:23 +0800 From: "Zhu, Yonghong" To: "Gao, Liming" , "devel@edk2.groups.io" CC: "Feng, YunhuaX" , "Feng, Bob C" , "Zhu, Yonghong" Subject: Re: [Patch] BaseTools: Sometime write file not immediate to disk Thread-Topic: [Patch] BaseTools: Sometime write file not immediate to disk Thread-Index: AQHU8CMkVxjBqX7UYUiDN6RrE7pvHKY2dMsA Date: Thu, 11 Apr 2019 05:51:21 +0000 Message-ID: References: <1554958635-9588-1-git-send-email-liming.gao@intel.com> In-Reply-To: <1554958635-9588-1-git-send-email-liming.gao@intel.com> Accept-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [10.239.127.40] MIME-Version: 1.0 Return-Path: yonghong.zhu@intel.com Content-Language: en-US Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Reviewed-by: Yonghong Zhu Best Regards, Zhu Yonghong -----Original Message----- From: Gao, Liming=20 Sent: Thursday, April 11, 2019 12:57 PM To: devel@edk2.groups.io Cc: Feng, YunhuaX ; Feng, Bob C ; Zhu, Yonghong Subject: [Patch] BaseTools: Sometime write file not immediate to disk 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/Pyth= on/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 impor= t uuid import subprocess +import tempfile from collections import OrderedDict =20 import Common.LongFilePathOs as os @@ -476,15 +477,23 @@ def SaveFileOnChange(File, Content, IsBinaryFile=3DTr= ue): if not os.access(DirName, os.W_OK): EdkLogger.error(None, PERMISSION_FAILURE, "Do not have write p= ermission on directory %s" % DirName) =20 + OpenMode =3D "w" if IsBinaryFile: + OpenMode =3D "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=3Dos.path.dirname(F= ile), delete=3DFalse) as tf: + tf.write(Content) + tempname =3D 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=3D'IOErro= r %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=3D'IOErro= r %s' % X) -- 2.13.0.windows.1