Skip to content

Commit baff429

Browse files
Merge pull request #58000 from nextcloud/setup-root-path-children-less-setup-33
[stable33] getById: don't setup for all users with access by default
2 parents bbf591b + 34448dd commit baff429

File tree

4 files changed

+36
-17
lines changed

4 files changed

+36
-17
lines changed

build/psalm-baseline.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3781,17 +3781,12 @@
37813781
<code><![CDATA[$this->mountManager->findByNumericId($numericId)]]></code>
37823782
<code><![CDATA[$this->mountManager->findByStorageId($storageId)]]></code>
37833783
<code><![CDATA[$this->mountManager->findIn($mountPoint)]]></code>
3784-
<code><![CDATA[$this->user]]></code>
37853784
</LessSpecificReturnStatement>
37863785
<MoreSpecificReturnType>
37873786
<code><![CDATA[MountPoint[]]]></code>
37883787
<code><![CDATA[\OC\Files\Mount\MountPoint[]]]></code>
37893788
<code><![CDATA[\OC\Files\Mount\MountPoint[]]]></code>
3790-
<code><![CDATA[\OC\User\User]]></code>
37913789
</MoreSpecificReturnType>
3792-
<NullableReturnStatement>
3793-
<code><![CDATA[$this->user]]></code>
3794-
</NullableReturnStatement>
37953790
<UndefinedMethod>
37963791
<code><![CDATA[remove]]></code>
37973792
</UndefinedMethod>

lib/private/Files/Mount/Manager.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,11 +230,11 @@ public function getSetupManager(): SetupManager {
230230
}
231231

232232
/**
233-
* Return all mounts in a path from a specific mount provider
233+
* Return all mounts in a path from a specific mount provider, indexed by mount point
234234
*
235235
* @param string $path
236236
* @param string[] $mountProviders
237-
* @return IMountPoint[]
237+
* @return array<string, IMountPoint>
238238
*/
239239
public function getMountsByMountProvider(string $path, array $mountProviders) {
240240
$this->getSetupManager()->setupForProvider($path, $mountProviders);

lib/private/Files/Node/Root.php

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
66
* SPDX-License-Identifier: AGPL-3.0-only
77
*/
8+
89
namespace OC\Files\Node;
910

1011
use OC\Files\FileInfo;
@@ -19,6 +20,8 @@
1920
use OCP\Cache\CappedMemoryCache;
2021
use OCP\EventDispatcher\IEventDispatcher;
2122
use OCP\Files\Cache\ICacheEntry;
23+
use OCP\Files\Config\ICachedMountFileInfo;
24+
use OCP\Files\Config\ICachedMountInfo;
2225
use OCP\Files\Config\IUserMountCache;
2326
use OCP\Files\Events\Node\FilesystemTornDownEvent;
2427
use OCP\Files\IRootFolder;
@@ -82,10 +85,8 @@ public function __construct(
8285

8386
/**
8487
* Get the user for which the filesystem is setup
85-
*
86-
* @return \OC\User\User
8788
*/
88-
public function getUser() {
89+
public function getUser(): ?IUser {
8990
return $this->user;
9091
}
9192

@@ -411,12 +412,12 @@ public function getByIdInPath(int $id, string $path): array {
411412
} else {
412413
$user = null;
413414
}
414-
$mountsContainingFile = $mountCache->getMountsForFileId($id, $user);
415+
$mountInfosContainingFiles = $mountCache->getMountsForFileId($id, $user);
415416

416417
// if the mount isn't in the cache yet, perform a setup first, then try again
417-
if (count($mountsContainingFile) === 0) {
418+
if (count($mountInfosContainingFiles) === 0) {
418419
$setupManager->setupForPath($path, true);
419-
$mountsContainingFile = $mountCache->getMountsForFileId($id, $user);
420+
$mountInfosContainingFiles = $mountCache->getMountsForFileId($id, $user);
420421
}
421422

422423
// when a user has access through the same storage through multiple paths
@@ -428,16 +429,31 @@ public function getByIdInPath(int $id, string $path): array {
428429

429430
$mountRootIds = array_map(function ($mount) {
430431
return $mount->getRootId();
431-
}, $mountsContainingFile);
432+
}, $mountInfosContainingFiles);
432433
$mountRootPaths = array_map(function ($mount) {
433434
return $mount->getRootInternalPath();
434-
}, $mountsContainingFile);
435+
}, $mountInfosContainingFiles);
435436
$mountProviders = array_unique(array_map(function ($mount) {
436437
return $mount->getMountProvider();
437-
}, $mountsContainingFile));
438+
}, $mountInfosContainingFiles));
439+
$mountPoints = array_map(fn (ICachedMountInfo $mountInfo) => $mountInfo->getMountPoint(), $mountInfosContainingFiles);
438440
$mountRoots = array_combine($mountRootIds, $mountRootPaths);
439441

440-
$mountsContainingFile = array_filter(array_map($this->mountManager->getMountFromMountInfo(...), $mountsContainingFile));
442+
$mounts = $this->mountManager->getMountsByMountProvider($path, $mountProviders);
443+
$mountsContainingFile = array_filter($mounts, fn (IMountPoint $mount) => in_array($mount->getMountPoint(), $mountPoints));
444+
445+
if (count($mountsContainingFile) == 0 && count($mountInfosContainingFiles) > 0) {
446+
if (!$user) {
447+
$user = $this->getUser()?->getUID();
448+
}
449+
if (!$user) {
450+
/** @var ICachedMountFileInfo $firstMount */
451+
$firstMount = current($mountInfosContainingFiles);
452+
$user = $firstMount->getUser()->getUID();
453+
}
454+
$mountInfosContainingFiles = array_filter($mountInfosContainingFiles, fn (ICachedMountInfo $mountInfo) => $mountInfo->getUser()->getUID() === $user);
455+
$mountsContainingFile = array_filter(array_map($this->mountManager->getMountFromMountInfo(...), $mountInfosContainingFiles));
456+
}
441457

442458
if (count($mountsContainingFile) === 0) {
443459
if ($user === $this->getAppDataDirectoryName()) {

tests/lib/Files/Node/FolderTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,8 @@ public function testGetById(): void {
541541

542542
$manager->method('getMountFromMountInfo')
543543
->willReturn($mount);
544+
$manager->method('getMountsByMountProvider')
545+
->willReturn([$mount]);
544546

545547
$node = new Folder($root, $view, '/bar/foo');
546548
$result = $node->getById(1);
@@ -586,6 +588,8 @@ public function testGetByIdMountRoot(): void {
586588

587589
$manager->method('getMountFromMountInfo')
588590
->willReturn($mount);
591+
$manager->method('getMountsByMountProvider')
592+
->willReturn([$mount]);
589593

590594
$node = new Folder($root, $view, '/bar');
591595
$result = $node->getById(1);
@@ -631,6 +635,8 @@ public function testGetByIdOutsideFolder(): void {
631635

632636
$manager->method('getMountFromMountInfo')
633637
->willReturn($mount);
638+
$manager->method('getMountsByMountProvider')
639+
->willReturn([$mount]);
634640

635641
$node = new Folder($root, $view, '/bar/foo');
636642
$result = $node->getById(1);
@@ -694,6 +700,8 @@ public function testGetByIdMultipleStorages(): void {
694700
}
695701
return null;
696702
});
703+
$manager->method('getMountsByMountProvider')
704+
->willReturn([$mount1, $mount2]);
697705

698706
$node = new Folder($root, $view, '/bar/foo');
699707
$result = $node->getById(1);

0 commit comments

Comments
 (0)