-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
[stable33] Add repair step for share targets with excess (2) #57858
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 hidden or 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,128 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
| /** | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OCA\Files_Sharing\Repair; | ||
|
|
||
| use OC\Files\SetupManager; | ||
| use OCA\Files_Sharing\ShareTargetValidator; | ||
| use OCP\DB\QueryBuilder\IQueryBuilder; | ||
| use OCP\Files\Mount\IMountManager; | ||
| use OCP\IDBConnection; | ||
| use OCP\IUserManager; | ||
| use OCP\Migration\IOutput; | ||
| use OCP\Migration\IRepairStep; | ||
| use OCP\Share\IManager; | ||
| use OCP\Share\IProviderFactory; | ||
| use OCP\Share\IShare; | ||
|
|
||
| class CleanupShareTarget implements IRepairStep { | ||
| /** we only care about shares with a user target, | ||
| * since the underling group/deck/talk share doesn't get moved | ||
| */ | ||
| private const USER_SHARE_TYPES = [ | ||
| IShare::TYPE_USER, | ||
| IShare::TYPE_USERGROUP, | ||
| IShare::TYPE_DECK_USER, | ||
| 11 // TYPE_USERROOM | ||
| ]; | ||
|
|
||
| public function __construct( | ||
| private readonly IDBConnection $connection, | ||
| private readonly IManager $shareManager, | ||
| private readonly IProviderFactory $shareProviderFactory, | ||
| private readonly ShareTargetValidator $shareTargetValidator, | ||
| private readonly IUserManager $userManager, | ||
| private readonly SetupManager $setupManager, | ||
| private readonly IMountManager $mountManager, | ||
| ) { | ||
| } | ||
|
|
||
| #[\Override] | ||
| public function getName() { | ||
| return 'Cleanup share names with false conflicts'; | ||
| } | ||
|
|
||
| #[\Override] | ||
| public function run(IOutput $output) { | ||
| $count = $this->countProblemShares(); | ||
| if ($count === 0) { | ||
| return; | ||
| } | ||
| $output->startProgress($count); | ||
|
|
||
| $lastUser = ''; | ||
| $userMounts = []; | ||
|
|
||
| foreach ($this->getProblemShares() as $shareInfo) { | ||
| $recipient = $this->userManager->getExistingUser($shareInfo['share_with']); | ||
| $share = $this->shareProviderFactory | ||
| ->getProviderForType((int)$shareInfo['share_type']) | ||
| ->getShareById($shareInfo['id'], $recipient->getUID()); | ||
|
|
||
| // since we ordered the share by user, we can reuse the last data until we get to the next user | ||
| if ($lastUser !== $recipient->getUID()) { | ||
| $lastUser = $recipient->getUID(); | ||
|
|
||
| $this->setupManager->tearDown(); | ||
| $this->setupManager->setupForUser($recipient); | ||
| $userMounts = $this->mountManager->getAll(); | ||
| } | ||
|
|
||
| $oldTarget = $share->getTarget(); | ||
| $newTarget = $this->cleanTarget($oldTarget); | ||
| $share->setTarget($newTarget); | ||
| $this->shareManager->moveShare($share, $recipient->getUID()); | ||
|
|
||
| $this->shareTargetValidator->verifyMountPoint( | ||
artonge marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| $recipient, | ||
| $share, | ||
| $userMounts, | ||
| [$share], | ||
| ); | ||
|
|
||
| $oldMountPoint = "/{$recipient->getUID()}/files$oldTarget/"; | ||
| $newMountPoint = "/{$recipient->getUID()}/files$newTarget/"; | ||
| $userMounts[$newMountPoint] = $userMounts[$oldMountPoint]; | ||
| unset($userMounts[$oldMountPoint]); | ||
|
|
||
| $output->advance(); | ||
| } | ||
| $output->finishProgress(); | ||
| $output->info("Fixed $count shares"); | ||
| } | ||
|
|
||
| private function countProblemShares(): int { | ||
| $query = $this->connection->getQueryBuilder(); | ||
| $query->select($query->func()->count('id')) | ||
| ->from('share') | ||
| ->where($query->expr()->like('file_target', $query->createNamedParameter('% (_) (_)%'))) | ||
| ->andWhere($query->expr()->in('share_type', $query->createNamedParameter(self::USER_SHARE_TYPES, IQueryBuilder::PARAM_INT_ARRAY), IQueryBuilder::PARAM_INT_ARRAY)); | ||
| return (int)$query->executeQuery()->fetchOne(); | ||
| } | ||
|
|
||
| /** | ||
| * @return \Traversable<array{id: string, share_type: string, share_with: string}> | ||
| */ | ||
| private function getProblemShares(): \Traversable { | ||
| $query = $this->connection->getQueryBuilder(); | ||
| $query->select('id', 'share_type', 'share_with') | ||
| ->from('share') | ||
| ->where($query->expr()->like('file_target', $query->createNamedParameter('% (_) (_)%'))) | ||
| ->andWhere($query->expr()->in('share_type', $query->createNamedParameter(self::USER_SHARE_TYPES, IQueryBuilder::PARAM_INT_ARRAY), IQueryBuilder::PARAM_INT_ARRAY)) | ||
| ->orderBy('share_with') | ||
| ->addOrderBy('id'); | ||
come-nc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| $result = $query->executeQuery(); | ||
| /** @var \Traversable<array{id: string, share_type: string, share_with: string}> $rows */ | ||
| $rows = $result->iterateAssociative(); | ||
| return $rows; | ||
| } | ||
|
|
||
| private function cleanTarget(string $target): string { | ||
| return preg_replace('/( \([2-9]\)){2,}/', '', $target); | ||
| } | ||
| } | ||
114 changes: 114 additions & 0 deletions
114
apps/files_sharing/tests/Repair/CleanupShareTargetTest.php
This file contains hidden or 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,114 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2026 | ||
| * SPDX-License-Identifier: AGPL-3.0-only | ||
| */ | ||
| namespace OCA\Files_Sharing\Tests\Repair; | ||
|
|
||
| use OC\Migration\NullOutput; | ||
| use OCA\Files_Sharing\Repair\CleanupShareTarget; | ||
| use OCA\Files_Sharing\Tests\TestCase; | ||
| use OCP\Files\NotFoundException; | ||
| use OCP\Server; | ||
| use OCP\Share\IShare; | ||
| use PHPUnit\Framework\Attributes\Group; | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
| #[Group(name: 'DB')] | ||
| class CleanupShareTargetTest extends TestCase { | ||
| public const TEST_FOLDER_NAME = '/folder_share_api_test'; | ||
|
|
||
| private CleanupShareTarget $cleanupShareTarget; | ||
|
|
||
| protected function setUp(): void { | ||
| parent::setUp(); | ||
| $this->cleanupShareTarget = Server::get(CleanupShareTarget::class); | ||
| } | ||
|
|
||
| private function createUserShare(string $by, string $target = self::TEST_FOLDER_NAME): IShare { | ||
| $userFolder = $this->rootFolder->getUserFolder($by); | ||
|
|
||
| try { | ||
| $node = $userFolder->get(self::TEST_FOLDER_NAME); | ||
| } catch (NotFoundException $e) { | ||
| $node = $userFolder->newFolder(self::TEST_FOLDER_NAME); | ||
| } | ||
| $share1 = $this->shareManager->newShare(); | ||
| $share1->setNode($node) | ||
| ->setSharedBy($by) | ||
| ->setSharedWith(self::TEST_FILES_SHARING_API_USER2) | ||
| ->setShareType(IShare::TYPE_USER) | ||
| ->setPermissions(31); | ||
| $share = $this->shareManager->createShare($share1); | ||
| $share->setStatus(IShare::STATUS_ACCEPTED); | ||
| $this->shareManager->updateShare($share); | ||
|
|
||
| $share->setTarget($target); | ||
| $this->shareManager->moveShare($share, self::TEST_FILES_SHARING_API_USER2); | ||
|
|
||
| $share = $this->shareManager->getShareById($share->getFullId()); | ||
| $this->assertEquals($target, $share->getTarget()); | ||
|
|
||
| return $share; | ||
| } | ||
|
|
||
| public function testBasicRepair() { | ||
| $share = $this->createUserShare(self::TEST_FILES_SHARING_API_USER1, self::TEST_FOLDER_NAME . ' (2) (2) (2) (2)'); | ||
|
|
||
| $this->cleanupShareTarget->run(new NullOutput()); | ||
|
|
||
| $share = $this->shareManager->getShareById($share->getFullId()); | ||
| $this->assertEquals(self::TEST_FOLDER_NAME, $share->getTarget()); | ||
| } | ||
|
|
||
| public function testRepairConflictFile() { | ||
| $share = $this->createUserShare(self::TEST_FILES_SHARING_API_USER1, self::TEST_FOLDER_NAME . ' (2) (2) (2) (2)'); | ||
|
|
||
| $userFolder2 = $this->rootFolder->getUserFolder(self::TEST_FILES_SHARING_API_USER2); | ||
| $folder = $userFolder2->newFolder(self::TEST_FOLDER_NAME); | ||
|
|
||
| $this->cleanupShareTarget->run(new NullOutput()); | ||
| $folder->delete(); | ||
|
|
||
| $share = $this->shareManager->getShareById($share->getFullId()); | ||
| $this->assertEquals(self::TEST_FOLDER_NAME . ' (2)', $share->getTarget()); | ||
| } | ||
|
|
||
| public function testRepairConflictShare() { | ||
| $share = $this->createUserShare(self::TEST_FILES_SHARING_API_USER1, self::TEST_FOLDER_NAME . ' (2) (2) (2) (2)'); | ||
|
|
||
| $share2 = $this->createUserShare(self::TEST_FILES_SHARING_API_USER3); | ||
|
|
||
| $this->cleanupShareTarget->run(new NullOutput()); | ||
|
|
||
| $share2 = $this->shareManager->getShareById($share2->getFullId()); | ||
| $this->assertEquals(self::TEST_FOLDER_NAME, $share2->getTarget()); | ||
| $share = $this->shareManager->getShareById($share->getFullId()); | ||
| $this->assertEquals(self::TEST_FOLDER_NAME . ' (2)', $share->getTarget()); | ||
| } | ||
|
|
||
| public function testRepairMultipleConflicting() { | ||
| $share = $this->createUserShare(self::TEST_FILES_SHARING_API_USER1, self::TEST_FOLDER_NAME . ' (2) (2) (2) (2)'); | ||
| $share2 = $this->createUserShare(self::TEST_FILES_SHARING_API_USER3, self::TEST_FOLDER_NAME . ' (2) (2) (2) (2) (2)'); | ||
|
|
||
| $this->cleanupShareTarget->run(new NullOutput()); | ||
|
|
||
| $share = $this->shareManager->getShareById($share->getFullId()); | ||
| $share2 = $this->shareManager->getShareById($share2->getFullId()); | ||
|
|
||
| // there is no guarantee for what order the 2 shares got repaired by | ||
| $targets = [ | ||
| $share->getTarget(), | ||
| $share2->getTarget(), | ||
| ]; | ||
| sort($targets); | ||
| $this->assertEquals([ | ||
| self::TEST_FOLDER_NAME, | ||
| self::TEST_FOLDER_NAME . ' (2)' | ||
| ], $targets); | ||
| } | ||
| } |
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.