From 17105cf7d126dc9765975dd54d2daf261a1c18a9 Mon Sep 17 00:00:00 2001 From: Philipp Date: Tue, 3 Jan 2023 23:06:17 +0100 Subject: [PATCH] Fix config read/write locking --- src/Core/Config/Util/ConfigFileManager.php | 25 +++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/Core/Config/Util/ConfigFileManager.php b/src/Core/Config/Util/ConfigFileManager.php index 378d7fbd4e..a3084b8323 100644 --- a/src/Core/Config/Util/ConfigFileManager.php +++ b/src/Core/Config/Util/ConfigFileManager.php @@ -173,7 +173,21 @@ class ConfigFileManager $filename = $this->configDir . '/' . self::CONFIG_DATA_FILE; if (file_exists($filename)) { - $dataArray = include $filename; + + $content = '' . $content); if (!is_array($dataArray)) { throw new ConfigFileException(sprintf('Error loading config file %s', $filename)); @@ -200,8 +214,13 @@ class ConfigFileManager throw new ConfigFileException('config source cannot get encoded'); } - if (!file_put_contents($this->configDir . '/' . self::CONFIG_DATA_FILE, $encodedData)) { - throw new ConfigFileException(sprintf('Cannot save data to file %s/%s', $this->configDir, self::CONFIG_DATA_FILE)); + $configStream = fopen($this->configDir . '/' . self::CONFIG_DATA_FILE, 'w+'); + + if (flock($configStream, LOCK_EX)) { + ftruncate($configStream, 0); + fwrite($configStream, $encodedData); + fflush($configStream); + flock($configStream, LOCK_UN); } }