-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add command to scan trashbin for database inconsistencies #37149
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* @copyright Copyright (c) 2023, Nextcloud, GmbH. | ||
* | ||
* @author Côme Chilliet <come.chilliet@nextcloud.com> | ||
* | ||
* @license AGPL-3.0-or-later | ||
* | ||
* 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/> | ||
* | ||
*/ | ||
|
||
namespace OCA\Files_Trashbin\Command; | ||
|
||
use OC\Core\Command\Base; | ||
use OCP\Files\IRootFolder; | ||
use OCP\IDBConnection; | ||
use OCP\IL10N; | ||
use OCP\IUserBackend; | ||
use OCA\Files_Trashbin\Trashbin; | ||
use OCA\Files_Trashbin\Helper; | ||
use OCP\IUserManager; | ||
use OCP\L10N\IFactory; | ||
use Symfony\Component\Console\Exception\InvalidOptionException; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
class ScanFileSystem extends Base { | ||
protected IUserManager $userManager; | ||
protected IRootFolder $rootFolder; | ||
protected IDBConnection $dbConnection; | ||
protected IL10N $l10n; | ||
|
||
public function __construct( | ||
IRootFolder $rootFolder, | ||
IUserManager $userManager, | ||
IDBConnection $dbConnection, | ||
IFactory $l10nFactory, | ||
) { | ||
parent::__construct(); | ||
$this->userManager = $userManager; | ||
$this->rootFolder = $rootFolder; | ||
$this->dbConnection = $dbConnection; | ||
$this->l10n = $l10nFactory->get('files_trashbin'); | ||
} | ||
|
||
protected function configure(): void { | ||
parent::configure(); | ||
$this | ||
->setName('trashbin:scan') | ||
->setDescription('Rescan trashbin for a user, and fix inconsistencies if needed') | ||
->addArgument( | ||
'user_id', | ||
InputArgument::OPTIONAL | InputArgument::IS_ARRAY, | ||
'scan all deleted files of the given user(s)' | ||
) | ||
->addOption( | ||
'all-users', | ||
null, | ||
InputOption::VALUE_NONE, | ||
'run action on all users' | ||
); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int { | ||
/** @var string[] $users */ | ||
$users = $input->getArgument('user_id'); | ||
if ((!empty($users)) and ($input->getOption('all-users'))) { | ||
throw new InvalidOptionException('Either specify a user_id or --all-users'); | ||
} elseif (!empty($users)) { | ||
foreach ($users as $user) { | ||
if ($this->userManager->userExists($user)) { | ||
$output->writeln("Restoring deleted files for user <info>$user</info>"); | ||
$this->scanDeletedFiles($user, $output); | ||
} else { | ||
$output->writeln("<error>Unknown user $user</error>"); | ||
return 1; | ||
} | ||
} | ||
} elseif ($input->getOption('all-users')) { | ||
$output->writeln('Restoring deleted files for all users'); | ||
foreach ($this->userManager->getBackends() as $backend) { | ||
$name = get_class($backend); | ||
if ($backend instanceof IUserBackend) { | ||
$name = $backend->getBackendName(); | ||
} | ||
$output->writeln("Restoring deleted files for users on backend <info>$name</info>"); | ||
$limit = 500; | ||
$offset = 0; | ||
do { | ||
$users = $backend->getUsers('', $limit, $offset); | ||
foreach ($users as $user) { | ||
$output->writeln("<info>$user</info>"); | ||
$this->scanDeletedFiles($user, $output); | ||
} | ||
$offset += $limit; | ||
} while (count($users) >= $limit); | ||
} | ||
} else { | ||
throw new InvalidOptionException('Either specify a user_id or --all-users'); | ||
} | ||
return 0; | ||
} | ||
|
||
/** | ||
* Scan deleted files for the given user | ||
*/ | ||
protected function scanDeletedFiles(string $uid, OutputInterface $output): void { | ||
\OC_Util::tearDownFS(); | ||
\OC_Util::setupFS($uid); | ||
\OC_User::setUserId($uid); | ||
|
||
$filesInTrash = Helper::getTrashFiles('/', $uid); | ||
$filesInTrashDatabase = Trashbin::getLocations($uid); | ||
Check failure on line 129 in apps/files_trashbin/lib/Command/ScanFileSystem.php GitHub Actions / static-code-analysisUndefinedMethod
|
||
|
||
$trashCount = count($filesInTrash); | ||
$trashCountDatabase = count($filesInTrashDatabase); | ||
if ($trashCount === 0 && $trashCountDatabase === 0) { | ||
$output->writeln("User has no deleted files in the trashbin"); | ||
return; | ||
} | ||
$output->writeln("Preparing to scan <info>$trashCount</info> files..."); | ||
$count = 0; | ||
foreach ($filesInTrash as $trashFile) { | ||
$filename = $trashFile->getName(); | ||
$timestamp = $trashFile->getMtime(); | ||
$humanTime = $this->l10n->l('datetime', $timestamp); | ||
if (isset($filesInTrashDatabase[$filename][$timestamp])) { | ||
$count++; | ||
$output->writeln("File <info>$filename</info> originally deleted at <info>$humanTime</info> is clean"); | ||
unset($filesInTrashDatabase[$filename][$timestamp]); | ||
} else { | ||
$output->writeln("<error>File <info>$filename</info> originally deleted at <info>$humanTime</info> is missing from database</error>"); | ||
} | ||
} | ||
|
||
$output->writeln("Found <info>$count</info> clean files out of <info>$trashCount</info> files."); | ||
|
||
$filesInTrashDatabase = array_filter($filesInTrashDatabase); | ||
|
||
$trashCountDatabase = count($filesInTrashDatabase); | ||
|
||
$output->writeln("Found <info>$trashCountDatabase</info> files in database missing from storage."); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check failure
Code scanning / Psalm
UndefinedMethod Error