Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nextcloud/server.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLukas Reschke <lukas@owncloud.com>2014-09-25 20:43:04 +0400
committerLukas Reschke <lukas@owncloud.com>2014-10-01 13:14:15 +0400
commitbc5c608e4e290b4766c1a722d4c9b12cafc019ce (patch)
tree03a8d16f2fccd94be3ff08897b32e04a96b5a36e /lib/private/config.php
parent02374e595af0d9910e2bd5e0b3181afe07df0625 (diff)
Add flock to config
This adds a file lock to the config in hope that this prevents race conditions as reported in https://github.com/owncloud/core/issues/11070 Testplan: - [ ] Delete config.php and make it read-only => Error is thrown that it is not writeable - [ ] Installation still works - [ ] Changing config settings works (i.e. using the SMTP config switches in the administration menu) - [ ] Your PC didn't blow up - [ ] Installing the news app and the "Disable AppCode checker" app did not destroy your installation Only skip the main config Otherwise read only additional configs might not be processed Test on tmpdir
Diffstat (limited to 'lib/private/config.php')
-rw-r--r--lib/private/config.php134
1 files changed, 81 insertions, 53 deletions
diff --git a/lib/private/config.php b/lib/private/config.php
index 82a1c46c9d5..7bf3863e2a6 100644
--- a/lib/private/config.php
+++ b/lib/private/config.php
@@ -1,37 +1,12 @@
<?php
/**
- * ownCloud
- *
* @author Frank Karlitschek
* @author Jakob Sack
* @copyright 2012 Frank Karlitschek frank@owncloud.org
*
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
- *
- * You should have received a copy of the GNU Affero General Public
- * License along with this library. If not, see <http://www.gnu.org/licenses/>.
- *
- */
-/*
- *
- * An example of config.php
- *
- * <?php
- * $CONFIG = array(
- * "database" => "mysql",
- * "firstrun" => false,
- * "pi" => 3.14
- * );
- * ?>
- *
+ * This file is licensed under the Affero General Public License version 3 or
+ * later.
+ * See the COPYING-README file.
*/
namespace OC;
@@ -41,26 +16,45 @@ namespace OC;
* configuration file of ownCloud.
*/
class Config {
- // associative array key => value
+ /** @var array Associative array ($key => $value) */
protected $cache = array();
-
+ /** @var string */
protected $configDir;
- protected $configFilename;
-
+ /** @var string */
+ protected $configFilePath;
+ /** @var string */
+ protected $configFileName;
+ /** @var bool */
protected $debugMode;
/**
- * @param string $configDir path to the config dir, needs to end with '/'
+ * @param string $configDir Path to the config dir, needs to end with '/'
+ * @param string $fileName (Optional) Name of the config file. Defaults to config.php
*/
- public function __construct($configDir) {
+ public function __construct($configDir, $fileName = 'config.php') {
$this->configDir = $configDir;
- $this->configFilename = $this->configDir.'config.php';
+ $this->configFilePath = $this->configDir.$fileName;
+ $this->configFileName = $fileName;
$this->readData();
- $this->setDebugMode(defined('DEBUG') && DEBUG);
+ $this->debugMode = (defined('DEBUG') && DEBUG);
+ }
+
+ /**
+ * Enables or disables the debug mode
+ * @param bool $state True to enable, false to disable
+ */
+ public function setDebugMode($state) {
+ $this->debugMode = $state;
+ $this->writeData();
+ $this->cache;
}
- public function setDebugMode($enable) {
- $this->debugMode = $enable;
+ /**
+ * Returns whether the debug mode is enabled or disabled
+ * @return bool True when enabled, false otherwise
+ */
+ public function isDebugMode() {
+ return $this->debugMode;
}
/**
@@ -128,27 +122,43 @@ class Config {
* Loads the config file
*
* Reads the config file and saves it to the cache
+ *
+ * @throws \Exception If no lock could be acquired or the config file has not been found
*/
private function readData() {
- // Default config
- $configFiles = array($this->configFilename);
- // Add all files in the config dir ending with config.php
- $extra = glob($this->configDir.'*.config.php');
+ // Default config should always get loaded
+ $configFiles = array($this->configFilePath);
+
+ // Add all files in the config dir ending with the same file name
+ $extra = glob($this->configDir.'*.'.$this->configFileName);
if (is_array($extra)) {
natsort($extra);
$configFiles = array_merge($configFiles, $extra);
}
+
// Include file and merge config
foreach ($configFiles as $file) {
- if (!file_exists($file)) {
+ if(!@touch($file) && $file === $this->configFilePath) {
+ // Writing to the main config might not be possible, e.g. if the wrong
+ // permissions are set (likely on a new installation)
continue;
}
+ $filePointer = fopen($file, 'r');
+
+ // Try to acquire a file lock
+ if(!flock($filePointer, LOCK_SH)) {
+ throw new \Exception(sprintf('Could not acquire a shared lock on the config file %s', $file));
+ }
+
unset($CONFIG);
- // ignore errors on include, this can happen when doing a fresh install
- @include $file;
- if (isset($CONFIG) && is_array($CONFIG)) {
+ include $file;
+ if(isset($CONFIG) && is_array($CONFIG)) {
$this->cache = array_merge($this->cache, $CONFIG);
}
+
+ // Close the file pointer and release the lock
+ flock($filePointer, LOCK_UN);
+ fclose($filePointer);
}
}
@@ -157,6 +167,8 @@ class Config {
*
* Saves the config to the config file.
*
+ * @throws HintException If the config file cannot be written to
+ * @throws \Exception If no file lock can be acquired
*/
private function writeData() {
// Create a php file ...
@@ -168,18 +180,34 @@ class Config {
$content .= var_export($this->cache, true);
$content .= ";\n";
- // Write the file
- $result = @file_put_contents($this->configFilename, $content);
- if (!$result) {
- $defaults = new \OC_Defaults;
+ touch ($this->configFilePath);
+ $filePointer = fopen($this->configFilePath, 'r+');
+
+ // Prevent others not to read the config
+ chmod($this->configFilePath, 0640);
+
+ // File does not exist, this can happen when doing a fresh install
+ if(!is_resource ($filePointer)) {
$url = \OC_Helper::linkToDocs('admin-dir_permissions');
throw new HintException(
"Can't write into config directory!",
'This can usually be fixed by '
- .'<a href="' . $url . '" target="_blank">giving the webserver write access to the config directory</a>.');
+ .'<a href="' . $url . '" target="_blank">giving the webserver write access to the config directory</a>.');
}
- // Prevent others not to read the config
- @chmod($this->configFilename, 0640);
+
+ // Try to acquire a file lock
+ if(!flock($filePointer, LOCK_EX)) {
+ throw new \Exception(sprintf('Could not acquire an exclusive lock on the config file %s', $this->configFilePath));
+ }
+
+ // Write the config and release the lock
+ ftruncate ($filePointer, 0);
+ fwrite($filePointer, $content);
+ fflush($filePointer);
+ flock($filePointer, LOCK_UN);
+ fclose($filePointer);
+
+ // Clear the opcode cache
\OC_Util::clearOpcodeCache();
}
}