Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion build/psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3640,7 +3640,6 @@
</file>
<file src="lib/private/Files/Node/Root.php">
<LessSpecificReturnStatement>
<code><![CDATA[$folders]]></code>
<code><![CDATA[$this->mountManager->findByNumericId($numericId)]]></code>
<code><![CDATA[$this->mountManager->findByStorageId($storageId)]]></code>
<code><![CDATA[$this->mountManager->findIn($mountPoint)]]></code>
Expand Down
2 changes: 1 addition & 1 deletion lib/private/Files/Node/Folder.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ public function getAppDataDirectoryName(): string {
* in.
*
* @param int $id
* @return array
* @return list<Node>
*/
protected function getByIdInRootMount(int $id): array {
if (!method_exists($this->root, 'createNode')) {
Expand Down
94 changes: 50 additions & 44 deletions lib/private/Files/Node/Root.php
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@
}
}
}
$node = current($this->getByIdInPath($id, $path));
$node = current($this->getByIdInPath($id, $path, true));
if (!$node) {
return null;
}
Expand All @@ -401,10 +401,10 @@
}

/**
* @param int $id
* @return Node[]
* @return list<INode>
* @note $onlyFirst is not part of the public API, only used by getFirstNodeByIdInPath
*/
public function getByIdInPath(int $id, string $path): array {
public function getByIdInPath(int $id, string $path, bool $onlyFirst = false): array {
$mountCache = $this->getUserMountCache();
$setupManager = $this->mountManager->getSetupManager();
if ($path !== '' && strpos($path, '/', 1) > 0) {
Expand All @@ -427,19 +427,14 @@
//
// so instead of using the cached entries directly, we instead filter the current mounts by the rootid of the cache entry

$mountRootIds = array_map(function ($mount) {
return $mount->getRootId();
}, $mountInfosContainingFiles);
$mountRootPaths = array_map(function ($mount) {
return $mount->getRootInternalPath();
}, $mountInfosContainingFiles);
$mountProviders = array_unique(array_map(function ($mount) {
return $mount->getMountProvider();
}, $mountInfosContainingFiles));
$mountPoints = array_map(fn (ICachedMountInfo $mountInfo) => $mountInfo->getMountPoint(), $mountInfosContainingFiles);
$mountRoots = array_combine($mountRootIds, $mountRootPaths);
/** @var array<int, string> $mountRoots */
$mountRoots = [];
foreach ($mountsContainingFile as $mountInfo) {

Check failure on line 433 in lib/private/Files/Node/Root.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

UndefinedVariable

lib/private/Files/Node/Root.php:433:12: UndefinedVariable: Cannot find referenced variable $mountsContainingFile (see https://psalm.dev/024)
$mountRoots[$mountInfo->getRootId()] = $mountInfo->getRootInternalPath();
}

$mounts = $this->mountManager->getMountsByMountProvider($path, $mountProviders);

Check failure on line 437 in lib/private/Files/Node/Root.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

UndefinedVariable

lib/private/Files/Node/Root.php:437:66: UndefinedVariable: Cannot find referenced variable $mountProviders (see https://psalm.dev/024)
$mountsContainingFile = array_filter($mounts, fn (IMountPoint $mount) => in_array($mount->getMountPoint(), $mountPoints));

// if we haven't found a relevant mount that is setup, but we do have relevant mount infos
Expand All @@ -461,59 +456,70 @@
$mountsContainingFile = array_filter(array_map($this->mountManager->getMountFromMountInfo(...), $mountInfosContainingFiles));
}

if (count($mountsContainingFile) === 0) {
if ($user === $this->getAppDataDirectoryName()) {
$folder = $this->get($path);
if ($folder instanceof Folder) {
return $folder->getByIdInRootMount($id);
} else {
throw new \Exception('getByIdInPath with non folder');
}
$userManager = Server::get(IUserManager::class);
$foundMount = false;
$nodes = [];
foreach ($mountsContainingFile as $mountInfo) {
$mount = $this->mountManager->getMountFromMountInfo($mountInfo);

Check failure on line 463 in lib/private/Files/Node/Root.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

InvalidArgument

lib/private/Files/Node/Root.php:463:56: InvalidArgument: Argument 1 of OC\Files\Mount\Manager::getMountFromMountInfo expects OCP\Files\Config\ICachedMountInfo, but OCP\Files\Mount\IMountPoint provided (see https://psalm.dev/004)
if ($mount === null) {
continue;
}
return [];
}
$foundMount = true;

$nodes = array_map(function (IMountPoint $mount) use ($id, $mountRoots) {
$rootInternalPath = $mountRoots[$mount->getStorageRootId()];
$cacheEntry = $mount->getStorage()->getCache()->get($id);
if (!$cacheEntry) {
return null;
$storage = $mount->getStorage();
if ($storage === null) {
continue;
}

$cacheEntry = $storage->getCache()->get($id);
if ($cacheEntry === false) {
continue;
}

$rootInternalPath = $mountRoots[$mount->getStorageRootId()];

// cache jails will hide the "true" internal path
$internalPath = ltrim($rootInternalPath . '/' . $cacheEntry->getPath(), '/');
$pathRelativeToMount = substr($internalPath, strlen($rootInternalPath));
$pathRelativeToMount = ltrim($pathRelativeToMount, '/');
$absolutePath = rtrim($mount->getMountPoint() . $pathRelativeToMount, '/');
$storage = $mount->getStorage();
if ($storage === null) {
return null;
}
$ownerId = $storage->getOwner($pathRelativeToMount);
if ($ownerId !== false) {
$owner = Server::get(IUserManager::class)->get($ownerId);
$owner = $userManager->get($ownerId);
} else {
$owner = null;
}
return $this->createNode($absolutePath, new FileInfo(
$node = $this->createNode($absolutePath, new FileInfo(
$absolutePath,
$storage,
$cacheEntry->getPath(),
$cacheEntry,
$mount,
$owner,
));
}, $mountsContainingFile);

$nodes = array_filter($nodes);
if (PathHelper::getRelativePath($path, $node->getPath()) !== null) {
$nodes[] = $node;
if ($onlyFirst) {
return $nodes;
}
}
}

$folders = array_filter($nodes, function (Node $node) use ($path) {
return PathHelper::getRelativePath($path, $node->getPath()) !== null;
});
usort($folders, function ($a, $b) {
return $b->getPath() <=> $a->getPath();
});
return $folders;
if (!$foundMount) {
if ($user === $this->getAppDataDirectoryName()) {
$folder = $this->get($path);
if ($folder instanceof Folder) {
return $folder->getByIdInRootMount($id);
} else {
throw new \Exception('getByIdInPath with non folder');
}
}
return [];
}

usort($nodes, static fn (Node $a, Node $b): int => $b->getPath() <=> $a->getPath());
return $nodes;
}

public function getNodeFromCacheEntryAndMount(ICacheEntry $cacheEntry, IMountPoint $mountPoint): INode {
Expand Down
2 changes: 1 addition & 1 deletion lib/public/Files/IRootFolder.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function getUserFolder($userId);
*
* @param int $id
* @param string $path
* @return Node[]
* @return list<Node>
*
* @since 24.0.0
*/
Expand Down
Loading