|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @copyright Copyright (c) 2021, Louis Chemineau <louis@chmn.me> |
| 4 | + * |
| 5 | + * @author Louis Chemineau <louis@chmn.me> |
| 6 | + * |
| 7 | + * @license AGPL-3.0 |
| 8 | + * |
| 9 | + * This code is free software: you can redistribute it and/or modify |
| 10 | + * it under the terms of the GNU Affero General Public License, version 3, |
| 11 | + * as published by the Free Software Foundation. |
| 12 | + * |
| 13 | + * This program is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | + * GNU Affero General Public License for more details. |
| 17 | + * |
| 18 | + * You should have received a copy of the GNU Affero General Public License, version 3, |
| 19 | + * along with this program. If not, see <http://www.gnu.org/licenses/> |
| 20 | + * |
| 21 | + */ |
| 22 | +namespace OCA\Files\Command; |
| 23 | + |
| 24 | +use Symfony\Component\Console\Helper\Table; |
| 25 | +use Symfony\Component\Console\Input\InputArgument; |
| 26 | +use Symfony\Component\Console\Input\InputInterface; |
| 27 | +use Symfony\Component\Console\Input\InputOption; |
| 28 | +use Symfony\Component\Console\Output\OutputInterface; |
| 29 | +use OC\ForbiddenException; |
| 30 | +use OC\Core\Command\Base; |
| 31 | +use OC\Core\Command\InterruptedException; |
| 32 | +use OC\Files\Search\SearchComparison; |
| 33 | +use OC\Files\Search\SearchOrder; |
| 34 | +use OC\Files\Search\SearchQuery; |
| 35 | +use OCP\Files\Search\ISearchComparison; |
| 36 | +use OCP\Files\Search\ISearchOrder; |
| 37 | +use OCP\Files\NotFoundException; |
| 38 | +use OCP\Files\IRootFolder; |
| 39 | +use OCP\IUserManager; |
| 40 | +use OCP\IDBConnection; |
| 41 | + |
| 42 | +class RepairMtime extends Base { |
| 43 | + private IUserManager $userManager; |
| 44 | + private IRootFolder $rootFolder; |
| 45 | + protected IDBConnection $connection; |
| 46 | + |
| 47 | + protected float $execTime = 0; |
| 48 | + protected int $filesCounter = 0; |
| 49 | + |
| 50 | + public function __construct(IDBConnection $connection, IUserManager $userManager, IRootFolder $rootFolder) { |
| 51 | + $this->connection = $connection; |
| 52 | + $this->userManager = $userManager; |
| 53 | + $this->rootFolder = $rootFolder; |
| 54 | + parent::__construct(); |
| 55 | + } |
| 56 | + |
| 57 | + protected function configure() { |
| 58 | + parent::configure(); |
| 59 | + |
| 60 | + $this |
| 61 | + ->setName('files:repair-mtime') |
| 62 | + ->setDescription('Repair files\' mtime') |
| 63 | + ->addArgument( |
| 64 | + 'user_id', |
| 65 | + InputArgument::OPTIONAL | InputArgument::IS_ARRAY, |
| 66 | + 'will repair mtime for all files of the given user(s)' |
| 67 | + ) |
| 68 | + ->addOption( |
| 69 | + 'all', |
| 70 | + null, |
| 71 | + InputOption::VALUE_NONE, |
| 72 | + 'will repair all files of all known users' |
| 73 | + ) |
| 74 | + ->addOption( |
| 75 | + 'dry-run', |
| 76 | + null, |
| 77 | + InputOption::VALUE_NONE, |
| 78 | + 'will list files instead of repairing them' |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + protected function execute(InputInterface $input, OutputInterface $output): int { |
| 83 | + if ($input->getOption('all')) { |
| 84 | + $users = $this->userManager->search(''); |
| 85 | + } else { |
| 86 | + $users = $input->getArgument('user_id'); |
| 87 | + } |
| 88 | + |
| 89 | + # check quantity of users to be process and show it on the command line |
| 90 | + $users_total = count($users); |
| 91 | + if ($users_total === 0) { |
| 92 | + $output->writeln('<error>Please specify the user id to scan, --all to scan for all users</error>'); |
| 93 | + return 1; |
| 94 | + } |
| 95 | + |
| 96 | + $this->initTools(); |
| 97 | + |
| 98 | + $user_count = 0; |
| 99 | + foreach ($users as $user) { |
| 100 | + if (is_object($user)) { |
| 101 | + $user = $user->getUID(); |
| 102 | + } |
| 103 | + ++$user_count; |
| 104 | + if ($this->userManager->userExists($user)) { |
| 105 | + $this->repairMtimeForUser( |
| 106 | + $user, |
| 107 | + $input->getOption('dry-run'), |
| 108 | + $output, |
| 109 | + ); |
| 110 | + } else { |
| 111 | + $output->writeln("<error>Unknown user $user_count $user</error>"); |
| 112 | + } |
| 113 | + |
| 114 | + try { |
| 115 | + $this->abortIfInterrupted(); |
| 116 | + } catch (InterruptedException $e) { |
| 117 | + break; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + $this->presentStats($output, $input->getOption('dry-run')); |
| 122 | + return 0; |
| 123 | + } |
| 124 | + |
| 125 | + public function repairMtimeForUser(string $userId, bool $dryRun, OutputInterface $output): void { |
| 126 | + $userFolder = $this->rootFolder->getUserFolder($userId); |
| 127 | + $user = $this->userManager->get($userId); |
| 128 | + |
| 129 | + $offset = 0; |
| 130 | + |
| 131 | + do { |
| 132 | + $invalidFiles = $userFolder |
| 133 | + ->search( |
| 134 | + new SearchQuery( |
| 135 | + new SearchComparison(ISearchComparison::COMPARE_LESS_THAN_EQUAL, 'mtime', 86400), |
| 136 | + 0, // 0 = no limits. |
| 137 | + $offset, |
| 138 | + [new SearchOrder(ISearchOrder::DIRECTION_DESCENDING, 'mtime')], |
| 139 | + $user |
| 140 | + ) |
| 141 | + ); |
| 142 | + |
| 143 | + $offset += count($invalidFiles); |
| 144 | + |
| 145 | + $this->connection->beginTransaction(); |
| 146 | + |
| 147 | + foreach ($invalidFiles as $file) { |
| 148 | + $this->filesCounter++; |
| 149 | + |
| 150 | + try { |
| 151 | + $filePath = $file->getPath(); |
| 152 | + $fileId = $file->getId(); |
| 153 | + $fileStorage = $file->getStorage(); |
| 154 | + |
| 155 | + // Default new mtime to the current time. |
| 156 | + $mtime = null; |
| 157 | + |
| 158 | + if ($fileStorage->instanceOfStorage("OC\Files\ObjectStore\ObjectStoreStorage")) { |
| 159 | + // Get LastModified property for S3 as primary storage. |
| 160 | + /** @var \OC\Files\ObjectStore\ObjectStoreStorage $fileStorage */ |
| 161 | + $headResult = $fileStorage->getObjectStore()->headObject("urn:oid:$fileId"); |
| 162 | + $date = \DateTime::createFromFormat(\DateTimeInterface::ISO8601, $headResult->get('LastModified')); |
| 163 | + $mtime = $date->getTimestamp(); |
| 164 | + } elseif ($file->getStorage()->instanceOfStorage("OCA\Files_External\Lib\Storage\AmazonS3")) { |
| 165 | + // Get LastModified property for S3 as external storage. |
| 166 | + /** @var \OCA\Files_External\Lib\Storage\AmazonS3 $fileStorage */ |
| 167 | + $headResult = $fileStorage->headObject("urn:oid:$fileId"); |
| 168 | + $date = \DateTime::createFromFormat(\DateTimeInterface::ISO8601, $headResult['LastModified']); |
| 169 | + $mtime = $date->getTimestamp(); |
| 170 | + } |
| 171 | + |
| 172 | + if ($dryRun) { |
| 173 | + $output->writeln("- Found '$filePath', would set the mtime to $mtime.", OutputInterface::VERBOSITY_VERBOSE); |
| 174 | + } else { |
| 175 | + $file->touch($mtime); |
| 176 | + $output->writeln("- Fixed $filePath", OutputInterface::VERBOSITY_VERBOSE); |
| 177 | + } |
| 178 | + } catch (ForbiddenException $e) { |
| 179 | + $output->writeln("<error>Home storage for user $userId not writable</error>"); |
| 180 | + $output->writeln('Make sure you\'re running the scan command only as the user the web server runs as'); |
| 181 | + } catch (InterruptedException $e) { |
| 182 | + # exit the function if ctrl-c has been pressed |
| 183 | + $output->writeln('Interrupted by user'); |
| 184 | + } catch (NotFoundException $e) { |
| 185 | + $output->writeln('<error>Path not found: ' . $e->getMessage() . '</error>'); |
| 186 | + } catch (\Exception $e) { |
| 187 | + $output->writeln('<error>Exception during scan: ' . $e->getMessage() . '</error>'); |
| 188 | + $output->writeln('<error>' . $e->getTraceAsString() . '</error>'); |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + $this->connection->commit(); |
| 193 | + } while (count($invalidFiles) > 0); |
| 194 | + } |
| 195 | + |
| 196 | + /** |
| 197 | + * Initialises some useful tools for the Command |
| 198 | + */ |
| 199 | + protected function initTools(): void { |
| 200 | + // Start the timer |
| 201 | + $this->execTime = -microtime(true); |
| 202 | + // Convert PHP errors to exceptions |
| 203 | + set_error_handler([$this, 'exceptionErrorHandler'], E_ALL); |
| 204 | + } |
| 205 | + |
| 206 | + /** |
| 207 | + * Processes PHP errors as exceptions in order to be able to keep track of problems |
| 208 | + * |
| 209 | + * @see https://www.php.net/manual/en/function.set-error-handler.php |
| 210 | + * |
| 211 | + * @param int $severity the level of the error raised |
| 212 | + * @param string $message |
| 213 | + * @param string $file the filename that the error was raised in |
| 214 | + * @param int $line the line number the error was raised |
| 215 | + * |
| 216 | + * @throws \ErrorException |
| 217 | + */ |
| 218 | + public function exceptionErrorHandler(int $severity, string $message, string $file, int $line) { |
| 219 | + if (!(error_reporting() & $severity)) { |
| 220 | + // This error code is not included in error_reporting |
| 221 | + return; |
| 222 | + } |
| 223 | + throw new \ErrorException($message, 0, $severity, $file, $line); |
| 224 | + } |
| 225 | + |
| 226 | + protected function presentStats(OutputInterface $output, bool $dryRun) { |
| 227 | + // Stop the timer |
| 228 | + $this->execTime += microtime(true); |
| 229 | + |
| 230 | + $columnName = 'Fixed files'; |
| 231 | + if ($dryRun) { |
| 232 | + $columnName = 'Found files'; |
| 233 | + } |
| 234 | + |
| 235 | + $table = new Table($output); |
| 236 | + $table |
| 237 | + ->setHeaders([$columnName, 'Elapsed time']) |
| 238 | + ->setRows([[$this->filesCounter, $this->formatExecTime()]]) |
| 239 | + ->render(); |
| 240 | + } |
| 241 | + |
| 242 | + /** |
| 243 | + * Formats microtime into a human readable format |
| 244 | + */ |
| 245 | + protected function formatExecTime(): string { |
| 246 | + $secs = round($this->execTime); |
| 247 | + # convert seconds into HH:MM:SS form |
| 248 | + return sprintf('%02d:%02d:%02d', ($secs / 3600), ($secs / 60 % 60), $secs % 60); |
| 249 | + } |
| 250 | +} |
0 commit comments