2013-09-01 19:47:48 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 17:07:57 +02:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2015-03-26 11:44:34 +01:00
|
|
|
* @author Bernhard Posselt <dev@bernhard-posselt.com>
|
2019-12-03 19:57:53 +01:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2016-07-21 17:07:57 +02:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2015-03-26 11:44:34 +01:00
|
|
|
* @author Jörn Friedrich Dreyer <jfd@butonic.de>
|
2020-04-29 11:57:22 +02:00
|
|
|
* @author Julius Härtl <jus@bitgrid.net>
|
2017-11-06 15:56:42 +01:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2015-03-26 11:44:34 +01:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-07-21 18:13:36 +02:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2016-07-21 17:07:57 +02:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2016-05-26 19:56:05 +02:00
|
|
|
* @author Stefan Weil <sw@weilnetz.de>
|
2020-12-16 14:54:15 +01:00
|
|
|
* @author Vincent Petry <vincent@nextcloud.com>
|
2015-03-26 11:44:34 +01:00
|
|
|
*
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
2019-12-03 19:57:53 +01:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2015-03-26 11:44:34 +01:00
|
|
|
*
|
2013-09-01 19:47:48 +02:00
|
|
|
*/
|
2022-02-02 16:12:57 +01:00
|
|
|
|
2013-09-01 19:47:48 +02:00
|
|
|
namespace OC\Files\Node;
|
|
|
|
|
2022-03-28 18:46:38 +02:00
|
|
|
use OC\Files\FileInfo;
|
2013-09-01 19:47:48 +02:00
|
|
|
use OC\Files\Mount\Manager;
|
2014-11-24 15:54:42 +01:00
|
|
|
use OC\Files\Mount\MountPoint;
|
2022-03-28 18:46:38 +02:00
|
|
|
use OC\Files\Utils\PathHelper;
|
2022-02-23 18:11:46 +01:00
|
|
|
use OC\Files\View;
|
2019-11-22 20:52:10 +01:00
|
|
|
use OC\Hooks\PublicEmitter;
|
2020-04-03 12:17:47 +02:00
|
|
|
use OC\User\NoUserException;
|
2023-11-23 10:22:34 +01:00
|
|
|
use OCP\Cache\CappedMemoryCache;
|
2022-02-23 18:11:46 +01:00
|
|
|
use OCP\EventDispatcher\IEventDispatcher;
|
2023-09-18 15:00:38 +02:00
|
|
|
use OCP\Files\Cache\ICacheEntry;
|
2016-09-18 18:36:53 +02:00
|
|
|
use OCP\Files\Config\IUserMountCache;
|
2022-02-23 18:29:08 +01:00
|
|
|
use OCP\Files\Events\Node\FilesystemTornDownEvent;
|
2019-11-22 20:52:10 +01:00
|
|
|
use OCP\Files\IRootFolder;
|
2022-03-28 18:47:17 +02:00
|
|
|
use OCP\Files\Mount\IMountPoint;
|
2023-06-08 23:32:16 +02:00
|
|
|
use OCP\Files\Node as INode;
|
2013-09-10 19:44:23 +02:00
|
|
|
use OCP\Files\NotFoundException;
|
|
|
|
use OCP\Files\NotPermittedException;
|
2024-02-16 12:34:07 +01:00
|
|
|
use OCP\ICache;
|
|
|
|
use OCP\ICacheFactory;
|
2022-02-23 18:11:46 +01:00
|
|
|
use OCP\IUser;
|
2016-11-02 23:16:51 +01:00
|
|
|
use OCP\IUserManager;
|
2022-03-17 16:42:53 +01:00
|
|
|
use Psr\Log\LoggerInterface;
|
2014-12-20 15:59:31 +01:00
|
|
|
|
2013-09-01 19:47:48 +02:00
|
|
|
/**
|
|
|
|
* Class Root
|
|
|
|
*
|
|
|
|
* Hooks available in scope \OC\Files
|
2013-09-10 19:44:23 +02:00
|
|
|
* - preWrite(\OCP\Files\Node $node)
|
|
|
|
* - postWrite(\OCP\Files\Node $node)
|
|
|
|
* - preCreate(\OCP\Files\Node $node)
|
|
|
|
* - postCreate(\OCP\Files\Node $node)
|
|
|
|
* - preDelete(\OCP\Files\Node $node)
|
|
|
|
* - postDelete(\OCP\Files\Node $node)
|
|
|
|
* - preTouch(\OC\FilesP\Node $node, int $mtime)
|
|
|
|
* - postTouch(\OCP\Files\Node $node)
|
|
|
|
* - preCopy(\OCP\Files\Node $source, \OCP\Files\Node $target)
|
|
|
|
* - postCopy(\OCP\Files\Node $source, \OCP\Files\Node $target)
|
|
|
|
* - preRename(\OCP\Files\Node $source, \OCP\Files\Node $target)
|
|
|
|
* - postRename(\OCP\Files\Node $source, \OCP\Files\Node $target)
|
2013-09-01 19:47:48 +02:00
|
|
|
*
|
|
|
|
* @package OC\Files\Node
|
|
|
|
*/
|
2014-12-20 15:59:31 +01:00
|
|
|
class Root extends Folder implements IRootFolder {
|
2022-02-23 18:11:46 +01:00
|
|
|
private Manager $mountManager;
|
|
|
|
private PublicEmitter $emitter;
|
|
|
|
private ?IUser $user;
|
|
|
|
private CappedMemoryCache $userFolderCache;
|
|
|
|
private IUserMountCache $userMountCache;
|
2022-03-17 16:42:53 +01:00
|
|
|
private LoggerInterface $logger;
|
2022-02-23 18:11:46 +01:00
|
|
|
private IUserManager $userManager;
|
|
|
|
private IEventDispatcher $eventDispatcher;
|
2024-02-16 12:34:07 +01:00
|
|
|
private ICache $pathByIdCache;
|
2016-09-18 18:36:53 +02:00
|
|
|
|
2013-09-01 19:47:48 +02:00
|
|
|
/**
|
2022-02-23 18:11:46 +01:00
|
|
|
* @param Manager $manager
|
|
|
|
* @param View $view
|
|
|
|
* @param IUser|null $user
|
2016-11-02 23:16:51 +01:00
|
|
|
*/
|
2022-02-23 18:11:46 +01:00
|
|
|
public function __construct(
|
|
|
|
$manager,
|
2022-02-02 16:12:57 +01:00
|
|
|
$view,
|
|
|
|
$user,
|
2022-02-23 18:11:46 +01:00
|
|
|
IUserMountCache $userMountCache,
|
2022-03-17 16:42:53 +01:00
|
|
|
LoggerInterface $logger,
|
2022-02-23 18:11:46 +01:00
|
|
|
IUserManager $userManager,
|
2024-02-16 12:34:07 +01:00
|
|
|
IEventDispatcher $eventDispatcher,
|
|
|
|
ICacheFactory $cacheFactory,
|
2022-02-23 18:11:46 +01:00
|
|
|
) {
|
2013-09-01 19:47:48 +02:00
|
|
|
parent::__construct($this, $view, '');
|
|
|
|
$this->mountManager = $manager;
|
|
|
|
$this->user = $user;
|
|
|
|
$this->emitter = new PublicEmitter();
|
2016-08-25 15:49:36 +02:00
|
|
|
$this->userFolderCache = new CappedMemoryCache();
|
2016-09-18 18:36:53 +02:00
|
|
|
$this->userMountCache = $userMountCache;
|
2016-11-02 23:16:51 +01:00
|
|
|
$this->logger = $logger;
|
|
|
|
$this->userManager = $userManager;
|
2022-02-23 18:29:08 +01:00
|
|
|
$eventDispatcher->addListener(FilesystemTornDownEvent::class, function () {
|
2022-02-23 18:11:46 +01:00
|
|
|
$this->userFolderCache = new CappedMemoryCache();
|
|
|
|
});
|
2024-02-16 12:34:07 +01:00
|
|
|
$this->pathByIdCache = $cacheFactory->createLocal('path-by-id');
|
2013-09-01 19:47:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the user for which the filesystem is setup
|
|
|
|
*
|
|
|
|
* @return \OC\User\User
|
|
|
|
*/
|
|
|
|
public function getUser() {
|
|
|
|
return $this->user;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $scope
|
|
|
|
* @param string $method
|
|
|
|
* @param callable $callback
|
|
|
|
*/
|
2015-05-08 14:27:22 +02:00
|
|
|
public function listen($scope, $method, callable $callback) {
|
2013-09-01 19:47:48 +02:00
|
|
|
$this->emitter->listen($scope, $method, $callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $scope optional
|
|
|
|
* @param string $method optional
|
|
|
|
* @param callable $callback optional
|
|
|
|
*/
|
2015-05-08 14:27:22 +02:00
|
|
|
public function removeListener($scope = null, $method = null, callable $callback = null) {
|
2013-09-01 19:47:48 +02:00
|
|
|
$this->emitter->removeListener($scope, $method, $callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $scope
|
|
|
|
* @param string $method
|
2014-02-06 16:30:58 +01:00
|
|
|
* @param Node[] $arguments
|
2013-09-01 19:47:48 +02:00
|
|
|
*/
|
2020-03-26 09:30:18 +01:00
|
|
|
public function emit($scope, $method, $arguments = []) {
|
2013-09-01 19:47:48 +02:00
|
|
|
$this->emitter->emit($scope, $method, $arguments);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param \OC\Files\Storage\Storage $storage
|
|
|
|
* @param string $mountPoint
|
|
|
|
* @param array $arguments
|
|
|
|
*/
|
2020-03-26 09:30:18 +01:00
|
|
|
public function mount($storage, $mountPoint, $arguments = []) {
|
2014-11-24 15:54:42 +01:00
|
|
|
$mount = new MountPoint($storage, $mountPoint, $arguments);
|
2013-09-01 19:47:48 +02:00
|
|
|
$this->mountManager->addMount($mount);
|
|
|
|
}
|
|
|
|
|
2023-06-15 23:21:56 +02:00
|
|
|
public function getMount(string $mountPoint): IMountPoint {
|
2013-09-01 19:47:48 +02:00
|
|
|
return $this->mountManager->find($mountPoint);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $mountPoint
|
2014-11-24 15:54:42 +01:00
|
|
|
* @return \OC\Files\Mount\MountPoint[]
|
2013-09-01 19:47:48 +02:00
|
|
|
*/
|
2023-06-15 23:21:56 +02:00
|
|
|
public function getMountsIn(string $mountPoint): array {
|
2013-09-01 19:47:48 +02:00
|
|
|
return $this->mountManager->findIn($mountPoint);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $storageId
|
2014-11-24 15:54:42 +01:00
|
|
|
* @return \OC\Files\Mount\MountPoint[]
|
2013-09-01 19:47:48 +02:00
|
|
|
*/
|
|
|
|
public function getMountByStorageId($storageId) {
|
|
|
|
return $this->mountManager->findByStorageId($storageId);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $numericId
|
2014-11-24 15:54:42 +01:00
|
|
|
* @return MountPoint[]
|
2013-09-01 19:47:48 +02:00
|
|
|
*/
|
|
|
|
public function getMountByNumericStorageId($numericId) {
|
|
|
|
return $this->mountManager->findByNumericId($numericId);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-11-24 15:54:42 +01:00
|
|
|
* @param \OC\Files\Mount\MountPoint $mount
|
2013-09-01 19:47:48 +02:00
|
|
|
*/
|
|
|
|
public function unMount($mount) {
|
|
|
|
$this->mountManager->remove($mount);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $path
|
2021-03-26 21:29:24 +01:00
|
|
|
* @return Node
|
2022-02-02 16:12:57 +01:00
|
|
|
* @throws \OCP\Files\NotPermittedException
|
|
|
|
* @throws \OCP\Files\NotFoundException
|
2013-09-01 19:47:48 +02:00
|
|
|
*/
|
|
|
|
public function get($path) {
|
|
|
|
$path = $this->normalizePath($path);
|
|
|
|
if ($this->isValidPath($path)) {
|
|
|
|
$fullPath = $this->getFullPath($path);
|
2023-02-08 14:34:40 +01:00
|
|
|
$fileInfo = $this->view->getFileInfo($fullPath, false);
|
2016-02-18 15:36:38 +01:00
|
|
|
if ($fileInfo) {
|
2023-02-08 14:34:40 +01:00
|
|
|
return $this->createNode($fullPath, $fileInfo, false);
|
2013-09-01 19:47:48 +02:00
|
|
|
} else {
|
2014-08-06 13:38:14 +02:00
|
|
|
throw new NotFoundException($path);
|
2013-09-01 19:47:48 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw new NotPermittedException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-07 19:51:27 +02:00
|
|
|
//most operations can't be done on the root
|
2013-09-01 19:47:48 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $targetPath
|
2022-03-28 18:47:17 +02:00
|
|
|
* @return Node
|
2022-02-02 16:12:57 +01:00
|
|
|
* @throws \OCP\Files\NotPermittedException
|
2013-09-01 19:47:48 +02:00
|
|
|
*/
|
|
|
|
public function rename($targetPath) {
|
|
|
|
throw new NotPermittedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function delete() {
|
|
|
|
throw new NotPermittedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $targetPath
|
2022-03-28 18:47:17 +02:00
|
|
|
* @return Node
|
2022-02-02 16:12:57 +01:00
|
|
|
* @throws \OCP\Files\NotPermittedException
|
2013-09-01 19:47:48 +02:00
|
|
|
*/
|
|
|
|
public function copy($targetPath) {
|
|
|
|
throw new NotPermittedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $mtime
|
2013-09-10 19:44:23 +02:00
|
|
|
* @throws \OCP\Files\NotPermittedException
|
2013-09-01 19:47:48 +02:00
|
|
|
*/
|
|
|
|
public function touch($mtime = null) {
|
|
|
|
throw new NotPermittedException();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return \OC\Files\Storage\Storage
|
2013-09-10 19:44:23 +02:00
|
|
|
* @throws \OCP\Files\NotFoundException
|
2013-09-01 19:47:48 +02:00
|
|
|
*/
|
|
|
|
public function getStorage() {
|
|
|
|
throw new NotFoundException();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getPath() {
|
|
|
|
return '/';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getInternalPath() {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getId() {
|
2022-03-03 17:15:02 +01:00
|
|
|
return 0;
|
2013-09-01 19:47:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function stat() {
|
2022-03-03 17:15:02 +01:00
|
|
|
return [];
|
2013-09-01 19:47:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getMTime() {
|
2022-03-03 17:15:02 +01:00
|
|
|
return 0;
|
2013-09-01 19:47:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-02-27 15:35:44 +01:00
|
|
|
* @param bool $includeMounts
|
2023-01-23 15:03:56 +01:00
|
|
|
* @return int|float
|
2013-09-01 19:47:48 +02:00
|
|
|
*/
|
2023-01-23 15:03:56 +01:00
|
|
|
public function getSize($includeMounts = true): int|float {
|
2022-03-03 17:15:02 +01:00
|
|
|
return 0;
|
2013-09-01 19:47:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getEtag() {
|
2022-03-03 17:15:02 +01:00
|
|
|
return '';
|
2013-09-01 19:47:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getPermissions() {
|
2014-11-25 16:28:41 +01:00
|
|
|
return \OCP\Constants::PERMISSION_CREATE;
|
2013-09-01 19:47:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isReadable() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isUpdateable() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isDeletable() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function isShareable() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-09-10 19:44:23 +02:00
|
|
|
* @throws \OCP\Files\NotFoundException
|
2013-09-01 19:47:48 +02:00
|
|
|
*/
|
2023-06-08 23:32:16 +02:00
|
|
|
public function getParent(): INode|IRootFolder {
|
2013-09-01 19:47:48 +02:00
|
|
|
throw new NotFoundException();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getName() {
|
|
|
|
return '';
|
|
|
|
}
|
2015-06-16 16:02:30 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a view to user's files folder
|
|
|
|
*
|
2018-11-15 11:51:28 +01:00
|
|
|
* @param string $userId user ID
|
2015-06-16 16:02:30 +02:00
|
|
|
* @return \OCP\Files\Folder
|
2020-04-03 12:17:47 +02:00
|
|
|
* @throws NoUserException
|
|
|
|
* @throws NotPermittedException
|
2015-06-16 16:02:30 +02:00
|
|
|
*/
|
|
|
|
public function getUserFolder($userId) {
|
2016-11-02 23:16:51 +01:00
|
|
|
$userObject = $this->userManager->get($userId);
|
2016-10-04 12:28:41 +02:00
|
|
|
|
|
|
|
if (is_null($userObject)) {
|
2022-04-20 12:15:39 +02:00
|
|
|
$e = new NoUserException('Backends provided no user object');
|
2016-11-02 23:16:51 +01:00
|
|
|
$this->logger->error(
|
|
|
|
sprintf(
|
|
|
|
'Backends provided no user object for %s',
|
|
|
|
$userId
|
|
|
|
),
|
|
|
|
[
|
|
|
|
'app' => 'files',
|
2022-04-20 12:15:39 +02:00
|
|
|
'exception' => $e,
|
2016-11-02 23:16:51 +01:00
|
|
|
]
|
|
|
|
);
|
2022-04-20 12:15:39 +02:00
|
|
|
throw $e;
|
2016-10-04 12:28:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$userId = $userObject->getUID();
|
|
|
|
|
2016-08-25 15:49:36 +02:00
|
|
|
if (!$this->userFolderCache->hasKey($userId)) {
|
2022-03-09 17:23:33 +01:00
|
|
|
if ($this->mountManager->getSetupManager()->isSetupComplete($userObject)) {
|
2022-03-08 16:20:05 +01:00
|
|
|
try {
|
2022-03-09 17:23:33 +01:00
|
|
|
$folder = $this->get('/' . $userId . '/files');
|
2022-03-24 15:41:34 +01:00
|
|
|
if (!$folder instanceof \OCP\Files\Folder) {
|
2022-09-21 17:44:32 +02:00
|
|
|
throw new \Exception("Account folder for \"$userId\" exists as a file");
|
2022-03-09 17:23:33 +01:00
|
|
|
}
|
2022-03-08 16:20:05 +01:00
|
|
|
} catch (NotFoundException $e) {
|
|
|
|
if (!$this->nodeExists('/' . $userId)) {
|
|
|
|
$this->newFolder('/' . $userId);
|
|
|
|
}
|
2022-03-24 15:41:34 +01:00
|
|
|
$folder = $this->newFolder('/' . $userId . '/files');
|
2016-08-25 15:50:02 +02:00
|
|
|
}
|
2022-03-09 17:23:33 +01:00
|
|
|
} else {
|
2023-02-08 13:48:42 +01:00
|
|
|
$folder = new LazyUserFolder($this, $userObject, $this->mountManager);
|
2022-03-09 17:23:33 +01:00
|
|
|
}
|
2016-08-25 15:50:02 +02:00
|
|
|
|
2016-08-25 15:49:36 +02:00
|
|
|
$this->userFolderCache->set($userId, $folder);
|
2015-06-16 16:02:30 +02:00
|
|
|
}
|
|
|
|
|
2016-08-25 15:49:36 +02:00
|
|
|
return $this->userFolderCache->get($userId);
|
|
|
|
}
|
2015-06-16 16:02:30 +02:00
|
|
|
|
2016-09-18 18:36:53 +02:00
|
|
|
public function getUserMountCache() {
|
|
|
|
return $this->userMountCache;
|
|
|
|
}
|
2022-03-28 18:47:17 +02:00
|
|
|
|
2024-02-08 11:34:22 +01:00
|
|
|
public function getFirstNodeByIdInPath(int $id, string $path): ?INode {
|
2024-02-16 12:34:07 +01:00
|
|
|
// scope the cache by user, so we don't return nodes for different users
|
|
|
|
if ($this->user) {
|
|
|
|
$cachedPath = $this->pathByIdCache->get($this->user->getUID() . '::' . $id);
|
|
|
|
if ($cachedPath && str_starts_with($path, $cachedPath)) {
|
|
|
|
// getting the node by path is significantly cheaper than finding it by id
|
|
|
|
$node = $this->get($cachedPath);
|
|
|
|
// by validating that the cached path still has the requested fileid we can work around the need to invalidate the cached path
|
|
|
|
// if the cached path is invalid or a different file now we fall back to the uncached logic
|
|
|
|
if ($node && $node->getId() === $id) {
|
|
|
|
return $node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$node = current($this->getByIdInPath($id, $path));
|
|
|
|
if (!$node) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->user) {
|
|
|
|
$this->pathByIdCache->set($this->user->getUID() . '::' . $id, $node->getPath());
|
|
|
|
}
|
|
|
|
return $node;
|
2024-02-08 11:34:22 +01:00
|
|
|
}
|
|
|
|
|
2022-03-28 18:47:17 +02:00
|
|
|
/**
|
|
|
|
* @param int $id
|
|
|
|
* @return Node[]
|
|
|
|
*/
|
|
|
|
public function getByIdInPath(int $id, string $path): array {
|
|
|
|
$mountCache = $this->getUserMountCache();
|
|
|
|
if (strpos($path, '/', 1) > 0) {
|
|
|
|
[, $user] = explode('/', $path);
|
|
|
|
} else {
|
|
|
|
$user = null;
|
|
|
|
}
|
|
|
|
$mountsContainingFile = $mountCache->getMountsForFileId($id, $user);
|
|
|
|
|
2022-04-11 19:15:58 +02:00
|
|
|
// if the mount isn't in the cache yet, perform a setup first, then try again
|
|
|
|
if (count($mountsContainingFile) === 0) {
|
|
|
|
$this->mountManager->getSetupManager()->setupForPath($path, true);
|
|
|
|
$mountsContainingFile = $mountCache->getMountsForFileId($id, $user);
|
|
|
|
}
|
|
|
|
|
2022-07-27 14:51:42 +02:00
|
|
|
// when a user has access through the same storage through multiple paths
|
2022-03-28 18:47:17 +02:00
|
|
|
// (such as an external storage that is both mounted for a user and shared to the user)
|
|
|
|
// the mount cache will only hold a single entry for the storage
|
|
|
|
// this can lead to issues as the different ways the user has access to a storage can have different permissions
|
|
|
|
//
|
|
|
|
// 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();
|
|
|
|
}, $mountsContainingFile);
|
|
|
|
$mountRootPaths = array_map(function ($mount) {
|
|
|
|
return $mount->getRootInternalPath();
|
|
|
|
}, $mountsContainingFile);
|
|
|
|
$mountProviders = array_unique(array_map(function ($mount) {
|
|
|
|
return $mount->getMountProvider();
|
|
|
|
}, $mountsContainingFile));
|
|
|
|
$mountRoots = array_combine($mountRootIds, $mountRootPaths);
|
|
|
|
|
|
|
|
$mounts = $this->mountManager->getMountsByMountProvider($path, $mountProviders);
|
|
|
|
|
|
|
|
$mountsContainingFile = array_filter($mounts, function ($mount) use ($mountRoots) {
|
|
|
|
return isset($mountRoots[$mount->getStorageRootId()]);
|
|
|
|
});
|
|
|
|
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$nodes = array_map(function (IMountPoint $mount) use ($id, $mountRoots) {
|
|
|
|
$rootInternalPath = $mountRoots[$mount->getStorageRootId()];
|
|
|
|
$cacheEntry = $mount->getStorage()->getCache()->get($id);
|
|
|
|
if (!$cacheEntry) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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, '/');
|
|
|
|
return $this->createNode($absolutePath, new FileInfo(
|
|
|
|
$absolutePath, $mount->getStorage(), $cacheEntry->getPath(), $cacheEntry, $mount,
|
|
|
|
\OC::$server->getUserManager()->get($mount->getStorage()->getOwner($pathRelativeToMount))
|
|
|
|
));
|
|
|
|
}, $mountsContainingFile);
|
|
|
|
|
|
|
|
$nodes = array_filter($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;
|
|
|
|
}
|
2023-09-18 15:00:38 +02:00
|
|
|
|
|
|
|
public function getNodeFromCacheEntryAndMount(ICacheEntry $cacheEntry, IMountPoint $mountPoint): INode {
|
|
|
|
$path = $cacheEntry->getPath();
|
|
|
|
$fullPath = $mountPoint->getMountPoint() . $path;
|
|
|
|
// todo: LazyNode?
|
|
|
|
$info = new FileInfo($fullPath, $mountPoint->getStorage(), $path, $cacheEntry, $mountPoint);
|
|
|
|
$parentPath = dirname($fullPath);
|
|
|
|
$parent = new LazyFolder($this, function () use ($parentPath) {
|
|
|
|
$parent = $this->get($parentPath);
|
|
|
|
if ($parent instanceof \OCP\Files\Folder) {
|
|
|
|
return $parent;
|
|
|
|
} else {
|
|
|
|
throw new \Exception("parent $parentPath is not a folder");
|
|
|
|
}
|
|
|
|
}, [
|
|
|
|
'path' => $parentPath,
|
|
|
|
]);
|
|
|
|
$isDir = $info->getType() === FileInfo::TYPE_FOLDER;
|
|
|
|
$view = new View('');
|
|
|
|
if ($isDir) {
|
|
|
|
return new Folder($this, $view, $path, $info, $parent);
|
|
|
|
} else {
|
|
|
|
return new File($this, $view, $path, $info, $parent);
|
|
|
|
}
|
|
|
|
}
|
2013-09-01 19:47:48 +02:00
|
|
|
}
|