Fix namespace duplication and other issues in repairlegacystorages

This commit is contained in:
Joas Schilling 2015-04-13 16:34:10 +02:00
parent 660aa7ff1e
commit 71de1d58cd
3 changed files with 77 additions and 27 deletions

View File

@ -216,7 +216,7 @@ class OC_User {
* @return bool * @return bool
* *
* Deletes a user * 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) { public static function deleteUser($uid) {
$user = self::getManager()->get($uid); $user = self::getManager()->get($uid);

View File

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

View File

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