Repair.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2020, Morris Jobke <hey@morrisjobke.de>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OC\Core\Command\Preview;
  26. use bantu\IniGetWrapper\IniGetWrapper;
  27. use OC\Preview\Storage\Root;
  28. use OCP\Files\Folder;
  29. use OCP\Files\IRootFolder;
  30. use OCP\Files\NotFoundException;
  31. use OCP\IConfig;
  32. use OCP\Lock\ILockingProvider;
  33. use OCP\Lock\LockedException;
  34. use Psr\Log\LoggerInterface;
  35. use Symfony\Component\Console\Command\Command;
  36. use Symfony\Component\Console\Helper\ProgressBar;
  37. use Symfony\Component\Console\Input\InputInterface;
  38. use Symfony\Component\Console\Input\InputOption;
  39. use Symfony\Component\Console\Output\OutputInterface;
  40. use Symfony\Component\Console\Question\ConfirmationQuestion;
  41. use function pcntl_signal;
  42. class Repair extends Command {
  43. private bool $stopSignalReceived = false;
  44. private int $memoryLimit;
  45. private int $memoryTreshold;
  46. public function __construct(
  47. protected IConfig $config,
  48. private IRootFolder $rootFolder,
  49. private LoggerInterface $logger,
  50. IniGetWrapper $phpIni,
  51. private ILockingProvider $lockingProvider,
  52. ) {
  53. $this->memoryLimit = (int)$phpIni->getBytes('memory_limit');
  54. $this->memoryTreshold = $this->memoryLimit - 25 * 1024 * 1024;
  55. parent::__construct();
  56. }
  57. protected function configure() {
  58. $this
  59. ->setName('preview:repair')
  60. ->setDescription('distributes the existing previews into subfolders')
  61. ->addOption('batch', 'b', InputOption::VALUE_NONE, 'Batch mode - will not ask to start the migration and start it right away.')
  62. ->addOption('dry', 'd', InputOption::VALUE_NONE, 'Dry mode - will not create, move or delete any files - in combination with the verbose mode one could check the operations.')
  63. ->addOption('delete', null, InputOption::VALUE_NONE, 'Delete instead of migrating them. Usefull if too many entries to migrate.');
  64. }
  65. protected function execute(InputInterface $input, OutputInterface $output): int {
  66. if ($this->memoryLimit !== -1) {
  67. $limitInMiB = round($this->memoryLimit / 1024 / 1024, 1);
  68. $thresholdInMiB = round($this->memoryTreshold / 1024 / 1024, 1);
  69. $output->writeln("Memory limit is $limitInMiB MiB");
  70. $output->writeln("Memory threshold is $thresholdInMiB MiB");
  71. $output->writeln("");
  72. $memoryCheckEnabled = true;
  73. } else {
  74. $output->writeln("No memory limit in place - disabled memory check. Set a PHP memory limit to automatically stop the execution of this migration script once memory consumption is close to this limit.");
  75. $output->writeln("");
  76. $memoryCheckEnabled = false;
  77. }
  78. $dryMode = $input->getOption('dry');
  79. $deleteMode = $input->getOption('delete');
  80. if ($dryMode) {
  81. $output->writeln("INFO: The migration is run in dry mode and will not modify anything.");
  82. $output->writeln("");
  83. } elseif ($deleteMode) {
  84. $output->writeln("WARN: The migration will _DELETE_ old previews.");
  85. $output->writeln("");
  86. }
  87. $instanceId = $this->config->getSystemValueString('instanceid');
  88. $output->writeln("This will migrate all previews from the old preview location to the new one.");
  89. $output->writeln('');
  90. $output->writeln('Fetching previews that need to be migrated …');
  91. /** @var \OCP\Files\Folder $currentPreviewFolder */
  92. $currentPreviewFolder = $this->rootFolder->get("appdata_$instanceId/preview");
  93. $directoryListing = $currentPreviewFolder->getDirectoryListing();
  94. $total = count($directoryListing);
  95. /**
  96. * by default there could be 0-9 a-f and the old-multibucket folder which are all fine
  97. */
  98. if ($total < 18) {
  99. $directoryListing = array_filter($directoryListing, function ($dir) {
  100. if ($dir->getName() === 'old-multibucket') {
  101. return false;
  102. }
  103. // a-f can't be a file ID -> removing from migration
  104. if (preg_match('!^[a-f]$!', $dir->getName())) {
  105. return false;
  106. }
  107. if (preg_match('!^[0-9]$!', $dir->getName())) {
  108. // ignore folders that only has folders in them
  109. if ($dir instanceof Folder) {
  110. foreach ($dir->getDirectoryListing() as $entry) {
  111. if (!$entry instanceof Folder) {
  112. return true;
  113. }
  114. }
  115. return false;
  116. }
  117. }
  118. return true;
  119. });
  120. $total = count($directoryListing);
  121. }
  122. if ($total === 0) {
  123. $output->writeln("All previews are already migrated.");
  124. return 0;
  125. }
  126. $output->writeln("A total of $total preview files need to be migrated.");
  127. $output->writeln("");
  128. $output->writeln("The migration will always migrate all previews of a single file in a batch. After each batch the process can be canceled by pressing CTRL-C. This will finish the current batch and then stop the migration. This migration can then just be started and it will continue.");
  129. if ($input->getOption('batch')) {
  130. $output->writeln('Batch mode active: migration is started right away.');
  131. } else {
  132. $helper = $this->getHelper('question');
  133. $question = new ConfirmationQuestion('<info>Should the migration be started? (y/[n]) </info>', false);
  134. if (!$helper->ask($input, $output, $question)) {
  135. return 0;
  136. }
  137. }
  138. // register the SIGINT listener late in here to be able to exit in the early process of this command
  139. pcntl_signal(SIGINT, [$this, 'sigIntHandler']);
  140. $output->writeln("");
  141. $output->writeln("");
  142. $section1 = $output->section();
  143. $section2 = $output->section();
  144. $progressBar = new ProgressBar($section2, $total);
  145. $progressBar->setFormat("%current%/%max% [%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s% Used Memory: %memory:6s%");
  146. $time = (new \DateTime())->format('H:i:s');
  147. $progressBar->setMessage("$time Starting …");
  148. $progressBar->maxSecondsBetweenRedraws(0.2);
  149. $progressBar->start();
  150. foreach ($directoryListing as $oldPreviewFolder) {
  151. pcntl_signal_dispatch();
  152. $name = $oldPreviewFolder->getName();
  153. $time = (new \DateTime())->format('H:i:s');
  154. $section1->writeln("$time Migrating previews of file with fileId $name …");
  155. $progressBar->display();
  156. if ($this->stopSignalReceived) {
  157. $section1->writeln("$time Stopping migration …");
  158. return 0;
  159. }
  160. if (!$oldPreviewFolder instanceof Folder) {
  161. $section1->writeln(" Skipping non-folder $name …");
  162. $progressBar->advance();
  163. continue;
  164. }
  165. if ($name === 'old-multibucket') {
  166. $section1->writeln(" Skipping fallback mount point $name …");
  167. $progressBar->advance();
  168. continue;
  169. }
  170. if (in_array($name, ['a', 'b', 'c', 'd', 'e', 'f'])) {
  171. $section1->writeln(" Skipping hex-digit folder $name …");
  172. $progressBar->advance();
  173. continue;
  174. }
  175. if (!preg_match('!^\d+$!', $name)) {
  176. $section1->writeln(" Skipping non-numeric folder $name …");
  177. $progressBar->advance();
  178. continue;
  179. }
  180. $newFoldername = Root::getInternalFolder($name);
  181. $memoryUsage = memory_get_usage();
  182. if ($memoryCheckEnabled && $memoryUsage > $this->memoryTreshold) {
  183. $section1->writeln("");
  184. $section1->writeln("");
  185. $section1->writeln("");
  186. $section1->writeln(" Stopped process 25 MB before reaching the memory limit to avoid a hard crash.");
  187. $time = (new \DateTime())->format('H:i:s');
  188. $section1->writeln("$time Reached memory limit and stopped to avoid hard crash.");
  189. return 1;
  190. }
  191. $lockName = 'occ preview:repair lock ' . $oldPreviewFolder->getId();
  192. try {
  193. $section1->writeln(" Locking \"$lockName\" …", OutputInterface::VERBOSITY_VERBOSE);
  194. $this->lockingProvider->acquireLock($lockName, ILockingProvider::LOCK_EXCLUSIVE);
  195. } catch (LockedException $e) {
  196. $section1->writeln(" Skipping because it is locked - another process seems to work on this …");
  197. continue;
  198. }
  199. $previews = $oldPreviewFolder->getDirectoryListing();
  200. if ($previews !== []) {
  201. try {
  202. $this->rootFolder->get("appdata_$instanceId/preview/$newFoldername");
  203. } catch (NotFoundException $e) {
  204. $section1->writeln(" Create folder preview/$newFoldername", OutputInterface::VERBOSITY_VERBOSE);
  205. if (!$dryMode) {
  206. $this->rootFolder->newFolder("appdata_$instanceId/preview/$newFoldername");
  207. }
  208. }
  209. foreach ($previews as $preview) {
  210. pcntl_signal_dispatch();
  211. $previewName = $preview->getName();
  212. if ($preview instanceof Folder) {
  213. $section1->writeln(" Skipping folder $name/$previewName …");
  214. $progressBar->advance();
  215. continue;
  216. }
  217. // Execute process
  218. if (!$dryMode) {
  219. // Delete preview instead of moving
  220. if ($deleteMode) {
  221. try {
  222. $section1->writeln(" Delete preview/$name/$previewName", OutputInterface::VERBOSITY_VERBOSE);
  223. $preview->delete();
  224. } catch (\Exception $e) {
  225. $this->logger->error("Failed to delete preview at preview/$name/$previewName", [
  226. 'app' => 'core',
  227. 'exception' => $e,
  228. ]);
  229. }
  230. } else {
  231. try {
  232. $section1->writeln(" Move preview/$name/$previewName to preview/$newFoldername", OutputInterface::VERBOSITY_VERBOSE);
  233. $preview->move("appdata_$instanceId/preview/$newFoldername/$previewName");
  234. } catch (\Exception $e) {
  235. $this->logger->error("Failed to move preview from preview/$name/$previewName to preview/$newFoldername", [
  236. 'app' => 'core',
  237. 'exception' => $e,
  238. ]);
  239. }
  240. }
  241. }
  242. }
  243. }
  244. if ($oldPreviewFolder->getDirectoryListing() === []) {
  245. $section1->writeln(" Delete empty folder preview/$name", OutputInterface::VERBOSITY_VERBOSE);
  246. if (!$dryMode) {
  247. try {
  248. $oldPreviewFolder->delete();
  249. } catch (\Exception $e) {
  250. $this->logger->error("Failed to delete empty folder preview/$name", [
  251. 'app' => 'core',
  252. 'exception' => $e,
  253. ]);
  254. }
  255. }
  256. }
  257. $this->lockingProvider->releaseLock($lockName, ILockingProvider::LOCK_EXCLUSIVE);
  258. $section1->writeln(" Unlocked", OutputInterface::VERBOSITY_VERBOSE);
  259. $section1->writeln(" Finished migrating previews of file with fileId $name …");
  260. $progressBar->advance();
  261. }
  262. $progressBar->finish();
  263. $output->writeln("");
  264. return 0;
  265. }
  266. protected function sigIntHandler() {
  267. echo "\nSignal received - will finish the step and then stop the migration.\n\n\n";
  268. $this->stopSignalReceived = true;
  269. }
  270. }