Skip to content

Commit

Permalink
Merge pull request #7819 from owncloud/stable6-datafolderexistence
Browse files Browse the repository at this point in the history
[stable6] [backport] Added .ocdata file to check for data folder validity
  • Loading branch information
Vincent Petry committed Mar 20, 2014
2 parents 6ccc817 + 288c40e commit 2c2fd5f
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 5 deletions.
1 change: 1 addition & 0 deletions lib/base.php
Expand Up @@ -525,6 +525,7 @@ public static function init() {
echo $error['hint'] . "\n\n";
}
} else {
OC_Response::setStatus(OC_Response::STATUS_SERVICE_UNAVAILABLE);
OC_Template::printGuestPage('', 'error', array('errors' => $errors));
}
exit;
Expand Down
4 changes: 4 additions & 0 deletions lib/private/response.php
Expand Up @@ -12,6 +12,7 @@ class OC_Response {
const STATUS_TEMPORARY_REDIRECT = 307;
const STATUS_NOT_FOUND = 404;
const STATUS_INTERNAL_SERVER_ERROR = 500;
const STATUS_SERVICE_UNAVAILABLE = 503;

/**
* @brief Enable response caching by sending correct HTTP headers
Expand Down Expand Up @@ -74,6 +75,9 @@ static public function setStatus($status) {
case self::STATUS_INTERNAL_SERVER_ERROR;
$status = $status . ' Internal Server Error';
break;
case self::STATUS_SERVICE_UNAVAILABLE;
$status = $status . ' Service Unavailable';
break;
}
header($protocol.' '.$status);
}
Expand Down
4 changes: 4 additions & 0 deletions lib/private/setup.php
Expand Up @@ -107,6 +107,10 @@ public static function install($options) {
//guess what this does
OC_Installer::installShippedApps();

// create empty file in data dir, so we can later find
// out that this is indeed an ownCloud data directory
file_put_contents(OC_Config::getValue('datadirectory', OC::$SERVERROOT.'/data').'/.ocdata', '');

//create htaccess files for apache hosts
if (isset($_SERVER['SERVER_SOFTWARE']) && strstr($_SERVER['SERVER_SOFTWARE'], 'Apache')) {
self::createHtaccess();
Expand Down
5 changes: 5 additions & 0 deletions lib/private/updater.php
Expand Up @@ -105,6 +105,11 @@ public function upgrade() {
}
$this->emit('\OC\Updater', 'maintenanceStart');

// create empty file in data dir, so we can later find
// out that this is indeed an ownCloud data directory
// (in case it didn't exist before)
file_put_contents(\OC_Config::getValue('datadirectory', \OC::$SERVERROOT.'/data').'/.ocdata', '');

/*
* START CONFIG CHANGES FOR OLDER VERSIONS
*/
Expand Down
32 changes: 28 additions & 4 deletions lib/private/util.php
Expand Up @@ -288,13 +288,19 @@ public static function formatDate( $timestamp, $dateOnly=false) {
* @return array arrays with error messages and hints
*/
public static function checkServer() {
$errors = array();
$CONFIG_DATADIRECTORY = OC_Config::getValue('datadirectory', OC::$SERVERROOT . '/data');

if (!\OC::needUpgrade() && OC_Config::getValue('installed', false)) {
// this check needs to be done every time
$errors = self::checkDataDirectoryValidity($CONFIG_DATADIRECTORY);
}

// Assume that if checkServer() succeeded before in this session, then all is fine.
if(\OC::$session->exists('checkServer_suceeded') && \OC::$session->get('checkServer_suceeded')) {
return array();
return $errors;
}

$errors = array();

$defaults = new \OC_Defaults();

$webServerRestart = false;
Expand Down Expand Up @@ -339,7 +345,6 @@ public static function checkServer() {
);
}
}
$CONFIG_DATADIRECTORY = OC_Config::getValue( "datadirectory", OC::$SERVERROOT."/data" );
// Create root dir.
if(!is_dir($CONFIG_DATADIRECTORY)) {
$success=@mkdir($CONFIG_DATADIRECTORY);
Expand Down Expand Up @@ -538,6 +543,25 @@ public static function checkDataDirectoryPermissions($dataDirectory) {
return $errors;
}

/**
* Check that the data directory exists and is valid by
* checking the existence of the ".ocdata" file.
*
* @param string $dataDirectory data directory path
* @return bool true if the data directory is valid, false otherwise
*/
public static function checkDataDirectoryValidity($dataDirectory) {
$errors = array();
if (!file_exists($dataDirectory.'/.ocdata')) {
$errors[] = array(
'error' => 'Data directory (' . $dataDirectory . ') is invalid',
'hint' => 'Please check that the data directory contains a file' .
' ".ocdata" in its root.'
);
}
return $errors;
}

/**
* @return void
*/
Expand Down
108 changes: 108 additions & 0 deletions tests/lib/utilcheckserver.php
@@ -0,0 +1,108 @@
<?php
/**
* Copyright (c) 2014 Vincent Petry <pvince81@owncloud.com>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/

/**
* Tests for server check functions
*/
class Test_Util_CheckServer extends PHPUnit_Framework_TestCase {

private $datadir;

public function setUp() {
$this->datadir = \OC_Config::getValue('datadirectory', \OC::$SERVERROOT . '/data');

file_put_contents($this->datadir . '/.ocdata', '');
}

public function tearDown() {
// clean up
@unlink($this->datadir . '/.ocdata');
}

/**
* Test that checkServer() returns no errors in the regular case.
*/
public function testCheckServer() {
$result = \OC_Util::checkServer();
$this->assertEmpty($result);
}

/**
* Test that checkServer() does not check the data dir validity
* when the server is not installed yet (else the setup cannot
* be run...)
*/
public function testCheckServerSkipDataDirValidityOnSetup() {
// simulate old version that didn't have it
unlink($this->datadir . '/.ocdata');

$session = \OC::$server->getSession();
$oldInstalled = \OC_Config::getValue('installed', false);

// simulate that the server isn't setup yet
\OC_Config::setValue('installed', false);

// even though ".ocdata" is missing, the error isn't
// triggered to allow setup to run
$result = \OC_Util::checkServer();
$this->assertEmpty($result);

// restore config
\OC_Config::setValue('installed', $oldInstalled);
}

/**
* Test that checkServer() does not check the data dir validity
* when an upgrade is required (else the upgrade cannot be
* performed...)
*/
public function testCheckServerSkipDataDirValidityOnUpgrade() {
// simulate old version that didn't have it
unlink($this->datadir . '/.ocdata');

$session = \OC::$server->getSession();
$oldCurrentVersion = $session->get('OC_Version');
$oldInstallVersion = \OC_Config::getValue('version', '0.0.0');

// upgrade condition to simulate needUpgrade() === true
$session->set('OC_Version', array(6, 0, 0, 2));
\OC_Config::setValue('version', '6.0.0.1');

// even though ".ocdata" is missing, the error isn't
// triggered to allow for upgrade
$result = \OC_Util::checkServer();
$this->assertEmpty($result);

// restore versions
$session->set('OC_Version', $oldCurrentVersion);
\OC_Config::setValue('version', $oldInstallVersion);
}

/**
* Test that checkDataDirectoryValidity returns no error
* when ".ocdata" is present.
*/
public function testCheckDataDirValidity() {
$result = \OC_Util::checkDataDirectoryValidity($this->datadir);
$this->assertEmpty($result);
}

/**
* Test that checkDataDirectoryValidity and checkServer
* both return an error when ".ocdata" is missing.
*/
public function testCheckDataDirValidityWhenFileMissing() {
unlink($this->datadir . '/.ocdata');
$result = \OC_Util::checkDataDirectoryValidity($this->datadir);
$this->assertEquals(1, count($result));

$result = \OC_Util::checkServer();
$this->assertEquals(1, count($result));
}

}
2 changes: 1 addition & 1 deletion version.php
@@ -1,7 +1,7 @@
<?php

// We only can count up. The 4. digit is only for the internal patchlevel to trigger DB upgrades between betas, final and RCs. This is _not_ the public version number. Reset minor/patchlevel when updating major/minor version number.
$OC_Version=array(6, 0, 2, 4);
$OC_Version=array(6, 0, 2, 5);

// The human readable string
$OC_VersionString='6.0.2';
Expand Down

0 comments on commit 2c2fd5f

Please sign in to comment.