Skip to content

Commit 4de187a

Browse files
committed
fix: improve performance of handling delete shares
Signed-off-by: Robin Appelman <robin@icewind.nl>
1 parent a66c792 commit 4de187a

4 files changed

Lines changed: 44 additions & 20 deletions

File tree

apps/files_sharing/lib/Listener/SharesUpdatedListener.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ public function __construct(
5555
public function handle(Event $event): void {
5656
if ($event instanceof UserShareAccessUpdatedEvent) {
5757
foreach ($event->getUsers() as $user) {
58-
$this->updateOrMarkUser($user, true);
58+
$this->updateOrMarkUser($user);
5959
}
6060
}
6161
if ($event instanceof UserAddedEvent || $event instanceof UserRemovedEvent) {
62-
$this->updateOrMarkUser($event->getUser(), true);
62+
$this->updateOrMarkUser($event->getUser());
6363
}
6464
if ($event instanceof ShareCreatedEvent || $event instanceof ShareTransferredEvent) {
6565
$share = $event->getShare();
@@ -75,8 +75,11 @@ public function handle(Event $event): void {
7575
}
7676
}
7777
if ($event instanceof BeforeShareDeletedEvent) {
78-
foreach ($this->shareManager->getUsersForShare($event->getShare()) as $user) {
79-
$this->updateOrMarkUser($user, false, [$event->getShare()]);
78+
$share = $event->getShare();
79+
foreach ($this->shareManager->getUsersForShare($share) as $user) {
80+
$this->markOrRun($user, function () use ($user, $share) {
81+
$this->shareUpdater->updateForDeletedShare($user, $share);
82+
});
8083
}
8184
}
8285
}
@@ -92,9 +95,9 @@ private function markOrRun(IUser $user, callable $callback): void {
9295
$this->updatedTime += $end - $start;
9396
}
9497

95-
private function updateOrMarkUser(IUser $user, bool $verifyMountPoints, array $ignoreShares = []): void {
96-
$this->markOrRun($user, function () use ($user, $verifyMountPoints, $ignoreShares) {
97-
$this->shareUpdater->updateForUser($user, $verifyMountPoints, $ignoreShares);
98+
private function updateOrMarkUser(IUser $user): void {
99+
$this->markOrRun($user, function () use ($user) {
100+
$this->shareUpdater->updateForUser($user);
98101
});
99102
}
100103

apps/files_sharing/lib/ShareRecipientUpdater.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function __construct(
2828
/**
2929
* Validate all received shares for a user
3030
*/
31-
public function updateForUser(IUser $user, bool $verifyMountPoints = true, array $ignoreShares = []): void {
31+
public function updateForUser(IUser $user): void {
3232
// prevent recursion
3333
if (isset($this->inUpdate[$user->getUID()])) {
3434
return;
@@ -40,20 +40,18 @@ public function updateForUser(IUser $user, bool $verifyMountPoints = true, array
4040
$mountPoints = array_map(fn (ICachedMountInfo $mount) => $mount->getMountPoint(), $cachedMounts);
4141
$mountsByPath = array_combine($mountPoints, $cachedMounts);
4242

43-
$shares = $this->shareMountProvider->getSuperSharesForUser($user, $ignoreShares);
43+
$shares = $this->shareMountProvider->getSuperSharesForUser($user);
4444

4545
// the share mounts have changed if either the number of shares doesn't matched the number of share mounts
4646
// or there is a share for which we don't have a mount yet.
4747
$mountsChanged = count($shares) !== count($shareMounts);
48-
foreach ($shares as &$share) {
48+
foreach ($shares as $share) {
4949
[$parentShare, $groupedShares] = $share;
5050
$mountPoint = '/' . $user->getUID() . '/files/' . trim($parentShare->getTarget(), '/') . '/';
5151
$mountKey = $parentShare->getNodeId() . '::' . $mountPoint;
5252
if (!isset($cachedMounts[$mountKey])) {
5353
$mountsChanged = true;
54-
if ($verifyMountPoints) {
55-
$this->shareTargetValidator->verifyMountPoint($user, $parentShare, $mountsByPath, $groupedShares);
56-
}
54+
$this->shareTargetValidator->verifyMountPoint($user, $parentShare, $mountsByPath, $groupedShares);
5755
}
5856
}
5957

@@ -78,4 +76,13 @@ public function updateForAddedShare(IUser $user, IShare $share): void {
7876

7977
$this->userMountCache->addMount($user, $mountPoint, $share->getNode()->getData(), MountProvider::class);
8078
}
79+
80+
/**
81+
* Process a single deleted share for a user
82+
*/
83+
public function updateForDeletedShare(IUser $user, IShare $share): void {
84+
$mountPoint = '/' . $user->getUID() . '/files/' . trim($share->getTarget(), '/') . '/';
85+
86+
$this->userMountCache->removeMount($mountPoint);
87+
}
8188
}

apps/files_sharing/tests/ShareRecipientUpdaterTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,4 +185,22 @@ public function testUpdateForUserRemoved() {
185185

186186
$this->updater->updateForUser($user1);
187187
}
188+
189+
public function testDeletedShare() {
190+
$share = $this->createMock(IShare::class);
191+
$share->method('getTarget')
192+
->willReturn('/target');
193+
$share->method('getNodeId')
194+
->willReturn(111);
195+
$user1 = $this->createUser('user1', '');
196+
197+
$this->shareTargetValidator->expects($this->never())
198+
->method('verifyMountPoint');
199+
200+
$this->userMountCache->expects($this->exactly(1))
201+
->method('removeMount')
202+
->with('/user1/files/target/');
203+
204+
$this->updater->updateForDeletedShare($user1, $share);
205+
}
188206
}

apps/files_sharing/tests/SharesUpdatedListenerTest.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,8 @@ public function testShareAccessUpdated() {
116116
$this->shareRecipientUpdater
117117
->expects($this->exactly(2))
118118
->method('updateForUser')
119-
->willReturnCallback(function (IUser $user, bool $verifyMountPoints = true, array $ignoreShares = []) use ($user1, $user2) {
119+
->willReturnCallback(function (IUser $user) use ($user1, $user2) {
120120
$this->assertContains($user, [$user1, $user2]);
121-
$this->assertEquals(true, $verifyMountPoints);
122-
$this->assertEquals([], $ignoreShares);
123121
});
124122

125123
$this->sharesUpdatedListener->handle($event);
@@ -137,11 +135,9 @@ public function testShareDeleted() {
137135

138136
$this->shareRecipientUpdater
139137
->expects($this->exactly(2))
140-
->method('updateForUser')
141-
->willReturnCallback(function (IUser $user, bool $verifyMountPoints = true, array $ignoreShares = []) use ($user1, $user2, $share) {
138+
->method('updateForDeletedShare')
139+
->willReturnCallback(function (IUser $user) use ($user1, $user2, $share) {
142140
$this->assertContains($user, [$user1, $user2]);
143-
$this->assertEquals(false, $verifyMountPoints);
144-
$this->assertEquals([$share], $ignoreShares);
145141
});
146142

147143
$this->sharesUpdatedListener->handle($event);

0 commit comments

Comments
 (0)