Merge pull request #15581 from owncloud/deduplicate-oc-repair-namespace

Fix namespace duplication and other issues in repairlegacystorages
This commit is contained in:
Morris Jobke 2015-04-13 21:51:38 +02:00
commit 5f66f867b6
3 changed files with 77 additions and 27 deletions

View File

@ -216,7 +216,7 @@ class OC_User {
* @return bool
*
* Deletes a user
* @deprecated Use \OC::$server->getUserManager->delete()
* @deprecated Use \OC::$server->getUserManager()->get() and then run delete() on the return
*/
public static function deleteUser($uid) {
$user = self::getManager()->get($uid);

View File

@ -22,7 +22,9 @@
namespace OC\Repair;
use OC\Files\Cache\Storage;
use OC\Hooks\BasicEmitter;
use OC\RepairException;
class RepairLegacyStorages extends BasicEmitter {
/**
@ -31,7 +33,7 @@ class RepairLegacyStorages extends BasicEmitter {
protected $config;
/**
* @var \OC\DB\Connection
* @var \OCP\IDBConnection
*/
protected $connection;
@ -40,7 +42,7 @@ class RepairLegacyStorages extends BasicEmitter {
/**
* @param \OCP\IConfig $config
* @param \OC\DB\Connection $connection
* @param \OCP\IDBConnection $connection
*/
public function __construct($config, $connection) {
$this->connection = $connection;
@ -83,8 +85,9 @@ class RepairLegacyStorages extends BasicEmitter {
*
* @param string $oldId old storage id
* @param int $oldNumericId old storage numeric id
*
* @param string $userId
* @return bool true if fixed, false otherwise
* @throws RepairException
*/
private function fixLegacyStorage($oldId, $oldNumericId, $userId = null) {
// check whether the new storage already exists
@ -94,7 +97,7 @@ class RepairLegacyStorages extends BasicEmitter {
$newId = 'home::' . $userId;
// check if target id already exists
$newNumericId = \OC\Files\Cache\Storage::getNumericStorageId($newId);
$newNumericId = Storage::getNumericStorageId($newId);
if (!is_null($newNumericId)) {
$newNumericId = (int)$newNumericId;
// try and resolve the conflict
@ -105,7 +108,7 @@ class RepairLegacyStorages extends BasicEmitter {
$this->findStorageInCacheStatement->closeCursor();
if ($row2 !== false) {
// two results means both storages have data, not auto-fixable
throw new \OC\RepairException(
throw new RepairException(
'Could not automatically fix legacy storage '
. '"' . $oldId . '" => "' . $newId . '"'
. ' because they both have data.'
@ -123,7 +126,7 @@ class RepairLegacyStorages extends BasicEmitter {
}
// delete storage including file cache
\OC\Files\Cache\Storage::remove($toDelete);
Storage::remove($toDelete);
// if we deleted the old id, the new id will be used
// automatically
@ -134,8 +137,8 @@ class RepairLegacyStorages extends BasicEmitter {
}
// rename old id to new id
$newId = \OC\Files\Cache\Storage::adjustStorageId($newId);
$oldId = \OC\Files\Cache\Storage::adjustStorageId($oldId);
$newId = Storage::adjustStorageId($newId);
$oldId = Storage::adjustStorageId($oldId);
$rowCount = $this->renameStorageStatement->execute(array($newId, $oldId));
$this->renameStorageStatement->closeCursor();
return ($rowCount === 1);
@ -180,7 +183,7 @@ class RepairLegacyStorages extends BasicEmitter {
$count++;
}
}
catch (\OC\RepairException $e) {
catch (RepairException $e) {
$hasWarnings = true;
$this->emit(
'\OC\Repair',
@ -199,7 +202,7 @@ class RepairLegacyStorages extends BasicEmitter {
// find at least one to make sure it's worth
// querying the user list
if ((int)$row['c'] > 0) {
$userManager = \OC_User::getManager();
$userManager = \OC::$server->getUserManager();
// use chunks to avoid caching too many users in memory
$limit = 30;
@ -209,7 +212,6 @@ class RepairLegacyStorages extends BasicEmitter {
// query the next page of users
$results = $userManager->search('', $limit, $offset);
$storageIds = array();
$userIds = array();
foreach ($results as $uid => $userObject) {
$storageId = $dataDirId . $uid . '/';
if (strlen($storageId) <= 64) {
@ -222,13 +224,13 @@ class RepairLegacyStorages extends BasicEmitter {
if (count($storageIds) > 0) {
// update the storages of these users
foreach ($storageIds as $uid => $storageId) {
$numericId = \OC\Files\Cache\Storage::getNumericStorageId($storageId);
$numericId = Storage::getNumericStorageId($storageId);
try {
if (!is_null($numericId) && $this->fixLegacyStorage($storageId, (int)$numericId)) {
$count++;
}
}
catch (\OC\RepairException $e) {
catch (RepairException $e) {
$hasWarnings = true;
$this->emit(
'\OC\Repair',

View File

@ -5,18 +5,25 @@
* later.
* See the COPYING-README file.
*/
namespace Test\Repair;
use OC\Files\Cache\Cache;
use OC\Files\Cache\Storage;
use Test\TestCase;
/**
* Tests for the converting of legacy storages to home storages.
*
* @see \OC\Repair\RepairLegacyStorages
*/
class RepairLegacyStorages extends \Test\TestCase {
class RepairLegacyStorages extends TestCase {
/** @var \OCP\IDBConnection */
private $connection;
/** @var \OCP\IConfig */
private $config;
private $user;
/** @var \OC\Repair\RepairLegacyStorages */
private $repair;
private $dataDir;
@ -31,7 +38,7 @@ class RepairLegacyStorages extends \Test\TestCase {
parent::setUp();
$this->config = \OC::$server->getConfig();
$this->connection = \OC_DB::getConnection();
$this->connection = \OC::$server->getDatabaseConnection();
$this->oldDataDir = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data/');
$this->repair = new \OC\Repair\RepairLegacyStorages($this->config, $this->connection);
@ -44,40 +51,49 @@ class RepairLegacyStorages extends \Test\TestCase {
}
protected function tearDown() {
\OC_User::deleteUser($this->user);
$user = \OC::$server->getUserManager()->get($this->user);
if ($user) {
$user->delete();
}
$sql = 'DELETE FROM `*PREFIX*storages`';
$this->connection->executeQuery($sql);
$sql = 'DELETE FROM `*PREFIX*filecache`';
$this->connection->executeQuery($sql);
\OCP\Config::setSystemValue('datadirectory', $this->oldDataDir);
$this->config->setSystemValue('datadirectory', $this->oldDataDir);
$this->config->setAppValue('core', 'repairlegacystoragesdone', 'no');
parent::tearDown();
}
/**
* @param string $dataDir
* @param string $userId
* @throws \Exception
*/
function prepareSettings($dataDir, $userId) {
// hard-coded string as we want a predictable fixed length
// no data will be written there
$this->dataDir = $dataDir;
\OCP\Config::setSystemValue('datadirectory', $this->dataDir);
$this->config->setSystemValue('datadirectory', $this->dataDir);
$this->user = $userId;
$this->legacyStorageId = 'local::' . $this->dataDir . $this->user . '/';
$this->newStorageId = 'home::' . $this->user;
\OC_User::createUser($this->user, $this->user);
\OC::$server->getUserManager()->createUser($this->user, $this->user);
}
/**
* Create a storage entry
*
* @param string $storageId
* @return int
*/
private function createStorage($storageId) {
$sql = 'INSERT INTO `*PREFIX*storages` (`id`)'
. ' VALUES (?)';
$storageId = \OC\Files\Cache\Storage::adjustStorageId($storageId);
$storageId = Storage::adjustStorageId($storageId);
$numRows = $this->connection->executeUpdate($sql, array($storageId));
$this->assertEquals(1, $numRows);
@ -87,11 +103,11 @@ class RepairLegacyStorages extends \Test\TestCase {
/**
* Returns the storage id based on the numeric id
*
* @param int $numericId numeric id of the storage
* @param int $storageId numeric id of the storage
* @return string storage id or null if not found
*/
private function getStorageId($storageId) {
$numericId = \OC\Files\Cache\Storage::getNumericStorageId($storageId);
$numericId = Storage::getNumericStorageId($storageId);
if (!is_null($numericId)) {
return (int)$numericId;
}
@ -104,7 +120,7 @@ class RepairLegacyStorages extends \Test\TestCase {
* @param string $storageId storage id
*/
private function createData($storageId) {
$cache = new \OC\Files\Cache\Cache($storageId);
$cache = new Cache($storageId);
$cache->put(
'dummyfile.txt',
array('size' => 5, 'mtime' => 12, 'mimetype' => 'text/plain')
@ -113,7 +129,11 @@ class RepairLegacyStorages extends \Test\TestCase {
/**
* Test that existing home storages are left alone when valid.
*
* @dataProvider settingsProvider
*
* @param string $dataDir
* @param string $userId
*/
public function testNoopWithExistingHomeStorage($dataDir, $userId) {
$this->prepareSettings($dataDir, $userId);
@ -128,7 +148,11 @@ class RepairLegacyStorages extends \Test\TestCase {
/**
* Test that legacy storages are converted to home storages when
* the latter does not exist.
*
* @dataProvider settingsProvider
*
* @param string $dataDir
* @param string $userId
*/
public function testConvertLegacyToHomeStorage($dataDir, $userId) {
$this->prepareSettings($dataDir, $userId);
@ -143,12 +167,16 @@ class RepairLegacyStorages extends \Test\TestCase {
/**
* Test that legacy storages are converted to home storages
* when home storage already exists but has no data.
*
* @dataProvider settingsProvider
*
* @param string $dataDir
* @param string $userId
*/
public function testConvertLegacyToExistingEmptyHomeStorage($dataDir, $userId) {
$this->prepareSettings($dataDir, $userId);
$legacyStorageNumId = $this->createStorage($this->legacyStorageId);
$newStorageNumId = $this->createStorage($this->newStorageId);
$this->createStorage($this->newStorageId);
$this->createData($this->legacyStorageId);
@ -162,11 +190,15 @@ class RepairLegacyStorages extends \Test\TestCase {
* Test that legacy storages are converted to home storages
* when home storage already exists and the legacy storage
* has no data.
*
* @dataProvider settingsProvider
*
* @param string $dataDir
* @param string $userId
*/
public function testConvertEmptyLegacyToHomeStorage($dataDir, $userId) {
$this->prepareSettings($dataDir, $userId);
$legacyStorageNumId = $this->createStorage($this->legacyStorageId);
$this->createStorage($this->legacyStorageId);
$newStorageNumId = $this->createStorage($this->newStorageId);
$this->createData($this->newStorageId);
@ -180,7 +212,11 @@ class RepairLegacyStorages extends \Test\TestCase {
/**
* Test that nothing is done when both conflicting legacy
* and home storage have data.
*
* @dataProvider settingsProvider
*
* @param string $dataDir
* @param string $userId
*/
public function testConflictNoop($dataDir, $userId) {
$this->prepareSettings($dataDir, $userId);
@ -205,7 +241,11 @@ class RepairLegacyStorages extends \Test\TestCase {
/**
* Test that the data dir local entry is left alone
*
* @dataProvider settingsProvider
*
* @param string $dataDir
* @param string $userId
*/
public function testDataDirEntryNoop($dataDir, $userId) {
$this->prepareSettings($dataDir, $userId);
@ -219,7 +259,11 @@ class RepairLegacyStorages extends \Test\TestCase {
/**
* Test that external local storages are left alone
*
* @dataProvider settingsProvider
*
* @param string $dataDir
* @param string $userId
*/
public function testLocalExtStorageNoop($dataDir, $userId) {
$this->prepareSettings($dataDir, $userId);
@ -233,7 +277,11 @@ class RepairLegacyStorages extends \Test\TestCase {
/**
* Test that other external storages are left alone
*
* @dataProvider settingsProvider
*
* @param string $dataDir
* @param string $userId
*/
public function testExtStorageNoop($dataDir, $userId) {
$this->prepareSettings($dataDir, $userId);