Fix config read/write locking

This commit is contained in:
Philipp 2023-01-03 23:06:17 +01:00
parent 2292263780
commit 17105cf7d1
No known key found for this signature in database
GPG key ID: 24A7501396EB5432

View file

@ -173,7 +173,21 @@ class ConfigFileManager
$filename = $this->configDir . '/' . self::CONFIG_DATA_FILE; $filename = $this->configDir . '/' . self::CONFIG_DATA_FILE;
if (file_exists($filename)) { if (file_exists($filename)) {
$dataArray = include $filename;
$content = '<?php return [];';
$configStream = fopen($filename, 'r');
if (flock($configStream, LOCK_SH)) {
$content = fread($configStream, filesize($filename));
if (!$content) {
throw new ConfigFileException(sprintf('Couldn\'t read file %s', $filename));
}
flock($configStream, LOCK_UN);
}
fclose($configStream);
$dataArray = eval('?>' . $content);
if (!is_array($dataArray)) { if (!is_array($dataArray)) {
throw new ConfigFileException(sprintf('Error loading config file %s', $filename)); throw new ConfigFileException(sprintf('Error loading config file %s', $filename));
@ -200,8 +214,13 @@ class ConfigFileManager
throw new ConfigFileException('config source cannot get encoded'); throw new ConfigFileException('config source cannot get encoded');
} }
if (!file_put_contents($this->configDir . '/' . self::CONFIG_DATA_FILE, $encodedData)) { $configStream = fopen($this->configDir . '/' . self::CONFIG_DATA_FILE, 'w+');
throw new ConfigFileException(sprintf('Cannot save data to file %s/%s', $this->configDir, self::CONFIG_DATA_FILE));
if (flock($configStream, LOCK_EX)) {
ftruncate($configStream, 0);
fwrite($configStream, $encodedData);
fflush($configStream);
flock($configStream, LOCK_UN);
} }
} }