2014-03-25 11:52:32 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 15:07:57 +00:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
|
|
|
* @author Aaron Wood <aaronjwood@gmail.com>
|
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-05-26 17:56:05 +00:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Vincent Petry <pvince81@owncloud.com>
|
|
|
|
*
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program 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, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
2014-03-25 11:52:32 +00:00
|
|
|
*/
|
2015-02-26 10:37:37 +00:00
|
|
|
|
2014-03-25 11:52:32 +00:00
|
|
|
namespace OC\Repair;
|
|
|
|
|
2015-04-13 14:34:10 +00:00
|
|
|
use OC\Files\Cache\Storage;
|
|
|
|
use OC\RepairException;
|
2016-04-22 13:35:39 +00:00
|
|
|
use OCP\Migration\IOutput;
|
|
|
|
use OCP\Migration\IRepairStep;
|
2014-03-25 11:52:32 +00:00
|
|
|
|
2016-04-22 13:35:39 +00:00
|
|
|
class RepairLegacyStorages implements IRepairStep{
|
2014-03-25 11:52:32 +00:00
|
|
|
/**
|
|
|
|
* @var \OCP\IConfig
|
|
|
|
*/
|
|
|
|
protected $config;
|
|
|
|
|
|
|
|
/**
|
2015-04-13 14:34:10 +00:00
|
|
|
* @var \OCP\IDBConnection
|
2014-03-25 11:52:32 +00:00
|
|
|
*/
|
|
|
|
protected $connection;
|
|
|
|
|
|
|
|
protected $findStorageInCacheStatement;
|
|
|
|
protected $renameStorageStatement;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param \OCP\IConfig $config
|
2015-04-13 14:34:10 +00:00
|
|
|
* @param \OCP\IDBConnection $connection
|
2014-03-25 11:52:32 +00:00
|
|
|
*/
|
|
|
|
public function __construct($config, $connection) {
|
|
|
|
$this->connection = $connection;
|
|
|
|
$this->config = $config;
|
|
|
|
|
|
|
|
$this->findStorageInCacheStatement = $this->connection->prepare(
|
|
|
|
'SELECT DISTINCT `storage` FROM `*PREFIX*filecache`'
|
|
|
|
. ' WHERE `storage` in (?, ?)'
|
|
|
|
);
|
|
|
|
$this->renameStorageStatement = $this->connection->prepare(
|
|
|
|
'UPDATE `*PREFIX*storages`'
|
|
|
|
. ' SET `id` = ?'
|
|
|
|
. ' WHERE `id` = ?'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getName() {
|
|
|
|
return 'Repair legacy storages';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Extracts the user id from a legacy storage id
|
|
|
|
*
|
|
|
|
* @param string $storageId legacy storage id in the
|
|
|
|
* format "local::/path/to/datadir/userid"
|
|
|
|
* @return string user id extracted from the storage id
|
|
|
|
*/
|
|
|
|
private function extractUserId($storageId) {
|
|
|
|
$storageId = rtrim($storageId, '/');
|
|
|
|
$pos = strrpos($storageId, '/');
|
|
|
|
return substr($storageId, $pos + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fix the given legacy storage by renaming the old id
|
|
|
|
* to the new id. If the new id already exists, whichever
|
|
|
|
* storage that has data in the file cache will be used.
|
|
|
|
* If both have data, nothing will be done and false is
|
|
|
|
* returned.
|
|
|
|
*
|
|
|
|
* @param string $oldId old storage id
|
|
|
|
* @param int $oldNumericId old storage numeric id
|
2015-04-13 14:34:10 +00:00
|
|
|
* @param string $userId
|
2014-03-25 11:52:32 +00:00
|
|
|
* @return bool true if fixed, false otherwise
|
2015-04-13 14:34:10 +00:00
|
|
|
* @throws RepairException
|
2014-03-25 11:52:32 +00:00
|
|
|
*/
|
|
|
|
private function fixLegacyStorage($oldId, $oldNumericId, $userId = null) {
|
|
|
|
// check whether the new storage already exists
|
|
|
|
if (is_null($userId)) {
|
|
|
|
$userId = $this->extractUserId($oldId);
|
|
|
|
}
|
|
|
|
$newId = 'home::' . $userId;
|
|
|
|
|
|
|
|
// check if target id already exists
|
2015-04-13 14:34:10 +00:00
|
|
|
$newNumericId = Storage::getNumericStorageId($newId);
|
2014-03-25 11:52:32 +00:00
|
|
|
if (!is_null($newNumericId)) {
|
|
|
|
$newNumericId = (int)$newNumericId;
|
|
|
|
// try and resolve the conflict
|
|
|
|
// check which one of "local::" or "home::" needs to be kept
|
2015-04-18 14:17:15 +00:00
|
|
|
$this->findStorageInCacheStatement->execute(array($oldNumericId, $newNumericId));
|
2014-03-25 11:52:32 +00:00
|
|
|
$row1 = $this->findStorageInCacheStatement->fetch();
|
|
|
|
$row2 = $this->findStorageInCacheStatement->fetch();
|
2014-08-20 21:49:04 +00:00
|
|
|
$this->findStorageInCacheStatement->closeCursor();
|
2014-03-25 11:52:32 +00:00
|
|
|
if ($row2 !== false) {
|
|
|
|
// two results means both storages have data, not auto-fixable
|
2015-04-13 14:34:10 +00:00
|
|
|
throw new RepairException(
|
2014-03-25 11:52:32 +00:00
|
|
|
'Could not automatically fix legacy storage '
|
|
|
|
. '"' . $oldId . '" => "' . $newId . '"'
|
|
|
|
. ' because they both have data.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if ($row1 === false || (int)$row1['storage'] === $oldNumericId) {
|
|
|
|
// old storage has data, then delete the empty new id
|
|
|
|
$toDelete = $newId;
|
|
|
|
} else if ((int)$row1['storage'] === $newNumericId) {
|
|
|
|
// new storage has data, then delete the empty old id
|
|
|
|
$toDelete = $oldId;
|
|
|
|
} else {
|
|
|
|
// unknown case, do not continue
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// delete storage including file cache
|
2015-04-13 14:34:10 +00:00
|
|
|
Storage::remove($toDelete);
|
2014-03-25 11:52:32 +00:00
|
|
|
|
|
|
|
// if we deleted the old id, the new id will be used
|
|
|
|
// automatically
|
|
|
|
if ($toDelete === $oldId) {
|
|
|
|
// nothing more to do
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// rename old id to new id
|
2015-04-13 14:34:10 +00:00
|
|
|
$newId = Storage::adjustStorageId($newId);
|
|
|
|
$oldId = Storage::adjustStorageId($oldId);
|
2014-03-25 11:52:32 +00:00
|
|
|
$rowCount = $this->renameStorageStatement->execute(array($newId, $oldId));
|
2014-08-20 21:49:04 +00:00
|
|
|
$this->renameStorageStatement->closeCursor();
|
2014-03-25 11:52:32 +00:00
|
|
|
return ($rowCount === 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts legacy home storage ids in the format
|
|
|
|
* "local::/data/dir/path/userid/" to the new format "home::userid"
|
|
|
|
*/
|
2016-04-22 13:35:39 +00:00
|
|
|
public function run(IOutput $out) {
|
2014-03-25 11:52:32 +00:00
|
|
|
// only run once
|
|
|
|
if ($this->config->getAppValue('core', 'repairlegacystoragesdone') === 'yes') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$dataDir = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data/');
|
|
|
|
$dataDir = rtrim($dataDir, '/') . '/';
|
|
|
|
$dataDirId = 'local::' . $dataDir;
|
|
|
|
|
|
|
|
$count = 0;
|
2015-02-27 11:44:04 +00:00
|
|
|
$hasWarnings = false;
|
2014-03-25 11:52:32 +00:00
|
|
|
|
|
|
|
$this->connection->beginTransaction();
|
|
|
|
|
2015-01-20 13:25:07 +00:00
|
|
|
// note: not doing a direct UPDATE with the REPLACE function
|
|
|
|
// because regexp search/extract is needed and it is not guaranteed
|
|
|
|
// to work on all database types
|
|
|
|
$sql = 'SELECT `id`, `numeric_id` FROM `*PREFIX*storages`'
|
|
|
|
. ' WHERE `id` LIKE ?'
|
|
|
|
. ' ORDER BY `id`';
|
2016-07-20 12:20:45 +00:00
|
|
|
$result = $this->connection->executeQuery($sql, array($this->connection->escapeLikeParameter($dataDirId) . '%'));
|
2015-01-20 13:25:07 +00:00
|
|
|
|
|
|
|
while ($row = $result->fetch()) {
|
|
|
|
$currentId = $row['id'];
|
|
|
|
// one entry is the datadir itself
|
|
|
|
if ($currentId === $dataDirId) {
|
|
|
|
continue;
|
|
|
|
}
|
2014-03-25 11:52:32 +00:00
|
|
|
|
2015-01-20 13:25:07 +00:00
|
|
|
try {
|
2014-03-25 11:52:32 +00:00
|
|
|
if ($this->fixLegacyStorage($currentId, (int)$row['numeric_id'])) {
|
|
|
|
$count++;
|
|
|
|
}
|
|
|
|
}
|
2015-04-13 14:34:10 +00:00
|
|
|
catch (RepairException $e) {
|
2015-02-27 11:44:04 +00:00
|
|
|
$hasWarnings = true;
|
2016-04-22 13:35:39 +00:00
|
|
|
$out->warning('Could not repair legacy storage ' . $currentId . ' automatically.');
|
2015-01-20 13:25:07 +00:00
|
|
|
}
|
|
|
|
}
|
2014-03-25 11:52:32 +00:00
|
|
|
|
2015-01-20 13:25:07 +00:00
|
|
|
// check for md5 ids, not in the format "prefix::"
|
|
|
|
$sql = 'SELECT COUNT(*) AS "c" FROM `*PREFIX*storages`'
|
|
|
|
. ' WHERE `id` NOT LIKE \'%::%\'';
|
|
|
|
$result = $this->connection->executeQuery($sql);
|
|
|
|
$row = $result->fetch();
|
2015-02-27 11:44:04 +00:00
|
|
|
|
2015-01-20 13:25:07 +00:00
|
|
|
// find at least one to make sure it's worth
|
|
|
|
// querying the user list
|
|
|
|
if ((int)$row['c'] > 0) {
|
2015-04-13 14:34:10 +00:00
|
|
|
$userManager = \OC::$server->getUserManager();
|
2015-01-20 13:25:07 +00:00
|
|
|
|
|
|
|
// use chunks to avoid caching too many users in memory
|
|
|
|
$limit = 30;
|
|
|
|
$offset = 0;
|
|
|
|
|
|
|
|
do {
|
|
|
|
// query the next page of users
|
|
|
|
$results = $userManager->search('', $limit, $offset);
|
|
|
|
$storageIds = array();
|
|
|
|
foreach ($results as $uid => $userObject) {
|
|
|
|
$storageId = $dataDirId . $uid . '/';
|
|
|
|
if (strlen($storageId) <= 64) {
|
|
|
|
// skip short storage ids as they were handled in the previous section
|
|
|
|
continue;
|
2014-03-25 11:52:32 +00:00
|
|
|
}
|
2015-01-20 13:25:07 +00:00
|
|
|
$storageIds[$uid] = $storageId;
|
|
|
|
}
|
2014-03-25 11:52:32 +00:00
|
|
|
|
2015-01-20 13:25:07 +00:00
|
|
|
if (count($storageIds) > 0) {
|
|
|
|
// update the storages of these users
|
|
|
|
foreach ($storageIds as $uid => $storageId) {
|
2015-04-13 14:34:10 +00:00
|
|
|
$numericId = Storage::getNumericStorageId($storageId);
|
2015-01-20 13:25:07 +00:00
|
|
|
try {
|
2014-03-25 11:52:32 +00:00
|
|
|
if (!is_null($numericId) && $this->fixLegacyStorage($storageId, (int)$numericId)) {
|
|
|
|
$count++;
|
|
|
|
}
|
|
|
|
}
|
2015-04-13 14:34:10 +00:00
|
|
|
catch (RepairException $e) {
|
2015-02-27 11:44:04 +00:00
|
|
|
$hasWarnings = true;
|
2016-04-22 13:35:39 +00:00
|
|
|
$out->warning('Could not repair legacy storage ' . $storageId . ' automatically.');
|
2015-01-20 13:25:07 +00:00
|
|
|
}
|
2014-03-25 11:52:32 +00:00
|
|
|
}
|
2015-01-20 13:25:07 +00:00
|
|
|
}
|
|
|
|
$offset += $limit;
|
|
|
|
} while (count($results) >= $limit);
|
|
|
|
}
|
2014-03-25 11:52:32 +00:00
|
|
|
|
2016-04-22 13:35:39 +00:00
|
|
|
$out->info('Updated ' . $count . ' legacy home storage ids');
|
2014-03-25 11:52:32 +00:00
|
|
|
|
2015-01-20 13:25:07 +00:00
|
|
|
$this->connection->commit();
|
2014-03-25 11:52:32 +00:00
|
|
|
|
2016-09-02 13:41:59 +00:00
|
|
|
Storage::getGlobalCache()->clearCache();
|
|
|
|
|
2015-02-27 11:44:04 +00:00
|
|
|
if ($hasWarnings) {
|
2016-04-22 13:35:39 +00:00
|
|
|
$out->warning('Some legacy storages could not be repaired. Please manually fix them then re-run ./occ maintenance:repair');
|
2015-02-27 11:44:04 +00:00
|
|
|
} else {
|
|
|
|
// if all were done, no need to redo the repair during next upgrade
|
|
|
|
$this->config->setAppValue('core', 'repairlegacystoragesdone', 'yes');
|
|
|
|
}
|
2014-03-25 11:52:32 +00:00
|
|
|
}
|
|
|
|
}
|