From b28a214fd90a0b7e6ad9ddfc8ed3f7efcbe0cc88 Mon Sep 17 00:00:00 2001 From: Chris Burr Date: Tue, 10 Dec 2024 22:10:39 +0100 Subject: [PATCH] fix (CS): use temporary file for writing configuration to disk --- src/DIRAC/ConfigurationSystem/private/ConfigurationData.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/DIRAC/ConfigurationSystem/private/ConfigurationData.py b/src/DIRAC/ConfigurationSystem/private/ConfigurationData.py index b4971bf64f4..16885d1e648 100755 --- a/src/DIRAC/ConfigurationSystem/private/ConfigurationData.py +++ b/src/DIRAC/ConfigurationSystem/private/ConfigurationData.py @@ -7,6 +7,7 @@ import _thread import time import datetime +import secrets from diraccfg import CFG @@ -352,11 +353,15 @@ def __backupCurrentConfiguration(self, backupName): def writeRemoteConfigurationToDisk(self, backupName=False): configurationFile = os.path.join(DIRAC.rootPath, "etc", f"{self.getName()}.cfg") + configurationFileTmp = f"{configurationFile}.{secrets.token_hex(8)}" try: - with open(configurationFile, "w") as fd: + with open(configurationFileTmp, "w") as fd: fd.write(str(self.remoteCFG)) + os.rename(configurationFileTmp, configurationFile) except Exception as e: gLogger.fatal("Cannot write new configuration to disk!", f"file {configurationFile} exception {repr(e)}") + if os.path.isfile(configurationFileTmp): + os.remove(configurationFileTmp) return S_ERROR(f"Can't write cs file {configurationFile}!: {repr(e).replace(',)', ')')}") if backupName: self.__backupCurrentConfiguration(backupName)