Skip to content

Commit aba8a7f

Browse files
authored
Merge pull request #57549 from nextcloud/carl/optimize-getdirectory-content
2 parents a7ea180 + eadcd1c commit aba8a7f

File tree

3 files changed

+33
-36
lines changed

3 files changed

+33
-36
lines changed

lib/private/Files/Cache/CacheEntry.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public function getEtag() {
8686
return $this->data['etag'];
8787
}
8888

89-
public function getPermissions() {
89+
public function getPermissions(): int {
9090
return $this->data['permissions'];
9191
}
9292

lib/private/Files/Mount/Manager.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ public function find(string $path): IMountPoint {
110110
/**
111111
* Find all mounts in $path
112112
*
113-
* @param string $path
114113
* @return IMountPoint[]
115114
*/
116115
public function findIn(string $path): array {
@@ -122,19 +121,18 @@ public function findIn(string $path): array {
122121
}
123122

124123
$result = [];
125-
$pathLength = \strlen($path);
126-
$mountPoints = array_keys($this->mounts);
127-
foreach ($mountPoints as $mountPoint) {
128-
if (substr($mountPoint, 0, $pathLength) === $path && \strlen($mountPoint) > $pathLength) {
129-
$result[] = $this->mounts[$mountPoint];
124+
$pathLen = strlen($path);
125+
foreach ($this->mounts as $mountPoint => $mount) {
126+
if (strlen($mountPoint) > $pathLen && str_starts_with($mountPoint, $path)) {
127+
$result[] = $mount;
130128
}
131129
}
132130

133131
$this->inPathCache[$path] = $result;
134132
return $result;
135133
}
136134

137-
public function clear() {
135+
public function clear(): void {
138136
$this->mounts = [];
139137
$this->pathCache->clear();
140138
$this->inPathCache->clear();

lib/private/Files/View.php

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
use OCP\Files\Mount\IMountPoint;
3232
use OCP\Files\NotFoundException;
3333
use OCP\Files\ReservedWordException;
34+
use OCP\Files\StorageInvalidException;
35+
use OCP\Files\StorageNotAvailableException;
3436
use OCP\IUser;
3537
use OCP\IUserManager;
3638
use OCP\L10N\IFactory;
@@ -1350,11 +1352,7 @@ public function hasUpdated($path, $time) {
13501352
return $this->basicOperation('hasUpdated', $path, [], $time);
13511353
}
13521354

1353-
/**
1354-
* @param string $ownerId
1355-
* @return IUser
1356-
*/
1357-
private function getUserObjectForOwner(string $ownerId) {
1355+
private function getUserObjectForOwner(string $ownerId): IUser {
13581356
return new LazyUser($ownerId, $this->userManager);
13591357
}
13601358

@@ -1511,35 +1509,38 @@ public function getDirectoryContent($directory, $mimetype_filter = '', ?\OCP\Fil
15111509
$contents = $cache->getFolderContentsById($folderId); //TODO: mimetype_filter
15121510

15131511
$sharingDisabled = \OCP\Util::isSharingDisabledForUser();
1512+
$permissionsMask = ~\OCP\Constants::PERMISSION_SHARE;
1513+
1514+
$files = [];
1515+
foreach ($contents as $content) {
1516+
$name = $content->getName();
1517+
$contentPath = $content->getPath();
15141518

1515-
$fileNames = array_map(function (ICacheEntry $content) {
1516-
return $content->getName();
1517-
}, $contents);
1518-
/**
1519-
* @var \OC\Files\FileInfo[] $fileInfos
1520-
*/
1521-
$fileInfos = array_map(function (ICacheEntry $content) use ($path, $storage, $mount, $sharingDisabled) {
15221519
if ($sharingDisabled) {
1523-
$content['permissions'] = $content['permissions'] & ~\OCP\Constants::PERMISSION_SHARE;
1524-
}
1525-
$ownerId = $storage->getOwner($content['path']);
1526-
if ($ownerId !== false) {
1527-
$owner = $this->getUserObjectForOwner($ownerId);
1528-
} else {
1529-
$owner = null;
1520+
$content['permissions'] = $content->getPermissions() & $permissionsMask;
15301521
}
1531-
return new FileInfo($path . '/' . $content['name'], $storage, $content['path'], $content, $mount, $owner);
1532-
}, $contents);
1533-
$files = array_combine($fileNames, $fileInfos);
1522+
1523+
$ownerId = $storage->getOwner($contentPath);
1524+
$owner = $ownerId !== false
1525+
? $this->getUserObjectForOwner($ownerId)
1526+
: null;
1527+
1528+
$files[$name] = new FileInfo(
1529+
$path . '/' . $name,
1530+
$storage,
1531+
$contentPath,
1532+
$content,
1533+
$mount,
1534+
$owner
1535+
);
1536+
}
15341537

15351538
//add a folder for any mountpoint in this directory and add the sizes of other mountpoints to the folders
15361539
$mounts = Filesystem::getMountManager()->findIn($path);
15371540

15381541
// make sure nested mounts are sorted after their parent mounts
15391542
// otherwise doesn't propagate the etag across storage boundaries correctly
1540-
usort($mounts, function (IMountPoint $a, IMountPoint $b) {
1541-
return $a->getMountPoint() <=> $b->getMountPoint();
1542-
});
1543+
usort($mounts, static fn (IMountPoint $a, IMountPoint $b): int => $a->getMountPoint() <=> $b->getMountPoint());
15431544

15441545
$dirLength = strlen($path);
15451546
foreach ($mounts as $mount) {
@@ -1553,9 +1554,7 @@ public function getDirectoryContent($directory, $mimetype_filter = '', ?\OCP\Fil
15531554
$subScanner = $subStorage->getScanner();
15541555
try {
15551556
$subScanner->scanFile('');
1556-
} catch (\OCP\Files\StorageNotAvailableException $e) {
1557-
continue;
1558-
} catch (\OCP\Files\StorageInvalidException $e) {
1557+
} catch (StorageNotAvailableException|StorageInvalidException) {
15591558
continue;
15601559
} catch (\Exception $e) {
15611560
// sometimes when the storage is not available it can be any exception

0 commit comments

Comments
 (0)