Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make Node::getParent lazy #38150

Merged
merged 5 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions apps/files_trashbin/lib/Trash/TrashItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,8 @@ public function getCreationTime(): int {
public function getUploadTime(): int {
return $this->fileInfo->getUploadTime();
}

public function getParentId(): int {
return $this->fileInfo->getParentId();
}
}
4 changes: 4 additions & 0 deletions lib/private/Files/FileInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -412,4 +412,8 @@ public function getCreationTime(): int {
public function getUploadTime(): int {
return (int) $this->data['upload_time'];
}

public function getParentId(): int {
return $this->data['parent'] ?? -1;
}
}
69 changes: 55 additions & 14 deletions lib/private/Files/Node/LazyFolder.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@

namespace OC\Files\Node;

use OC\Files\Filesystem;
use OC\Files\Utils\PathHelper;
use OCP\Files\Folder;
use OCP\Constants;
use OCP\Files\IRootFolder;
use OCP\Files\Mount\IMountPoint;
use OCP\Files\NotPermittedException;

/**
* Class LazyFolder
Expand All @@ -41,23 +44,33 @@
*/
class LazyFolder implements Folder {
/** @var \Closure(): Folder */
private $folderClosure;

/** @var LazyFolder | null */
protected $folder = null;

private \Closure $folderClosure;
protected ?Folder $folder = null;
protected IRootFolder $rootFolder;
protected array $data;

/**
* LazyFolder constructor.
*
* @param IRootFolder $rootFolder
* @param \Closure(): Folder $folderClosure
* @param array $data
*/
public function __construct(\Closure $folderClosure, array $data = []) {
public function __construct(IRootFolder $rootFolder, \Closure $folderClosure, array $data = []) {
$this->rootFolder = $rootFolder;
$this->folderClosure = $folderClosure;
$this->data = $data;
}

protected function getRootFolder(): IRootFolder {
return $this->rootFolder;
}

protected function getRealFolder(): Folder {
if ($this->folder === null) {
$this->folder = call_user_func($this->folderClosure);
}
return $this->folder;
}

/**
* Magic method to first get the real rootFolder and then
* call $method with $args on it
Expand All @@ -67,11 +80,7 @@ public function __construct(\Closure $folderClosure, array $data = []) {
* @return mixed
*/
public function __call($method, $args) {
if ($this->folder === null) {
$this->folder = call_user_func($this->folderClosure);
}

return call_user_func_array([$this->folder, $method], $args);
return call_user_func_array([$this->getRealFolder(), $method], $args);
}

/**
Expand Down Expand Up @@ -148,7 +157,7 @@ public function unMount($mount) {
* @inheritDoc
*/
public function get($path) {
return $this->__call(__FUNCTION__, func_get_args());
return $this->getRootFolder()->get($this->getFullPath($path));
}

/**
Expand Down Expand Up @@ -207,6 +216,9 @@ public function getInternalPath() {
* @inheritDoc
*/
public function getId() {
if (isset($this->data['fileid'])) {
return $this->data['fileid'];
}
return $this->__call(__FUNCTION__, func_get_args());
}

Expand All @@ -221,20 +233,29 @@ public function stat() {
* @inheritDoc
*/
public function getMTime() {
if (isset($this->data['mtime'])) {
return $this->data['mtime'];
}
return $this->__call(__FUNCTION__, func_get_args());
}

/**
* @inheritDoc
*/
public function getSize($includeMounts = true): int|float {
if (isset($this->data['size'])) {
return $this->data['size'];
}
return $this->__call(__FUNCTION__, func_get_args());
}

/**
* @inheritDoc
*/
public function getEtag() {
if (isset($this->data['etag'])) {
return $this->data['etag'];
}
return $this->__call(__FUNCTION__, func_get_args());
}

Expand Down Expand Up @@ -299,6 +320,12 @@ public function getParent() {
* @inheritDoc
*/
public function getName() {
if (isset($this->data['path'])) {
return basename($this->data['path']);
}
if (isset($this->data['name'])) {
return $this->data['name'];
}
return $this->__call(__FUNCTION__, func_get_args());
}

Expand Down Expand Up @@ -390,6 +417,13 @@ public function getExtension(): string {
* @inheritDoc
*/
public function getFullPath($path) {
if (isset($this->data['path'])) {
$path = PathHelper::normalizePath($path);
if (!Filesystem::isValidPath($path)) {
throw new NotPermittedException('Invalid path "' . $path . '"');
}
return $this->data['path'] . $path;
}
return $this->__call(__FUNCTION__, func_get_args());
}

Expand Down Expand Up @@ -533,4 +567,11 @@ public function getUploadTime(): int {
public function getRelativePath($path) {
return PathHelper::getRelativePath($this->getPath(), $path);
}

public function getParentId(): int {
if (isset($this->data['parent'])) {
return $this->data['parent'];
}
return $this->__call(__FUNCTION__, func_get_args());
}
}
15 changes: 12 additions & 3 deletions lib/private/Files/Node/LazyRoot.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,18 @@
* @package OC\Files\Node
*/
class LazyRoot extends LazyFolder implements IRootFolder {
/**
* @inheritDoc
*/
public function __construct(\Closure $folderClosure, array $data = []) {
parent::__construct($this, $folderClosure, $data);
}

protected function getRootFolder(): IRootFolder {
$folder = $this->getRealFolder();
if (!$folder instanceof IRootFolder) {
throw new \Exception('Lazy root folder closure didn\'t return a root folder');
}
return $folder;
}

public function getUserFolder($userId) {
return $this->__call(__FUNCTION__, func_get_args());
}
Expand Down
16 changes: 7 additions & 9 deletions lib/private/Files/Node/LazyUserFolder.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,17 @@
use Psr\Log\LoggerInterface;

class LazyUserFolder extends LazyFolder {
private IRootFolder $root;
private IUser $user;
private string $path;
private IMountManager $mountManager;

public function __construct(IRootFolder $rootFolder, IUser $user, IMountManager $mountManager) {
$this->root = $rootFolder;
$this->user = $user;
$this->mountManager = $mountManager;
$this->path = '/' . $user->getUID() . '/files';
parent::__construct(function () use ($user): Folder {
parent::__construct($rootFolder, function () use ($user): Folder {
try {
$node = $this->root->get($this->path);
$node = $this->getRootFolder()->get($this->path);
if ($node instanceof File) {
$e = new \RuntimeException();
\OCP\Server::get(LoggerInterface::class)->error('User root storage is not a folder: ' . $this->path, [
Expand All @@ -56,10 +54,10 @@ public function __construct(IRootFolder $rootFolder, IUser $user, IMountManager
}
return $node;
} catch (NotFoundException $e) {
if (!$this->root->nodeExists('/' . $user->getUID())) {
$this->root->newFolder('/' . $user->getUID());
if (!$this->getRootFolder()->nodeExists('/' . $user->getUID())) {
$this->getRootFolder()->newFolder('/' . $user->getUID());
}
return $this->root->newFolder($this->path);
return $this->getRootFolder()->newFolder($this->path);
}
}, [
'path' => $this->path,
Expand All @@ -71,15 +69,15 @@ public function __construct(IRootFolder $rootFolder, IUser $user, IMountManager
}

public function get($path) {
return $this->root->get('/' . $this->user->getUID() . '/files/' . ltrim($path, '/'));
return $this->getRootFolder()->get('/' . $this->user->getUID() . '/files/' . ltrim($path, '/'));
}

/**
* @param int $id
* @return \OCP\Files\Node[]
*/
public function getById($id) {
return $this->root->getByIdInPath((int)$id, $this->getPath());
return $this->getRootFolder()->getByIdInPath((int)$id, $this->getPath());
}

public function getMountPoint() {
Expand Down
28 changes: 16 additions & 12 deletions lib/private/Files/Node/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,7 @@ class Node implements INode {

protected ?FileInfo $fileInfo;

/**
* @var Node|null
*/
protected $parent;
protected ?INode $parent;

private bool $infoHasSubMountsIncluded;

Expand Down Expand Up @@ -300,7 +297,16 @@ public function getParent(): INode|IRootFolder {
return $this->root;
}

$this->parent = $this->root->get($newPath);
// gather the metadata we already know about our parent
$parentData = [
'path' => $newPath,
'fileid' => $this->getFileInfo()->getParentId(),
];

// and create lazy folder with it instead of always querying
$this->parent = new LazyFolder($this->root, function () use ($newPath) {
return $this->root->get($newPath);
}, $parentData);
}

return $this->parent;
Expand Down Expand Up @@ -328,13 +334,7 @@ protected function normalizePath($path) {
* @return bool
*/
public function isValidPath($path) {
if (!$path || $path[0] !== '/') {
$path = '/' . $path;
}
if (strstr($path, '/../') || strrchr($path, '/') === '/..') {
return false;
}
return true;
return Filesystem::isValidPath($path);
}

public function isMounted() {
Expand Down Expand Up @@ -477,4 +477,8 @@ public function getCreationTime(): int {
public function getUploadTime(): int {
return $this->getFileInfo()->getUploadTime();
}

public function getParentId(): int {
return $this->fileInfo->getParentId();
}
}
9 changes: 9 additions & 0 deletions lib/public/Files/FileInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,4 +299,13 @@ public function getCreationTime(): int;
* @since 18.0.0
*/
public function getUploadTime(): int;

/**
* Get the fileid or the parent folder
* or -1 if this item has no parent folder (because it is the root)
*
* @return int
* @since 28.0.0
*/
public function getParentId(): int;
}