Merge pull request #39154 from nextcloud/backport/38625/stable25

[stable25] fix: expect interface, not a specific implementation
This commit is contained in:
Arthur Schiwon 2023-07-06 13:36:58 +02:00 committed by GitHub
commit 48a7a20ab6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 63 additions and 41 deletions

View file

@ -26,15 +26,14 @@
namespace OC\Files\Cache;
use OC\Files\Cache\Wrapper\CacheJail;
use OC\Files\Node\Root;
use OC\Files\Search\QueryOptimizer\QueryOptimizer;
use OC\Files\Search\SearchBinaryOperator;
use OC\SystemConfig;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\Files\Cache\ICache;
use OCP\Files\Cache\ICacheEntry;
use OCP\Files\Folder;
use OCP\Files\IMimeTypeLoader;
use OCP\Files\IRootFolder;
use OCP\Files\Mount\IMountPoint;
use OCP\Files\Search\ISearchBinaryOperator;
use OCP\Files\Search\ISearchQuery;
@ -197,24 +196,29 @@ class QuerySearchHelper {
}
/**
* @return array{array<string, ICache>, array<string, IMountPoint>}
* @return array{0?: array<array-key, ICache>, 1?: array<array-key, IMountPoint>}
*/
public function getCachesAndMountPointsForSearch(Root $root, string $path, bool $limitToHome = false): array {
public function getCachesAndMountPointsForSearch(IRootFolder $root, string $path, bool $limitToHome = false): array {
$rootLength = strlen($path);
$mount = $root->getMount($path);
$storage = $mount->getStorage();
if ($storage === null) {
return [];
}
$internalPath = $mount->getInternalPath($path);
if ($internalPath !== '') {
// a temporary CacheJail is used to handle filtering down the results to within this folder
/** @var ICache[] $caches */
$caches = ['' => new CacheJail($storage->getCache(''), $internalPath)];
} else {
/** @var ICache[] $caches */
$caches = ['' => $storage->getCache('')];
}
/** @var IMountPoint[] $mountByMountPoint */
$mountByMountPoint = ['' => $mount];
if (!$limitToHome) {
/** @var IMountPoint[] $mounts */
$mounts = $root->getMountsIn($path);
foreach ($mounts as $mount) {
$storage = $mount->getStorage();

View file

@ -149,9 +149,6 @@ class FileInfo implements \OCP\Files\FileInfo, \ArrayAccess {
return $this->path;
}
/**
* @return \OCP\Files\Storage
*/
public function getStorage() {
return $this->storage;
}

View file

@ -39,6 +39,7 @@ use OC\Files\Utils\PathHelper;
use OCP\Files\Cache\ICacheEntry;
use OCP\Files\FileInfo;
use OCP\Files\Mount\IMountPoint;
use OCP\Files\Node as INode;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\Files\Search\ISearchBinaryOperator;
@ -108,12 +109,7 @@ class Folder extends Node implements \OCP\Files\Folder {
}, $folderContent);
}
/**
* @param string $path
* @param FileInfo $info
* @return File|Folder
*/
protected function createNode($path, FileInfo $info = null) {
protected function createNode(string $path, ?FileInfo $info = null): INode {
if (is_null($info)) {
$isDir = $this->view->is_dir($path);
} else {
@ -234,6 +230,8 @@ class Folder extends Node implements \OCP\Files\Folder {
/** @var QuerySearchHelper $searchHelper */
$searchHelper = \OC::$server->get(QuerySearchHelper::class);
/** @var \OCP\Files\Cache\ICache[] $caches */
/** @var \OCP\Files\Mount\IMountPoint[] $mountByMountPoint */
[$caches, $mountByMountPoint] = $searchHelper->getCachesAndMountPointsForSearch($this->root, $this->path, $limitToHome);
$resultsPerCache = $searchHelper->searchInCaches($query, $caches);
@ -328,9 +326,16 @@ class Folder extends Node implements \OCP\Files\Folder {
* @param int $id
* @return array
*/
protected function getByIdInRootMount(int $id): array {
protected function getByIdInRootMount(int $id): array {
if (!method_exists($this->root, 'createNode')) {
// Always expected to be false. Being a method of Folder, this is
// always implemented. For it is an internal method and should not
// be exposed and made public, it is not part of an interface.
return [];
}
$mount = $this->root->getMount('');
$cacheEntry = $mount->getStorage()->getCache($this->path)->get($id);
$storage = $mount->getStorage();
$cacheEntry = $storage ? $storage->getCache($this->path)->get($id) : null;
if (!$cacheEntry) {
return [];
}
@ -345,7 +350,7 @@ class Folder extends Node implements \OCP\Files\Folder {
return [$this->root->createNode(
$absolutePath, new \OC\Files\FileInfo(
$absolutePath,
$mount->getStorage(),
$storage,
$cacheEntry->getPath(),
$cacheEntry,
$mount
@ -383,7 +388,7 @@ class Folder extends Node implements \OCP\Files\Folder {
/**
* @param int $limit
* @param int $offset
* @return \OCP\Files\Node[]
* @return INode[]
*/
public function getRecent($limit, $offset = 0) {
$filterOutNonEmptyFolder = new SearchBinaryOperator(

View file

@ -28,6 +28,7 @@ namespace OC\Files\Node;
use OC\Files\Utils\PathHelper;
use OCP\Constants;
use OCP\Files\Mount\IMountPoint;
/**
* Class LazyFolder
@ -110,14 +111,14 @@ class LazyFolder implements \OCP\Files\Folder {
/**
* @inheritDoc
*/
public function getMount($mountPoint) {
public function getMount(string $mountPoint): IMountPoint {
return $this->__call(__FUNCTION__, func_get_args());
}
/**
* @inheritDoc
* @return IMountPoint[]
*/
public function getMountsIn($mountPoint) {
public function getMountsIn(string $mountPoint): array {
return $this->__call(__FUNCTION__, func_get_args());
}

View file

@ -34,6 +34,8 @@ use OC\Files\Mount\MoveableMount;
use OC\Files\Utils\PathHelper;
use OCP\Files\FileInfo;
use OCP\Files\InvalidPathException;
use OCP\Files\IRootFolder;
use OCP\Files\Node as INode;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\Lock\LockedException;
@ -41,16 +43,13 @@ use OCP\PreConditionNotMetException;
use Symfony\Component\EventDispatcher\GenericEvent;
// FIXME: this class really should be abstract
class Node implements \OCP\Files\Node {
class Node implements INode {
/**
* @var \OC\Files\View $view
*/
protected $view;
/**
* @var \OC\Files\Node\Root $root
*/
protected $root;
protected IRootFolder $root;
/**
* @var string $path Absolute path to the node (e.g. /admin/files/folder/file)
@ -73,7 +72,7 @@ class Node implements \OCP\Files\Node {
* @param string $path
* @param FileInfo $fileInfo
*/
public function __construct($root, $view, $path, $fileInfo = null, ?Node $parent = null) {
public function __construct(IRootFolder $root, $view, $path, $fileInfo = null, ?Node $parent = null) {
if (Filesystem::normalizePath($view->getRoot()) !== '/') {
throw new PreConditionNotMetException('The view passed to the node should not have any fake root set');
}
@ -124,7 +123,9 @@ class Node implements \OCP\Files\Node {
$args = !empty($args) ? $args : [$this];
$dispatcher = \OC::$server->getEventDispatcher();
foreach ($hooks as $hook) {
$this->root->emit('\OC\Files', $hook, $args);
if (method_exists($this->root, 'emit')) {
$this->root->emit('\OC\Files', $hook, $args);
}
$dispatcher->dispatch('\OCP\Files::' . $hook, new GenericEvent($args));
}
}
@ -285,7 +286,8 @@ class Node implements \OCP\Files\Node {
}
/**
* @return Node
* @return INode|IRootFolder
* @throws NotFoundException
*/
public function getParent() {
if ($this->parent === null) {
@ -396,7 +398,7 @@ class Node implements \OCP\Files\Node {
/**
* @param string $targetPath
* @return \OC\Files\Node\Node
* @return INode
* @throws InvalidPathException
* @throws NotFoundException
* @throws NotPermittedException if copy not allowed or failed
@ -422,7 +424,7 @@ class Node implements \OCP\Files\Node {
/**
* @param string $targetPath
* @return \OC\Files\Node\Node
* @return INode
* @throws InvalidPathException
* @throws NotFoundException
* @throws NotPermittedException if move not allowed or failed

View file

@ -45,6 +45,7 @@ use OCP\Files\Config\IUserMountCache;
use OCP\Files\Events\Node\FilesystemTornDownEvent;
use OCP\Files\IRootFolder;
use OCP\Files\Mount\IMountPoint;
use OCP\Files\Node as INode;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
use OCP\IUser;
@ -153,11 +154,7 @@ class Root extends Folder implements IRootFolder {
$this->mountManager->addMount($mount);
}
/**
* @param string $mountPoint
* @return \OC\Files\Mount\MountPoint
*/
public function getMount($mountPoint) {
public function getMount(string $mountPoint): IMountPoint {
return $this->mountManager->find($mountPoint);
}
@ -165,7 +162,7 @@ class Root extends Folder implements IRootFolder {
* @param string $mountPoint
* @return \OC\Files\Mount\MountPoint[]
*/
public function getMountsIn($mountPoint) {
public function getMountsIn(string $mountPoint): array {
return $this->mountManager->findIn($mountPoint);
}
@ -339,7 +336,7 @@ class Root extends Folder implements IRootFolder {
}
/**
* @return Node
* @return INode|IRootFolder
* @throws \OCP\Files\NotFoundException
*/
public function getParent() {

View file

@ -57,6 +57,7 @@ class SystemTagsInFilesDetector {
}
$query = new SearchQuery($operator, $limit, $offset, []);
/** @var \OCP\Files\Cache\ICache[] $caches */
[$caches, ] = $this->searchHelper->getCachesAndMountPointsForSearch(
$this->getRootFolder($folder),
$folder->getPath(),

View file

@ -26,6 +26,7 @@ namespace OCP\Files;
use OC\Hooks\Emitter;
use OC\User\NoUserException;
use OCP\Files\Mount\IMountPoint;
/**
* Interface IRootFolder
@ -56,4 +57,16 @@ interface IRootFolder extends Folder, Emitter {
* @since 24.0.0
*/
public function getByIdInPath(int $id, string $path);
/**
* @return IMountPoint[]
*
* @since 25.0.9
*/
public function getMountsIn(string $mountPoint): array;
/**
* @since 25.0.9
*/
public function getMount(string $mountPoint): IMountPoint;
}

View file

@ -24,6 +24,7 @@ use OC\Files\Search\SearchQuery;
use OC\Files\Storage\Temporary;
use OC\Files\Storage\Wrapper\Jail;
use OCP\Files\Cache\ICacheEntry;
use OCP\Files\IRootFolder;
use OCP\Files\Mount\IMountPoint;
use OCP\Files\NotFoundException;
use OCP\Files\Search\ISearchComparison;
@ -462,12 +463,13 @@ class FolderTest extends NodeTest {
}
public function testIsSubNode() {
$file = new Node(null, $this->view, '/foo/bar');
$folder = new Folder(null, $this->view, '/foo');
$rootFolderMock = $this->createMock(IRootFolder::class);
$file = new Node($rootFolderMock, $this->view, '/foo/bar');
$folder = new Folder($rootFolderMock, $this->view, '/foo');
$this->assertTrue($folder->isSubNode($file));
$this->assertFalse($folder->isSubNode($folder));
$file = new Node(null, $this->view, '/foobar');
$file = new Node($rootFolderMock, $this->view, '/foobar');
$this->assertFalse($folder->isSubNode($file));
}