RepairLegacyStorages.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Aaron Wood <aaronjwood@gmail.com>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Thomas Müller <thomas.mueller@tmit.eu>
  9. * @author Vincent Petry <pvince81@owncloud.com>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OC\Repair;
  27. use OC\Files\Cache\Storage;
  28. use OC\RepairException;
  29. use OCP\Migration\IOutput;
  30. use OCP\Migration\IRepairStep;
  31. class RepairLegacyStorages implements IRepairStep{
  32. /**
  33. * @var \OCP\IConfig
  34. */
  35. protected $config;
  36. /**
  37. * @var \OCP\IDBConnection
  38. */
  39. protected $connection;
  40. protected $findStorageInCacheStatement;
  41. protected $renameStorageStatement;
  42. /**
  43. * @param \OCP\IConfig $config
  44. * @param \OCP\IDBConnection $connection
  45. */
  46. public function __construct($config, $connection) {
  47. $this->connection = $connection;
  48. $this->config = $config;
  49. $this->findStorageInCacheStatement = $this->connection->prepare(
  50. 'SELECT DISTINCT `storage` FROM `*PREFIX*filecache`'
  51. . ' WHERE `storage` in (?, ?)'
  52. );
  53. $this->renameStorageStatement = $this->connection->prepare(
  54. 'UPDATE `*PREFIX*storages`'
  55. . ' SET `id` = ?'
  56. . ' WHERE `id` = ?'
  57. );
  58. }
  59. public function getName() {
  60. return 'Repair legacy storages';
  61. }
  62. /**
  63. * Extracts the user id from a legacy storage id
  64. *
  65. * @param string $storageId legacy storage id in the
  66. * format "local::/path/to/datadir/userid"
  67. * @return string user id extracted from the storage id
  68. */
  69. private function extractUserId($storageId) {
  70. $storageId = rtrim($storageId, '/');
  71. $pos = strrpos($storageId, '/');
  72. return substr($storageId, $pos + 1);
  73. }
  74. /**
  75. * Fix the given legacy storage by renaming the old id
  76. * to the new id. If the new id already exists, whichever
  77. * storage that has data in the file cache will be used.
  78. * If both have data, nothing will be done and false is
  79. * returned.
  80. *
  81. * @param string $oldId old storage id
  82. * @param int $oldNumericId old storage numeric id
  83. * @param string $userId
  84. * @return bool true if fixed, false otherwise
  85. * @throws RepairException
  86. */
  87. private function fixLegacyStorage($oldId, $oldNumericId, $userId = null) {
  88. // check whether the new storage already exists
  89. if (is_null($userId)) {
  90. $userId = $this->extractUserId($oldId);
  91. }
  92. $newId = 'home::' . $userId;
  93. // check if target id already exists
  94. $newNumericId = Storage::getNumericStorageId($newId);
  95. if (!is_null($newNumericId)) {
  96. $newNumericId = (int)$newNumericId;
  97. // try and resolve the conflict
  98. // check which one of "local::" or "home::" needs to be kept
  99. $this->findStorageInCacheStatement->execute(array($oldNumericId, $newNumericId));
  100. $row1 = $this->findStorageInCacheStatement->fetch();
  101. $row2 = $this->findStorageInCacheStatement->fetch();
  102. $this->findStorageInCacheStatement->closeCursor();
  103. if ($row2 !== false) {
  104. // two results means both storages have data, not auto-fixable
  105. throw new RepairException(
  106. 'Could not automatically fix legacy storage '
  107. . '"' . $oldId . '" => "' . $newId . '"'
  108. . ' because they both have data.'
  109. );
  110. }
  111. if ($row1 === false || (int)$row1['storage'] === $oldNumericId) {
  112. // old storage has data, then delete the empty new id
  113. $toDelete = $newId;
  114. } else if ((int)$row1['storage'] === $newNumericId) {
  115. // new storage has data, then delete the empty old id
  116. $toDelete = $oldId;
  117. } else {
  118. // unknown case, do not continue
  119. return false;
  120. }
  121. // delete storage including file cache
  122. Storage::remove($toDelete);
  123. // if we deleted the old id, the new id will be used
  124. // automatically
  125. if ($toDelete === $oldId) {
  126. // nothing more to do
  127. return true;
  128. }
  129. }
  130. // rename old id to new id
  131. $newId = Storage::adjustStorageId($newId);
  132. $oldId = Storage::adjustStorageId($oldId);
  133. $rowCount = $this->renameStorageStatement->execute(array($newId, $oldId));
  134. $this->renameStorageStatement->closeCursor();
  135. return ($rowCount === 1);
  136. }
  137. /**
  138. * Converts legacy home storage ids in the format
  139. * "local::/data/dir/path/userid/" to the new format "home::userid"
  140. */
  141. public function run(IOutput $out) {
  142. // only run once
  143. if ($this->config->getAppValue('core', 'repairlegacystoragesdone') === 'yes') {
  144. return;
  145. }
  146. $dataDir = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data/');
  147. $dataDir = rtrim($dataDir, '/') . '/';
  148. $dataDirId = 'local::' . $dataDir;
  149. $count = 0;
  150. $hasWarnings = false;
  151. $this->connection->beginTransaction();
  152. // note: not doing a direct UPDATE with the REPLACE function
  153. // because regexp search/extract is needed and it is not guaranteed
  154. // to work on all database types
  155. $sql = 'SELECT `id`, `numeric_id` FROM `*PREFIX*storages`'
  156. . ' WHERE `id` LIKE ?'
  157. . ' ORDER BY `id`';
  158. $result = $this->connection->executeQuery($sql, array($this->connection->escapeLikeParameter($dataDirId) . '%'));
  159. while ($row = $result->fetch()) {
  160. $currentId = $row['id'];
  161. // one entry is the datadir itself
  162. if ($currentId === $dataDirId) {
  163. continue;
  164. }
  165. try {
  166. if ($this->fixLegacyStorage($currentId, (int)$row['numeric_id'])) {
  167. $count++;
  168. }
  169. }
  170. catch (RepairException $e) {
  171. $hasWarnings = true;
  172. $out->warning('Could not repair legacy storage ' . $currentId . ' automatically.');
  173. }
  174. }
  175. // check for md5 ids, not in the format "prefix::"
  176. $sql = 'SELECT COUNT(*) AS "c" FROM `*PREFIX*storages`'
  177. . ' WHERE `id` NOT LIKE \'%::%\'';
  178. $result = $this->connection->executeQuery($sql);
  179. $row = $result->fetch();
  180. // find at least one to make sure it's worth
  181. // querying the user list
  182. if ((int)$row['c'] > 0) {
  183. $userManager = \OC::$server->getUserManager();
  184. // use chunks to avoid caching too many users in memory
  185. $limit = 30;
  186. $offset = 0;
  187. do {
  188. // query the next page of users
  189. $results = $userManager->search('', $limit, $offset);
  190. $storageIds = array();
  191. foreach ($results as $uid => $userObject) {
  192. $storageId = $dataDirId . $uid . '/';
  193. if (strlen($storageId) <= 64) {
  194. // skip short storage ids as they were handled in the previous section
  195. continue;
  196. }
  197. $storageIds[$uid] = $storageId;
  198. }
  199. if (count($storageIds) > 0) {
  200. // update the storages of these users
  201. foreach ($storageIds as $uid => $storageId) {
  202. $numericId = Storage::getNumericStorageId($storageId);
  203. try {
  204. if (!is_null($numericId) && $this->fixLegacyStorage($storageId, (int)$numericId)) {
  205. $count++;
  206. }
  207. }
  208. catch (RepairException $e) {
  209. $hasWarnings = true;
  210. $out->warning('Could not repair legacy storage ' . $storageId . ' automatically.');
  211. }
  212. }
  213. }
  214. $offset += $limit;
  215. } while (count($results) >= $limit);
  216. }
  217. $out->info('Updated ' . $count . ' legacy home storage ids');
  218. $this->connection->commit();
  219. if ($hasWarnings) {
  220. $out->warning('Some legacy storages could not be repaired. Please manually fix them then re-run ./occ maintenance:repair');
  221. } else {
  222. // if all were done, no need to redo the repair during next upgrade
  223. $this->config->setAppValue('core', 'repairlegacystoragesdone', 'yes');
  224. }
  225. }
  226. }