|
7 | 7 | * SPDX-License-Identifier: AGPL-3.0-or-later
|
8 | 8 | */
|
9 | 9 |
|
| 10 | + |
10 | 11 | class UpdateException extends \Exception {
|
11 | 12 |
|
12 | 13 | /** @param list<string> $data */
|
@@ -63,18 +64,7 @@ class Updater {
|
63 | 64 | public function __construct(string $baseDir) {
|
64 | 65 | $this->baseDir = $baseDir;
|
65 | 66 |
|
66 |
| - if ($dir = getenv('NEXTCLOUD_CONFIG_DIR')) { |
67 |
| - $configFileName = rtrim($dir, '/') . '/config.php'; |
68 |
| - } else { |
69 |
| - $configFileName = $this->baseDir . '/../config/config.php'; |
70 |
| - } |
71 |
| - if (!file_exists($configFileName)) { |
72 |
| - throw new \Exception('Could not find config.php. Is this file in the "updater" subfolder of Nextcloud?'); |
73 |
| - } |
74 |
| - |
75 |
| - /** @var array $CONFIG */ |
76 |
| - require_once $configFileName; |
77 |
| - $this->configValues = $CONFIG; |
| 67 | + [$this->configValues] = $this->readConfigFile(); |
78 | 68 |
|
79 | 69 | if (php_sapi_name() !== 'cli' && ($this->configValues['upgrade.disable-web'] ?? false)) {
|
80 | 70 | // updater disabled
|
@@ -116,6 +106,38 @@ public function __construct(string $baseDir) {
|
116 | 106 | $this->buildTime = $buildTime;
|
117 | 107 | }
|
118 | 108 |
|
| 109 | + /** |
| 110 | + * @return array{array, string} |
| 111 | + */ |
| 112 | + private function readConfigFile(): array { |
| 113 | + if ($dir = getenv('NEXTCLOUD_CONFIG_DIR')) { |
| 114 | + $configFileName = realpath($dir . '/config.php'); |
| 115 | + } else { |
| 116 | + $configFileName = $this->baseDir . '/config/config.php'; |
| 117 | + } |
| 118 | + if (!file_exists($configFileName)) { |
| 119 | + throw new \Exception('Could not find config.php (' . $configFileName . '). Is this file in the "updater" subfolder of Nextcloud?'); |
| 120 | + } |
| 121 | + $filePointer = @fopen($configFileName, 'r'); |
| 122 | + if ($filePointer === false) { |
| 123 | + throw new \Exception('Could not open config.php (' . $configFileName . ').'); |
| 124 | + } |
| 125 | + if (!flock($filePointer, LOCK_SH)) { |
| 126 | + throw new \Exception('Could not acquire a shared lock on the config file (' . $configFileName . ')'); |
| 127 | + } |
| 128 | + |
| 129 | + try { |
| 130 | + require $configFileName; |
| 131 | + } finally { |
| 132 | + // Close the file pointer and release the lock |
| 133 | + flock($filePointer, LOCK_UN); |
| 134 | + fclose($filePointer); |
| 135 | + } |
| 136 | + |
| 137 | + /** @var array $CONFIG */ |
| 138 | + return [$CONFIG,$configFileName]; |
| 139 | + } |
| 140 | + |
119 | 141 | /**
|
120 | 142 | * Returns whether the web updater is disabled
|
121 | 143 | *
|
@@ -374,27 +396,17 @@ public function checkWritePermissions(): void {
|
374 | 396 | public function setMaintenanceMode(bool $state): void {
|
375 | 397 | $this->silentLog('[info] setMaintenanceMode("' . ($state ? 'true' : 'false') . '")');
|
376 | 398 |
|
377 |
| - if ($dir = getenv('NEXTCLOUD_CONFIG_DIR')) { |
378 |
| - $configFileName = rtrim($dir, '/') . '/config.php'; |
379 |
| - } else { |
380 |
| - $configFileName = $this->baseDir . '/../config/config.php'; |
381 |
| - } |
| 399 | + [$CONFIG, $configFileName] = $this->readConfigFile(); |
382 | 400 | $this->silentLog('[info] configFileName ' . $configFileName);
|
383 | 401 |
|
384 |
| - // usually is already tested in the constructor but just to be on the safe side |
385 |
| - if (!file_exists($configFileName)) { |
386 |
| - throw new \Exception('Could not find config.php.'); |
387 |
| - } |
388 |
| - /** @var array $CONFIG */ |
389 |
| - require $configFileName; |
390 | 402 | $CONFIG['maintenance'] = $state;
|
391 | 403 | $content = "<?php\n";
|
392 | 404 | $content .= '$CONFIG = ';
|
393 | 405 | $content .= var_export($CONFIG, true);
|
394 | 406 | $content .= ";\n";
|
395 |
| - $state = file_put_contents($configFileName, $content); |
396 |
| - if ($state === false) { |
397 |
| - throw new \Exception('Could not write to config.php'); |
| 407 | + $writeSuccess = file_put_contents($configFileName, $content, LOCK_EX); |
| 408 | + if ($writeSuccess === false) { |
| 409 | + throw new \Exception('Could not write to config.php (' . $configFileName . ')'); |
398 | 410 | }
|
399 | 411 | $this->silentLog('[info] end of setMaintenanceMode()');
|
400 | 412 | }
|
|
0 commit comments