Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fix: lock config file when reading and writing
this is in accordance how the config files are handled in \OC\Config

Signed-off-by: Arthur Schiwon <[email protected]>
  • Loading branch information
blizzz authored and backportbot[bot] committed Feb 10, 2025
commit e2eefbc17c554d923d9590f9041cf41e7b30c553
38 changes: 34 additions & 4 deletions lib/Updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,23 @@ public function __construct(string $baseDir) {
if (!file_exists($configFileName)) {
throw new \Exception('Could not find config.php. Is this file in the "updater" subfolder of Nextcloud?');
}
$filePointer = @fopen($configFileName, 'r');
if ($filePointer === false) {
throw new \Exception('Could not open config.php.');
}
if (!flock($filePointer, LOCK_SH)) {
throw new \Exception(sprintf('Could not acquire a shared lock on the config file'));
}

try {
require_once $configFileName;
} finally {
// Close the file pointer and release the lock
flock($filePointer, LOCK_UN);
fclose($filePointer);
}

/** @var array $CONFIG */
require_once $configFileName;
$this->configValues = $CONFIG;

if (php_sapi_name() !== 'cli' && ($this->configValues['upgrade.disable-web'] ?? false)) {
Expand Down Expand Up @@ -365,15 +379,31 @@ public function setMaintenanceMode(bool $state): void {
if (!file_exists($configFileName)) {
throw new \Exception('Could not find config.php.');
}

$filePointer = @fopen($configFileName, 'r');
if ($filePointer === false) {
throw new \Exception('Could not open config.php.');
}
if (!flock($filePointer, LOCK_SH)) {
throw new \Exception(sprintf('Could not acquire a shared lock on the config file'));
}

try {
require $configFileName;
} finally {
// Close the file pointer and release the lock
flock($filePointer, LOCK_UN);
fclose($filePointer);
}

/** @var array $CONFIG */
require $configFileName;
$CONFIG['maintenance'] = $state;
$content = "<?php\n";
$content .= '$CONFIG = ';
$content .= var_export($CONFIG, true);
$content .= ";\n";
$state = file_put_contents($configFileName, $content);
if ($state === false) {
$writeSuccess = file_put_contents($configFileName, $content, LOCK_EX);
if ($writeSuccess === false) {
throw new \Exception('Could not write to config.php');
}
$this->silentLog('[info] end of setMaintenanceMode()');
Expand Down